1 // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 //
9
10 #include "system_wrappers/include/field_trial.h"
11
12 #include <stddef.h>
13
14 #include <map>
15 #include <string>
16
17 #include "absl/strings/string_view.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/logging.h"
20 #include "rtc_base/string_encode.h"
21
22 // Simple field trial implementation, which allows client to
23 // specify desired flags in InitFieldTrialsFromString.
24 namespace webrtc {
25 namespace field_trial {
26
27 static const char* trials_init_string = NULL;
28
29 #ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
30 namespace {
31 constexpr char kPersistentStringSeparator = '/';
32 // Validates the given field trial string.
33 // E.g.:
34 // "WebRTC-experimentFoo/Enabled/WebRTC-experimentBar/Enabled100kbps/"
35 // Assigns the process to group "Enabled" on WebRTCExperimentFoo trial
36 // and to group "Enabled100kbps" on WebRTCExperimentBar.
37 //
38 // E.g. invalid config:
39 // "WebRTC-experiment1/Enabled" (note missing / separator at the end).
FieldTrialsStringIsValidInternal(const absl::string_view trials)40 bool FieldTrialsStringIsValidInternal(const absl::string_view trials) {
41 if (trials.empty())
42 return true;
43
44 size_t next_item = 0;
45 std::map<absl::string_view, absl::string_view> field_trials;
46 while (next_item < trials.length()) {
47 size_t name_end = trials.find(kPersistentStringSeparator, next_item);
48 if (name_end == trials.npos || next_item == name_end)
49 return false;
50 size_t group_name_end =
51 trials.find(kPersistentStringSeparator, name_end + 1);
52 if (group_name_end == trials.npos || name_end + 1 == group_name_end)
53 return false;
54 absl::string_view name = trials.substr(next_item, name_end - next_item);
55 absl::string_view group_name =
56 trials.substr(name_end + 1, group_name_end - name_end - 1);
57
58 next_item = group_name_end + 1;
59
60 // Fail if duplicate with different group name.
61 if (field_trials.find(name) != field_trials.end() &&
62 field_trials.find(name)->second != group_name) {
63 return false;
64 }
65
66 field_trials[name] = group_name;
67 }
68
69 return true;
70 }
71 } // namespace
72
FieldTrialsStringIsValid(const char * trials_string)73 bool FieldTrialsStringIsValid(const char* trials_string) {
74 return FieldTrialsStringIsValidInternal(trials_string);
75 }
76
InsertOrReplaceFieldTrialStringsInMap(std::map<std::string,std::string> * fieldtrial_map,const absl::string_view trials_string)77 void InsertOrReplaceFieldTrialStringsInMap(
78 std::map<std::string, std::string>* fieldtrial_map,
79 const absl::string_view trials_string) {
80 if (FieldTrialsStringIsValidInternal(trials_string)) {
81 std::vector<std::string> tokens;
82 rtc::split(std::string(trials_string), '/', &tokens);
83 // Skip last token which is empty due to trailing '/'.
84 for (size_t idx = 0; idx < tokens.size() - 1; idx += 2) {
85 (*fieldtrial_map)[tokens[idx]] = tokens[idx + 1];
86 }
87 } else {
88 RTC_DCHECK(false) << "Invalid field trials string:" << trials_string;
89 }
90 }
91
MergeFieldTrialsStrings(const char * first,const char * second)92 std::string MergeFieldTrialsStrings(const char* first, const char* second) {
93 std::map<std::string, std::string> fieldtrial_map;
94 InsertOrReplaceFieldTrialStringsInMap(&fieldtrial_map, first);
95 InsertOrReplaceFieldTrialStringsInMap(&fieldtrial_map, second);
96
97 // Merge into fieldtrial string.
98 std::string merged = "";
99 for (auto const& fieldtrial : fieldtrial_map) {
100 merged += fieldtrial.first + '/' + fieldtrial.second + '/';
101 }
102 return merged;
103 }
104
FindFullName(const std::string & name)105 std::string FindFullName(const std::string& name) {
106 if (trials_init_string == NULL)
107 return std::string();
108
109 std::string trials_string(trials_init_string);
110 if (trials_string.empty())
111 return std::string();
112
113 size_t next_item = 0;
114 while (next_item < trials_string.length()) {
115 // Find next name/value pair in field trial configuration string.
116 size_t field_name_end =
117 trials_string.find(kPersistentStringSeparator, next_item);
118 if (field_name_end == trials_string.npos || field_name_end == next_item)
119 break;
120 size_t field_value_end =
121 trials_string.find(kPersistentStringSeparator, field_name_end + 1);
122 if (field_value_end == trials_string.npos ||
123 field_value_end == field_name_end + 1)
124 break;
125 std::string field_name(trials_string, next_item,
126 field_name_end - next_item);
127 std::string field_value(trials_string, field_name_end + 1,
128 field_value_end - field_name_end - 1);
129 next_item = field_value_end + 1;
130
131 if (name == field_name)
132 return field_value;
133 }
134 return std::string();
135 }
136 #endif // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
137
138 // Optionally initialize field trial from a string.
InitFieldTrialsFromString(const char * trials_string)139 void InitFieldTrialsFromString(const char* trials_string) {
140 RTC_LOG(LS_INFO) << "Setting field trial string:" << trials_string;
141 #ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
142 if (trials_string) {
143 RTC_DCHECK(FieldTrialsStringIsValidInternal(trials_string))
144 << "Invalid field trials string:" << trials_string;
145 };
146 #endif // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
147 trials_init_string = trials_string;
148 }
149
GetFieldTrialString()150 const char* GetFieldTrialString() {
151 return trials_init_string;
152 }
153
154 } // namespace field_trial
155 } // namespace webrtc
156