Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [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 11:04:48 +0200 | [diff] [blame] | 13 | #include "api/field_trials_view.h" |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
Jonas Oreland | d7f9550 | 2022-03-22 16:52:08 +0100 | [diff] [blame] | 15 | #include "system_wrappers/include/field_trial.h" |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 16 | #include "test/field_trial.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // This part is copied from system_wrappers/field_trial.cc. |
Danil Chapovalov | 385b6c5 | 2022-04-08 16:01:50 +0200 | [diff] [blame] | 21 | void InsertIntoMap( |
| 22 | std::map<std::string, std::string, std::less<>>& key_value_map, |
| 23 | absl::string_view s) { |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 24 | std::string::size_type field_start = 0; |
| 25 | while (field_start < s.size()) { |
| 26 | std::string::size_type separator_pos = s.find('/', field_start); |
| 27 | RTC_CHECK_NE(separator_pos, std::string::npos) |
| 28 | << "Missing separator '/' after field trial key."; |
| 29 | RTC_CHECK_GT(separator_pos, field_start) |
| 30 | << "Field trial key cannot be empty."; |
Danil Chapovalov | 385b6c5 | 2022-04-08 16:01:50 +0200 | [diff] [blame] | 31 | std::string key(s.substr(field_start, separator_pos - field_start)); |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 32 | field_start = separator_pos + 1; |
| 33 | |
| 34 | RTC_CHECK_LT(field_start, s.size()) |
| 35 | << "Missing value after field trial key. String ended."; |
| 36 | separator_pos = s.find('/', field_start); |
| 37 | RTC_CHECK_NE(separator_pos, std::string::npos) |
| 38 | << "Missing terminating '/' in field trial string."; |
| 39 | RTC_CHECK_GT(separator_pos, field_start) |
| 40 | << "Field trial value cannot be empty."; |
Danil Chapovalov | 385b6c5 | 2022-04-08 16:01:50 +0200 | [diff] [blame] | 41 | std::string value(s.substr(field_start, separator_pos - field_start)); |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 42 | field_start = separator_pos + 1; |
| 43 | |
| 44 | key_value_map[key] = value; |
| 45 | } |
| 46 | // This check is technically redundant due to earlier checks. |
| 47 | // We nevertheless keep the check to make it clear that the entire |
| 48 | // string has been processed, and without indexing past the end. |
| 49 | RTC_CHECK_EQ(field_start, s.size()); |
| 50 | } |
| 51 | |
| 52 | } // namespace |
| 53 | |
| 54 | namespace webrtc { |
| 55 | namespace test { |
| 56 | |
| 57 | ScopedKeyValueConfig::ScopedKeyValueConfig() |
| 58 | : ScopedKeyValueConfig(nullptr, "") {} |
| 59 | |
Danil Chapovalov | 385b6c5 | 2022-04-08 16:01:50 +0200 | [diff] [blame] | 60 | ScopedKeyValueConfig::ScopedKeyValueConfig(absl::string_view s) |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 61 | : ScopedKeyValueConfig(nullptr, s) {} |
| 62 | |
| 63 | ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig& parent, |
Danil Chapovalov | 385b6c5 | 2022-04-08 16:01:50 +0200 | [diff] [blame] | 64 | absl::string_view s) |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 65 | : ScopedKeyValueConfig(&parent, s) {} |
| 66 | |
| 67 | ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig* parent, |
Danil Chapovalov | 385b6c5 | 2022-04-08 16:01:50 +0200 | [diff] [blame] | 68 | absl::string_view s) |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 69 | : parent_(parent), leaf_(nullptr) { |
| 70 | InsertIntoMap(key_value_map_, s); |
Jonas Oreland | 8ca0613 | 2022-03-14 12:52:48 +0100 | [diff] [blame] | 71 | |
| 72 | if (!s.empty()) { |
| 73 | // Also store field trials in global string (until we get rid of it). |
| 74 | scoped_field_trials_ = std::make_unique<ScopedFieldTrials>(s); |
| 75 | } |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 76 | |
| 77 | if (parent == nullptr) { |
| 78 | // We are root, set leaf_. |
| 79 | leaf_ = this; |
| 80 | } else { |
| 81 | // Link root to new leaf. |
| 82 | GetRoot(parent)->leaf_ = this; |
| 83 | RTC_DCHECK(leaf_ == nullptr); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | ScopedKeyValueConfig::~ScopedKeyValueConfig() { |
| 88 | if (parent_) { |
| 89 | GetRoot(parent_)->leaf_ = parent_; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | ScopedKeyValueConfig* ScopedKeyValueConfig::GetRoot(ScopedKeyValueConfig* n) { |
| 94 | while (n->parent_ != nullptr) { |
| 95 | n = n->parent_; |
| 96 | } |
| 97 | return n; |
| 98 | } |
| 99 | |
| 100 | std::string ScopedKeyValueConfig::Lookup(absl::string_view key) const { |
| 101 | if (parent_ == nullptr) { |
| 102 | return leaf_->LookupRecurse(key); |
| 103 | } else { |
| 104 | return LookupRecurse(key); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | std::string ScopedKeyValueConfig::LookupRecurse(absl::string_view key) const { |
Danil Chapovalov | 385b6c5 | 2022-04-08 16:01:50 +0200 | [diff] [blame] | 109 | auto it = key_value_map_.find(key); |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 110 | if (it != key_value_map_.end()) |
| 111 | return it->second; |
| 112 | |
| 113 | if (parent_) { |
| 114 | return parent_->LookupRecurse(key); |
| 115 | } |
| 116 | |
Jonas Oreland | d7f9550 | 2022-03-22 16:52:08 +0100 | [diff] [blame] | 117 | // When at the root, check the global string so that test programs using |
| 118 | // a mix between ScopedKeyValueConfig and the global string continue to work |
| 119 | return webrtc::field_trial::FindFullName(std::string(key)); |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | } // namespace test |
| 123 | } // namespace webrtc |