You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "system_wrappers/include/field_trial.h"
  10. #include <stddef.h>
  11. #include <map>
  12. #include <string>
  13. #include "absl/strings/string_view.h"
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/logging.h"
  16. // Simple field trial implementation, which allows client to
  17. // specify desired flags in InitFieldTrialsFromString.
  18. namespace webrtc {
  19. namespace field_trial {
  20. static const char* trials_init_string = NULL;
  21. #ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
  22. namespace {
  23. constexpr char kPersistentStringSeparator = '/';
  24. // Validates the given field trial string.
  25. // E.g.:
  26. // "WebRTC-experimentFoo/Enabled/WebRTC-experimentBar/Enabled100kbps/"
  27. // Assigns the process to group "Enabled" on WebRTCExperimentFoo trial
  28. // and to group "Enabled100kbps" on WebRTCExperimentBar.
  29. //
  30. // E.g. invalid config:
  31. // "WebRTC-experiment1/Enabled" (note missing / separator at the end).
  32. bool FieldTrialsStringIsValid(const absl::string_view trials) {
  33. if (trials.empty())
  34. return true;
  35. size_t next_item = 0;
  36. std::map<absl::string_view, absl::string_view> field_trials;
  37. while (next_item < trials.length()) {
  38. size_t name_end = trials.find(kPersistentStringSeparator, next_item);
  39. if (name_end == trials.npos || next_item == name_end)
  40. return false;
  41. size_t group_name_end =
  42. trials.find(kPersistentStringSeparator, name_end + 1);
  43. if (group_name_end == trials.npos || name_end + 1 == group_name_end)
  44. return false;
  45. absl::string_view name = trials.substr(next_item, name_end - next_item);
  46. absl::string_view group_name =
  47. trials.substr(name_end + 1, group_name_end - name_end - 1);
  48. next_item = group_name_end + 1;
  49. // Fail if duplicate with different group name.
  50. if (field_trials.find(name) != field_trials.end() &&
  51. field_trials.find(name)->second != group_name) {
  52. return false;
  53. }
  54. field_trials[name] = group_name;
  55. }
  56. return true;
  57. }
  58. } // namespace
  59. std::string FindFullName(const std::string& name) {
  60. if (trials_init_string == NULL)
  61. return std::string();
  62. std::string trials_string(trials_init_string);
  63. if (trials_string.empty())
  64. return std::string();
  65. size_t next_item = 0;
  66. while (next_item < trials_string.length()) {
  67. // Find next name/value pair in field trial configuration string.
  68. size_t field_name_end =
  69. trials_string.find(kPersistentStringSeparator, next_item);
  70. if (field_name_end == trials_string.npos || field_name_end == next_item)
  71. break;
  72. size_t field_value_end =
  73. trials_string.find(kPersistentStringSeparator, field_name_end + 1);
  74. if (field_value_end == trials_string.npos ||
  75. field_value_end == field_name_end + 1)
  76. break;
  77. std::string field_name(trials_string, next_item,
  78. field_name_end - next_item);
  79. std::string field_value(trials_string, field_name_end + 1,
  80. field_value_end - field_name_end - 1);
  81. next_item = field_value_end + 1;
  82. if (name == field_name)
  83. return field_value;
  84. }
  85. return std::string();
  86. }
  87. #endif // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
  88. // Optionally initialize field trial from a string.
  89. void InitFieldTrialsFromString(const char* trials_string) {
  90. RTC_LOG(LS_INFO) << "Setting field trial string:" << trials_string;
  91. #ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
  92. if (trials_string) {
  93. RTC_DCHECK(FieldTrialsStringIsValid(trials_string))
  94. << "Invalid field trials string:" << trials_string;
  95. };
  96. #endif // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
  97. trials_init_string = trials_string;
  98. }
  99. const char* GetFieldTrialString() {
  100. return trials_init_string;
  101. }
  102. } // namespace field_trial
  103. } // namespace webrtc