Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / common / variations / variations_util.cc
blob3c283368c91acac9df913587c0eca2336d12e763
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/child_process_logging.h"
13 #include "chrome/common/crash_keys.h"
14 #include "chrome/common/variations/fieldtrial_testing_config.h"
15 #include "components/variations/active_field_trials.h"
16 #include "components/variations/variations_associated_data.h"
17 #include "net/base/escape.h"
19 namespace chrome_variations {
21 namespace {
23 std::string EscapeValue(const std::string& value) {
24 return net::UnescapeURLComponent(value, net::UnescapeRule::URL_SPECIAL_CHARS);
27 } // namespace
29 void SetChildProcessLoggingVariationList() {
30 std::vector<std::string> experiment_strings;
31 variations::GetFieldTrialActiveGroupIdsAsStrings(&experiment_strings);
32 crash_keys::SetVariationsList(experiment_strings);
35 bool AssociateParamsFromString(const std::string& varations_string) {
36 // Format: Trial1.Group1:k1/v1/k2/v2,Trial2.Group2:k1/v1/k2/v2
37 for (const base::StringPiece& experiment_group : base::SplitStringPiece(
38 varations_string, ",",
39 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
40 std::vector<base::StringPiece> experiment = base::SplitStringPiece(
41 experiment_group, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
42 if (experiment.size() != 2)
43 return false;
45 std::vector<std::string> group_parts = base::SplitString(
46 experiment[0], ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
47 if (group_parts.size() != 2)
48 return false;
50 std::vector<std::string> key_values = base::SplitString(
51 experiment[1], "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
52 if (key_values.size() % 2 != 0)
53 return false;
55 std::map<std::string, std::string> params;
56 for (size_t i = 0; i < key_values.size(); i += 2) {
57 std::string key = EscapeValue(key_values[i]);
58 std::string value = EscapeValue(key_values[i + 1]);
59 params[key] = value;
61 std::string trial = EscapeValue(group_parts[0]);
62 std::string group = EscapeValue(group_parts[1]);
63 variations::AssociateVariationParams(trial, group, params);
65 return true;
68 void AssociateParamsFromFieldTrialConfig(
69 const FieldTrialTestingConfig& config) {
70 for (size_t i = 0; i < config.groups_size; ++i) {
71 const FieldTrialTestingGroup& group = config.groups[i];
72 if (group.params_size != 0) {
73 std::map<std::string, std::string> params;
74 for (size_t j = 0; j < group.params_size; ++j) {
75 const FieldTrialGroupParams& param = group.params[j];
76 params[param.key] = param.value;
78 variations::AssociateVariationParams(group.study, group.group_name,
79 params);
81 base::FieldTrialList::CreateFieldTrial(group.study, group.group_name);
85 void AssociateDefaultFieldTrialConfig() {
86 AssociateParamsFromFieldTrialConfig(kFieldTrialConfig);
89 } // namespace chrome_variations