blob: 3b35c3d92ba7c8e3d518f89f941d1d60746652ca [file] [log] [blame]
Jonas Orelanded99dae2022-03-09 08:28:101/*
2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "test/scoped_key_value_config.h"
12
Jonas Orelande62c2f22022-03-29 09:04:4813#include "api/field_trials_view.h"
Jonas Orelanded99dae2022-03-09 08:28:1014#include "rtc_base/checks.h"
Jonas Orelandd7f95502022-03-22 15:52:0815#include "system_wrappers/include/field_trial.h"
Jonas Orelanded99dae2022-03-09 08:28:1016#include "test/field_trial.h"
17
18namespace {
19
20// This part is copied from system_wrappers/field_trial.cc.
21void InsertIntoMap(std::map<std::string, std::string>& key_value_map,
22 const std::string& s) {
23 std::string::size_type field_start = 0;
24 while (field_start < s.size()) {
25 std::string::size_type separator_pos = s.find('/', field_start);
26 RTC_CHECK_NE(separator_pos, std::string::npos)
27 << "Missing separator '/' after field trial key.";
28 RTC_CHECK_GT(separator_pos, field_start)
29 << "Field trial key cannot be empty.";
30 std::string key = s.substr(field_start, separator_pos - field_start);
31 field_start = separator_pos + 1;
32
33 RTC_CHECK_LT(field_start, s.size())
34 << "Missing value after field trial key. String ended.";
35 separator_pos = s.find('/', field_start);
36 RTC_CHECK_NE(separator_pos, std::string::npos)
37 << "Missing terminating '/' in field trial string.";
38 RTC_CHECK_GT(separator_pos, field_start)
39 << "Field trial value cannot be empty.";
40 std::string value = s.substr(field_start, separator_pos - field_start);
41 field_start = separator_pos + 1;
42
43 key_value_map[key] = value;
44 }
45 // This check is technically redundant due to earlier checks.
46 // We nevertheless keep the check to make it clear that the entire
47 // string has been processed, and without indexing past the end.
48 RTC_CHECK_EQ(field_start, s.size());
49}
50
51} // namespace
52
53namespace webrtc {
54namespace test {
55
56ScopedKeyValueConfig::ScopedKeyValueConfig()
57 : ScopedKeyValueConfig(nullptr, "") {}
58
59ScopedKeyValueConfig::ScopedKeyValueConfig(const std::string& s)
60 : ScopedKeyValueConfig(nullptr, s) {}
61
62ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig& parent,
63 const std::string& s)
64 : ScopedKeyValueConfig(&parent, s) {}
65
66ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig* parent,
67 const std::string& s)
68 : parent_(parent), leaf_(nullptr) {
69 InsertIntoMap(key_value_map_, s);
Jonas Oreland8ca06132022-03-14 11:52:4870
71 if (!s.empty()) {
72 // Also store field trials in global string (until we get rid of it).
73 scoped_field_trials_ = std::make_unique<ScopedFieldTrials>(s);
74 }
Jonas Orelanded99dae2022-03-09 08:28:1075
76 if (parent == nullptr) {
77 // We are root, set leaf_.
78 leaf_ = this;
79 } else {
80 // Link root to new leaf.
81 GetRoot(parent)->leaf_ = this;
82 RTC_DCHECK(leaf_ == nullptr);
83 }
84}
85
86ScopedKeyValueConfig::~ScopedKeyValueConfig() {
87 if (parent_) {
88 GetRoot(parent_)->leaf_ = parent_;
89 }
90}
91
92ScopedKeyValueConfig* ScopedKeyValueConfig::GetRoot(ScopedKeyValueConfig* n) {
93 while (n->parent_ != nullptr) {
94 n = n->parent_;
95 }
96 return n;
97}
98
99std::string ScopedKeyValueConfig::Lookup(absl::string_view key) const {
100 if (parent_ == nullptr) {
101 return leaf_->LookupRecurse(key);
102 } else {
103 return LookupRecurse(key);
104 }
105}
106
107std::string ScopedKeyValueConfig::LookupRecurse(absl::string_view key) const {
108 auto it = key_value_map_.find(std::string(key));
109 if (it != key_value_map_.end())
110 return it->second;
111
112 if (parent_) {
113 return parent_->LookupRecurse(key);
114 }
115
Jonas Orelandd7f95502022-03-22 15:52:08116 // When at the root, check the global string so that test programs using
117 // a mix between ScopedKeyValueConfig and the global string continue to work
118 return webrtc::field_trial::FindFullName(std::string(key));
Jonas Orelanded99dae2022-03-09 08:28:10119}
120
121} // namespace test
122} // namespace webrtc