Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 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 | |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 13 | #include "api/field_trials_view.h" |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
Jonas Oreland | d7f9550 | 2022-03-22 15:52:08 | [diff] [blame] | 15 | #include "system_wrappers/include/field_trial.h" |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 16 | #include "test/field_trial.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // This part is copied from system_wrappers/field_trial.cc. |
| 21 | void 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 | |
| 53 | namespace webrtc { |
| 54 | namespace test { |
| 55 | |
| 56 | ScopedKeyValueConfig::ScopedKeyValueConfig() |
| 57 | : ScopedKeyValueConfig(nullptr, "") {} |
| 58 | |
| 59 | ScopedKeyValueConfig::ScopedKeyValueConfig(const std::string& s) |
| 60 | : ScopedKeyValueConfig(nullptr, s) {} |
| 61 | |
| 62 | ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig& parent, |
| 63 | const std::string& s) |
| 64 | : ScopedKeyValueConfig(&parent, s) {} |
| 65 | |
| 66 | ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig* parent, |
| 67 | const std::string& s) |
| 68 | : parent_(parent), leaf_(nullptr) { |
| 69 | InsertIntoMap(key_value_map_, s); |
Jonas Oreland | 8ca0613 | 2022-03-14 11:52:48 | [diff] [blame] | 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 | } |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 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 | |
| 86 | ScopedKeyValueConfig::~ScopedKeyValueConfig() { |
| 87 | if (parent_) { |
| 88 | GetRoot(parent_)->leaf_ = parent_; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | ScopedKeyValueConfig* ScopedKeyValueConfig::GetRoot(ScopedKeyValueConfig* n) { |
| 93 | while (n->parent_ != nullptr) { |
| 94 | n = n->parent_; |
| 95 | } |
| 96 | return n; |
| 97 | } |
| 98 | |
| 99 | std::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 | |
| 107 | std::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 Oreland | d7f9550 | 2022-03-22 15:52:08 | [diff] [blame] | 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)); |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | } // namespace test |
| 122 | } // namespace webrtc |