Chromium Code Reviews| Index: chrome/browser/prefs/tracked/protected_pref_store.h |
| diff --git a/chrome/browser/prefs/tracked/protected_pref_store.h b/chrome/browser/prefs/tracked/protected_pref_store.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b7bdda563bb582ed8893a4f35edbd2c2c3d82562 |
| --- /dev/null |
| +++ b/chrome/browser/prefs/tracked/protected_pref_store.h |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2014 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 CHROME_BROWSER_PREFS_TRACKED_PROTECTED_PREF_STORE_H_ |
| +#define CHROME_BROWSER_PREFS_TRACKED_PROTECTED_PREF_STORE_H_ |
| + |
| +#include <set> |
| +#include <vector> |
| + |
| +#include "base/callback.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/macros.h" |
| +#include "base/observer_list.h" |
| +#include "base/prefs/persistent_pref_store.h" |
| +#include "base/strings/string_piece.h" |
| +#include "chrome/browser/prefs/pref_hash_filter.h" |
| + |
| +class PrefHashStore; |
| + |
| + |
| +// Provides a unified PersistentPrefStore implementation that splits its storage |
| +// and retrieval between two underlying PersistentPrefStore instances: one each |
| +// for protected and unprotected preferences. |
| +class ProtectedPrefStore : public PersistentPrefStore { |
| + public: |
| + // Creates an instance that delegates to |protected_pref_store| for the |
| + // preferences named in |protected_pref_names| and to |unprotected_pref_store| |
| + // for all others. If an unprotected preference is present in |
| + // |protected_pref_store| (i.e. because it was previously protected) it will |
| + // be migrated back to |unprotected_pref_store| upon access via a non-const |
| + // method. |
| + // |on_initialization| will be invoked when both stores have been initialized, |
| + // before observers of the combined store are notified. |
| + ProtectedPrefStore( |
| + const scoped_refptr<PersistentPrefStore>& unprotected_pref_store, |
| + const scoped_refptr<PersistentPrefStore>& protected_pref_store, |
| + const std::set<std::string>& protected_pref_names, |
| + const base::Closure& on_initialization); |
| + |
| + // PrefStore implementation |
| + virtual void AddObserver(Observer* observer) OVERRIDE; |
| + virtual void RemoveObserver(Observer* observer) OVERRIDE; |
| + virtual bool HasObservers() const OVERRIDE; |
| + virtual bool IsInitializationComplete() const OVERRIDE; |
| + virtual bool GetValue(const std::string& key, |
| + const base::Value** result) const OVERRIDE; |
| + |
| + // WriteablePrefStore implementation |
| + virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE; |
| + virtual void RemoveValue(const std::string& key) OVERRIDE; |
| + |
| + // PersistentPrefStore implementation |
| + virtual bool GetMutableValue(const std::string& key, |
| + base::Value** result) OVERRIDE; |
| + virtual void ReportValueChanged(const std::string& key) OVERRIDE; |
| + virtual void SetValueSilently(const std::string& key, |
| + base::Value* value) OVERRIDE; |
| + virtual bool ReadOnly() const OVERRIDE; |
| + virtual PrefReadError GetReadError() const OVERRIDE; |
| + virtual PrefReadError ReadPrefs() OVERRIDE; |
| + virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; |
| + virtual void CommitPendingWrite() OVERRIDE; |
| + |
| + private: |
| + // Observes events from the underlying stores and synthesizes external events |
| + // via |on_initialization|, |read_error_delegate_|, and |observers_|. |
| + class TeeObserver : public PrefStore::Observer { |
|
Bernhard Bauer
2014/03/26 15:05:50
Hmm... do you even need this class? The outer clas
erikwright (departed)
2014/03/26 15:17:14
In my personal opinion it breaks encapsulation by
Bernhard Bauer
2014/03/26 16:41:11
Hm, I would have been okay with that. Clients do s
erikwright (departed)
2014/03/26 21:08:12
I don't really agree with the reasoning.
I find t
Bernhard Bauer
2014/03/27 15:10:45
Some problems with the current design:
* You have
erikwright (departed)
2014/03/27 19:26:21
You mean the extra dereference? I suppose so, in t
|
| + public: |
| + explicit TeeObserver(ProtectedPrefStore* outer); |
| + |
| + // PrefStore::Observer implementation |
| + virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; |
| + virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; |
| + |
| + private: |
| + ProtectedPrefStore* outer_; |
| + uint8 failed_sub_initializations_; |
|
Bernhard Bauer
2014/03/26 15:05:50
Nit: The style guide says to only use unsigned typ
erikwright (departed)
2014/03/26 21:08:12
Done.
|
| + uint8 successful_sub_initializations_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TeeObserver); |
| + }; |
| + |
| + virtual ~ProtectedPrefStore(); |
| + |
| + // Returns |protected_pref_store| if |key| is protected or a value is present |
| + // in |protected_pref_store|. This could happen if |key| was previously |
| + // protected. |
| + const PersistentPrefStore* StoreForKey(const std::string& key) const; |
| + |
| + // Returns |protected_pref_store| if |key| is protected. If |key| is |
| + // unprotected but has a value in |protected_pref_store|, moves the value to |
| + // |unprotected_pref_store| before returning |unprotected_pref_store|. |
| + PersistentPrefStore* StoreForKey(const std::string& key); |
| + |
| + scoped_refptr<PersistentPrefStore> unprotected_pref_store_; |
| + scoped_refptr<PersistentPrefStore> protected_pref_store_; |
|
Bernhard Bauer
2014/03/26 15:05:50
It's a bit strange that the ProtectedPrefStore has
erikwright (departed)
2014/03/26 15:17:14
We all agree on this.
It's a bit more than a Unif
Bernhard Bauer
2014/03/26 16:41:11
SGTM.
|
| + std::set<std::string> protected_preference_names_; |
| + base::Closure on_initialization_; |
| + |
| + scoped_ptr<PersistentPrefStore::ReadErrorDelegate> read_error_delegate_; |
| + ObserverList<PrefStore::Observer, true> observers_; |
| + TeeObserver tee_observer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ProtectedPrefStore); |
| +}; |
| + |
| +#endif // CHROME_BROWSER_PREFS_TRACKED_PROTECTED_PREF_STORE_H_ |