| 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 CHROME_COMMON_JSON_PREF_STORE_H_ | 5 #ifndef CHROME_COMMON_JSON_PREF_STORE_H_ |
| 6 #define CHROME_COMMON_JSON_PREF_STORE_H_ | 6 #define CHROME_COMMON_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/file_path.h" | 13 #include "base/file_path.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "chrome/common/important_file_writer.h" | 16 #include "chrome/common/important_file_writer.h" |
| 17 #include "chrome/common/persistent_pref_store.h" | 17 #include "chrome/common/persistent_pref_store.h" |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 class DictionaryValue; | 20 class DictionaryValue; |
| 21 class MessageLoopProxy; | 21 class SequencedWorkerPool; |
| 22 class TaskRunner; |
| 22 class Value; | 23 class Value; |
| 23 } | 24 } |
| 24 | 25 |
| 25 class FilePath; | 26 class FilePath; |
| 26 | 27 |
| 27 // A writable PrefStore implementation that is used for user preferences. | 28 // A writable PrefStore implementation that is used for user preferences. |
| 28 class JsonPrefStore : public PersistentPrefStore, | 29 class JsonPrefStore : public PersistentPrefStore, |
| 29 public ImportantFileWriter::DataSerializer { | 30 public ImportantFileWriter::DataSerializer { |
| 30 public: | 31 public: |
| 31 // |file_message_loop_proxy| is the MessageLoopProxy for a thread on which | 32 // |worker_pool| is the SequencedWorkerPool on which file I/O can be done. |
| 32 // file I/O can be done. | 33 // The worker pool will also ensure that write requests for the same file are |
| 33 JsonPrefStore(const FilePath& pref_filename, | 34 // sequenced properly and that such request block shutdown. |
| 34 base::MessageLoopProxy* file_message_loop_proxy); | 35 static JsonPrefStore* Create(const FilePath& pref_filename, |
| 36 base::SequencedWorkerPool* worker_pool); |
| 37 |
| 38 // |task_runner| is the TaskRunner for a thread on which file I/O can be done. |
| 39 static JsonPrefStore* Create(const FilePath& pref_filename, |
| 40 base::TaskRunner* task_runner); |
| 35 | 41 |
| 36 // PrefStore overrides: | 42 // PrefStore overrides: |
| 37 virtual ReadResult GetValue(const std::string& key, | 43 virtual ReadResult GetValue(const std::string& key, |
| 38 const base::Value** result) const OVERRIDE; | 44 const base::Value** result) const OVERRIDE; |
| 39 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; | 45 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; |
| 40 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; | 46 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; |
| 41 virtual size_t NumberOfObservers() const OVERRIDE; | 47 virtual size_t NumberOfObservers() const OVERRIDE; |
| 42 virtual bool IsInitializationComplete() const OVERRIDE; | 48 virtual bool IsInitializationComplete() const OVERRIDE; |
| 43 | 49 |
| 44 // PersistentPrefStore overrides: | 50 // PersistentPrefStore overrides: |
| (...skipping 11 matching lines...) Expand all Loading... |
| 56 virtual void CommitPendingWrite() OVERRIDE; | 62 virtual void CommitPendingWrite() OVERRIDE; |
| 57 virtual void ReportValueChanged(const std::string& key) OVERRIDE; | 63 virtual void ReportValueChanged(const std::string& key) OVERRIDE; |
| 58 | 64 |
| 59 // This method is called after JSON file has been read. Method takes | 65 // This method is called after JSON file has been read. Method takes |
| 60 // ownership of the |value| pointer. Note, this method is used with | 66 // ownership of the |value| pointer. Note, this method is used with |
| 61 // asynchronous file reading, so class exposes it only for the internal needs. | 67 // asynchronous file reading, so class exposes it only for the internal needs. |
| 62 // (read: do not call it manually). | 68 // (read: do not call it manually). |
| 63 void OnFileRead(base::Value* value_owned, PrefReadError error, bool no_dir); | 69 void OnFileRead(base::Value* value_owned, PrefReadError error, bool no_dir); |
| 64 | 70 |
| 65 private: | 71 private: |
| 72 // |task_runner| is the TaskRunner for a thread on which file I/O can be done. |
| 73 JsonPrefStore(const FilePath& pref_filename, |
| 74 base::TaskRunner* task_runner); |
| 66 virtual ~JsonPrefStore(); | 75 virtual ~JsonPrefStore(); |
| 67 | 76 |
| 68 // ImportantFileWriter::DataSerializer overrides: | 77 // ImportantFileWriter::DataSerializer overrides: |
| 69 virtual bool SerializeData(std::string* output) OVERRIDE; | 78 virtual bool SerializeData(std::string* output) OVERRIDE; |
| 70 | 79 |
| 71 FilePath path_; | 80 FilePath path_; |
| 72 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_; | 81 scoped_refptr<base::TaskRunner> task_runner_; |
| 73 | 82 |
| 74 scoped_ptr<base::DictionaryValue> prefs_; | 83 scoped_ptr<base::DictionaryValue> prefs_; |
| 75 | 84 |
| 76 bool read_only_; | 85 bool read_only_; |
| 77 | 86 |
| 78 // Helper for safely writing pref data. | 87 // Helper for safely writing pref data. |
| 79 ImportantFileWriter writer_; | 88 ImportantFileWriter writer_; |
| 80 | 89 |
| 81 ObserverList<PrefStore::Observer, true> observers_; | 90 ObserverList<PrefStore::Observer, true> observers_; |
| 82 | 91 |
| 83 scoped_ptr<ReadErrorDelegate> error_delegate_; | 92 scoped_ptr<ReadErrorDelegate> error_delegate_; |
| 84 | 93 |
| 85 bool initialized_; | 94 bool initialized_; |
| 86 PrefReadError read_error_; | 95 PrefReadError read_error_; |
| 87 | 96 |
| 88 std::set<std::string> keys_need_empty_value_; | 97 std::set<std::string> keys_need_empty_value_; |
| 89 | 98 |
| 90 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 99 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
| 91 }; | 100 }; |
| 92 | 101 |
| 93 #endif // CHROME_COMMON_JSON_PREF_STORE_H_ | 102 #endif // CHROME_COMMON_JSON_PREF_STORE_H_ |
| OLD | NEW |