OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_PREF_STORE_CLIENT_MIXIN_H_ |
| 6 #define SERVICES_PREFERENCES_PUBLIC_CPP_PREF_STORE_CLIENT_MIXIN_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/observer_list.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" |
| 14 #include "services/preferences/public/interfaces/preferences.mojom.h" |
| 15 |
| 16 namespace base { |
| 17 class DictionaryValue; |
| 18 class Value; |
| 19 } |
| 20 |
| 21 namespace prefs { |
| 22 |
| 23 // An mixin implementation of PrefStore which uses prefs::mojom::PrefStore as |
| 24 // the backing store of the preferences. |
| 25 // |
| 26 // PrefStoreClientMixin provides synchronous access to the preferences stored by |
| 27 // the backing store by caching them locally. |
| 28 template <typename BasePrefStore> |
| 29 class PrefStoreClientMixin : public BasePrefStore, |
| 30 public mojom::PrefStoreObserver { |
| 31 public: |
| 32 PrefStoreClientMixin(); |
| 33 |
| 34 // BasePrefStore: |
| 35 void AddObserver(PrefStore::Observer* observer) override; |
| 36 void RemoveObserver(PrefStore::Observer* observer) override; |
| 37 bool HasObservers() const override; |
| 38 bool IsInitializationComplete() const override; |
| 39 bool GetValue(const std::string& key, |
| 40 const base::Value** result) const override; |
| 41 std::unique_ptr<base::DictionaryValue> GetValues() const override; |
| 42 |
| 43 protected: |
| 44 ~PrefStoreClientMixin() override; |
| 45 |
| 46 // Initializes |this|. This must be called before any of the other public or |
| 47 // protected member functions. |
| 48 void Init(std::unique_ptr<base::DictionaryValue> initial_prefs, |
| 49 bool initialized, |
| 50 mojom::PrefStoreObserverRequest observer_request); |
| 51 |
| 52 base::DictionaryValue& GetMutableValues(); |
| 53 void ReportPrefValueChanged(const std::string& key); |
| 54 |
| 55 private: |
| 56 // prefs::mojom::PreferenceObserver: |
| 57 void OnPrefChanged(const std::string& key, |
| 58 std::unique_ptr<base::Value> value) override; |
| 59 void OnInitializationCompleted(bool succeeded) override; |
| 60 |
| 61 // Cached preferences. |
| 62 // If null, indicates that initialization failed. |
| 63 std::unique_ptr<base::DictionaryValue> cached_prefs_; |
| 64 |
| 65 base::ObserverList<PrefStore::Observer, true> observers_; |
| 66 |
| 67 // Has the PrefStore we're observing been initialized? |
| 68 bool initialized_ = false; |
| 69 |
| 70 mojo::Binding<mojom::PrefStoreObserver> observer_binding_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(PrefStoreClientMixin); |
| 73 }; |
| 74 |
| 75 extern template class PrefStoreClientMixin<::PrefStore>; |
| 76 extern template class PrefStoreClientMixin<::PersistentPrefStore>; |
| 77 |
| 78 } // namespace prefs |
| 79 |
| 80 #endif // SERVICES_PREFERENCES_PUBLIC_CPP_PREF_STORE_CLIENT_MIXIN_H_ |
OLD | NEW |