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/file_path.h" | 13 #include "base/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_proxy.h" | 16 #include "base/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/persistent_pref_store.h" | 19 #include "base/prefs/persistent_pref_store.h" |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 class DictionaryValue; | 22 class DictionaryValue; |
23 class MessageLoopProxy; | 23 class SequencedWorkerPool; |
| 24 class SequencedTaskRunner; |
24 class Value; | 25 class Value; |
25 } | 26 } |
26 | 27 |
27 class FilePath; | 28 class FilePath; |
28 | 29 |
29 // A writable PrefStore implementation that is used for user preferences. | 30 // A writable PrefStore implementation that is used for user preferences. |
30 class BASE_PREFS_EXPORT JsonPrefStore | 31 class BASE_PREFS_EXPORT JsonPrefStore |
31 : public PersistentPrefStore, | 32 : public PersistentPrefStore, |
32 public base::ImportantFileWriter::DataSerializer { | 33 public base::ImportantFileWriter::DataSerializer { |
33 public: | 34 public: |
34 // |file_message_loop_proxy| is the MessageLoopProxy for a thread on which | 35 // Returns instance of SequencedTaskRunner which guarantees that file |
35 // file I/O can be done. | 36 // operations on the same file will be executed in sequenced order. |
| 37 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( |
| 38 const FilePath& pref_filename, |
| 39 base::SequencedWorkerPool* worker_pool); |
| 40 |
| 41 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally |
| 42 // created by GetTaskRunnerForFile() method above. |
36 JsonPrefStore(const FilePath& pref_filename, | 43 JsonPrefStore(const FilePath& pref_filename, |
37 base::MessageLoopProxy* file_message_loop_proxy); | 44 base::SequencedTaskRunner* sequenced_task_runner); |
38 | 45 |
39 // PrefStore overrides: | 46 // PrefStore overrides: |
40 virtual ReadResult GetValue(const std::string& key, | 47 virtual ReadResult GetValue(const std::string& key, |
41 const base::Value** result) const OVERRIDE; | 48 const base::Value** result) const OVERRIDE; |
42 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; | 49 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; |
43 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; | 50 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; |
44 virtual size_t NumberOfObservers() const OVERRIDE; | 51 virtual size_t NumberOfObservers() const OVERRIDE; |
45 virtual bool IsInitializationComplete() const OVERRIDE; | 52 virtual bool IsInitializationComplete() const OVERRIDE; |
46 | 53 |
47 // PersistentPrefStore overrides: | 54 // PersistentPrefStore overrides: |
(...skipping 17 matching lines...) Expand all Loading... |
65 // (read: do not call it manually). | 72 // (read: do not call it manually). |
66 void OnFileRead(base::Value* value_owned, PrefReadError error, bool no_dir); | 73 void OnFileRead(base::Value* value_owned, PrefReadError error, bool no_dir); |
67 | 74 |
68 private: | 75 private: |
69 virtual ~JsonPrefStore(); | 76 virtual ~JsonPrefStore(); |
70 | 77 |
71 // ImportantFileWriter::DataSerializer overrides: | 78 // ImportantFileWriter::DataSerializer overrides: |
72 virtual bool SerializeData(std::string* output) OVERRIDE; | 79 virtual bool SerializeData(std::string* output) OVERRIDE; |
73 | 80 |
74 FilePath path_; | 81 FilePath path_; |
75 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_; | 82 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
76 | 83 |
77 scoped_ptr<base::DictionaryValue> prefs_; | 84 scoped_ptr<base::DictionaryValue> prefs_; |
78 | 85 |
79 bool read_only_; | 86 bool read_only_; |
80 | 87 |
81 // Helper for safely writing pref data. | 88 // Helper for safely writing pref data. |
82 base::ImportantFileWriter writer_; | 89 base::ImportantFileWriter writer_; |
83 | 90 |
84 ObserverList<PrefStore::Observer, true> observers_; | 91 ObserverList<PrefStore::Observer, true> observers_; |
85 | 92 |
86 scoped_ptr<ReadErrorDelegate> error_delegate_; | 93 scoped_ptr<ReadErrorDelegate> error_delegate_; |
87 | 94 |
88 bool initialized_; | 95 bool initialized_; |
89 PrefReadError read_error_; | 96 PrefReadError read_error_; |
90 | 97 |
91 std::set<std::string> keys_need_empty_value_; | 98 std::set<std::string> keys_need_empty_value_; |
92 | 99 |
93 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 100 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
94 }; | 101 }; |
95 | 102 |
96 #endif // BASE_PREFS_JSON_PREF_STORE_H_ | 103 #endif // BASE_PREFS_JSON_PREF_STORE_H_ |
OLD | NEW |