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/core/previews_experiments.h" | 5 #include "components/previews/core/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 maximum number of recent previews navigations the black list looks at to |
| 28 // determine if a domain is blacklisted. |
| 29 const char kMaxStoredHistoryLength[] = "stored_history_length"; |
| 30 |
| 31 // The maximum number of hosts allowed in the in memory black list. |
| 32 const char kMaxHostsInBlackList[] = "max_hosts_in_blacklist"; |
| 33 |
| 34 // The number of recent navigations that were opted out of that would trigger |
| 35 // the domain to be blacklisted. |
| 36 const char kOptOutThreshold[] = "opt_out_threshold"; |
| 37 |
| 38 // The amount of time a domain remains blacklisted due to opt outs. |
| 39 const char kBlackListDurationInDays[] = "black_list_duration_in_days"; |
| 40 |
24 // The string that corresponds to enabled for the variation param experiments. | 41 // The string that corresponds to enabled for the variation param experiments. |
25 const char kExperimentEnabled[] = "true"; | 42 const char kExperimentEnabled[] = "true"; |
26 | 43 |
| 44 // In seconds. Domains are blacklisted for 30 days. |
| 45 constexpr int kDefaultBlackListDurationInDays = 30; |
| 46 |
| 47 // Returns the parameter value of |param| as a string. If there is no value for |
| 48 // |param|, returns an empty string. |
| 49 std::string ParamValue(const std::string& param) { |
| 50 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) |
| 51 return std::string(); |
| 52 std::map<std::string, std::string> experiment_params; |
| 53 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, |
| 54 &experiment_params)) { |
| 55 return std::string(); |
| 56 } |
| 57 std::map<std::string, std::string>::const_iterator it = |
| 58 experiment_params.find(param); |
| 59 return it == experiment_params.end() ? std::string() : it->second; |
| 60 } |
| 61 |
27 } // namespace | 62 } // namespace |
28 | 63 |
29 namespace previews { | 64 namespace params { |
| 65 |
| 66 size_t MaxStoredHistoryLengthForBlackList() { |
| 67 std::string param_value = ParamValue(kMaxStoredHistoryLength); |
| 68 size_t history_length; |
| 69 if (!base::StringToSizeT(param_value, &history_length)) { |
| 70 return 4; |
| 71 } |
| 72 return history_length; |
| 73 } |
| 74 |
| 75 size_t MaxInMemoryHostsInBlackList() { |
| 76 std::string param_value = ParamValue(kMaxHostsInBlackList); |
| 77 size_t max_hosts; |
| 78 if (!base::StringToSizeT(param_value, &max_hosts)) { |
| 79 return 100; |
| 80 } |
| 81 return max_hosts; |
| 82 } |
| 83 |
| 84 int BlackListOptOutThreshold() { |
| 85 std::string param_value = ParamValue(kOptOutThreshold); |
| 86 int opt_out_threshold; |
| 87 if (!base::StringToInt(param_value, &opt_out_threshold)) { |
| 88 return 2; |
| 89 } |
| 90 return opt_out_threshold; |
| 91 } |
| 92 |
| 93 base::TimeDelta BlackListDuration() { |
| 94 std::string param_value = ParamValue(kBlackListDurationInDays); |
| 95 int duration; |
| 96 if (!base::StringToInt(param_value, &duration)) { |
| 97 return base::TimeDelta::FromDays(kDefaultBlackListDurationInDays); |
| 98 } |
| 99 return base::TimeDelta::FromDays(duration); |
| 100 } |
| 101 |
| 102 } // namespace params |
30 | 103 |
31 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { | 104 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { |
32 // By convention, an experiment in the client-side previews study enables use | 105 // 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 | 106 // of at least one client-side previews optimization if its name begins with |
34 // "Enabled." | 107 // "Enabled." |
35 return base::StartsWith( | 108 return base::StartsWith( |
36 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), | 109 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), |
37 kEnabled, base::CompareCase::SENSITIVE); | 110 kEnabled, base::CompareCase::SENSITIVE); |
38 } | 111 } |
39 | 112 |
40 bool IsOfflinePreviewsEnabled() { | 113 bool IsOfflinePreviewsEnabled() { |
41 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) | 114 std::string param_value = ParamValue(kOfflinePagesSlowNetwork); |
42 return false; | 115 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 } | 116 } |
52 | 117 |
53 bool EnableOfflinePreviewsForTesting() { | 118 bool EnableOfflinePreviewsForTesting() { |
54 std::map<std::string, std::string> params; | 119 std::map<std::string, std::string> params; |
55 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; | 120 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; |
56 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, | 121 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, |
57 kEnabled, params) && | 122 kEnabled, params) && |
58 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, | 123 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, |
59 kEnabled); | 124 kEnabled); |
60 } | 125 } |
61 | 126 |
62 } // namespace previews | 127 } // namespace previews |
OLD | NEW |