Chromium Code Reviews| 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_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "components/variations/variations_associated_data.h" | 13 #include "components/variations/variations_associated_data.h" |
| 14 | 14 |
| 15 namespace previews { | 15 namespace previews { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // The group of client-side previews experiments. | 19 // The group of client-side previews experiments. |
| 20 const char kClientSidePreviewsFieldTrial[] = "ClientSidePreviews"; | 20 const char kClientSidePreviewsFieldTrial[] = "ClientSidePreviews"; |
| 21 | 21 |
| 22 const char kEnabled[] = "Enabled"; | 22 const char kEnabled[] = "Enabled"; |
| 23 | 23 |
| 24 // Allow offline pages to show for prohibitively slow networks. | 24 // Allow offline pages to show for prohibitively slow networks. |
| 25 const char kOfflinePagesSlowNetwork[] = "show_offline_pages"; | 25 const char kOfflinePagesSlowNetwork[] = "show_offline_pages"; |
| 26 | 26 |
| 27 // The maximum number of recent previews navigations the black list looks at to | 27 // The maximum number of recent previews navigations the black list looks at to |
| 28 // determine if a host is blacklisted. | 28 // determine if a host is blacklisted. |
| 29 const char kMaxStoredHistoryLength[] = "stored_history_length"; | 29 const char kMaxStoredHistoryLengthPerHost[] = |
| 30 "per_host_max_stored_history_length"; | |
| 31 | |
| 32 // The maximum number of recent previews navigations the black list looks at to | |
| 33 // determine if all previews navigations should be disallowed. | |
| 34 const char kMaxStoredHistoryLengthGeneral[] = | |
| 35 "general_max_stored_history_length"; | |
| 30 | 36 |
| 31 // The maximum number of hosts allowed in the in memory black list. | 37 // The maximum number of hosts allowed in the in memory black list. |
| 32 const char kMaxHostsInBlackList[] = "max_hosts_in_blacklist"; | 38 const char kMaxHostsInBlackList[] = "max_hosts_in_blacklist"; |
| 33 | 39 |
| 34 // The number of recent navigations that were opted out of that would trigger | 40 // The number of recent navigations that were opted out of that would trigger |
| 35 // the host to be blacklisted. | 41 // the host to be blacklisted. |
| 36 const char kOptOutThreshold[] = "opt_out_threshold"; | 42 const char kPerHostOptOutThreshold[] = "per_host_opt_out_threshold"; |
| 43 | |
| 44 // The number of recent navigations that were opted out of that would trigger | |
| 45 // all previews navigations to be disallowed. | |
| 46 const char kGeneralOptOutThreshold[] = "general_opt_out_threshold"; | |
| 37 | 47 |
| 38 // The amount of time a host remains blacklisted due to opt outs. | 48 // The amount of time a host remains blacklisted due to opt outs. |
| 39 const char kBlackListDurationInDays[] = "black_list_duration_in_days"; | 49 const char kPerHostBlackListDurationInDays[] = |
| 50 "per_host_black_list_duration_in_days"; | |
| 51 | |
| 52 // The amount of time a host remains blacklisted due to opt outs. | |
| 53 const char kGeneralBlackListDurationInDays[] = | |
| 54 "general_black_list_duration_in_days"; | |
| 40 | 55 |
| 41 // The string that corresponds to enabled for the variation param experiments. | 56 // The string that corresponds to enabled for the variation param experiments. |
| 42 const char kExperimentEnabled[] = "true"; | 57 const char kExperimentEnabled[] = "true"; |
| 43 | 58 |
| 44 // In seconds. Hosts are blacklisted for 30 days. | 59 // Hosts are blacklisted for 30 days. |
| 45 constexpr int kDefaultBlackListDurationInDays = 30; | 60 const int kDefaultPerHostBlackListDurationInDays = 30; |
| 61 | |
| 62 // General previews navigations are disallowed for 100 years. | |
| 63 constexpr int kDefaultGeneralBlackListDurationInDays = 365 * 100; | |
|
tbansal1
2016/10/21 22:56:26
that's a bit long.
RyanSturm
2016/10/24 22:24:40
Done.
| |
| 46 | 64 |
| 47 // Returns the parameter value of |param| as a string. If there is no value for | 65 // Returns the parameter value of |param| as a string. If there is no value for |
| 48 // |param|, returns an empty string. | 66 // |param|, returns an empty string. |
| 49 std::string ParamValue(const std::string& param) { | 67 std::string ParamValue(const std::string& param) { |
| 50 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) | 68 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) |
| 51 return std::string(); | 69 return std::string(); |
| 52 std::map<std::string, std::string> experiment_params; | 70 std::map<std::string, std::string> experiment_params; |
| 53 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, | 71 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, |
| 54 &experiment_params)) { | 72 &experiment_params)) { |
| 55 return std::string(); | 73 return std::string(); |
| 56 } | 74 } |
| 57 std::map<std::string, std::string>::const_iterator it = | 75 std::map<std::string, std::string>::const_iterator it = |
| 58 experiment_params.find(param); | 76 experiment_params.find(param); |
| 59 return it == experiment_params.end() ? std::string() : it->second; | 77 return it == experiment_params.end() ? std::string() : it->second; |
| 60 } | 78 } |
| 61 | 79 |
| 62 } // namespace | 80 } // namespace |
| 63 | 81 |
| 64 namespace params { | 82 namespace params { |
| 65 | 83 |
| 66 size_t MaxStoredHistoryLengthForBlackList() { | 84 size_t MaxStoredHistoryLengthForPerHostBlackList() { |
| 67 std::string param_value = ParamValue(kMaxStoredHistoryLength); | 85 std::string param_value = ParamValue(kMaxStoredHistoryLengthPerHost); |
| 68 size_t history_length; | 86 size_t history_length; |
| 69 if (!base::StringToSizeT(param_value, &history_length)) { | 87 if (!base::StringToSizeT(param_value, &history_length)) { |
| 70 return 4; | 88 return 4; |
| 71 } | 89 } |
| 72 return history_length; | 90 return history_length; |
| 73 } | 91 } |
| 74 | 92 |
| 93 size_t MaxStoredHistoryLengthForGeneralBlackList() { | |
| 94 std::string param_value = ParamValue(kMaxStoredHistoryLengthGeneral); | |
| 95 size_t history_length; | |
| 96 if (!base::StringToSizeT(param_value, &history_length)) { | |
| 97 return 10; | |
| 98 } | |
| 99 return history_length; | |
| 100 } | |
| 101 | |
| 75 size_t MaxInMemoryHostsInBlackList() { | 102 size_t MaxInMemoryHostsInBlackList() { |
| 76 std::string param_value = ParamValue(kMaxHostsInBlackList); | 103 std::string param_value = ParamValue(kMaxHostsInBlackList); |
| 77 size_t max_hosts; | 104 size_t max_hosts; |
| 78 if (!base::StringToSizeT(param_value, &max_hosts)) { | 105 if (!base::StringToSizeT(param_value, &max_hosts)) { |
| 79 return 100; | 106 return 100; |
| 80 } | 107 } |
| 81 return max_hosts; | 108 return max_hosts; |
| 82 } | 109 } |
| 83 | 110 |
| 84 int BlackListOptOutThreshold() { | 111 int PerHostBlackListOptOutThreshold() { |
| 85 std::string param_value = ParamValue(kOptOutThreshold); | 112 std::string param_value = ParamValue(kPerHostOptOutThreshold); |
| 86 int opt_out_threshold; | 113 int opt_out_threshold; |
| 87 if (!base::StringToInt(param_value, &opt_out_threshold)) { | 114 if (!base::StringToInt(param_value, &opt_out_threshold)) { |
| 88 return 2; | 115 return 2; |
| 89 } | 116 } |
| 90 return opt_out_threshold; | 117 return opt_out_threshold; |
| 91 } | 118 } |
| 92 | 119 |
| 93 base::TimeDelta BlackListDuration() { | 120 int GeneralBlackListOptOutThreshold() { |
| 94 std::string param_value = ParamValue(kBlackListDurationInDays); | 121 std::string param_value = ParamValue(kGeneralOptOutThreshold); |
| 122 int opt_out_threshold; | |
| 123 if (!base::StringToInt(param_value, &opt_out_threshold)) { | |
| 124 return 4; | |
| 125 } | |
| 126 return opt_out_threshold; | |
| 127 } | |
| 128 | |
| 129 base::TimeDelta PerHostBlackListDuration() { | |
| 130 std::string param_value = ParamValue(kPerHostBlackListDurationInDays); | |
| 95 int duration; | 131 int duration; |
| 96 if (!base::StringToInt(param_value, &duration)) { | 132 if (!base::StringToInt(param_value, &duration)) { |
| 97 return base::TimeDelta::FromDays(kDefaultBlackListDurationInDays); | 133 return base::TimeDelta::FromDays(kDefaultPerHostBlackListDurationInDays); |
| 134 } | |
| 135 return base::TimeDelta::FromDays(duration); | |
| 136 } | |
| 137 | |
| 138 base::TimeDelta GeneralBlackListPerHostDuration() { | |
| 139 std::string param_value = ParamValue(kGeneralBlackListDurationInDays); | |
| 140 int duration; | |
| 141 if (!base::StringToInt(param_value, &duration)) { | |
| 142 return base::TimeDelta::FromDays(kDefaultGeneralBlackListDurationInDays); | |
| 98 } | 143 } |
| 99 return base::TimeDelta::FromDays(duration); | 144 return base::TimeDelta::FromDays(duration); |
| 100 } | 145 } |
| 101 | 146 |
| 102 } // namespace params | 147 } // namespace params |
| 103 | 148 |
| 104 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { | 149 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { |
| 105 // By convention, an experiment in the client-side previews study enables use | 150 // By convention, an experiment in the client-side previews study enables use |
| 106 // of at least one client-side previews optimization if its name begins with | 151 // of at least one client-side previews optimization if its name begins with |
| 107 // "Enabled." | 152 // "Enabled." |
| 108 return base::StartsWith( | 153 return base::StartsWith( |
| 109 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), | 154 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), |
| 110 kEnabled, base::CompareCase::SENSITIVE); | 155 kEnabled, base::CompareCase::SENSITIVE); |
| 111 } | 156 } |
| 112 | 157 |
| 113 bool IsOfflinePreviewsEnabled() { | 158 bool IsOfflinePreviewsEnabled() { |
| 114 return ParamValue(kOfflinePagesSlowNetwork) == kExperimentEnabled; | 159 return ParamValue(kOfflinePagesSlowNetwork) == kExperimentEnabled; |
| 115 } | 160 } |
| 116 | 161 |
| 117 bool EnableOfflinePreviewsForTesting() { | 162 bool EnableOfflinePreviewsForTesting() { |
| 118 std::map<std::string, std::string> params; | 163 std::map<std::string, std::string> params; |
| 119 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; | 164 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; |
| 120 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, | 165 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, |
| 121 kEnabled, params) && | 166 kEnabled, params) && |
| 122 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, | 167 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, |
| 123 kEnabled); | 168 kEnabled); |
| 124 } | 169 } |
| 125 | 170 |
| 126 } // namespace previews | 171 } // namespace previews |
| OLD | NEW |