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 #include "base/prefs/json_pref_store.h"
10 #include "base/callback.h"
11 #include "base/file_util.h"
12 #include "base/json/json_file_value_serializer.h"
13 #include "base/json/json_string_value_serializer.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/sequenced_task_runner.h"
17 #include "base/threading/sequenced_worker_pool.h"
18 #include "base/values.h"
22 // Some extensions we'll tack on to copies of the Preferences files.
23 const base::FilePath::CharType
* kBadExtension
= FILE_PATH_LITERAL("bad");
25 // Differentiates file loading between origin thread and passed
27 class FileThreadDeserializer
28 : public base::RefCountedThreadSafe
<FileThreadDeserializer
> {
30 FileThreadDeserializer(JsonPrefStore
* delegate
,
31 base::SequencedTaskRunner
* sequenced_task_runner
)
33 error_(PersistentPrefStore::PREF_READ_ERROR_NONE
),
35 sequenced_task_runner_(sequenced_task_runner
),
36 origin_loop_proxy_(base::MessageLoopProxy::current()) {
39 void Start(const base::FilePath
& path
) {
40 DCHECK(origin_loop_proxy_
->BelongsToCurrentThread());
41 sequenced_task_runner_
->PostTask(
43 base::Bind(&FileThreadDeserializer::ReadFileAndReport
,
47 // Deserializes JSON on the sequenced task runner.
48 void ReadFileAndReport(const base::FilePath
& path
) {
49 DCHECK(sequenced_task_runner_
->RunsTasksOnCurrentThread());
51 value_
.reset(DoReading(path
, &error_
, &no_dir_
));
53 origin_loop_proxy_
->PostTask(
55 base::Bind(&FileThreadDeserializer::ReportOnOriginThread
, this));
58 // Reports deserialization result on the origin thread.
59 void ReportOnOriginThread() {
60 DCHECK(origin_loop_proxy_
->BelongsToCurrentThread());
61 delegate_
->OnFileRead(value_
.release(), error_
, no_dir_
);
64 static base::Value
* DoReading(const base::FilePath
& path
,
65 PersistentPrefStore::PrefReadError
* error
,
68 std::string error_msg
;
69 JSONFileValueSerializer
serializer(path
);
70 base::Value
* value
= serializer
.Deserialize(&error_code
, &error_msg
);
71 HandleErrors(value
, path
, error_code
, error_msg
, error
);
72 *no_dir
= !base::PathExists(path
.DirName());
76 static void HandleErrors(const base::Value
* value
,
77 const base::FilePath
& path
,
79 const std::string
& error_msg
,
80 PersistentPrefStore::PrefReadError
* error
);
83 friend class base::RefCountedThreadSafe
<FileThreadDeserializer
>;
84 ~FileThreadDeserializer() {}
87 PersistentPrefStore::PrefReadError error_
;
88 scoped_ptr
<base::Value
> value_
;
89 const scoped_refptr
<JsonPrefStore
> delegate_
;
90 const scoped_refptr
<base::SequencedTaskRunner
> sequenced_task_runner_
;
91 const scoped_refptr
<base::MessageLoopProxy
> origin_loop_proxy_
;
95 void FileThreadDeserializer::HandleErrors(
96 const base::Value
* value
,
97 const base::FilePath
& path
,
99 const std::string
& error_msg
,
100 PersistentPrefStore::PrefReadError
* error
) {
101 *error
= PersistentPrefStore::PREF_READ_ERROR_NONE
;
103 DVLOG(1) << "Error while loading JSON file: " << error_msg
104 << ", file: " << path
.value();
105 switch (error_code
) {
106 case JSONFileValueSerializer::JSON_ACCESS_DENIED
:
107 *error
= PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED
;
109 case JSONFileValueSerializer::JSON_CANNOT_READ_FILE
:
110 *error
= PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER
;
112 case JSONFileValueSerializer::JSON_FILE_LOCKED
:
113 *error
= PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED
;
115 case JSONFileValueSerializer::JSON_NO_SUCH_FILE
:
116 *error
= PersistentPrefStore::PREF_READ_ERROR_NO_FILE
;
119 *error
= PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE
;
120 // JSON errors indicate file corruption of some sort.
121 // Since the file is corrupt, move it to the side and continue with
122 // empty preferences. This will result in them losing their settings.
123 // We keep the old file for possible support and debugging assistance
124 // as well as to detect if they're seeing these errors repeatedly.
125 // TODO(erikkay) Instead, use the last known good file.
126 base::FilePath bad
= path
.ReplaceExtension(kBadExtension
);
128 // If they've ever had a parse error before, put them in another bucket.
129 // TODO(erikkay) if we keep this error checking for very long, we may
130 // want to differentiate between recent and long ago errors.
131 if (base::PathExists(bad
))
132 *error
= PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT
;
133 base::Move(path
, bad
);
136 } else if (!value
->IsType(base::Value::TYPE_DICTIONARY
)) {
137 *error
= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE
;
143 scoped_refptr
<base::SequencedTaskRunner
> JsonPrefStore::GetTaskRunnerForFile(
144 const base::FilePath
& filename
,
145 base::SequencedWorkerPool
* worker_pool
) {
146 std::string
token("json_pref_store-");
147 token
.append(filename
.AsUTF8Unsafe());
148 return worker_pool
->GetSequencedTaskRunnerWithShutdownBehavior(
149 worker_pool
->GetNamedSequenceToken(token
),
150 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
153 JsonPrefStore::JsonPrefStore(const base::FilePath
& filename
,
154 base::SequencedTaskRunner
* sequenced_task_runner
)
156 sequenced_task_runner_(sequenced_task_runner
),
157 prefs_(new base::DictionaryValue()),
159 writer_(filename
, sequenced_task_runner
),
161 read_error_(PREF_READ_ERROR_OTHER
) {}
163 bool JsonPrefStore::GetValue(const std::string
& key
,
164 const base::Value
** result
) const {
165 base::Value
* tmp
= NULL
;
166 if (!prefs_
->Get(key
, &tmp
))
174 void JsonPrefStore::AddObserver(PrefStore::Observer
* observer
) {
175 observers_
.AddObserver(observer
);
178 void JsonPrefStore::RemoveObserver(PrefStore::Observer
* observer
) {
179 observers_
.RemoveObserver(observer
);
182 bool JsonPrefStore::HasObservers() const {
183 return observers_
.might_have_observers();
186 bool JsonPrefStore::IsInitializationComplete() const {
190 bool JsonPrefStore::GetMutableValue(const std::string
& key
,
191 base::Value
** result
) {
192 return prefs_
->Get(key
, result
);
195 void JsonPrefStore::SetValue(const std::string
& key
, base::Value
* value
) {
197 scoped_ptr
<base::Value
> new_value(value
);
198 base::Value
* old_value
= NULL
;
199 prefs_
->Get(key
, &old_value
);
200 if (!old_value
|| !value
->Equals(old_value
)) {
201 prefs_
->Set(key
, new_value
.release());
202 ReportValueChanged(key
);
206 void JsonPrefStore::SetValueSilently(const std::string
& key
,
207 base::Value
* value
) {
209 scoped_ptr
<base::Value
> new_value(value
);
210 base::Value
* old_value
= NULL
;
211 prefs_
->Get(key
, &old_value
);
212 if (!old_value
|| !value
->Equals(old_value
)) {
213 prefs_
->Set(key
, new_value
.release());
215 writer_
.ScheduleWrite(this);
219 void JsonPrefStore::RemoveValue(const std::string
& key
) {
220 if (prefs_
->RemovePath(key
, NULL
))
221 ReportValueChanged(key
);
224 bool JsonPrefStore::ReadOnly() const {
228 PersistentPrefStore::PrefReadError
JsonPrefStore::GetReadError() const {
232 PersistentPrefStore::PrefReadError
JsonPrefStore::ReadPrefs() {
234 OnFileRead(NULL
, PREF_READ_ERROR_FILE_NOT_SPECIFIED
, false);
235 return PREF_READ_ERROR_FILE_NOT_SPECIFIED
;
241 FileThreadDeserializer::DoReading(path_
, &error
, &no_dir
);
242 OnFileRead(value
, error
, no_dir
);
246 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate
*error_delegate
) {
247 initialized_
= false;
248 error_delegate_
.reset(error_delegate
);
250 OnFileRead(NULL
, PREF_READ_ERROR_FILE_NOT_SPECIFIED
, false);
254 // Start async reading of the preferences file. It will delete itself
256 scoped_refptr
<FileThreadDeserializer
> deserializer(
257 new FileThreadDeserializer(this, sequenced_task_runner_
.get()));
258 deserializer
->Start(path_
);
261 void JsonPrefStore::CommitPendingWrite() {
262 if (writer_
.HasPendingWrite() && !read_only_
)
263 writer_
.DoScheduledWrite();
266 void JsonPrefStore::ReportValueChanged(const std::string
& key
) {
267 FOR_EACH_OBSERVER(PrefStore::Observer
, observers_
, OnPrefValueChanged(key
));
269 writer_
.ScheduleWrite(this);
272 void JsonPrefStore::OnFileRead(base::Value
* value_owned
,
273 PersistentPrefStore::PrefReadError error
,
275 scoped_ptr
<base::Value
> value(value_owned
);
279 FOR_EACH_OBSERVER(PrefStore::Observer
,
281 OnInitializationCompleted(false));
288 case PREF_READ_ERROR_ACCESS_DENIED
:
289 case PREF_READ_ERROR_FILE_OTHER
:
290 case PREF_READ_ERROR_FILE_LOCKED
:
291 case PREF_READ_ERROR_JSON_TYPE
:
292 case PREF_READ_ERROR_FILE_NOT_SPECIFIED
:
295 case PREF_READ_ERROR_NONE
:
297 prefs_
.reset(static_cast<base::DictionaryValue
*>(value
.release()));
299 case PREF_READ_ERROR_NO_FILE
:
300 // If the file just doesn't exist, maybe this is first run. In any case
301 // there's no harm in writing out default prefs in this case.
303 case PREF_READ_ERROR_JSON_PARSE
:
304 case PREF_READ_ERROR_JSON_REPEAT
:
307 NOTREACHED() << "Unknown error: " << error
;
310 if (error_delegate_
.get() && error
!= PREF_READ_ERROR_NONE
)
311 error_delegate_
->OnError(error
);
313 FOR_EACH_OBSERVER(PrefStore::Observer
,
315 OnInitializationCompleted(true));
318 JsonPrefStore::~JsonPrefStore() {
319 CommitPendingWrite();
322 bool JsonPrefStore::SerializeData(std::string
* output
) {
323 JSONStringValueSerializer
serializer(output
);
324 serializer
.set_pretty_print(true);
325 return serializer
.Serialize(*prefs_
);