Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / chrome / common / variations / variations_util.cc
blobb203ae1697bba037359e472a8bbe359b54e781bb
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/common/variations/variations_util.h"
7 #include <string>
8 #include <vector>
10 #include "base/metrics/field_trial.h"
11 #include "base/strings/string_split.h"
12 #include "chrome/common/variations/fieldtrial_testing_config.h"
13 #include "components/variations/variations_associated_data.h"
14 #include "net/base/escape.h"
16 namespace chrome_variations {
18 namespace {
20 std::string EscapeValue(const std::string& value) {
21 return net::UnescapeURLComponent(value, net::UnescapeRule::URL_SPECIAL_CHARS);
24 } // namespace
26 bool AssociateParamsFromString(const std::string& varations_string) {
27 // Format: Trial1.Group1:k1/v1/k2/v2,Trial2.Group2:k1/v1/k2/v2
28 for (const base::StringPiece& experiment_group : base::SplitStringPiece(
29 varations_string, ",",
30 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
31 std::vector<base::StringPiece> experiment = base::SplitStringPiece(
32 experiment_group, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
33 if (experiment.size() != 2)
34 return false;
36 std::vector<std::string> group_parts = base::SplitString(
37 experiment[0], ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
38 if (group_parts.size() != 2)
39 return false;
41 std::vector<std::string> key_values = base::SplitString(
42 experiment[1], "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
43 if (key_values.size() % 2 != 0)
44 return false;
46 std::map<std::string, std::string> params;
47 for (size_t i = 0; i < key_values.size(); i += 2) {
48 std::string key = EscapeValue(key_values[i]);
49 std::string value = EscapeValue(key_values[i + 1]);
50 params[key] = value;
52 std::string trial = EscapeValue(group_parts[0]);
53 std::string group = EscapeValue(group_parts[1]);
54 variations::AssociateVariationParams(trial, group, params);
56 return true;
59 void AssociateParamsFromFieldTrialConfig(
60 const FieldTrialTestingConfig& config) {
61 for (size_t i = 0; i < config.groups_size; ++i) {
62 const FieldTrialTestingGroup& group = config.groups[i];
63 if (group.params_size != 0) {
64 std::map<std::string, std::string> params;
65 for (size_t j = 0; j < group.params_size; ++j) {
66 const FieldTrialGroupParams& param = group.params[j];
67 params[param.key] = param.value;
69 variations::AssociateVariationParams(group.study, group.group_name,
70 params);
72 base::FieldTrialList::CreateFieldTrial(group.study, group.group_name);
76 void AssociateDefaultFieldTrialConfig() {
77 AssociateParamsFromFieldTrialConfig(kFieldTrialConfig);
80 } // namespace chrome_variations