diff options
Diffstat (limited to 'chromium/components/prefs')
29 files changed, 129 insertions, 185 deletions
diff --git a/chromium/components/prefs/android/BUILD.gn b/chromium/components/prefs/android/BUILD.gn index e2157481e8b..63ff474d4c3 100644 --- a/chromium/components/prefs/android/BUILD.gn +++ b/chromium/components/prefs/android/BUILD.gn @@ -18,17 +18,13 @@ android_library("java") { annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ] } -java_library("junit") { - # Skip platform checks since Robolectric depends on requires_android targets. - bypass_platform_checks = true - testonly = true +robolectric_library("junit") { sources = [ "java/src/org/chromium/components/prefs/PrefServiceTest.java" ] deps = [ ":java", "//base:base_java_test_support", "//base:base_junit_test_support", "//base/test:test_support_java", - "//third_party/android_deps:robolectric_all_java", "//third_party/junit", "//third_party/mockito:mockito_java", ] diff --git a/chromium/components/prefs/default_pref_store.cc b/chromium/components/prefs/default_pref_store.cc index 2c86687ff96..c2570e372d3 100644 --- a/chromium/components/prefs/default_pref_store.cc +++ b/chromium/components/prefs/default_pref_store.cc @@ -18,8 +18,8 @@ bool DefaultPrefStore::GetValue(const std::string& key, return prefs_.GetValue(key, result); } -std::unique_ptr<base::DictionaryValue> DefaultPrefStore::GetValues() const { - return prefs_.AsDictionaryValue(); +base::Value::Dict DefaultPrefStore::GetValues() const { + return prefs_.AsDict(); } void DefaultPrefStore::AddObserver(PrefStore::Observer* observer) { diff --git a/chromium/components/prefs/default_pref_store.h b/chromium/components/prefs/default_pref_store.h index d6b691b6b7c..60abda81c2a 100644 --- a/chromium/components/prefs/default_pref_store.h +++ b/chromium/components/prefs/default_pref_store.h @@ -27,7 +27,7 @@ class COMPONENTS_PREFS_EXPORT DefaultPrefStore : public PrefStore { // PrefStore implementation: bool GetValue(const std::string& key, const base::Value** result) const override; - std::unique_ptr<base::DictionaryValue> GetValues() const override; + base::Value::Dict GetValues() const override; void AddObserver(PrefStore::Observer* observer) override; void RemoveObserver(PrefStore::Observer* observer) override; bool HasObservers() const override; diff --git a/chromium/components/prefs/in_memory_pref_store.cc b/chromium/components/prefs/in_memory_pref_store.cc index 1eb5e9fa6c5..13d169a9dff 100644 --- a/chromium/components/prefs/in_memory_pref_store.cc +++ b/chromium/components/prefs/in_memory_pref_store.cc @@ -19,8 +19,8 @@ bool InMemoryPrefStore::GetValue(const std::string& key, return prefs_.GetValue(key, value); } -std::unique_ptr<base::DictionaryValue> InMemoryPrefStore::GetValues() const { - return prefs_.AsDictionaryValue(); +base::Value::Dict InMemoryPrefStore::GetValues() const { + return prefs_.AsDict(); } bool InMemoryPrefStore::GetMutableValue(const std::string& key, diff --git a/chromium/components/prefs/in_memory_pref_store.h b/chromium/components/prefs/in_memory_pref_store.h index ed21f7e0ac1..0e7dc6bdbe4 100644 --- a/chromium/components/prefs/in_memory_pref_store.h +++ b/chromium/components/prefs/in_memory_pref_store.h @@ -11,6 +11,7 @@ #include "base/compiler_specific.h" #include "base/observer_list.h" +#include "base/values.h" #include "components/prefs/persistent_pref_store.h" #include "components/prefs/pref_value_map.h" @@ -28,7 +29,7 @@ class COMPONENTS_PREFS_EXPORT InMemoryPrefStore : public PersistentPrefStore { // PrefStore implementation. bool GetValue(const std::string& key, const base::Value** result) const override; - std::unique_ptr<base::DictionaryValue> GetValues() const override; + base::Value::Dict GetValues() const override; void AddObserver(PrefStore::Observer* observer) override; void RemoveObserver(PrefStore::Observer* observer) override; bool HasObservers() const override; diff --git a/chromium/components/prefs/json_pref_store.cc b/chromium/components/prefs/json_pref_store.cc index 40115a0b201..e8718a0170f 100644 --- a/chromium/components/prefs/json_pref_store.cc +++ b/chromium/components/prefs/json_pref_store.cc @@ -179,8 +179,8 @@ bool JsonPrefStore::GetValue(const std::string& key, return true; } -std::unique_ptr<base::DictionaryValue> JsonPrefStore::GetValues() const { - return prefs_->CreateDeepCopy(); +base::Value::Dict JsonPrefStore::GetValues() const { + return prefs_->GetDict().Clone(); } void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { diff --git a/chromium/components/prefs/json_pref_store.h b/chromium/components/prefs/json_pref_store.h index c9a132a33de..8322bc2ea63 100644 --- a/chromium/components/prefs/json_pref_store.h +++ b/chromium/components/prefs/json_pref_store.h @@ -20,6 +20,7 @@ #include "base/observer_list.h" #include "base/sequence_checker.h" #include "base/task/thread_pool.h" +#include "base/values.h" #include "components/prefs/persistent_pref_store.h" #include "components/prefs/pref_filter.h" #include "components/prefs/prefs_export.h" @@ -27,14 +28,12 @@ class PrefFilter; namespace base { -class DictionaryValue; class FilePath; class JsonPrefStoreCallbackTest; class JsonPrefStoreLossyWriteTest; class SequencedTaskRunner; class WriteCallbacksObserver; -class Value; -} +} // namespace base // A writable PrefStore implementation that is used for user preferences. class COMPONENTS_PREFS_EXPORT JsonPrefStore @@ -77,7 +76,7 @@ class COMPONENTS_PREFS_EXPORT JsonPrefStore // PrefStore overrides: bool GetValue(const std::string& key, const base::Value** result) const override; - std::unique_ptr<base::DictionaryValue> GetValues() const override; + base::Value::Dict GetValues() const override; void AddObserver(PrefStore::Observer* observer) override; void RemoveObserver(PrefStore::Observer* observer) override; bool HasObservers() const override; diff --git a/chromium/components/prefs/overlay_user_pref_store.cc b/chromium/components/prefs/overlay_user_pref_store.cc index 1b0299bf51c..2ecbb657b69 100644 --- a/chromium/components/prefs/overlay_user_pref_store.cc +++ b/chromium/components/prefs/overlay_user_pref_store.cc @@ -86,7 +86,7 @@ bool OverlayUserPrefStore::GetValue(const std::string& key, return persistent_user_pref_store_->GetValue(key, result); } -std::unique_ptr<base::DictionaryValue> OverlayUserPrefStore::GetValues() const { +base::Value::Dict OverlayUserPrefStore::GetValues() const { auto values = ephemeral_user_pref_store_->GetValues(); auto persistent_values = persistent_user_pref_store_->GetValues(); @@ -95,9 +95,10 @@ std::unique_ptr<base::DictionaryValue> OverlayUserPrefStore::GetValues() const { // overwritten by the content of |persistent_user_pref_store_| (the persistent // store). for (const auto& key : persistent_names_set_) { - absl::optional<base::Value> out_value = persistent_values->ExtractPath(key); + absl::optional<base::Value> out_value = + persistent_values.ExtractByDottedPath(key); if (out_value.has_value()) { - values->SetPath(key, std::move(*out_value)); + values.SetByDottedPath(key, std::move(*out_value)); } } return values; diff --git a/chromium/components/prefs/overlay_user_pref_store.h b/chromium/components/prefs/overlay_user_pref_store.h index d4f676c56ec..bcee6577c50 100644 --- a/chromium/components/prefs/overlay_user_pref_store.h +++ b/chromium/components/prefs/overlay_user_pref_store.h @@ -12,6 +12,7 @@ #include "base/memory/ref_counted.h" #include "base/observer_list.h" +#include "base/values.h" #include "components/prefs/persistent_pref_store.h" #include "components/prefs/pref_value_map.h" #include "components/prefs/prefs_export.h" @@ -44,7 +45,7 @@ class COMPONENTS_PREFS_EXPORT OverlayUserPrefStore bool IsInitializationComplete() const override; bool GetValue(const std::string& key, const base::Value** result) const override; - std::unique_ptr<base::DictionaryValue> GetValues() const override; + base::Value::Dict GetValues() const override; // Methods of PersistentPrefStore. bool GetMutableValue(const std::string& key, base::Value** result) override; diff --git a/chromium/components/prefs/overlay_user_pref_store_unittest.cc b/chromium/components/prefs/overlay_user_pref_store_unittest.cc index 067188d9653..c1b701d135a 100644 --- a/chromium/components/prefs/overlay_user_pref_store_unittest.cc +++ b/chromium/components/prefs/overlay_user_pref_store_unittest.cc @@ -261,17 +261,19 @@ TEST_F(OverlayUserPrefStoreTest, GetValues) { WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); auto values = overlay_->GetValues(); - const Value* value = nullptr; // Check that an overlay preference is returned. - ASSERT_TRUE(values->Get(persistent_key, &value)); + const Value* value = values.FindByDottedPath(persistent_key); + ASSERT_TRUE(value); EXPECT_EQ(base::Value(42), *value); // Check that an underlay preference is returned. - ASSERT_TRUE(values->Get(regular_key, &value)); + value = values.FindByDottedPath(regular_key); + ASSERT_TRUE(value); EXPECT_EQ(base::Value(43), *value); // Check that the overlay is preferred. - ASSERT_TRUE(values->Get(shared_key, &value)); + value = values.FindByDottedPath(shared_key); + ASSERT_TRUE(value); EXPECT_EQ(base::Value(43), *value); } diff --git a/chromium/components/prefs/pref_change_registrar.cc b/chromium/components/prefs/pref_change_registrar.cc index 6048ef403d8..e4c03103cea 100644 --- a/chromium/components/prefs/pref_change_registrar.cc +++ b/chromium/components/prefs/pref_change_registrar.cc @@ -71,16 +71,6 @@ bool PrefChangeRegistrar::IsObserved(const std::string& pref) { return observers_.find(pref) != observers_.end(); } -bool PrefChangeRegistrar::IsManaged() { - for (ObserverMap::const_iterator it = observers_.begin(); - it != observers_.end(); ++it) { - const PrefService::Preference* pref = service_->FindPreference(it->first); - if (pref && pref->IsManaged()) - return true; - } - return false; -} - void PrefChangeRegistrar::OnPreferenceChanged(PrefService* service, const std::string& pref) { if (IsObserved(pref)) diff --git a/chromium/components/prefs/pref_change_registrar.h b/chromium/components/prefs/pref_change_registrar.h index d41e707cacf..4feb887a3c5 100644 --- a/chromium/components/prefs/pref_change_registrar.h +++ b/chromium/components/prefs/pref_change_registrar.h @@ -59,9 +59,6 @@ class COMPONENTS_PREFS_EXPORT PrefChangeRegistrar final : public PrefObserver { // Check whether |pref| is in the set of preferences being observed. bool IsObserved(const std::string& pref); - // Check whether any of the observed preferences has the managed bit set. - bool IsManaged(); - // Return the PrefService for this registrar. PrefService* prefs(); const PrefService* prefs() const; diff --git a/chromium/components/prefs/pref_change_registrar_unittest.cc b/chromium/components/prefs/pref_change_registrar_unittest.cc index 8e94bfaf698..12c488fdb75 100644 --- a/chromium/components/prefs/pref_change_registrar_unittest.cc +++ b/chromium/components/prefs/pref_change_registrar_unittest.cc @@ -158,21 +158,6 @@ TEST_F(ObserveSetOfPreferencesTest, IsObserved) { EXPECT_FALSE(pref_set->IsObserved(kApplicationLocale)); } -TEST_F(ObserveSetOfPreferencesTest, IsManaged) { - std::unique_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar()); - EXPECT_FALSE(pref_set->IsManaged()); - pref_service_->SetManagedPref(kHomePage, - std::make_unique<Value>("http://crbug.com")); - EXPECT_TRUE(pref_set->IsManaged()); - pref_service_->SetManagedPref(kHomePageIsNewTabPage, - std::make_unique<Value>(true)); - EXPECT_TRUE(pref_set->IsManaged()); - pref_service_->RemoveManagedPref(kHomePage); - EXPECT_TRUE(pref_set->IsManaged()); - pref_service_->RemoveManagedPref(kHomePageIsNewTabPage); - EXPECT_FALSE(pref_set->IsManaged()); -} - TEST_F(ObserveSetOfPreferencesTest, Observe) { using testing::_; using testing::Mock; diff --git a/chromium/components/prefs/pref_member_unittest.cc b/chromium/components/prefs/pref_member_unittest.cc index c11ddf59ffa..0b229166fac 100644 --- a/chromium/components/prefs/pref_member_unittest.cc +++ b/chromium/components/prefs/pref_member_unittest.cc @@ -213,7 +213,7 @@ TEST_F(PrefMemberTest, BasicGetAndSet) { string_list.Init(kStringListPref, &prefs); // Check the defaults - EXPECT_EQ(expected_list, *prefs.GetValueList(kStringListPref)); + EXPECT_EQ(expected_list, prefs.GetValueList(kStringListPref)); EXPECT_EQ(expected_vector, string_list.GetValue()); EXPECT_EQ(expected_vector, *string_list); EXPECT_TRUE(string_list.IsDefaultValue()); @@ -223,7 +223,7 @@ TEST_F(PrefMemberTest, BasicGetAndSet) { expected_vector.push_back("foo"); string_list.SetValue(expected_vector); - EXPECT_EQ(expected_list, *prefs.GetValueList(kStringListPref)); + EXPECT_EQ(expected_list, prefs.GetValueList(kStringListPref)); EXPECT_EQ(expected_vector, string_list.GetValue()); EXPECT_EQ(expected_vector, *string_list); EXPECT_FALSE(string_list.IsDefaultValue()); @@ -233,7 +233,7 @@ TEST_F(PrefMemberTest, BasicGetAndSet) { expected_vector.push_back("bar"); prefs.SetList(kStringListPref, expected_list.Clone()); - EXPECT_EQ(expected_list, *prefs.GetValueList(kStringListPref)); + EXPECT_EQ(expected_list, prefs.GetValueList(kStringListPref)); EXPECT_EQ(expected_vector, string_list.GetValue()); EXPECT_EQ(expected_vector, *string_list); EXPECT_FALSE(string_list.IsDefaultValue()); @@ -243,7 +243,7 @@ TEST_F(PrefMemberTest, BasicGetAndSet) { expected_vector.erase(expected_vector.begin()); prefs.SetList(kStringListPref, expected_list.Clone()); - EXPECT_EQ(expected_list, *prefs.GetValueList(kStringListPref)); + EXPECT_EQ(expected_list, prefs.GetValueList(kStringListPref)); EXPECT_EQ(expected_vector, string_list.GetValue()); EXPECT_EQ(expected_vector, *string_list); EXPECT_FALSE(string_list.IsDefaultValue()); diff --git a/chromium/components/prefs/pref_service.cc b/chromium/components/prefs/pref_service.cc index 344e68dc7b3..5d736f9fec5 100644 --- a/chromium/components/prefs/pref_service.cc +++ b/chromium/components/prefs/pref_service.cc @@ -62,7 +62,7 @@ void CheckForNewPrefChangesInPrefStore( if (!pref_store) return; auto values = pref_store->GetValues(); - for (auto item : values->DictItems()) { + for (auto item : values) { // If the key already presents, skip it as a store with higher precedence // already sets the entry. if (pref_changed_map->find(item.first) != pref_changed_map->end()) @@ -222,72 +222,40 @@ void PrefService::SchedulePendingLossyWrites() { } bool PrefService::GetBoolean(const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const base::Value* value = GetPreferenceValueChecked(path); - if (!value || !value->is_bool()) + const base::Value& value = GetValue(path); + if (!value.is_bool()) return false; - return value->GetBool(); + return value.GetBool(); } int PrefService::GetInteger(const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const base::Value* value = GetPreferenceValueChecked(path); - if (!value || !value->is_int()) + const base::Value& value = GetValue(path); + if (!value.is_int()) return 0; - return value->GetInt(); + return value.GetInt(); } double PrefService::GetDouble(const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const base::Value* value = GetPreferenceValueChecked(path); - if (!value || !value->is_double()) + const base::Value& value = GetValue(path); + if (!value.is_double()) return 0.0; - return value->GetDouble(); + return value.GetDouble(); } std::string PrefService::GetString(const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const base::Value* value = GetPreferenceValueChecked(path); - if (!value || !value->is_string()) + const base::Value& value = GetValue(path); + if (!value.is_string()) return std::string(); - return value->GetString(); + return value.GetString(); } base::FilePath PrefService::GetFilePath(const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const base::Value* value = GetPreferenceValueChecked(path); - if (!value) - return base::FilePath(); - absl::optional<base::FilePath> result = base::ValueToFilePath(*value); + const base::Value& value = GetValue(path); + absl::optional<base::FilePath> result = base::ValueToFilePath(value); DCHECK(result); return *result; } -const base::Value::Dict* PrefService::GetValueDict( - const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const base::Value* value = GetDictionary(path); - if (!value) - return nullptr; - return &value->GetDict(); -} - -const base::Value::List* PrefService::GetValueList( - const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const base::Value* value = GetList(path); - if (!value) - return nullptr; - return &value->GetList(); -} - bool PrefService::HasPrefPath(const std::string& path) const { const Preference* pref = FindPreference(path); return pref && !pref->IsDefaultValue(); @@ -380,21 +348,30 @@ bool PrefService::IsUserModifiablePreference( } const base::Value* PrefService::Get(const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - return GetPreferenceValueChecked(path); + return &GetValue(path); } const base::Value* PrefService::GetDictionary(const std::string& path) const { + const base::Value& value = GetValue(path); + DCHECK(value.is_dict()); + return &value; +} + +const base::Value& PrefService::GetValue(const std::string& path) const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + return *GetPreferenceValueChecked(path); +} - const base::Value* value = GetPreferenceValueChecked(path); - if (!value) - return nullptr; - if (value->type() != base::Value::Type::DICTIONARY) { - NOTREACHED(); - return nullptr; - } - return value; +const base::Value::Dict& PrefService::GetValueDict( + const std::string& path) const { + const base::Value& value = GetValue(path); + return value.GetDict(); +} + +const base::Value::List& PrefService::GetValueList( + const std::string& path) const { + const base::Value& value = GetValue(path); + return value.GetList(); } const base::Value* PrefService::GetUserPrefValue( @@ -438,16 +415,9 @@ const base::Value* PrefService::GetDefaultPrefValue( } const base::Value* PrefService::GetList(const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const base::Value* value = GetPreferenceValueChecked(path); - if (!value) - return nullptr; - if (value->type() != base::Value::Type::LIST) { - NOTREACHED(); - return nullptr; - } - return value; + const base::Value& value = GetValue(path); + DCHECK(value.is_list()); + return &value; } void PrefService::AddPrefObserver(const std::string& path, PrefObserver* obs) { @@ -585,7 +555,7 @@ void PrefService::SetInt64(const std::string& path, int64_t value) { } int64_t PrefService::GetInt64(const std::string& path) const { - const base::Value* value = GetPreferenceValueChecked(path); + const base::Value& value = GetValue(path); absl::optional<int64_t> integer = base::ValueToInt64(value); DCHECK(integer); return integer.value_or(0); @@ -596,14 +566,12 @@ void PrefService::SetUint64(const std::string& path, uint64_t value) { } uint64_t PrefService::GetUint64(const std::string& path) const { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const base::Value* value = GetPreferenceValueChecked(path); - if (!value || !value->is_string()) + const base::Value& value = GetValue(path); + if (!value.is_string()) return 0; uint64_t result; - base::StringToUint64(value->GetString(), &result); + base::StringToUint64(value.GetString(), &result); return result; } @@ -612,7 +580,7 @@ void PrefService::SetTime(const std::string& path, base::Time value) { } base::Time PrefService::GetTime(const std::string& path) const { - const base::Value* value = GetPreferenceValueChecked(path); + const base::Value& value = GetValue(path); absl::optional<base::Time> time = base::ValueToTime(value); DCHECK(time); return time.value_or(base::Time()); @@ -623,7 +591,7 @@ void PrefService::SetTimeDelta(const std::string& path, base::TimeDelta value) { } base::TimeDelta PrefService::GetTimeDelta(const std::string& path) const { - const base::Value* value = GetPreferenceValueChecked(path); + const base::Value& value = GetValue(path); absl::optional<base::TimeDelta> time_delta = base::ValueToTimeDelta(value); DCHECK(time_delta); return time_delta.value_or(base::TimeDelta()); diff --git a/chromium/components/prefs/pref_service.h b/chromium/components/prefs/pref_service.h index 0c8ca54a489..74da1aa4684 100644 --- a/chromium/components/prefs/pref_service.h +++ b/chromium/components/prefs/pref_service.h @@ -241,18 +241,30 @@ class COMPONENTS_PREFS_EXPORT PrefService { double GetDouble(const std::string& path) const; std::string GetString(const std::string& path) const; base::FilePath GetFilePath(const std::string& path) const; - const base::Value::Dict* GetValueDict(const std::string& path) const; - const base::Value::List* GetValueList(const std::string& path) const; + // DEPRECATED: Prefer GetValue(), GetValueDict(), and GetValueList(). // Returns the branch if it exists, or the registered default value otherwise. // Note that |path| must point to a registered preference. In that case, these // functions will never return NULL. - const base::Value* Get(const std::string& path) const; - // DEPRECATED: Prefer Get(), GetValueDict(), and GetValueList(). // TODO(https://crbug.com/1334665): Remove these methods. + const base::Value* Get(const std::string& path) const; const base::Value* GetDictionary(const std::string& path) const; const base::Value* GetList(const std::string& path) const; + // Returns the branch if it exists, or the registered default value otherwise. + // `path` must point to a registered preference (DCHECK). + const base::Value& GetValue(const std::string& path) const; + + // Returns the branch if it exists, or the registered default value otherwise. + // `path` must point to a registered preference whose value and registered + // default are of type `base::Value::Type::DICT (DCHECK). + const base::Value::Dict& GetValueDict(const std::string& path) const; + + // Returns the branch if it exists, or the registered default value otherwise. + // `path` must point to a registered preference whose value and registered + // default are of type `base::Value::Type::LIST (DCHECK). + const base::Value::List& GetValueList(const std::string& path) const; + // Removes a user pref and restores the pref to its default value. void ClearPref(const std::string& path); diff --git a/chromium/components/prefs/pref_store.h b/chromium/components/prefs/pref_store.h index 4c0a4bd0b6c..4ffde0aa022 100644 --- a/chromium/components/prefs/pref_store.h +++ b/chromium/components/prefs/pref_store.h @@ -9,13 +9,9 @@ #include <string> #include "base/memory/ref_counted.h" +#include "base/values.h" #include "components/prefs/prefs_export.h" -namespace base { -class DictionaryValue; -class Value; -} - // This is an abstract interface for reading and writing from/to a persistent // preference store, used by PrefService. An implementation using a JSON file // can be found in JsonPrefStore, while an implementation without any backing @@ -56,8 +52,8 @@ class COMPONENTS_PREFS_EXPORT PrefStore : public base::RefCounted<PrefStore> { virtual bool GetValue(const std::string& key, const base::Value** result) const = 0; - // Get all the values. Never returns a null pointer. - virtual std::unique_ptr<base::DictionaryValue> GetValues() const = 0; + // Get all the values. + virtual base::Value::Dict GetValues() const = 0; protected: friend class base::RefCounted<PrefStore>; diff --git a/chromium/components/prefs/pref_test_utils.cc b/chromium/components/prefs/pref_test_utils.cc index 8f7bcd8ed5d..dc26f4ce60f 100644 --- a/chromium/components/prefs/pref_test_utils.cc +++ b/chromium/components/prefs/pref_test_utils.cc @@ -20,7 +20,7 @@ void WaitForPrefValue(PrefService* pref_service, PrefChangeRegistrar pref_changes; pref_changes.Init(pref_service); pref_changes.Add(path, base::BindLambdaForTesting([&]() { - if (value == *(pref_service->Get(path))) + if (value == pref_service->GetValue(path)) run_loop.Quit(); })); run_loop.Run(); diff --git a/chromium/components/prefs/pref_value_map.cc b/chromium/components/prefs/pref_value_map.cc index 168638d2201..6f88ede6c5c 100644 --- a/chromium/components/prefs/pref_value_map.cc +++ b/chromium/components/prefs/pref_value_map.cc @@ -169,10 +169,10 @@ void PrefValueMap::GetDifferingKeys( differing_keys->push_back(other_pref->first); } -std::unique_ptr<base::DictionaryValue> PrefValueMap::AsDictionaryValue() const { - auto dictionary = std::make_unique<base::DictionaryValue>(); +base::Value::Dict PrefValueMap::AsDict() const { + base::Value::Dict dictionary; for (const auto& value : prefs_) - dictionary->SetPath(value.first, value.second.Clone()); + dictionary.SetByDottedPath(value.first, value.second.Clone()); return dictionary; } diff --git a/chromium/components/prefs/pref_value_map.h b/chromium/components/prefs/pref_value_map.h index 94f26b2738c..794acb10d4b 100644 --- a/chromium/components/prefs/pref_value_map.h +++ b/chromium/components/prefs/pref_value_map.h @@ -6,17 +6,12 @@ #define COMPONENTS_PREFS_PREF_VALUE_MAP_H_ #include <map> -#include <memory> #include <string> #include <vector> +#include "base/values.h" #include "components/prefs/prefs_export.h" -namespace base { -class DictionaryValue; -class Value; -} - // A generic string to value map used by the PrefStore implementations. class COMPONENTS_PREFS_EXPORT PrefValueMap { public: @@ -89,8 +84,8 @@ class COMPONENTS_PREFS_EXPORT PrefValueMap { void GetDifferingKeys(const PrefValueMap* other, std::vector<std::string>* differing_keys) const; - // Copies the map into a dictionary value. - std::unique_ptr<base::DictionaryValue> AsDictionaryValue() const; + // Copies the map into a Value::Dict. + base::Value::Dict AsDict() const; private: Map prefs_; diff --git a/chromium/components/prefs/pref_value_map_unittest.cc b/chromium/components/prefs/pref_value_map_unittest.cc index 234a76ca920..b4cfe13c70f 100644 --- a/chromium/components/prefs/pref_value_map_unittest.cc +++ b/chromium/components/prefs/pref_value_map_unittest.cc @@ -21,7 +21,7 @@ TEST(PrefValueMapTest, SetValue) { EXPECT_TRUE(map.SetValue("key", Value("hi mom!"))); EXPECT_TRUE(map.GetValue("key", &result)); - EXPECT_TRUE(Value("hi mom!").Equals(result)); + EXPECT_EQ("hi mom!", *result); } TEST(PrefValueMapTest, GetAndSetIntegerValue) { diff --git a/chromium/components/prefs/scoped_user_pref_update_unittest.cc b/chromium/components/prefs/scoped_user_pref_update_unittest.cc index f344c79c211..56ed2efbe74 100644 --- a/chromium/components/prefs/scoped_user_pref_update_unittest.cc +++ b/chromium/components/prefs/scoped_user_pref_update_unittest.cc @@ -55,25 +55,21 @@ TEST_F(ScopedUserPrefUpdateTest, RegularUse) { Mock::VerifyAndClearExpectations(&observer_); // Modifications happen online and are instantly visible, though. - const base::Value* current_value = prefs_.GetDictionary(kPref); - ASSERT_TRUE(current_value); - EXPECT_EQ(expected_dictionary, *current_value); + EXPECT_EQ(expected_dictionary, prefs_.GetValueDict(kPref)); // Now we are leaving the scope of the update so we should be notified. observer_.Expect(kPref, &expected_dictionary); } Mock::VerifyAndClearExpectations(&observer_); - const base::Value* current_value = prefs_.GetDictionary(kPref); - ASSERT_TRUE(current_value); - EXPECT_EQ(expected_dictionary, *current_value); + EXPECT_EQ(expected_dictionary, prefs_.GetValueDict(kPref)); } TEST_F(ScopedUserPrefUpdateTest, NeverTouchAnything) { - const base::Value* old_value = prefs_.GetDictionary(kPref); + const base::Value::Dict& old_value = prefs_.GetValueDict(kPref); EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); { DictionaryPrefUpdate update(&prefs_, kPref); } - const base::Value* new_value = prefs_.GetDictionary(kPref); + const base::Value::Dict& new_value = prefs_.GetValueDict(kPref); EXPECT_EQ(old_value, new_value); Mock::VerifyAndClearExpectations(&observer_); } @@ -85,11 +81,11 @@ TEST_F(ScopedUserPrefUpdateTest, UpdatingListPrefWithDefaults) { std::string pref_name = "mypref"; prefs_.registry()->RegisterListPref(pref_name, std::move(defaults)); - EXPECT_EQ(2u, prefs_.GetValueList(pref_name)->size()); + EXPECT_EQ(2u, prefs_.GetValueList(pref_name).size()); ListPrefUpdate update(&prefs_, pref_name); update->Append("thirdvalue"); - EXPECT_EQ(3u, prefs_.GetValueList(pref_name)->size()); + EXPECT_EQ(3u, prefs_.GetValueList(pref_name).size()); } TEST_F(ScopedUserPrefUpdateTest, UpdatingDictionaryPrefWithDefaults) { @@ -99,9 +95,9 @@ TEST_F(ScopedUserPrefUpdateTest, UpdatingDictionaryPrefWithDefaults) { std::string pref_name = "mypref"; prefs_.registry()->RegisterDictionaryPref(pref_name, std::move(defaults)); - EXPECT_EQ(2u, prefs_.GetDictionary(pref_name)->DictSize()); + EXPECT_EQ(2u, prefs_.GetValueDict(pref_name).size()); DictionaryPrefUpdate update(&prefs_, pref_name); update->SetStringKey("thirdkey", "value"); - EXPECT_EQ(3u, prefs_.GetDictionary(pref_name)->DictSize()); + EXPECT_EQ(3u, prefs_.GetValueDict(pref_name).size()); } diff --git a/chromium/components/prefs/segregated_pref_store.cc b/chromium/components/prefs/segregated_pref_store.cc index 0f1ab05e7b0..82fef703eb6 100644 --- a/chromium/components/prefs/segregated_pref_store.cc +++ b/chromium/components/prefs/segregated_pref_store.cc @@ -89,14 +89,15 @@ bool SegregatedPrefStore::GetValue(const std::string& key, return StoreForKey(key)->GetValue(key, result); } -std::unique_ptr<base::DictionaryValue> SegregatedPrefStore::GetValues() const { - auto values = default_pref_store_->GetValues(); - auto selected_pref_store_values = selected_pref_store_->GetValues(); +base::Value::Dict SegregatedPrefStore::GetValues() const { + base::Value::Dict values = default_pref_store_->GetValues(); + base::Value::Dict selected_pref_store_values = + selected_pref_store_->GetValues(); for (const auto& key : selected_preference_names_) { - if (const base::Value* value = selected_pref_store_values->FindPath(key)) { - values->SetPath(key, value->Clone()); + if (base::Value* value = selected_pref_store_values.FindByDottedPath(key)) { + values.SetByDottedPath(key, std::move(*value)); } else { - values->RemoveKey(key); + values.Remove(key); } } return values; diff --git a/chromium/components/prefs/segregated_pref_store.h b/chromium/components/prefs/segregated_pref_store.h index 2c172050c3f..61aa3ade248 100644 --- a/chromium/components/prefs/segregated_pref_store.h +++ b/chromium/components/prefs/segregated_pref_store.h @@ -54,7 +54,7 @@ class COMPONENTS_PREFS_EXPORT SegregatedPrefStore : public PersistentPrefStore { bool IsInitializationComplete() const override; bool GetValue(const std::string& key, const base::Value** result) const override; - std::unique_ptr<base::DictionaryValue> GetValues() const override; + base::Value::Dict GetValues() const override; // WriteablePrefStore implementation void SetValue(const std::string& key, diff --git a/chromium/components/prefs/segregated_pref_store_unittest.cc b/chromium/components/prefs/segregated_pref_store_unittest.cc index 4014acd8eff..3a1ef887ed5 100644 --- a/chromium/components/prefs/segregated_pref_store_unittest.cc +++ b/chromium/components/prefs/segregated_pref_store_unittest.cc @@ -378,17 +378,19 @@ TEST_F(SegregatedPrefStoreTest, GetValues) { WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); auto values = segregated_store_->GetValues(); - const base::Value* value = nullptr; // Check that a selected preference is returned. - ASSERT_TRUE(values->Get(kSelectedPref, &value)); + const base::Value* value = values.Find(kSelectedPref); + ASSERT_TRUE(value); EXPECT_EQ(base::Value(kValue1), *value); // Check that a a default preference is returned. - ASSERT_TRUE(values->Get(kUnselectedPref, &value)); + value = values.Find(kUnselectedPref); + ASSERT_TRUE(value); EXPECT_EQ(base::Value(kValue2), *value); // Check that the selected preference is preferred. - ASSERT_TRUE(values->Get(kSharedPref, &value)); + value = values.Find(kSharedPref); + ASSERT_TRUE(value); EXPECT_EQ(base::Value(kValue1), *value); } diff --git a/chromium/components/prefs/testing_pref_store.cc b/chromium/components/prefs/testing_pref_store.cc index 5d3d1ec0b5c..27f61f5b1c4 100644 --- a/chromium/components/prefs/testing_pref_store.cc +++ b/chromium/components/prefs/testing_pref_store.cc @@ -26,8 +26,8 @@ bool TestingPrefStore::GetValue(const std::string& key, return prefs_.GetValue(key, value); } -std::unique_ptr<base::DictionaryValue> TestingPrefStore::GetValues() const { - return prefs_.AsDictionaryValue(); +base::Value::Dict TestingPrefStore::GetValues() const { + return prefs_.AsDict(); } bool TestingPrefStore::GetMutableValue(const std::string& key, diff --git a/chromium/components/prefs/testing_pref_store.h b/chromium/components/prefs/testing_pref_store.h index a4b5d000b04..8e0e0b35c4c 100644 --- a/chromium/components/prefs/testing_pref_store.h +++ b/chromium/components/prefs/testing_pref_store.h @@ -11,6 +11,7 @@ #include "base/compiler_specific.h" #include "base/observer_list.h" +#include "base/values.h" #include "components/prefs/persistent_pref_store.h" #include "components/prefs/pref_value_map.h" @@ -27,7 +28,7 @@ class TestingPrefStore : public PersistentPrefStore { // Overriden from PrefStore. bool GetValue(const std::string& key, const base::Value** result) const override; - std::unique_ptr<base::DictionaryValue> GetValues() const override; + base::Value::Dict GetValues() const override; void AddObserver(PrefStore::Observer* observer) override; void RemoveObserver(PrefStore::Observer* observer) override; bool HasObservers() const override; diff --git a/chromium/components/prefs/value_map_pref_store.cc b/chromium/components/prefs/value_map_pref_store.cc index 14fa1ee756d..a4c0d022f8d 100644 --- a/chromium/components/prefs/value_map_pref_store.cc +++ b/chromium/components/prefs/value_map_pref_store.cc @@ -17,8 +17,8 @@ bool ValueMapPrefStore::GetValue(const std::string& key, return prefs_.GetValue(key, value); } -std::unique_ptr<base::DictionaryValue> ValueMapPrefStore::GetValues() const { - return prefs_.AsDictionaryValue(); +base::Value::Dict ValueMapPrefStore::GetValues() const { + return prefs_.AsDict(); } void ValueMapPrefStore::AddObserver(PrefStore::Observer* observer) { diff --git a/chromium/components/prefs/value_map_pref_store.h b/chromium/components/prefs/value_map_pref_store.h index 68c0b0e43e5..d1083c11243 100644 --- a/chromium/components/prefs/value_map_pref_store.h +++ b/chromium/components/prefs/value_map_pref_store.h @@ -11,6 +11,7 @@ #include <string> #include "base/observer_list.h" +#include "base/values.h" #include "components/prefs/pref_value_map.h" #include "components/prefs/prefs_export.h" #include "components/prefs/writeable_pref_store.h" @@ -27,7 +28,7 @@ class COMPONENTS_PREFS_EXPORT ValueMapPrefStore : public WriteablePrefStore { // PrefStore overrides: bool GetValue(const std::string& key, const base::Value** value) const override; - std::unique_ptr<base::DictionaryValue> GetValues() const override; + base::Value::Dict GetValues() const override; void AddObserver(PrefStore::Observer* observer) override; void RemoveObserver(PrefStore::Observer* observer) override; bool HasObservers() const override; |