Make FTS2 inclusion in SQLite optional
[chromium-blink-merge.git] / base / prefs / json_pref_store.h
blob7f26c89867e6433d58e53c4279557a3aafcb29b0
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(
51 const base::FilePath& pref_filename,
52 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
53 scoped_ptr<PrefFilter> pref_filter);
55 // |sequenced_task_runner| must be a shutdown-blocking task runner, ideally
56 // created by the GetTaskRunnerForFile() method above.
57 // |pref_filename| is the path to the file to read prefs from.
58 // |pref_alternate_filename| is the path to an alternate file which the
59 // desired prefs may have previously been written to. If |pref_filename|
60 // doesn't exist and |pref_alternate_filename| does, |pref_alternate_filename|
61 // will be moved to |pref_filename| before the read occurs.
62 JsonPrefStore(
63 const base::FilePath& pref_filename,
64 const base::FilePath& pref_alternate_filename,
65 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
66 scoped_ptr<PrefFilter> pref_filter);
68 // PrefStore overrides:
69 virtual bool GetValue(const std::string& key,
70 const base::Value** result) const OVERRIDE;
71 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE;
72 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE;
73 virtual bool HasObservers() const OVERRIDE;
74 virtual bool IsInitializationComplete() const OVERRIDE;
76 // PersistentPrefStore overrides:
77 virtual bool GetMutableValue(const std::string& key,
78 base::Value** result) OVERRIDE;
79 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE;
80 virtual void SetValueSilently(const std::string& key,
81 base::Value* value) OVERRIDE;
82 virtual void RemoveValue(const std::string& key) OVERRIDE;
83 virtual bool ReadOnly() const OVERRIDE;
84 virtual PrefReadError GetReadError() const OVERRIDE;
85 // Note this method may be asynchronous if this instance has a |pref_filter_|
86 // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE.
87 // See details in pref_filter.h.
88 virtual PrefReadError ReadPrefs() OVERRIDE;
89 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE;
90 virtual void CommitPendingWrite() OVERRIDE;
91 virtual void ReportValueChanged(const std::string& key) OVERRIDE;
93 // Just like RemoveValue(), but doesn't notify observers. Used when doing some
94 // cleanup that shouldn't otherwise alert observers.
95 void RemoveValueSilently(const std::string& key);
97 // Registers |on_next_successful_write| to be called once, on the next
98 // successful write event of |writer_|.
99 void RegisterOnNextSuccessfulWriteCallback(
100 const base::Closure& on_next_successful_write);
102 private:
103 virtual ~JsonPrefStore();
105 // This method is called after the JSON file has been read. It then hands
106 // |value| (or an empty dictionary in some read error cases) to the
107 // |pref_filter| if one is set. It also gives a callback pointing at
108 // FinalizeFileRead() to that |pref_filter_| which is then responsible for
109 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead()
110 // is invoked directly.
111 void OnFileRead(scoped_ptr<ReadResult> read_result);
113 // ImportantFileWriter::DataSerializer overrides:
114 virtual bool SerializeData(std::string* output) OVERRIDE;
116 // This method is called after the JSON file has been read and the result has
117 // potentially been intercepted and modified by |pref_filter_|.
118 // |initialization_successful| is pre-determined by OnFileRead() and should
119 // be used when reporting OnInitializationCompleted().
120 // |schedule_write| indicates whether a write should be immediately scheduled
121 // (typically because the |pref_filter_| has already altered the |prefs|) --
122 // this will be ignored if this store is read-only.
123 void FinalizeFileRead(bool initialization_successful,
124 scoped_ptr<base::DictionaryValue> prefs,
125 bool schedule_write);
127 const base::FilePath path_;
128 const base::FilePath alternate_path_;
129 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
131 scoped_ptr<base::DictionaryValue> prefs_;
133 bool read_only_;
135 // Helper for safely writing pref data.
136 base::ImportantFileWriter writer_;
138 scoped_ptr<PrefFilter> pref_filter_;
139 ObserverList<PrefStore::Observer, true> observers_;
141 scoped_ptr<ReadErrorDelegate> error_delegate_;
143 bool initialized_;
144 bool filtering_in_progress_;
145 PrefReadError read_error_;
147 std::set<std::string> keys_need_empty_value_;
149 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore);
152 #endif // BASE_PREFS_JSON_PREF_STORE_H_