| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SERVICES_PREFERENCES_PUBLIC_CPP_PREFS_OBSERVER_STORE_H_ |
| 6 #define SERVICES_PREFERENCES_PUBLIC_CPP_PREFS_OBSERVER_STORE_H_ |
| 7 |
| 8 #include <set> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "components/prefs/value_map_pref_store.h" |
| 12 #include "mojo/public/cpp/bindings/binding.h" |
| 13 #include "services/preferences/public/cpp/preferences_export.h" |
| 14 #include "services/preferences/public/interfaces/preferences.mojom.h" |
| 15 |
| 16 namespace shell { |
| 17 class Connector; |
| 18 } |
| 19 |
| 20 class PrefObserverStoreTest; |
| 21 |
| 22 // An implementation of PrefStore which uses prefs::mojom::PreferenceManager as |
| 23 // the backing of the preferences. |
| 24 // |
| 25 // PrefObserverStore caches the values locally to provide synchronous access. |
| 26 // The cache is empty until the first notification of OnPreferencesChanged is |
| 27 // received from the prefs::mojom::PreferenceManager. Upon this completion any |
| 28 // PrefStore::Observer will be notified of OnInitializationCompleted. |
| 29 // |
| 30 // Currently this does not support RemoveValue or GetMutableValue. |
| 31 class SERVICES_PREFERENCES_EXPORT PrefObserverStore |
| 32 : public ValueMapPrefStore, |
| 33 public prefs::mojom::PreferenceObserver { |
| 34 public: |
| 35 PrefObserverStore(shell::Connector* connector); |
| 36 |
| 37 // Defines the set of |keys| which PrefOvserverStore will handle. Begins |
| 38 // listening for changes to these from |prefs_manager_|. |
| 39 void Init(const std::set<std::string>& keys); |
| 40 |
| 41 // PrefStore: |
| 42 bool GetValue(const std::string& key, |
| 43 const base::Value** value) const override; |
| 44 |
| 45 // ValueMapPrefStore: |
| 46 void SetValue(const std::string& key, |
| 47 std::unique_ptr<base::Value> value, |
| 48 uint32_t flags) override; |
| 49 void RemoveValue(const std::string& key, uint32_t flags) override; |
| 50 bool GetMutableValue(const std::string& key, base::Value** value) override; |
| 51 void ReportValueChanged(const std::string& key, uint32_t flags) override; |
| 52 void SetValueSilently(const std::string& key, |
| 53 std::unique_ptr<base::Value> value, |
| 54 uint32_t flags) override; |
| 55 |
| 56 protected: |
| 57 // base::RefCounted<PrefStore>: |
| 58 ~PrefObserverStore() override; |
| 59 |
| 60 private: |
| 61 friend class PrefObserverStoreTest; |
| 62 |
| 63 // Notifies |prefs_manager_| of the change in |key| - |value| pair. |
| 64 void SetValueOnPreferenceManager(const std::string& key, base::Value* value); |
| 65 |
| 66 // prefs::mojom::PreferenceObserver: |
| 67 void OnPreferencesChanged( |
| 68 prefs::mojom::PreferenceMapPtr preferences) override; |
| 69 |
| 70 mojo::Binding<prefs::mojom::PreferenceObserver> prefs_binding_; |
| 71 prefs::mojom::PreferenceManagerPtr prefs_manager_ptr_; |
| 72 // Typically contained in |prefs_manager_ptr_| however tests may override. |
| 73 prefs::mojom::PreferenceManager* prefs_manager_; |
| 74 |
| 75 std::set<std::string> keys_; |
| 76 |
| 77 // True upon the first OnPreferencesChanged received after Init. |
| 78 bool initialized_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(PrefObserverStore); |
| 81 }; |
| 82 |
| 83 #endif // SERVICES_PREFERENCES_PUBLIC_CPP_PREFS_OBSERVER_STORE_H_ |
| OLD | NEW |