| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef SERVICES_PREFERENCES_PUBLIC_CPP_PREFS_OBSERVER_STORE_H_ | 5 #ifndef SERVICES_PREFERENCES_PUBLIC_CPP_PREFS_OBSERVER_STORE_H_ |
| 6 #define SERVICES_PREFERENCES_PUBLIC_CPP_PREFS_OBSERVER_STORE_H_ | 6 #define SERVICES_PREFERENCES_PUBLIC_CPP_PREFS_OBSERVER_STORE_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "components/prefs/value_map_pref_store.h" | 11 #include "components/prefs/value_map_pref_store.h" |
| 12 #include "mojo/common/common_custom_types.mojom.h" | 12 #include "mojo/common/common_custom_types.mojom.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
| 14 #include "services/preferences/public/interfaces/preferences.mojom.h" | 14 #include "services/preferences/public/interfaces/preferences.mojom.h" |
| 15 | 15 |
| 16 namespace preferences { |
| 17 |
| 16 class PrefObserverStoreTest; | 18 class PrefObserverStoreTest; |
| 17 | 19 |
| 18 // An implementation of PrefStore which uses prefs::mojom::PreferenceManager as | 20 // An implementation of PrefStore which uses prefs::mojom::PreferenceManager as |
| 19 // the backing of the preferences. | 21 // the backing of the preferences. |
| 20 // | 22 // |
| 21 // PrefObserverStore caches the values locally to provide synchronous access. | 23 // PrefObserverStore caches the values locally to provide synchronous access. |
| 22 // The cache is empty until the first notification of OnPreferencesChanged is | 24 // The cache is empty until the first notification of OnPreferencesChanged is |
| 23 // received from the prefs::mojom::PreferenceManager. Upon recieving an initial | 25 // received from the prefs::mojom::PreferenceManager. Upon recieving an initial |
| 24 // OnPreferencesChanged initialization will be considered as completed, and any | 26 // OnPreferencesChanged initialization will be considered as completed, and any |
| 25 // PrefStore::Observer will be notified of OnInitializationCompleted. | 27 // PrefStore::Observer will be notified of OnInitializationCompleted. |
| 26 // | 28 // |
| 27 // Currently this does not support RemoveValue. | 29 // Currently this does not support RemoveValue. |
| 28 class PrefObserverStore : public ValueMapPrefStore, | 30 class PrefObserverStore : public ValueMapPrefStore, |
| 29 public prefs::mojom::PreferencesObserver { | 31 public prefs::mojom::PreferencesObserver { |
| 30 public: | 32 public: |
| 31 explicit PrefObserverStore( | 33 explicit PrefObserverStore( |
| 32 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr); | 34 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr); |
| 33 | 35 |
| 34 // Defines the set of |keys| which PrefOvserverStore will handle. Begins | 36 // Adds a set of |keys| which PrefObserverStore will handle. Begins listening |
| 35 // listening for changes to these from |prefs_manager_|. | 37 // for changes to these from |prefs_manager_|. |
| 36 void Init(const std::set<std::string>& keys); | 38 void Subscribe(const std::set<std::string>& keys); |
| 37 | 39 |
| 38 // PrefStore: | 40 // PrefStore: |
| 39 bool GetValue(const std::string& key, | 41 bool GetValue(const std::string& key, |
| 40 const base::Value** value) const override; | 42 const base::Value** value) const override; |
| 41 | 43 |
| 42 // ValueMapPrefStore: | 44 // ValueMapPrefStore: |
| 43 void SetValue(const std::string& key, | 45 void SetValue(const std::string& key, |
| 44 std::unique_ptr<base::Value> value, | 46 std::unique_ptr<base::Value> value, |
| 45 uint32_t flags) override; | 47 uint32_t flags) override; |
| 46 void RemoveValue(const std::string& key, uint32_t flags) override; | 48 void RemoveValue(const std::string& key, uint32_t flags) override; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 68 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr_; | 70 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr_; |
| 69 | 71 |
| 70 std::set<std::string> keys_; | 72 std::set<std::string> keys_; |
| 71 | 73 |
| 72 // True upon the first OnPreferencesChanged received after Init. | 74 // True upon the first OnPreferencesChanged received after Init. |
| 73 bool initialized_; | 75 bool initialized_; |
| 74 | 76 |
| 75 DISALLOW_COPY_AND_ASSIGN(PrefObserverStore); | 77 DISALLOW_COPY_AND_ASSIGN(PrefObserverStore); |
| 76 }; | 78 }; |
| 77 | 79 |
| 80 } // namespace preferences |
| 78 #endif // SERVICES_PREFERENCES_PUBLIC_CPP_PREFS_OBSERVER_STORE_H_ | 81 #endif // SERVICES_PREFERENCES_PUBLIC_CPP_PREFS_OBSERVER_STORE_H_ |
| OLD | NEW |