OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/previews/previews_experiments.h" | 5 #include "components/previews/previews_experiments.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
11 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "components/variations/variations_associated_data.h" | 13 #include "components/variations/variations_associated_data.h" |
13 | 14 |
15 namespace previews { | |
16 | |
14 namespace { | 17 namespace { |
15 | 18 |
16 // The group of client-side previews experiments. | 19 // The group of client-side previews experiments. |
17 const char kClientSidePreviewsFieldTrial[] = "ClientSidePreviews"; | 20 const char kClientSidePreviewsFieldTrial[] = "ClientSidePreviews"; |
18 | 21 |
19 const char kEnabled[] = "Enabled"; | 22 const char kEnabled[] = "Enabled"; |
20 | 23 |
21 // Allow offline pages to show for prohibitively slow networks. | 24 // Allow offline pages to show for prohibitively slow networks. |
22 const char kOfflinePagesSlowNetwork[] = "show_offline_pages"; | 25 const char kOfflinePagesSlowNetwork[] = "show_offline_pages"; |
23 | 26 |
27 // The number of most recent previews navigations the black list looks at to | |
28 // determine if a domain is blacklisted. | |
29 const char kStoredHistoryLength[] = "stored_history_length"; | |
30 | |
31 // The number of recent navigations that were opted out of that would trigger | |
32 // the domain to be blacklisted. | |
33 const char kOptOutThreshold[] = "opt_out_threshold"; | |
34 | |
35 // The amount of time a domain remains blacklisted due to opt outs. | |
36 const char kBlackListDuration[] = "black_list_duration"; | |
37 | |
24 // The string that corresponds to enabled for the variation param experiments. | 38 // The string that corresponds to enabled for the variation param experiments. |
25 const char kExperimentEnabled[] = "true"; | 39 const char kExperimentEnabled[] = "true"; |
26 | 40 |
41 // Returns the parameter value of |param| as a string. If there is no value for | |
42 // |param|, reutnrs an empty string. | |
tbansal1
2016/09/14 17:17:29
typo in returns
RyanSturm
2016/09/14 18:36:43
Done.
| |
43 std::string ParamValue(std::string param) { | |
tbansal1
2016/09/14 17:17:30
pass |param| by const ref
RyanSturm
2016/09/14 18:36:43
Done.
| |
44 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) | |
45 return std::string(); | |
46 std::map<std::string, std::string> experiment_params; | |
47 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, | |
48 &experiment_params)) { | |
49 return std::string(); | |
50 } | |
51 std::map<std::string, std::string>::const_iterator it = | |
52 experiment_params.find(param); | |
53 if (it == experiment_params.end()) | |
54 return std::string(); | |
55 return it->second; | |
56 } | |
57 | |
27 } // namespace | 58 } // namespace |
28 | 59 |
29 namespace previews { | 60 namespace params { |
61 | |
62 int StoredHistoryLengthForBlackList() { | |
63 std::string param_value = ParamValue(kStoredHistoryLength); | |
64 int history_length; | |
65 if (!base::StringToInt(param_value, &history_length)) { | |
66 return 0; | |
67 } | |
68 return history_length; | |
69 } | |
70 | |
71 int BlackListOptOutThreshold() { | |
72 std::string param_value = ParamValue(kOptOutThreshold); | |
73 int opt_out_threshold; | |
74 if (!base::StringToInt(param_value, &opt_out_threshold)) { | |
75 return 0; | |
76 } | |
77 return opt_out_threshold; | |
78 } | |
79 | |
80 base::TimeDelta BlackListDuration() { | |
81 std::string param_value = ParamValue(kBlackListDuration); | |
82 int duration; | |
83 if (!base::StringToInt(param_value, &duration)) { | |
84 return base::TimeDelta::FromSeconds(0); | |
85 } | |
86 return base::TimeDelta::FromSeconds(duration); | |
87 } | |
88 | |
89 bool BlackListParamsAreValid() { | |
90 return BlackListDuration() > base::TimeDelta::FromSeconds(0) && | |
91 BlackListOptOutThreshold > 0 && StoredHistoryLengthForBlackList() > 0; | |
92 } | |
tbansal1
2016/09/14 20:17:50
nit: space between params.
also, add the comment /
RyanSturm
2016/09/14 21:01:03
Done.
| |
93 } | |
30 | 94 |
31 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { | 95 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { |
32 // By convention, an experiment in the client-side previews study enables use | 96 // By convention, an experiment in the client-side previews study enables use |
33 // of at least one client-side previews optimization if its name begins with | 97 // of at least one client-side previews optimization if its name begins with |
34 // "Enabled." | 98 // "Enabled." |
35 return base::StartsWith( | 99 return base::StartsWith( |
36 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), | 100 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), |
37 kEnabled, base::CompareCase::SENSITIVE); | 101 kEnabled, base::CompareCase::SENSITIVE); |
38 } | 102 } |
39 | 103 |
40 bool IsOfflinePreviewsEnabled() { | 104 bool IsOfflinePreviewsEnabled() { |
41 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) | 105 std::string param_value = ParamValue(kOfflinePagesSlowNetwork); |
42 return false; | 106 return param_value == kExperimentEnabled; |
43 std::map<std::string, std::string> experiment_params; | |
44 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, | |
45 &experiment_params)) { | |
46 return false; | |
47 } | |
48 std::map<std::string, std::string>::const_iterator it = | |
49 experiment_params.find(kOfflinePagesSlowNetwork); | |
50 return it != experiment_params.end() && it->second == kExperimentEnabled; | |
51 } | 107 } |
52 | 108 |
53 bool EnableOfflinePreviewsForTesting() { | 109 bool EnableOfflinePreviewsForTesting() { |
54 std::map<std::string, std::string> params; | 110 std::map<std::string, std::string> params; |
55 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; | 111 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; |
56 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, | 112 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, |
57 kEnabled, params) && | 113 kEnabled, params) && |
58 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, | 114 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, |
59 kEnabled); | 115 kEnabled); |
60 } | 116 } |
61 | 117 |
118 bool EnableBlackListParamsForTesting(const int history_length, | |
119 const int opt_out_threshold, | |
tbansal1
2016/09/14 17:17:30
Why is this function needed? Can you not set the v
RyanSturm
2016/09/14 18:36:43
This uses internal strings that I don't need to ex
tbansal1
2016/09/14 20:17:50
These are not really internal strings since they w
RyanSturm
2016/09/14 21:01:03
Done.
| |
120 const int duration_time_in_seconds) { | |
121 std::map<std::string, std::string> params; | |
122 params[kStoredHistoryLength] = base::IntToString(history_length); | |
123 params[kOptOutThreshold] = base::IntToString(opt_out_threshold); | |
124 params[kBlackListDuration] = base::IntToString(duration_time_in_seconds); | |
125 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, | |
126 kEnabled, params) && | |
127 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, | |
128 kEnabled); | |
129 } | |
130 | |
62 } // namespace previews | 131 } // namespace previews |
OLD | NEW |