| 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/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "components/variations/variations_associated_data.h" | 14 #include "components/variations/variations_associated_data.h" |
| 15 | 15 |
| 16 namespace previews { | 16 namespace previews { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // The group of client-side previews experiments. | 20 // The group of client-side previews experiments. |
| 21 const char kClientSidePreviewsFieldTrial[] = "ClientSidePreviews"; | 21 const char kClientSidePreviewsFieldTrial[] = "ClientSidePreviews"; |
| 22 | 22 |
| 23 const char kEnabled[] = "Enabled"; | 23 const char kEnabled[] = "Enabled"; |
| 24 | 24 |
| 25 // Allow offline pages to show for prohibitively slow networks. | 25 // Allow offline pages to show for prohibitively slow networks. |
| 26 const char kOfflinePagesSlowNetwork[] = "show_offline_pages"; | 26 const char kOfflinePagesSlowNetwork[] = "show_offline_pages"; |
| 27 | 27 |
| 28 // The maximum number of recent previews navigations the black list looks at to | 28 // The maximum number of recent previews navigations the black list looks at to |
| 29 // determine if a host is blacklisted. | 29 // determine if a host is blacklisted. |
| 30 const char kMaxStoredHistoryLength[] = "stored_history_length"; | 30 const char kMaxStoredHistoryLengthPerHost[] = |
| 31 "per_host_max_stored_history_length"; |
| 32 |
| 33 // The maximum number of recent previews navigations the black list looks at to |
| 34 // determine if all previews navigations should be disallowed. |
| 35 const char kMaxStoredHistoryLengthHostIndifferent[] = |
| 36 "host_indifferent_max_stored_history_length"; |
| 31 | 37 |
| 32 // The maximum number of hosts allowed in the in memory black list. | 38 // The maximum number of hosts allowed in the in memory black list. |
| 33 const char kMaxHostsInBlackList[] = "max_hosts_in_blacklist"; | 39 const char kMaxHostsInBlackList[] = "max_hosts_in_blacklist"; |
| 34 | 40 |
| 35 // The number of recent navigations that were opted out of that would trigger | 41 // The number of recent navigations that were opted out of that would trigger |
| 36 // the host to be blacklisted. | 42 // the host to be blacklisted. |
| 37 const char kOptOutThreshold[] = "opt_out_threshold"; | 43 const char kPerHostOptOutThreshold[] = "per_host_opt_out_threshold"; |
| 44 |
| 45 // The number of recent navigations that were opted out of that would trigger |
| 46 // all previews navigations to be disallowed. |
| 47 const char kHostIndifferentOptOutThreshold[] = |
| 48 "host_indifferent_opt_out_threshold"; |
| 38 | 49 |
| 39 // The amount of time a host remains blacklisted due to opt outs. | 50 // The amount of time a host remains blacklisted due to opt outs. |
| 40 const char kBlackListDurationInDays[] = "black_list_duration_in_days"; | 51 const char kPerHostBlackListDurationInDays[] = |
| 52 "per_host_black_list_duration_in_days"; |
| 53 |
| 54 // The amount of time a host remains blacklisted due to opt outs. |
| 55 const char kHostIndifferentBlackListDurationInDays[] = |
| 56 "host_indifferent_black_list_duration_in_days"; |
| 41 | 57 |
| 42 // The amount of time after any opt out that no previews should be shown. | 58 // The amount of time after any opt out that no previews should be shown. |
| 43 const char kSingleOptOutDurationInSeconds[] = | 59 const char kSingleOptOutDurationInSeconds[] = |
| 44 "single_opt_out_duration_in_seconds"; | 60 "single_opt_out_duration_in_seconds"; |
| 45 | 61 |
| 46 // The string that corresponds to enabled for the variation param experiments. | 62 // The string that corresponds to enabled for the variation param experiments. |
| 47 const char kExperimentEnabled[] = "true"; | 63 const char kExperimentEnabled[] = "true"; |
| 48 | 64 |
| 49 // In seconds. Hosts are blacklisted for 30 days. | |
| 50 const int kDefaultBlackListDurationInDays = 30; | |
| 51 | |
| 52 // In seconds. Previews are not shown for 5 minutes after an opt out. | |
| 53 constexpr int kDefaultSingleOptOutDurationInSeconds = 60 * 5; | |
| 54 | |
| 55 // 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 |
| 56 // |param|, returns an empty string. | 66 // |param|, returns an empty string. |
| 57 std::string ParamValue(const std::string& param) { | 67 std::string ParamValue(const std::string& param) { |
| 58 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) | 68 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) |
| 59 return std::string(); | 69 return std::string(); |
| 60 std::map<std::string, std::string> experiment_params; | 70 std::map<std::string, std::string> experiment_params; |
| 61 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, | 71 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, |
| 62 &experiment_params)) { | 72 &experiment_params)) { |
| 63 return std::string(); | 73 return std::string(); |
| 64 } | 74 } |
| 65 std::map<std::string, std::string>::const_iterator it = | 75 std::map<std::string, std::string>::const_iterator it = |
| 66 experiment_params.find(param); | 76 experiment_params.find(param); |
| 67 return it == experiment_params.end() ? std::string() : it->second; | 77 return it == experiment_params.end() ? std::string() : it->second; |
| 68 } | 78 } |
| 69 | 79 |
| 70 } // namespace | 80 } // namespace |
| 71 | 81 |
| 72 namespace params { | 82 namespace params { |
| 73 | 83 |
| 74 size_t MaxStoredHistoryLengthForBlackList() { | 84 size_t MaxStoredHistoryLengthForPerHostBlackList() { |
| 75 std::string param_value = ParamValue(kMaxStoredHistoryLength); | 85 std::string param_value = ParamValue(kMaxStoredHistoryLengthPerHost); |
| 76 size_t history_length; | 86 size_t history_length; |
| 77 if (!base::StringToSizeT(param_value, &history_length)) { | 87 if (!base::StringToSizeT(param_value, &history_length)) { |
| 78 return 4; | 88 return 4; |
| 79 } | 89 } |
| 80 return history_length; | 90 return history_length; |
| 81 } | 91 } |
| 82 | 92 |
| 93 size_t MaxStoredHistoryLengthForHostIndifferentBlackList() { |
| 94 std::string param_value = ParamValue(kMaxStoredHistoryLengthHostIndifferent); |
| 95 size_t history_length; |
| 96 if (!base::StringToSizeT(param_value, &history_length)) { |
| 97 return 10; |
| 98 } |
| 99 return history_length; |
| 100 } |
| 101 |
| 83 size_t MaxInMemoryHostsInBlackList() { | 102 size_t MaxInMemoryHostsInBlackList() { |
| 84 std::string param_value = ParamValue(kMaxHostsInBlackList); | 103 std::string param_value = ParamValue(kMaxHostsInBlackList); |
| 85 size_t max_hosts; | 104 size_t max_hosts; |
| 86 if (!base::StringToSizeT(param_value, &max_hosts)) { | 105 if (!base::StringToSizeT(param_value, &max_hosts)) { |
| 87 return 100; | 106 return 100; |
| 88 } | 107 } |
| 89 return max_hosts; | 108 return max_hosts; |
| 90 } | 109 } |
| 91 | 110 |
| 92 int BlackListOptOutThreshold() { | 111 int PerHostBlackListOptOutThreshold() { |
| 93 std::string param_value = ParamValue(kOptOutThreshold); | 112 std::string param_value = ParamValue(kPerHostOptOutThreshold); |
| 94 int opt_out_threshold; | 113 int opt_out_threshold; |
| 95 if (!base::StringToInt(param_value, &opt_out_threshold)) { | 114 if (!base::StringToInt(param_value, &opt_out_threshold)) { |
| 96 return 2; | 115 return 2; |
| 97 } | 116 } |
| 98 return opt_out_threshold; | 117 return opt_out_threshold; |
| 99 } | 118 } |
| 100 | 119 |
| 101 base::TimeDelta BlackListDuration() { | 120 int HostIndifferentBlackListOptOutThreshold() { |
| 102 std::string param_value = ParamValue(kBlackListDurationInDays); | 121 std::string param_value = ParamValue(kHostIndifferentOptOutThreshold); |
| 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); |
| 103 int duration; | 131 int duration; |
| 104 if (!base::StringToInt(param_value, &duration)) { | 132 if (!base::StringToInt(param_value, &duration)) { |
| 105 return base::TimeDelta::FromDays(kDefaultBlackListDurationInDays); | 133 return base::TimeDelta::FromDays(30); |
| 134 } |
| 135 return base::TimeDelta::FromDays(duration); |
| 136 } |
| 137 |
| 138 base::TimeDelta HostIndifferentBlackListPerHostDuration() { |
| 139 std::string param_value = ParamValue(kHostIndifferentBlackListDurationInDays); |
| 140 int duration; |
| 141 if (!base::StringToInt(param_value, &duration)) { |
| 142 return base::TimeDelta::FromDays(365 * 100); |
| 106 } | 143 } |
| 107 return base::TimeDelta::FromDays(duration); | 144 return base::TimeDelta::FromDays(duration); |
| 108 } | 145 } |
| 109 | 146 |
| 110 base::TimeDelta SingleOptOutDuration() { | 147 base::TimeDelta SingleOptOutDuration() { |
| 111 std::string param_value = ParamValue(kSingleOptOutDurationInSeconds); | 148 std::string param_value = ParamValue(kSingleOptOutDurationInSeconds); |
| 112 int duration; | 149 int duration; |
| 113 if (!base::StringToInt(param_value, &duration)) { | 150 if (!base::StringToInt(param_value, &duration)) { |
| 114 return base::TimeDelta::FromSeconds(kDefaultSingleOptOutDurationInSeconds); | 151 return base::TimeDelta::FromSeconds(60 * 5); |
| 115 } | 152 } |
| 116 return base::TimeDelta::FromSeconds(duration); | 153 return base::TimeDelta::FromSeconds(duration); |
| 117 } | 154 } |
| 118 | 155 |
| 119 } // namespace params | 156 } // namespace params |
| 120 | 157 |
| 121 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { | 158 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { |
| 122 // By convention, an experiment in the client-side previews study enables use | 159 // By convention, an experiment in the client-side previews study enables use |
| 123 // of at least one client-side previews optimization if its name begins with | 160 // of at least one client-side previews optimization if its name begins with |
| 124 // "Enabled." | 161 // "Enabled." |
| (...skipping 15 matching lines...) Expand all Loading... |
| 140 bool EnableOfflinePreviewsForTesting() { | 177 bool EnableOfflinePreviewsForTesting() { |
| 141 std::map<std::string, std::string> params; | 178 std::map<std::string, std::string> params; |
| 142 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; | 179 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; |
| 143 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, | 180 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, |
| 144 kEnabled, params) && | 181 kEnabled, params) && |
| 145 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, | 182 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, |
| 146 kEnabled); | 183 kEnabled); |
| 147 } | 184 } |
| 148 | 185 |
| 149 } // namespace previews | 186 } // namespace previews |
| OLD | NEW |