| 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 kMaxStoredHistoryLengthHostIndifferent[] = |
| 35 "host_indifferent_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 kHostIndifferentOptOutThreshold[] = |
| 47 "host_indifferent_opt_out_threshold"; |
| 37 | 48 |
| 38 // The amount of time a host remains blacklisted due to opt outs. | 49 // The amount of time a host remains blacklisted due to opt outs. |
| 39 const char kBlackListDurationInDays[] = "black_list_duration_in_days"; | 50 const char kPerHostBlackListDurationInDays[] = |
| 51 "per_host_black_list_duration_in_days"; |
| 52 |
| 53 // The amount of time a host remains blacklisted due to opt outs. |
| 54 const char kHostIndifferentBlackListDurationInDays[] = |
| 55 "host_indifferent_black_list_duration_in_days"; |
| 40 | 56 |
| 41 // The string that corresponds to enabled for the variation param experiments. | 57 // The string that corresponds to enabled for the variation param experiments. |
| 42 const char kExperimentEnabled[] = "true"; | 58 const char kExperimentEnabled[] = "true"; |
| 43 | 59 |
| 44 // In seconds. Hosts are blacklisted for 30 days. | 60 // Hosts are blacklisted for 30 days. |
| 45 constexpr int kDefaultBlackListDurationInDays = 30; | 61 const int kDefaultPerHostDurationInDays = 30; |
| 62 |
| 63 // Host indifferent previews navigations are disallowed for 100 years. |
| 64 constexpr int kDefaultHostIndifferentDurationInDays = 365 * 100; |
| 46 | 65 |
| 47 // Returns the parameter value of |param| as a string. If there is no value for | 66 // Returns the parameter value of |param| as a string. If there is no value for |
| 48 // |param|, returns an empty string. | 67 // |param|, returns an empty string. |
| 49 std::string ParamValue(const std::string& param) { | 68 std::string ParamValue(const std::string& param) { |
| 50 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) | 69 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) |
| 51 return std::string(); | 70 return std::string(); |
| 52 std::map<std::string, std::string> experiment_params; | 71 std::map<std::string, std::string> experiment_params; |
| 53 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, | 72 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, |
| 54 &experiment_params)) { | 73 &experiment_params)) { |
| 55 return std::string(); | 74 return std::string(); |
| 56 } | 75 } |
| 57 std::map<std::string, std::string>::const_iterator it = | 76 std::map<std::string, std::string>::const_iterator it = |
| 58 experiment_params.find(param); | 77 experiment_params.find(param); |
| 59 return it == experiment_params.end() ? std::string() : it->second; | 78 return it == experiment_params.end() ? std::string() : it->second; |
| 60 } | 79 } |
| 61 | 80 |
| 62 } // namespace | 81 } // namespace |
| 63 | 82 |
| 64 namespace params { | 83 namespace params { |
| 65 | 84 |
| 66 size_t MaxStoredHistoryLengthForBlackList() { | 85 size_t MaxStoredHistoryLengthForPerHostBlackList() { |
| 67 std::string param_value = ParamValue(kMaxStoredHistoryLength); | 86 std::string param_value = ParamValue(kMaxStoredHistoryLengthPerHost); |
| 68 size_t history_length; | 87 size_t history_length; |
| 69 if (!base::StringToSizeT(param_value, &history_length)) { | 88 if (!base::StringToSizeT(param_value, &history_length)) { |
| 70 return 4; | 89 return 4; |
| 71 } | 90 } |
| 72 return history_length; | 91 return history_length; |
| 73 } | 92 } |
| 74 | 93 |
| 94 size_t MaxStoredHistoryLengthForHostIndifferentBlackList() { |
| 95 std::string param_value = ParamValue(kMaxStoredHistoryLengthHostIndifferent); |
| 96 size_t history_length; |
| 97 if (!base::StringToSizeT(param_value, &history_length)) { |
| 98 return 10; |
| 99 } |
| 100 return history_length; |
| 101 } |
| 102 |
| 75 size_t MaxInMemoryHostsInBlackList() { | 103 size_t MaxInMemoryHostsInBlackList() { |
| 76 std::string param_value = ParamValue(kMaxHostsInBlackList); | 104 std::string param_value = ParamValue(kMaxHostsInBlackList); |
| 77 size_t max_hosts; | 105 size_t max_hosts; |
| 78 if (!base::StringToSizeT(param_value, &max_hosts)) { | 106 if (!base::StringToSizeT(param_value, &max_hosts)) { |
| 79 return 100; | 107 return 100; |
| 80 } | 108 } |
| 81 return max_hosts; | 109 return max_hosts; |
| 82 } | 110 } |
| 83 | 111 |
| 84 int BlackListOptOutThreshold() { | 112 int PerHostBlackListOptOutThreshold() { |
| 85 std::string param_value = ParamValue(kOptOutThreshold); | 113 std::string param_value = ParamValue(kPerHostOptOutThreshold); |
| 86 int opt_out_threshold; | 114 int opt_out_threshold; |
| 87 if (!base::StringToInt(param_value, &opt_out_threshold)) { | 115 if (!base::StringToInt(param_value, &opt_out_threshold)) { |
| 88 return 2; | 116 return 2; |
| 89 } | 117 } |
| 90 return opt_out_threshold; | 118 return opt_out_threshold; |
| 91 } | 119 } |
| 92 | 120 |
| 93 base::TimeDelta BlackListDuration() { | 121 int HostIndifferentBlackListOptOutThreshold() { |
| 94 std::string param_value = ParamValue(kBlackListDurationInDays); | 122 std::string param_value = ParamValue(kHostIndifferentOptOutThreshold); |
| 123 int opt_out_threshold; |
| 124 if (!base::StringToInt(param_value, &opt_out_threshold)) { |
| 125 return 4; |
| 126 } |
| 127 return opt_out_threshold; |
| 128 } |
| 129 |
| 130 base::TimeDelta PerHostBlackListDuration() { |
| 131 std::string param_value = ParamValue(kPerHostBlackListDurationInDays); |
| 95 int duration; | 132 int duration; |
| 96 if (!base::StringToInt(param_value, &duration)) { | 133 if (!base::StringToInt(param_value, &duration)) { |
| 97 return base::TimeDelta::FromDays(kDefaultBlackListDurationInDays); | 134 return base::TimeDelta::FromDays(kDefaultPerHostDurationInDays); |
| 135 } |
| 136 return base::TimeDelta::FromDays(duration); |
| 137 } |
| 138 |
| 139 base::TimeDelta HostIndifferentBlackListPerHostDuration() { |
| 140 std::string param_value = ParamValue(kHostIndifferentBlackListDurationInDays); |
| 141 int duration; |
| 142 if (!base::StringToInt(param_value, &duration)) { |
| 143 return base::TimeDelta::FromDays(kDefaultHostIndifferentDurationInDays); |
| 98 } | 144 } |
| 99 return base::TimeDelta::FromDays(duration); | 145 return base::TimeDelta::FromDays(duration); |
| 100 } | 146 } |
| 101 | 147 |
| 102 } // namespace params | 148 } // namespace params |
| 103 | 149 |
| 104 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { | 150 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { |
| 105 // By convention, an experiment in the client-side previews study enables use | 151 // 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 | 152 // of at least one client-side previews optimization if its name begins with |
| 107 // "Enabled." | 153 // "Enabled." |
| 108 return base::StartsWith( | 154 return base::StartsWith( |
| 109 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), | 155 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), |
| 110 kEnabled, base::CompareCase::SENSITIVE); | 156 kEnabled, base::CompareCase::SENSITIVE); |
| 111 } | 157 } |
| 112 | 158 |
| 113 bool IsOfflinePreviewsEnabled() { | 159 bool IsOfflinePreviewsEnabled() { |
| 114 return ParamValue(kOfflinePagesSlowNetwork) == kExperimentEnabled; | 160 return ParamValue(kOfflinePagesSlowNetwork) == kExperimentEnabled; |
| 115 } | 161 } |
| 116 | 162 |
| 117 bool EnableOfflinePreviewsForTesting() { | 163 bool EnableOfflinePreviewsForTesting() { |
| 118 std::map<std::string, std::string> params; | 164 std::map<std::string, std::string> params; |
| 119 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; | 165 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; |
| 120 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, | 166 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, |
| 121 kEnabled, params) && | 167 kEnabled, params) && |
| 122 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, | 168 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, |
| 123 kEnabled); | 169 kEnabled); |
| 124 } | 170 } |
| 125 | 171 |
| 126 } // namespace previews | 172 } // namespace previews |
| OLD | NEW |