| Index: components/previews/previews_experiments.cc
|
| diff --git a/components/previews/previews_experiments.cc b/components/previews/previews_experiments.cc
|
| index 35abce728d98247e689e9e0d1fdcf9072d672928..1cf61917d3ad595cf864e51a294154b2af1e945e 100644
|
| --- a/components/previews/previews_experiments.cc
|
| +++ b/components/previews/previews_experiments.cc
|
| @@ -1,62 +1,131 @@
|
| // 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|, reutnrs an empty string.
|
| +std::string ParamValue(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 {
|
| +
|
| +int StoredHistoryLengthForBlackList() {
|
| + std::string param_value = ParamValue(kStoredHistoryLength);
|
| + int history_length;
|
| + if (!base::StringToInt(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);
|
| + }
|
| + return base::TimeDelta::FromSeconds(duration);
|
| +}
|
| +
|
| +bool BlackListParamsAreValid() {
|
| + return BlackListDuration() > base::TimeDelta::FromSeconds(0) &&
|
| + BlackListOptOutThreshold > 0 && StoredHistoryLengthForBlackList() > 0;
|
| +}
|
| +}
|
|
|
| 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;
|
| + std::string param_value = ParamValue(kOfflinePagesSlowNetwork);
|
| + return param_value == kExperimentEnabled;
|
| }
|
|
|
| bool EnableOfflinePreviewsForTesting() {
|
| std::map<std::string, std::string> params;
|
| params[kOfflinePagesSlowNetwork] = kExperimentEnabled;
|
| return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial,
|
| kEnabled, params) &&
|
| base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial,
|
| kEnabled);
|
| }
|
|
|
| +bool EnableBlackListParamsForTesting(const int history_length,
|
| + const int opt_out_threshold,
|
| + const int duration_time_in_seconds) {
|
| + std::map<std::string, std::string> params;
|
| + params[kStoredHistoryLength] = base::IntToString(history_length);
|
| + params[kOptOutThreshold] = base::IntToString(opt_out_threshold);
|
| + params[kBlackListDuration] = base::IntToString(duration_time_in_seconds);
|
| + return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial,
|
| + kEnabled, params) &&
|
| + base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial,
|
| + kEnabled);
|
| +}
|
| +
|
| } // namespace previews
|
|
|