1 /*
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
13 #include "rtc_base/checks.h"
14 #include "system_wrappers/include/field_trial.h"
15 #include "test/field_trial.h"
16
17 namespace {
18
19 // This part is copied from system_wrappers/field_trial.cc.
InsertIntoMap(std::map<std::string,std::string,std::less<>> & key_value_map,absl::string_view s)20 void InsertIntoMap(
21 std::map<std::string, std::string, std::less<>>& key_value_map,
22 absl::string_view 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
53 namespace webrtc {
54 namespace test {
55
ScopedKeyValueConfig()56 ScopedKeyValueConfig::ScopedKeyValueConfig()
57 : ScopedKeyValueConfig(nullptr, "") {}
58
ScopedKeyValueConfig(absl::string_view s)59 ScopedKeyValueConfig::ScopedKeyValueConfig(absl::string_view s)
60 : ScopedKeyValueConfig(nullptr, s) {}
61
ScopedKeyValueConfig(ScopedKeyValueConfig & parent,absl::string_view s)62 ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig& parent,
63 absl::string_view s)
64 : ScopedKeyValueConfig(&parent, s) {}
65
ScopedKeyValueConfig(ScopedKeyValueConfig * parent,absl::string_view s)66 ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig* parent,
67 absl::string_view s)
68 : parent_(parent), leaf_(nullptr) {
69 InsertIntoMap(key_value_map_, s);
70
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 }
75
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
~ScopedKeyValueConfig()86 ScopedKeyValueConfig::~ScopedKeyValueConfig() {
87 if (parent_) {
88 GetRoot(parent_)->leaf_ = parent_;
89 }
90 }
91
GetRoot(ScopedKeyValueConfig * n)92 ScopedKeyValueConfig* ScopedKeyValueConfig::GetRoot(ScopedKeyValueConfig* n) {
93 while (n->parent_ != nullptr) {
94 n = n->parent_;
95 }
96 return n;
97 }
98
GetValue(absl::string_view key) const99 std::string ScopedKeyValueConfig::GetValue(absl::string_view key) const {
100 if (parent_ == nullptr) {
101 return leaf_->LookupRecurse(key);
102 } else {
103 return LookupRecurse(key);
104 }
105 }
106
LookupRecurse(absl::string_view key) const107 std::string ScopedKeyValueConfig::LookupRecurse(absl::string_view key) const {
108 auto it = key_value_map_.find(key);
109 if (it != key_value_map_.end())
110 return it->second;
111
112 if (parent_) {
113 return parent_->LookupRecurse(key);
114 }
115
116 // 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));
119 }
120
121 } // namespace test
122 } // namespace webrtc
123