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/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 | |
|
tbansal1
2016/09/15 16:34:25
s/The number of most recent/The maximum number of
RyanSturm
2016/09/19 18:07:25
Done.
| |
| 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"; | |
|
tbansal1
2016/09/15 16:34:25
Specify the units of duration both in the var name
RyanSturm
2016/09/19 18:07:25
Done.
| |
| 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 // In seconds. Domains are blacklisted for 30 days. | |
| 42 constexpr int kDefaultBlackListDuration = 30 * 24 * 60 * 60; | |
|
tbansal1
2016/09/15 16:34:25
may be change to kDefaultBlackListDurationDays = 3
RyanSturm
2016/09/19 18:07:25
Done.
| |
| 43 | |
| 44 // Returns the parameter value of |param| as a string. If there is no value for | |
| 45 // |param|, returns an empty string. | |
| 46 std::string ParamValue(const std::string& param) { | |
| 47 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) | |
| 48 return std::string(); | |
| 49 std::map<std::string, std::string> experiment_params; | |
| 50 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, | |
| 51 &experiment_params)) { | |
| 52 return std::string(); | |
| 53 } | |
| 54 std::map<std::string, std::string>::const_iterator it = | |
| 55 experiment_params.find(param); | |
| 56 if (it == experiment_params.end()) | |
|
tbansal1
2016/09/15 16:34:25
replace the last three lines by a ternary operator
RyanSturm
2016/09/19 18:07:25
Done.
| |
| 57 return std::string(); | |
| 58 return it->second; | |
| 59 } | |
| 60 | |
| 27 } // namespace | 61 } // namespace |
| 28 | 62 |
| 29 namespace previews { | 63 namespace params { |
| 64 | |
| 65 size_t StoredHistoryLengthForBlackList() { | |
| 66 std::string param_value = ParamValue(kStoredHistoryLength); | |
| 67 size_t history_length; | |
| 68 if (!base::StringToSizeT(param_value, &history_length)) { | |
| 69 return 4; | |
|
tbansal1
2016/09/15 16:34:25
Note that this returns "4" even if the Chrome is n
RyanSturm
2016/09/19 18:07:25
I believe so, the blacklist won't be used until pr
| |
| 70 } | |
| 71 return history_length; | |
| 72 } | |
| 73 | |
| 74 int BlackListOptOutThreshold() { | |
| 75 std::string param_value = ParamValue(kOptOutThreshold); | |
| 76 int opt_out_threshold; | |
| 77 if (!base::StringToInt(param_value, &opt_out_threshold)) { | |
| 78 return 2; | |
| 79 } | |
| 80 return opt_out_threshold; | |
| 81 } | |
| 82 | |
| 83 base::TimeDelta BlackListDuration() { | |
| 84 std::string param_value = ParamValue(kBlackListDuration); | |
| 85 int duration; | |
| 86 if (!base::StringToInt(param_value, &duration)) { | |
| 87 return base::TimeDelta::FromSeconds(kDefaultBlackListDuration); | |
| 88 } | |
| 89 return base::TimeDelta::FromSeconds(duration); | |
| 90 } | |
| 91 | |
| 92 } // namespace params | |
| 30 | 93 |
| 31 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { | 94 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { |
| 32 // By convention, an experiment in the client-side previews study enables use | 95 // 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 | 96 // of at least one client-side previews optimization if its name begins with |
| 34 // "Enabled." | 97 // "Enabled." |
| 35 return base::StartsWith( | 98 return base::StartsWith( |
| 36 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), | 99 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), |
| 37 kEnabled, base::CompareCase::SENSITIVE); | 100 kEnabled, base::CompareCase::SENSITIVE); |
| 38 } | 101 } |
| 39 | 102 |
| 40 bool IsOfflinePreviewsEnabled() { | 103 bool IsOfflinePreviewsEnabled() { |
| 41 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) | 104 std::string param_value = ParamValue(kOfflinePagesSlowNetwork); |
| 42 return false; | 105 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 } | 106 } |
| 52 | 107 |
| 53 bool EnableOfflinePreviewsForTesting() { | 108 bool EnableOfflinePreviewsForTesting() { |
| 54 std::map<std::string, std::string> params; | 109 std::map<std::string, std::string> params; |
| 55 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; | 110 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; |
| 56 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, | 111 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, |
| 57 kEnabled, params) && | 112 kEnabled, params) && |
| 58 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, | 113 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, |
| 59 kEnabled); | 114 kEnabled); |
| 60 } | 115 } |
| 61 | 116 |
| 62 } // namespace previews | 117 } // namespace previews |
| OLD | NEW |