1 // Copyright (c) 2012 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 CEF_LIBCEF_BROWSER_PREFS_PREF_STORE_H_ 6 #define CEF_LIBCEF_BROWSER_PREFS_PREF_STORE_H_ 7 8 #include <stdint.h> 9 10 #include <string> 11 12 #include "base/compiler_specific.h" 13 #include "base/macros.h" 14 #include "base/observer_list.h" 15 #include "components/prefs/persistent_pref_store.h" 16 #include "components/prefs/pref_value_map.h" 17 18 // Preference store implementation that supports explicit manipulation of the 19 // contents of the store, triggering notifications where appropriate. 20 // Based on components/prefs/testing_pref_store.h. 21 class CefPrefStore : public PersistentPrefStore { 22 public: 23 CefPrefStore(); 24 25 // Overriden from PrefStore. 26 bool GetValue(const std::string& key, 27 const base::Value** result) const override; 28 std::unique_ptr<base::DictionaryValue> GetValues() const override; 29 void AddObserver(PrefStore::Observer* observer) override; 30 void RemoveObserver(PrefStore::Observer* observer) override; 31 bool HasObservers() const override; 32 bool IsInitializationComplete() const override; 33 34 // PersistentPrefStore overrides: 35 bool GetMutableValue(const std::string& key, base::Value** result) override; 36 void ReportValueChanged(const std::string& key, uint32_t flags) override; 37 void SetValue(const std::string& key, 38 std::unique_ptr<base::Value> value, 39 uint32_t flags) override; 40 void SetValueSilently(const std::string& key, 41 std::unique_ptr<base::Value> value, 42 uint32_t flags) override; 43 void RemoveValuesByPrefixSilently(const std::string& prefix) override; 44 void RemoveValue(const std::string& key, uint32_t flags) override; 45 bool ReadOnly() const override; 46 PrefReadError GetReadError() const override; 47 PersistentPrefStore::PrefReadError ReadPrefs() override; 48 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; 49 virtual void CommitPendingWrite( 50 base::OnceClosure done_callback, 51 base::OnceClosure synchronous_done_callback) override; 52 void SchedulePendingLossyWrites() override; 53 void ClearMutableValues() override; 54 void OnStoreDeletionFromDisk() override; 55 56 // Marks the store as having completed initialization. 57 void SetInitializationCompleted(); 58 59 // Used for tests to trigger notifications explicitly. 60 void NotifyPrefValueChanged(const std::string& key); 61 void NotifyInitializationCompleted(); 62 63 // Some convenience getters/setters. 64 void SetString(const std::string& key, const std::string& value); 65 void SetInteger(const std::string& key, int value); 66 void SetBoolean(const std::string& key, bool value); 67 68 bool GetString(const std::string& key, std::string* value) const; 69 bool GetInteger(const std::string& key, int* value) const; 70 bool GetBoolean(const std::string& key, bool* value) const; 71 72 // Determines whether ReadPrefsAsync completes immediately. Defaults to false 73 // (non-blocking). To block, invoke this with true (blocking) before the call 74 // to ReadPrefsAsync. To unblock, invoke again with false (non-blocking) after 75 // the call to ReadPrefsAsync. 76 void SetBlockAsyncRead(bool block_async_read); 77 78 // Getter and Setter methods for setting and getting the state of the 79 // |TestingPrefStore|. 80 virtual void set_read_only(bool read_only); 81 void set_read_success(bool read_success); 82 void set_read_error(PersistentPrefStore::PrefReadError read_error); committed()83 bool committed() { return committed_; } 84 85 protected: 86 ~CefPrefStore() override; 87 88 private: 89 // Stores the preference values. 90 PrefValueMap prefs_; 91 92 // Flag that indicates if the PrefStore is read-only 93 bool read_only_; 94 95 // The result to pass to PrefStore::Observer::OnInitializationCompleted 96 bool read_success_; 97 98 // The result to return from ReadPrefs or ReadPrefsAsync. 99 PersistentPrefStore::PrefReadError read_error_; 100 101 // Whether a call to ReadPrefsAsync should block. 102 bool block_async_read_; 103 104 // Whether there is a pending call to ReadPrefsAsync. 105 bool pending_async_read_; 106 107 // Whether initialization has been completed. 108 bool init_complete_; 109 110 // Whether the store contents have been committed to disk since the last 111 // mutation. 112 bool committed_; 113 114 std::unique_ptr<ReadErrorDelegate> error_delegate_; 115 base::ObserverList<PrefStore::Observer, true>::Unchecked observers_; 116 117 DISALLOW_COPY_AND_ASSIGN(CefPrefStore); 118 }; 119 120 #endif // COMPONENTS_PREFS_TESTING_PREF_STORE_H_ 121