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 BASE_PREFS_JSON_PREF_STORE_H_ 6 #define BASE_PREFS_JSON_PREF_STORE_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/callback_forward.h" 13 #include "base/compiler_specific.h" 14 #include "base/files/file_path.h" 15 #include "base/files/important_file_writer.h" 16 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/weak_ptr.h" 18 #include "base/message_loop/message_loop_proxy.h" 19 #include "base/observer_list.h" 20 #include "base/prefs/base_prefs_export.h" 21 #include "base/prefs/persistent_pref_store.h" 22 23 class PrefFilter; 24 25 namespace base { 26 class DictionaryValue; 27 class FilePath; 28 class SequencedTaskRunner; 29 class SequencedWorkerPool; 30 class Value; 31 } 32 33 34 // A writable PrefStore implementation that is used for user preferences. 35 class BASE_PREFS_EXPORT JsonPrefStore 36 : public PersistentPrefStore, 37 public base::ImportantFileWriter::DataSerializer, 38 public base::SupportsWeakPtr<JsonPrefStore> { 39 public: 40 // Returns instance of SequencedTaskRunner which guarantees that file 41 // operations on the same file will be executed in sequenced order. 42 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( 43 const base::FilePath& pref_filename, 44 base::SequencedWorkerPool* worker_pool); 45 46 // Same as the constructor below with no alternate filename. 47 JsonPrefStore(const base::FilePath& pref_filename, 48 base::SequencedTaskRunner* sequenced_task_runner, 49 scoped_ptr<PrefFilter> pref_filter); 50 51 // |sequenced_task_runner| must be a shutdown-blocking task runner, ideally 52 // created by the GetTaskRunnerForFile() method above. 53 // |pref_filename| is the path to the file to read prefs from. 54 // |pref_alternate_filename| is the path to an alternate file which the 55 // desired prefs may have previously been written to. If |pref_filename| 56 // doesn't exist and |pref_alternate_filename| does, |pref_alternate_filename| 57 // will be moved to |pref_filename| before the read occurs. 58 JsonPrefStore(const base::FilePath& pref_filename, 59 const base::FilePath& pref_alternate_filename, 60 base::SequencedTaskRunner* sequenced_task_runner, 61 scoped_ptr<PrefFilter> pref_filter); 62 63 // PrefStore overrides: 64 virtual bool GetValue(const std::string& key, 65 const base::Value** result) const OVERRIDE; 66 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; 67 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; 68 virtual bool HasObservers() const OVERRIDE; 69 virtual bool IsInitializationComplete() const OVERRIDE; 70 71 // PersistentPrefStore overrides: 72 virtual bool GetMutableValue(const std::string& key, 73 base::Value** result) OVERRIDE; 74 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE; 75 virtual void SetValueSilently(const std::string& key, 76 base::Value* value) OVERRIDE; 77 virtual void RemoveValue(const std::string& key) OVERRIDE; 78 virtual bool ReadOnly() const OVERRIDE; 79 virtual PrefReadError GetReadError() const OVERRIDE; 80 // Note this method may be asynchronous if this instance has a |pref_filter_| 81 // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE. 82 // See details in pref_filter.h. 83 virtual PrefReadError ReadPrefs() OVERRIDE; 84 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; 85 virtual void CommitPendingWrite() OVERRIDE; 86 virtual void ReportValueChanged(const std::string& key) OVERRIDE; 87 88 // Just like RemoveValue(), but doesn't notify observers. Used when doing some 89 // cleanup that shouldn't otherwise alert observers. 90 void RemoveValueSilently(const std::string& key); 91 92 // Registers |on_next_successful_write| to be called once, on the next 93 // successful write event of |writer_|. 94 void RegisterOnNextSuccessfulWriteCallback( 95 const base::Closure& on_next_successful_write); 96 97 // This method is called after the JSON file has been read. It then hands 98 // |value| (or an empty dictionary in some read error cases) to the 99 // |pref_filter| if one is set. It also gives a callback pointing at 100 // FinalizeFileRead() to that |pref_filter_| which is then responsible for 101 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() 102 // is invoked directly. 103 // Note, this method is used with asynchronous file reading, so this class 104 // exposes it only for the internal needs (read: do not call it manually). 105 // TODO(gab): Move this method to the private section and hand a callback to 106 // it to FileThreadDeserializer rather than exposing this public method and 107 // giving a JsonPrefStore* to FileThreadDeserializer. 108 void OnFileRead(scoped_ptr<base::Value> value, 109 PrefReadError error, 110 bool no_dir); 111 112 private: 113 virtual ~JsonPrefStore(); 114 115 // ImportantFileWriter::DataSerializer overrides: 116 virtual bool SerializeData(std::string* output) OVERRIDE; 117 118 // This method is called after the JSON file has been read and the result has 119 // potentially been intercepted and modified by |pref_filter_|. 120 // |initialization_successful| is pre-determined by OnFileRead() and should 121 // be used when reporting OnInitializationCompleted(). 122 // |schedule_write| indicates whether a write should be immediately scheduled 123 // (typically because the |pref_filter_| has already altered the |prefs|) -- 124 // this will be ignored if this store is read-only. 125 void FinalizeFileRead(bool initialization_successful, 126 scoped_ptr<base::DictionaryValue> prefs, 127 bool schedule_write); 128 129 const base::FilePath path_; 130 const base::FilePath alternate_path_; 131 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; 132 133 scoped_ptr<base::DictionaryValue> prefs_; 134 135 bool read_only_; 136 137 // Helper for safely writing pref data. 138 base::ImportantFileWriter writer_; 139 140 scoped_ptr<PrefFilter> pref_filter_; 141 ObserverList<PrefStore::Observer, true> observers_; 142 143 scoped_ptr<ReadErrorDelegate> error_delegate_; 144 145 bool initialized_; 146 bool filtering_in_progress_; 147 PrefReadError read_error_; 148 149 std::set<std::string> keys_need_empty_value_; 150 151 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); 152 }; 153 154 #endif // BASE_PREFS_JSON_PREF_STORE_H_ 155