| 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" |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 std::string param_value = | 182 std::string param_value = |
| 183 ClientSidePreviewsParamValue(kEffectiveConnectionTypeThreshold); | 183 ClientSidePreviewsParamValue(kEffectiveConnectionTypeThreshold); |
| 184 net::EffectiveConnectionType effective_connection_type; | 184 net::EffectiveConnectionType effective_connection_type; |
| 185 if (!net::GetEffectiveConnectionTypeForName(param_value, | 185 if (!net::GetEffectiveConnectionTypeForName(param_value, |
| 186 &effective_connection_type)) { | 186 &effective_connection_type)) { |
| 187 effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G; | 187 effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G; |
| 188 } | 188 } |
| 189 return effective_connection_type; | 189 return effective_connection_type; |
| 190 } | 190 } |
| 191 | 191 |
| 192 bool IsOfflinePreviewsEnabled() { |
| 193 // Check if "show_offline_pages" is set to "true". |
| 194 return ClientSidePreviewsParamValue(kOfflinePagesSlowNetwork) == |
| 195 kExperimentEnabled; |
| 196 } |
| 197 |
| 198 int OfflinePreviewsVersion() { |
| 199 int version; |
| 200 if (!base::StringToInt(ClientSidePreviewsParamValue(kVersion), &version)) |
| 201 version = 0; |
| 202 return version; |
| 203 } |
| 204 |
| 192 } // namespace params | 205 } // namespace params |
| 193 | 206 |
| 194 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { | 207 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { |
| 195 // By convention, an experiment in the client-side previews study enables use | 208 // By convention, an experiment in the client-side previews study enables use |
| 196 // of at least one client-side previews optimization if its name begins with | 209 // of at least one client-side previews optimization if its name begins with |
| 197 // "Enabled." | 210 // "Enabled." |
| 198 return base::StartsWith( | 211 return base::StartsWith( |
| 199 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), | 212 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), |
| 200 kEnabled, base::CompareCase::SENSITIVE); | 213 kEnabled, base::CompareCase::SENSITIVE); |
| 201 } | 214 } |
| 202 | 215 |
| 203 bool IsPreviewsTypeEnabled(PreviewsType type) { | |
| 204 switch (type) { | |
| 205 case PreviewsType::OFFLINE: | |
| 206 return ClientSidePreviewsParamValue(kOfflinePagesSlowNetwork) == | |
| 207 kExperimentEnabled; | |
| 208 default: | |
| 209 NOTREACHED(); | |
| 210 return false; | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 int GetPreviewsTypeVersion(PreviewsType type) { | |
| 215 int version = 0; // default | |
| 216 switch (type) { | |
| 217 case PreviewsType::OFFLINE: | |
| 218 base::StringToInt(ClientSidePreviewsParamValue(kVersion), &version); | |
| 219 return version; | |
| 220 // List remaining enum cases vs. default to catch when new one is added. | |
| 221 case PreviewsType::NONE: | |
| 222 break; | |
| 223 case PreviewsType::LAST: | |
| 224 break; | |
| 225 } | |
| 226 NOTREACHED(); | |
| 227 return -1; | |
| 228 } | |
| 229 | |
| 230 std::unique_ptr<PreviewsTypeList> GetEnabledPreviews() { | |
| 231 std::unique_ptr<PreviewsTypeList> enabled_previews(new PreviewsTypeList()); | |
| 232 | |
| 233 // Loop across all previews types (relies on sequential enum values). | |
| 234 for (int i = static_cast<int>(PreviewsType::NONE) + 1; | |
| 235 i < static_cast<int>(PreviewsType::LAST); ++i) { | |
| 236 PreviewsType type = static_cast<PreviewsType>(i); | |
| 237 if (IsPreviewsTypeEnabled(type)) { | |
| 238 enabled_previews->push_back({type, GetPreviewsTypeVersion(type)}); | |
| 239 } | |
| 240 } | |
| 241 return enabled_previews; | |
| 242 } | |
| 243 | |
| 244 bool EnableOfflinePreviewsForTesting() { | 216 bool EnableOfflinePreviewsForTesting() { |
| 245 std::map<std::string, std::string> params; | 217 std::map<std::string, std::string> params; |
| 246 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; | 218 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; |
| 247 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, | 219 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, |
| 248 kEnabled, params) && | 220 kEnabled, params) && |
| 249 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, | 221 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, |
| 250 kEnabled); | 222 kEnabled); |
| 251 } | 223 } |
| 252 | 224 |
| 253 } // namespace previews | 225 } // namespace previews |
| OLD | NEW |