OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/flags_ui/flags_state.h" | 5 #include "components/flags_ui/flags_state.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 result->Append(std::move(value)); | 190 result->Append(std::move(value)); |
191 } | 191 } |
192 return result; | 192 return result; |
193 } | 193 } |
194 | 194 |
195 // Registers variation parameters specified by |feature_variation| for the field | 195 // Registers variation parameters specified by |feature_variation| for the field |
196 // trial named |feature_trial_name|, unless a group for this trial has already | 196 // trial named |feature_trial_name|, unless a group for this trial has already |
197 // been created (e.g. via command-line switches that take precedence over | 197 // been created (e.g. via command-line switches that take precedence over |
198 // about:flags). In the trial, the function creates a new constant group called | 198 // about:flags). In the trial, the function creates a new constant group called |
199 // |kTrialGroupAboutFlags|. | 199 // |kTrialGroupAboutFlags|. |
200 void RegisterFeatureVariationParameters( | 200 base::FieldTrial* RegisterFeatureVariationParameters( |
201 const std::string& feature_trial_name, | 201 const std::string& feature_trial_name, |
202 const FeatureEntry::FeatureVariation* feature_variation) { | 202 const FeatureEntry::FeatureVariation* feature_variation) { |
203 std::map<std::string, std::string> params; | 203 std::map<std::string, std::string> params; |
204 if (feature_variation) { | 204 if (feature_variation) { |
205 // Copy the parameters for non-null variations. | 205 // Copy the parameters for non-null variations. |
206 for (int i = 0; i < feature_variation->num_params; ++i) { | 206 for (int i = 0; i < feature_variation->num_params; ++i) { |
207 params[feature_variation->params[i].param_name] = | 207 params[feature_variation->params[i].param_name] = |
208 feature_variation->params[i].param_value; | 208 feature_variation->params[i].param_value; |
209 } | 209 } |
210 } | 210 } |
211 | 211 |
212 bool success = variations::AssociateVariationParams( | 212 bool success = variations::AssociateVariationParams( |
213 feature_trial_name, internal::kTrialGroupAboutFlags, params); | 213 feature_trial_name, internal::kTrialGroupAboutFlags, params); |
214 if (success) { | 214 if (!success) |
215 // Successful association also means that no group is created and selected | 215 return nullptr; |
216 // for the trial, yet. Thus, create the trial to select the group. This way, | 216 // Successful association also means that no group is created and selected |
217 // the parameters cannot get overwritten in later phases (such as from the | 217 // for the trial, yet. Thus, create the trial to select the group. This way, |
218 // server). | 218 // the parameters cannot get overwritten in later phases (such as from the |
219 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( | 219 // server). |
220 feature_trial_name, internal::kTrialGroupAboutFlags); | 220 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( |
221 if (!trial) { | 221 feature_trial_name, internal::kTrialGroupAboutFlags); |
222 DLOG(WARNING) << "Could not create the trial " << feature_trial_name | 222 if (!trial) { |
223 << " with group " << internal::kTrialGroupAboutFlags; | 223 DLOG(WARNING) << "Could not create the trial " << feature_trial_name |
224 } | 224 << " with group " << internal::kTrialGroupAboutFlags; |
225 } | 225 } |
| 226 return trial; |
226 } | 227 } |
227 | 228 |
228 } // namespace | 229 } // namespace |
229 | 230 |
230 // Keeps track of affected switches for each FeatureEntry, based on which | 231 // Keeps track of affected switches for each FeatureEntry, based on which |
231 // choice is selected for it. | 232 // choice is selected for it. |
232 struct SwitchEntry { | 233 struct SwitchEntry { |
233 // Corresponding base::Feature to toggle. | 234 // Corresponding base::Feature to toggle. |
234 std::string feature_name; | 235 std::string feature_name; |
235 | 236 |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 flags_storage->SetFlags(no_entries); | 430 flags_storage->SetFlags(no_entries); |
430 } | 431 } |
431 | 432 |
432 void FlagsState::Reset() { | 433 void FlagsState::Reset() { |
433 needs_restart_ = false; | 434 needs_restart_ = false; |
434 flags_switches_.clear(); | 435 flags_switches_.clear(); |
435 appended_switches_.clear(); | 436 appended_switches_.clear(); |
436 } | 437 } |
437 | 438 |
438 void FlagsState::RegisterAllFeatureVariationParameters( | 439 void FlagsState::RegisterAllFeatureVariationParameters( |
439 FlagsStorage* flags_storage) { | 440 FlagsStorage* flags_storage, |
| 441 base::FeatureList* feature_list) { |
440 std::set<std::string> enabled_entries; | 442 std::set<std::string> enabled_entries; |
441 GetSanitizedEnabledFlagsForCurrentPlatform(flags_storage, &enabled_entries); | 443 GetSanitizedEnabledFlagsForCurrentPlatform(flags_storage, &enabled_entries); |
442 | 444 |
443 for (size_t i = 0; i < num_feature_entries_; ++i) { | 445 for (size_t i = 0; i < num_feature_entries_; ++i) { |
444 const FeatureEntry& e = feature_entries_[i]; | 446 const FeatureEntry& e = feature_entries_[i]; |
445 if (e.type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE) { | 447 if (e.type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE) { |
446 for (int j = 0; j < e.num_options; ++j) { | 448 for (int j = 0; j < e.num_options; ++j) { |
447 const FeatureEntry::FeatureVariation* variation = | 449 const FeatureEntry::FeatureVariation* variation = |
448 e.VariationForOption(j); | 450 e.VariationForOption(j); |
449 if (e.StateForOption(j) == FeatureEntry::FeatureState::ENABLED && | 451 if (e.StateForOption(j) == FeatureEntry::FeatureState::ENABLED && |
450 enabled_entries.count(e.NameForOption(j))) { | 452 enabled_entries.count(e.NameForOption(j))) { |
451 // If the option is enabled by the user, register it. | 453 // If the option is selected by the user & has variation, register it. |
452 RegisterFeatureVariationParameters(e.feature_trial_name, variation); | 454 base::FieldTrial* field_trial = RegisterFeatureVariationParameters( |
453 // TODO(jkrcal) The code does not associate the feature with the field | 455 e.feature_trial_name, variation); |
454 // trial |e.feature_trial_name|. The reason is that features | 456 |
455 // overridden in chrome://flags are translated to command-line flags | 457 if (!field_trial) |
456 // and thus treated earlier in the initialization. The fix requires | 458 continue; |
457 // larger changes. As a result: | 459 feature_list->RegisterFieldTrialOverride( |
458 // - the API calls to variations::GetVariationParamValueByFeature and | 460 e.feature->name, |
459 // to variations::GetVariationParamsByFeature do not work; and | 461 base::FeatureList::OverrideState::OVERRIDE_ENABLE_FEATURE, |
460 // - the API call to base::FeatureList::IsEnabled does not mark the | 462 field_trial); |
461 // field trial as active (and the trial does not appear in UMA). | |
462 // If the code calls variations::GetVariationParamValue or | |
463 // variations::GetVariationParams providing the trial name, everything | |
464 // should work fine. | |
465 } | 463 } |
466 } | 464 } |
467 } | 465 } |
468 } | 466 } |
469 } | 467 } |
470 | 468 |
471 void FlagsState::GetFlagFeatureEntries( | 469 void FlagsState::GetFlagFeatureEntries( |
472 FlagsStorage* flags_storage, | 470 FlagsStorage* flags_storage, |
473 FlagAccess access, | 471 FlagAccess access, |
474 base::ListValue* supported_entries, | 472 base::ListValue* supported_entries, |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 } | 721 } |
724 | 722 |
725 std::set<std::string> new_enabled_entries = | 723 std::set<std::string> new_enabled_entries = |
726 base::STLSetIntersection<std::set<std::string>>(platform_entries, | 724 base::STLSetIntersection<std::set<std::string>>(platform_entries, |
727 *result); | 725 *result); |
728 | 726 |
729 result->swap(new_enabled_entries); | 727 result->swap(new_enabled_entries); |
730 } | 728 } |
731 | 729 |
732 } // namespace flags_ui | 730 } // namespace flags_ui |
OLD | NEW |