Chromium Code Reviews| Index: components/previews/previews_experiments.cc |
| diff --git a/components/previews/previews_experiments.cc b/components/previews/previews_experiments.cc |
| index 35abce728d98247e689e9e0d1fdcf9072d672928..c7f5d7578b77b307e72d208736f888d7fb01a5c7 100644 |
| --- a/components/previews/previews_experiments.cc |
| +++ b/components/previews/previews_experiments.cc |
| @@ -1,62 +1,110 @@ |
| // Copyright 2016 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| #include "components/previews/previews_experiments.h" |
| #include <map> |
| #include <string> |
| #include "base/metrics/field_trial.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_util.h" |
| #include "components/variations/variations_associated_data.h" |
| +namespace previews { |
| + |
| namespace { |
| // The group of client-side previews experiments. |
| const char kClientSidePreviewsFieldTrial[] = "ClientSidePreviews"; |
| const char kEnabled[] = "Enabled"; |
| // Allow offline pages to show for prohibitively slow networks. |
| const char kOfflinePagesSlowNetwork[] = "show_offline_pages"; |
| +// The number of most recent previews navigations the black list looks at to |
| +// determine if a domain is blacklisted. |
| +const char kStoredHistoryLength[] = "stored_history_length"; |
| + |
| +// The number of recent navigations that were opted out of that would trigger |
| +// the domain to be blacklisted. |
| +const char kOptOutThreshold[] = "opt_out_threshold"; |
| + |
| +// The amount of time a domain remains blacklisted due to opt outs. |
| +const char kBlackListDuration[] = "black_list_duration"; |
| + |
| // The string that corresponds to enabled for the variation param experiments. |
| const char kExperimentEnabled[] = "true"; |
| +// Returns the parameter value of |param| as a string. If there is no value for |
| +// |param|, returns an empty string. |
| +std::string ParamValue(const std::string& param) { |
| + if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) |
| + return std::string(); |
| + std::map<std::string, std::string> experiment_params; |
| + if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, |
| + &experiment_params)) { |
| + return std::string(); |
| + } |
| + std::map<std::string, std::string>::const_iterator it = |
| + experiment_params.find(param); |
| + if (it == experiment_params.end()) |
| + return std::string(); |
| + return it->second; |
| +} |
| + |
| } // namespace |
| -namespace previews { |
| +namespace params { |
| + |
| +size_t StoredHistoryLengthForBlackList() { |
| + std::string param_value = ParamValue(kStoredHistoryLength); |
| + size_t history_length; |
| + if (!base::StringToSizeT(param_value, &history_length)) { |
| + return 0; |
| + } |
| + return history_length; |
| +} |
| + |
| +int BlackListOptOutThreshold() { |
| + std::string param_value = ParamValue(kOptOutThreshold); |
| + int opt_out_threshold; |
| + if (!base::StringToInt(param_value, &opt_out_threshold)) { |
| + return 0; |
| + } |
| + return opt_out_threshold; |
| +} |
| + |
| +base::TimeDelta BlackListDuration() { |
| + std::string param_value = ParamValue(kBlackListDuration); |
| + int duration; |
| + if (!base::StringToInt(param_value, &duration)) { |
| + return base::TimeDelta::FromSeconds(0); |
|
tbansal1
2016/09/14 21:36:26
Can you rather use some meaningful default values?
RyanSturm
2016/09/14 22:41:26
Done.
|
| + } |
| + return base::TimeDelta::FromSeconds(duration); |
| +} |
| + |
| +bool BlackListParamsAreValid() { |
|
tbansal1
2016/09/14 21:36:26
This function should not be needed. I would rather
RyanSturm
2016/09/14 22:41:26
Done.
|
| + return BlackListDuration() > base::TimeDelta::FromSeconds(0) && |
| + BlackListOptOutThreshold > 0 && StoredHistoryLengthForBlackList() > 0; |
| +} |
| + |
| +} // namespace params |
| bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { |
| // By convention, an experiment in the client-side previews study enables use |
| // of at least one client-side previews optimization if its name begins with |
| // "Enabled." |
| return base::StartsWith( |
| base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), |
| kEnabled, base::CompareCase::SENSITIVE); |
| } |
| bool IsOfflinePreviewsEnabled() { |
| - if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) |
| - return false; |
| - std::map<std::string, std::string> experiment_params; |
| - if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, |
| - &experiment_params)) { |
| - return false; |
| - } |
| - std::map<std::string, std::string>::const_iterator it = |
| - experiment_params.find(kOfflinePagesSlowNetwork); |
| - return it != experiment_params.end() && it->second == kExperimentEnabled; |
| -} |
| - |
| -bool EnableOfflinePreviewsForTesting() { |
| - std::map<std::string, std::string> params; |
| - params[kOfflinePagesSlowNetwork] = kExperimentEnabled; |
| - return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, |
| - kEnabled, params) && |
| - base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, |
| - kEnabled); |
| + std::string param_value = ParamValue(kOfflinePagesSlowNetwork); |
| + return param_value == kExperimentEnabled; |
| } |
| } // namespace previews |