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 "api/field_trials.h"
12
13 #include <atomic>
14
15 #include "rtc_base/checks.h"
16 #include "system_wrappers/include/field_trial.h"
17
18 namespace {
19
20 // This part is copied from system_wrappers/field_trial.cc.
InsertIntoMap(const std::string & s)21 webrtc::flat_map<std::string, std::string> InsertIntoMap(const std::string& s) {
22 std::string::size_type field_start = 0;
23 webrtc::flat_map<std::string, std::string> key_value_map;
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 // If a key is specified multiple times, only the value linked to the first
44 // key is stored. note: This will crash in debug build when calling
45 // InitFieldTrialsFromString().
46 key_value_map.emplace(key, value);
47 }
48 // This check is technically redundant due to earlier checks.
49 // We nevertheless keep the check to make it clear that the entire
50 // string has been processed, and without indexing past the end.
51 RTC_CHECK_EQ(field_start, s.size());
52
53 return key_value_map;
54 }
55
56 // Makes sure that only one instance is created, since the usage
57 // of global string makes behaviour unpredicatable otherwise.
58 // TODO(bugs.webrtc.org/10335): Remove once global string is gone.
59 std::atomic<bool> instance_created_{false};
60
61 } // namespace
62
63 namespace webrtc {
64
FieldTrials(const std::string & s)65 FieldTrials::FieldTrials(const std::string& s)
66 : uses_global_(true),
67 field_trial_string_(s),
68 previous_field_trial_string_(webrtc::field_trial::GetFieldTrialString()),
69 key_value_map_(InsertIntoMap(s)) {
70 // TODO(bugs.webrtc.org/10335): Remove the global string!
71 field_trial::InitFieldTrialsFromString(field_trial_string_.c_str());
72 RTC_CHECK(!instance_created_.exchange(true))
73 << "Only one instance may be instanciated at any given time!";
74 }
75
CreateNoGlobal(const std::string & s)76 std::unique_ptr<FieldTrials> FieldTrials::CreateNoGlobal(const std::string& s) {
77 return std::unique_ptr<FieldTrials>(new FieldTrials(s, true));
78 }
79
FieldTrials(const std::string & s,bool)80 FieldTrials::FieldTrials(const std::string& s, bool)
81 : uses_global_(false),
82 previous_field_trial_string_(nullptr),
83 key_value_map_(InsertIntoMap(s)) {}
84
~FieldTrials()85 FieldTrials::~FieldTrials() {
86 // TODO(bugs.webrtc.org/10335): Remove the global string!
87 if (uses_global_) {
88 field_trial::InitFieldTrialsFromString(previous_field_trial_string_);
89 RTC_CHECK(instance_created_.exchange(false));
90 }
91 }
92
GetValue(absl::string_view key) const93 std::string FieldTrials::GetValue(absl::string_view key) const {
94 auto it = key_value_map_.find(std::string(key));
95 if (it != key_value_map_.end())
96 return it->second;
97
98 // Check the global string so that programs using
99 // a mix between FieldTrials and the global string continue to work
100 // TODO(bugs.webrtc.org/10335): Remove the global string!
101 if (uses_global_) {
102 return field_trial::FindFullName(std::string(key));
103 }
104 return "";
105 }
106
107 } // namespace webrtc
108