Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / prefs / json_pref_store.h
blob6df9e239b11cdc9a7c6b18a9196c4a8d90d76d5e
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.
5 #ifndef BASE_PREFS_JSON_PREF_STORE_H_
6 #define BASE_PREFS_JSON_PREF_STORE_H_
8 #include <set>
9 #include <string>
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 #include "base/threading/non_thread_safe.h"
24 class PrefFilter;
26 namespace base {
27 class DictionaryValue;
28 class FilePath;
29 class SequencedTaskRunner;
30 class SequencedWorkerPool;
31 class Value;
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 base::NonThreadSafe {
40 public:
41 struct ReadResult;
43 // Returns instance of SequencedTaskRunner which guarantees that file
44 // operations on the same file will be executed in sequenced order.
45 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile(
46 const base::FilePath& pref_filename,
47 base::SequencedWorkerPool* worker_pool);
49 // Same as the constructor below with no alternate filename.
50 JsonPrefStore(const base::FilePath& pref_filename,
51 base::SequencedTaskRunner* sequenced_task_runner,
52 scoped_ptr<PrefFilter> pref_filter);
54 // |sequenced_task_runner| must be a shutdown-blocking task runner, ideally
55 // created by the GetTaskRunnerForFile() method above.
56 // |pref_filename| is the path to the file to read prefs from.
57 // |pref_alternate_filename| is the path to an alternate file which the
58 // desired prefs may have previously been written to. If |pref_filename|
59 // doesn't exist and |pref_alternate_filename| does, |pref_alternate_filename|
60 // will be moved to |pref_filename| before the read occurs.
61 JsonPrefStore(const base::FilePath& pref_filename,
62 const base::FilePath& pref_alternate_filename,
63 base::SequencedTaskRunner* sequenced_task_runner,
64 scoped_ptr<PrefFilter> pref_filter);
66 // PrefStore overrides:
67 virtual bool GetValue(const std::string& key,
68 const base::Value** result) const OVERRIDE;
69 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE;
70 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE;
71 virtual bool HasObservers() const OVERRIDE;
72 virtual bool IsInitializationComplete() const OVERRIDE;
74 // PersistentPrefStore overrides:
75 virtual bool GetMutableValue(const std::string& key,
76 base::Value** result) OVERRIDE;
77 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE;
78 virtual void SetValueSilently(const std::string& key,
79 base::Value* value) OVERRIDE;
80 virtual void RemoveValue(const std::string& key) OVERRIDE;
81 virtual bool ReadOnly() const OVERRIDE;
82 virtual PrefReadError GetReadError() const OVERRIDE;
83 // Note this method may be asynchronous if this instance has a |pref_filter_|
84 // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE.
85 // See details in pref_filter.h.
86 virtual PrefReadError ReadPrefs() OVERRIDE;
87 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE;
88 virtual void CommitPendingWrite() OVERRIDE;
89 virtual void ReportValueChanged(const std::string& key) OVERRIDE;
91 // Just like RemoveValue(), but doesn't notify observers. Used when doing some
92 // cleanup that shouldn't otherwise alert observers.
93 void RemoveValueSilently(const std::string& key);
95 // Registers |on_next_successful_write| to be called once, on the next
96 // successful write event of |writer_|.
97 void RegisterOnNextSuccessfulWriteCallback(
98 const base::Closure& on_next_successful_write);
100 private:
101 virtual ~JsonPrefStore();
103 // This method is called after the JSON file has been read. It then hands
104 // |value| (or an empty dictionary in some read error cases) to the
105 // |pref_filter| if one is set. It also gives a callback pointing at
106 // FinalizeFileRead() to that |pref_filter_| which is then responsible for
107 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead()
108 // is invoked directly.
109 void OnFileRead(scoped_ptr<ReadResult> read_result);
111 // ImportantFileWriter::DataSerializer overrides:
112 virtual bool SerializeData(std::string* output) OVERRIDE;
114 // This method is called after the JSON file has been read and the result has
115 // potentially been intercepted and modified by |pref_filter_|.
116 // |initialization_successful| is pre-determined by OnFileRead() and should
117 // be used when reporting OnInitializationCompleted().
118 // |schedule_write| indicates whether a write should be immediately scheduled
119 // (typically because the |pref_filter_| has already altered the |prefs|) --
120 // this will be ignored if this store is read-only.
121 void FinalizeFileRead(bool initialization_successful,
122 scoped_ptr<base::DictionaryValue> prefs,
123 bool schedule_write);
125 const base::FilePath path_;
126 const base::FilePath alternate_path_;
127 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
129 scoped_ptr<base::DictionaryValue> prefs_;
131 bool read_only_;
133 // Helper for safely writing pref data.
134 base::ImportantFileWriter writer_;
136 scoped_ptr<PrefFilter> pref_filter_;
137 ObserverList<PrefStore::Observer, true> observers_;
139 scoped_ptr<ReadErrorDelegate> error_delegate_;
141 bool initialized_;
142 bool filtering_in_progress_;
143 PrefReadError read_error_;
145 std::set<std::string> keys_need_empty_value_;
147 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore);
150 #endif // BASE_PREFS_JSON_PREF_STORE_H_