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_
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"
27 class DictionaryValue
;
29 class SequencedTaskRunner
;
30 class SequencedWorkerPool
;
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
{
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.
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.
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 bool GetValue(const std::string
& key
,
70 const base::Value
** result
) const override
;
71 void AddObserver(PrefStore::Observer
* observer
) override
;
72 void RemoveObserver(PrefStore::Observer
* observer
) override
;
73 bool HasObservers() const override
;
74 bool IsInitializationComplete() const override
;
76 // PersistentPrefStore overrides:
77 bool GetMutableValue(const std::string
& key
, base::Value
** result
) override
;
78 void SetValue(const std::string
& key
, base::Value
* value
) override
;
79 void SetValueSilently(const std::string
& key
, base::Value
* value
) override
;
80 void RemoveValue(const std::string
& key
) override
;
81 bool ReadOnly() const override
;
82 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 PrefReadError
ReadPrefs() override
;
87 void ReadPrefsAsync(ReadErrorDelegate
* error_delegate
) override
;
88 void CommitPendingWrite() override
;
89 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
);
101 ~JsonPrefStore() override
;
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 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_
;
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_
;
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_