pref_service_flags_storage.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/flags_ui/pref_service_flags_storage.h"
  5. #include "base/logging.h"
  6. #include "base/values.h"
  7. #include "build/build_config.h"
  8. #include "build/chromeos_buildflags.h"
  9. #include "components/flags_ui/flags_ui_pref_names.h"
  10. #include "components/pref_registry/pref_registry_syncable.h"
  11. #include "components/prefs/pref_registry_simple.h"
  12. #include "components/prefs/pref_service.h"
  13. #include "components/prefs/scoped_user_pref_update.h"
  14. namespace flags_ui {
  15. PrefServiceFlagsStorage::PrefServiceFlagsStorage(PrefService* prefs)
  16. : prefs_(prefs) {}
  17. PrefServiceFlagsStorage::~PrefServiceFlagsStorage() {}
  18. std::set<std::string> PrefServiceFlagsStorage::GetFlags() const {
  19. const base::Value::List& enabled_experiments =
  20. prefs_->GetValueList(prefs::kAboutFlagsEntries);
  21. std::set<std::string> flags;
  22. for (const auto& entry : enabled_experiments) {
  23. if (!entry.is_string()) {
  24. LOG(WARNING) << "Invalid entry in " << prefs::kAboutFlagsEntries;
  25. continue;
  26. }
  27. flags.insert(entry.GetString());
  28. }
  29. return flags;
  30. }
  31. bool PrefServiceFlagsStorage::SetFlags(const std::set<std::string>& flags) {
  32. ListPrefUpdate update(prefs_, prefs::kAboutFlagsEntries);
  33. base::Value* experiments_list = update.Get();
  34. DCHECK(experiments_list->is_list());
  35. experiments_list->ClearList();
  36. for (const auto& flag : flags)
  37. experiments_list->Append(flag);
  38. return true;
  39. }
  40. std::string PrefServiceFlagsStorage::GetOriginListFlag(
  41. const std::string& internal_entry_name) const {
  42. const base::Value::Dict& origin_lists =
  43. prefs_->GetValueDict(prefs::kAboutFlagsOriginLists);
  44. if (const std::string* s = origin_lists.FindString(internal_entry_name))
  45. return *s;
  46. return std::string();
  47. }
  48. void PrefServiceFlagsStorage::SetOriginListFlag(
  49. const std::string& internal_entry_name,
  50. const std::string& origin_list_value) {
  51. DictionaryPrefUpdate update(prefs_, prefs::kAboutFlagsOriginLists);
  52. update->SetStringPath(internal_entry_name, origin_list_value);
  53. }
  54. void PrefServiceFlagsStorage::CommitPendingWrites() {
  55. prefs_->CommitPendingWrite();
  56. }
  57. // static
  58. void PrefServiceFlagsStorage::RegisterPrefs(PrefRegistrySimple* registry) {
  59. registry->RegisterListPref(prefs::kAboutFlagsEntries);
  60. registry->RegisterDictionaryPref(prefs::kAboutFlagsOriginLists);
  61. }
  62. #if BUILDFLAG(IS_CHROMEOS_ASH)
  63. // static
  64. void PrefServiceFlagsStorage::RegisterProfilePrefs(
  65. user_prefs::PrefRegistrySyncable* registry) {
  66. registry->RegisterListPref(prefs::kAboutFlagsEntries);
  67. registry->RegisterDictionaryPref(prefs::kAboutFlagsOriginLists);
  68. }
  69. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  70. } // namespace flags_ui