| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_PREFS_JSON_PREF_STORE_H_ | 5 #ifndef BASE_PREFS_JSON_PREF_STORE_H_ |
| 6 #define BASE_PREFS_JSON_PREF_STORE_H_ | 6 #define BASE_PREFS_JSON_PREF_STORE_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/important_file_writer.h" | 14 #include "base/files/important_file_writer.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/message_loop/message_loop_proxy.h" | 16 #include "base/message_loop/message_loop_proxy.h" |
| 17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 18 #include "base/prefs/base_prefs_export.h" | 18 #include "base/prefs/base_prefs_export.h" |
| 19 #include "base/prefs/json.pb.h" |
| 19 #include "base/prefs/persistent_pref_store.h" | 20 #include "base/prefs/persistent_pref_store.h" |
| 21 #include "base/prefs/pref_value_map.h" |
| 22 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 20 | 23 |
| 21 class PrefFilter; | 24 class PrefFilter; |
| 22 | 25 |
| 23 namespace base { | 26 namespace base { |
| 24 class DictionaryValue; | 27 class DictionaryValue; |
| 25 class FilePath; | 28 class FilePath; |
| 29 class SequencedWorkerPool; |
| 26 class SequencedTaskRunner; | 30 class SequencedTaskRunner; |
| 27 class SequencedWorkerPool; | |
| 28 class Value; | 31 class Value; |
| 29 } | 32 } |
| 30 | 33 |
| 31 | |
| 32 // A writable PrefStore implementation that is used for user preferences. | 34 // A writable PrefStore implementation that is used for user preferences. |
| 33 class BASE_PREFS_EXPORT JsonPrefStore | 35 class BASE_PREFS_EXPORT LevelDBPrefStore : public PersistentPrefStore { |
| 34 : public PersistentPrefStore, | |
| 35 public base::ImportantFileWriter::DataSerializer { | |
| 36 public: | 36 public: |
| 37 // Returns instance of SequencedTaskRunner which guarantees that file | |
| 38 // operations on the same file will be executed in sequenced order. | |
| 39 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( | |
| 40 const base::FilePath& pref_filename, | |
| 41 base::SequencedWorkerPool* worker_pool); | |
| 42 | |
| 43 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally | 37 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally |
| 44 // created by GetTaskRunnerForFile() method above. | 38 // created by GetTaskRunnerForFile() method above. |
| 45 JsonPrefStore(const base::FilePath& pref_filename, | 39 LevelDBPrefStore(const base::FilePath& pref_filename, |
| 46 base::SequencedTaskRunner* sequenced_task_runner, | 40 base::SequencedTaskRunner* sequenced_task_runner, |
| 47 scoped_ptr<PrefFilter> pref_filter); | 41 scoped_ptr<PrefFilter> pref_filter); |
| 48 | 42 |
| 49 // PrefStore overrides: | 43 // PrefStore overrides: |
| 50 virtual bool GetValue(const std::string& key, | 44 virtual bool GetValue(const std::string& key, |
| 51 const base::Value** result) const OVERRIDE; | 45 const base::Value** result) const OVERRIDE; |
| 52 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; | 46 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; |
| 53 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; | 47 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; |
| 54 virtual bool HasObservers() const OVERRIDE; | 48 virtual bool HasObservers() const OVERRIDE; |
| 55 virtual bool IsInitializationComplete() const OVERRIDE; | 49 virtual bool IsInitializationComplete() const OVERRIDE; |
| 56 | 50 |
| 57 // PersistentPrefStore overrides: | 51 // PersistentPrefStore overrides: |
| 58 virtual bool GetMutableValue(const std::string& key, | 52 virtual bool GetMutableValue(const std::string& key, |
| 59 base::Value** result) OVERRIDE; | 53 base::Value** result) OVERRIDE; |
| 54 // Takes ownership of value. |
| 60 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE; | 55 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE; |
| 61 virtual void SetValueSilently(const std::string& key, | 56 virtual void SetValueSilently(const std::string& key, |
| 62 base::Value* value) OVERRIDE; | 57 base::Value* value) OVERRIDE; |
| 63 virtual void RemoveValue(const std::string& key) OVERRIDE; | 58 virtual void RemoveValue(const std::string& key) OVERRIDE; |
| 64 virtual bool ReadOnly() const OVERRIDE; | 59 virtual bool ReadOnly() const OVERRIDE; |
| 65 virtual PrefReadError GetReadError() const OVERRIDE; | 60 virtual PrefReadError GetReadError() const OVERRIDE; |
| 66 virtual PrefReadError ReadPrefs() OVERRIDE; | 61 virtual PrefReadError ReadPrefs() OVERRIDE; |
| 67 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; | 62 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; |
| 68 virtual void CommitPendingWrite() OVERRIDE; | 63 virtual void CommitPendingWrite() OVERRIDE; |
| 69 virtual void ReportValueChanged(const std::string& key) OVERRIDE; | 64 virtual void ReportValueChanged(const std::string& key) OVERRIDE; |
| 70 | 65 |
| 71 // This method is called after JSON file has been read. Method takes | 66 void OnStorageRead(PrefReadError error, bool no_dir); |
| 72 // ownership of the |value| pointer. Note, this method is used with | |
| 73 // asynchronous file reading, so class exposes it only for the internal needs. | |
| 74 // (read: do not call it manually). | |
| 75 void OnFileRead(base::Value* value_owned, PrefReadError error, bool no_dir); | |
| 76 | 67 |
| 77 private: | 68 private: |
| 78 virtual ~JsonPrefStore(); | 69 virtual ~LevelDBPrefStore(); |
| 79 | 70 |
| 80 // ImportantFileWriter::DataSerializer overrides: | 71 void PersistOnFileThread(const std::string& key, const std::string& value); |
| 81 virtual bool SerializeData(std::string* output) OVERRIDE; | 72 void PersistFromUIThread(const std::string& key, base::Value* value); |
| 73 void RemoveOnFileThread(const std::string& key); |
| 74 void RemoveFromUIThread(const std::string& key); |
| 75 |
| 76 void NotifyObservers(const std::string& key); |
| 82 | 77 |
| 83 base::FilePath path_; | 78 base::FilePath path_; |
| 84 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | 79 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
| 80 const scoped_refptr<base::SequencedTaskRunner> original_task_runner_; |
| 85 | 81 |
| 86 scoped_ptr<base::DictionaryValue> prefs_; | 82 PrefValueMap prefs_; |
| 83 |
| 84 // Open writes some stuff to the DB so we can't necessarily do it on the UI |
| 85 // thread. |
| 86 scoped_ptr<leveldb::DB> db_; |
| 87 | 87 |
| 88 bool read_only_; | 88 bool read_only_; |
| 89 | 89 |
| 90 // Helper for safely writing pref data. | |
| 91 base::ImportantFileWriter writer_; | |
| 92 | |
| 93 scoped_ptr<PrefFilter> pref_filter_; | 90 scoped_ptr<PrefFilter> pref_filter_; |
| 94 ObserverList<PrefStore::Observer, true> observers_; | 91 ObserverList<PrefStore::Observer, true> observers_; |
| 95 | 92 |
| 96 scoped_ptr<ReadErrorDelegate> error_delegate_; | 93 scoped_ptr<ReadErrorDelegate> error_delegate_; |
| 97 | 94 |
| 98 bool initialized_; | 95 bool initialized_; |
| 99 PrefReadError read_error_; | 96 PrefReadError read_error_; |
| 100 | 97 |
| 98 // Only access from the UI thread. |
| 101 std::set<std::string> keys_need_empty_value_; | 99 std::set<std::string> keys_need_empty_value_; |
| 102 | 100 |
| 103 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 101 DISALLOW_COPY_AND_ASSIGN(LevelDBPrefStore); |
| 104 }; | 102 }; |
| 105 | 103 |
| 106 #endif // BASE_PREFS_JSON_PREF_STORE_H_ | 104 #endif // BASE_PREFS_JSON_PREF_STORE_H_ |
| OLD | NEW |