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