Chromium Code Reviews| Index: components/prefs/pref_observer_store.h |
| diff --git a/components/prefs/pref_observer_store.h b/components/prefs/pref_observer_store.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..193ccddffdfae49a9c96dc07d3344d65554f6cb3 |
| --- /dev/null |
| +++ b/components/prefs/pref_observer_store.h |
| @@ -0,0 +1,84 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_PREFS_PREFERENCE_OBSERVER_STORE_H_ |
| +#define COMPONENTS_PREFS_PREFERENCE_OBSERVER_STORE_H_ |
| + |
| +#include <set> |
| + |
| +#include "base/macros.h" |
| +#include "components/prefs/base_prefs_export.h" |
| +#include "components/prefs/public/interfaces/preferences.mojom.h" |
| +#include "components/prefs/value_map_pref_store.h" |
| +#include "mojo/public/cpp/bindings/binding.h" |
| +#include "mojo/public/cpp/bindings/string.h" |
| + |
| +namespace shell { |
| +class Connector; |
| +} |
| + |
| +class PrefObserverStoreTest; |
| + |
| +// An implementation of PrefStore which uses prefs::mojom::PreferenceManager as |
| +// the backing of the preferences. |
| +// |
| +// PrefObserverStore caches the values locally to provide synchronous access. |
| +// The cache is empty until the first notification of OnPreferencesChanged is |
| +// received from the prefs::mojom::PreferenceManager. Upon this completion any |
| +// PrefStore::Observer will be notified of OnInitializationCompleted. |
| +// |
| +// Currently this does not support RemoveValue or GetMutableValue. |
| +class COMPONENTS_PREFS_EXPORT PrefObserverStore |
| + : public ValueMapPrefStore, |
| + public prefs::mojom::PreferenceObserver { |
| + public: |
| + PrefObserverStore(shell::Connector* connector); |
| + |
| + // Defines the set of |keys| which PrefOvserverStore will handle. Begins |
| + // listening for changes to these from |prefs_manager_|. |
| + void Init(const std::set<mojo::String>& keys); |
|
sadrul
2016/06/23 17:30:17
Can this be std::vector<std::string> instead?
jonross
2016/06/24 15:13:06
I had went with mojo::String as I was seeing this
|
| + |
| + // PrefStore: |
| + bool GetValue(const std::string& key, |
| + const base::Value** value) const override; |
| + |
| + // ValueMapPrefStore: |
| + void SetValue(const std::string& key, |
| + std::unique_ptr<base::Value> value, |
| + uint32_t flags) override; |
| + void RemoveValue(const std::string& key, uint32_t flags) override; |
| + bool GetMutableValue(const std::string& key, base::Value** value) override; |
| + void ReportValueChanged(const std::string& key, uint32_t flags) override; |
| + void SetValueSilently(const std::string& key, |
| + std::unique_ptr<base::Value> value, |
| + uint32_t flags) override; |
| + |
| + protected: |
| + // base::RefCounted<PrefStore>: |
| + ~PrefObserverStore() override; |
| + |
| + private: |
| + friend class PrefObserverStoreTest; |
| + |
| + // Notifies |prefs_manager_| of the change in |key| - |value| pair. |
| + void SetValueOnPreferenceManager(const std::string& key, base::Value* value); |
| + |
| + // prefs::mojom::PreferenceObserver: |
| + void OnPreferencesChanged( |
| + prefs::mojom::PreferenceMapPtr preferences) override; |
| + |
| + mojo::Binding<prefs::mojom::PreferenceObserver> prefs_binding_; |
| + prefs::mojom::PreferenceManagerPtr prefs_manager_ptr_; |
| + // Typically contained in |prefs_manager_ptr_| however tests may override. |
| + prefs::mojom::PreferenceManager* prefs_manager_; |
| + |
| + std::set<mojo::String> keys_; |
| + |
| + // True upon the first OnPreferencesChanged received after Init. |
| + bool initialized_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PrefObserverStore); |
| +}; |
| + |
| +#endif // COMPONENTS_PREFS_PREFERENCE_OBSERVER_STORE_H_ |