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/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/json/json_file_value_serializer.h"
14 #include "base/json/json_string_value_serializer.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/metrics/histogram.h"
17 #include "base/prefs/pref_filter.h"
18 #include "base/sequenced_task_runner.h"
19 #include "base/strings/string_util.h"
20 #include "base/task_runner_util.h"
21 #include "base/threading/sequenced_worker_pool.h"
22 #include "base/values.h"
24 // Result returned from internal read tasks.
25 struct JsonPrefStore::ReadResult
{
30 scoped_ptr
<base::Value
> value
;
35 DISALLOW_COPY_AND_ASSIGN(ReadResult
);
38 JsonPrefStore::ReadResult::ReadResult()
39 : error(PersistentPrefStore::PREF_READ_ERROR_NONE
), no_dir(false) {
42 JsonPrefStore::ReadResult::~ReadResult() {
47 // Some extensions we'll tack on to copies of the Preferences files.
48 const base::FilePath::CharType kBadExtension
[] = FILE_PATH_LITERAL("bad");
50 PersistentPrefStore::PrefReadError
HandleReadErrors(
51 const base::Value
* value
,
52 const base::FilePath
& path
,
54 const std::string
& error_msg
) {
56 DVLOG(1) << "Error while loading JSON file: " << error_msg
57 << ", file: " << path
.value();
59 case JSONFileValueDeserializer::JSON_ACCESS_DENIED
:
60 return PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED
;
62 case JSONFileValueDeserializer::JSON_CANNOT_READ_FILE
:
63 return PersistentPrefStore::PREF_READ_ERROR_FILE_OTHER
;
65 case JSONFileValueDeserializer::JSON_FILE_LOCKED
:
66 return PersistentPrefStore::PREF_READ_ERROR_FILE_LOCKED
;
68 case JSONFileValueDeserializer::JSON_NO_SUCH_FILE
:
69 return PersistentPrefStore::PREF_READ_ERROR_NO_FILE
;
72 // JSON errors indicate file corruption of some sort.
73 // Since the file is corrupt, move it to the side and continue with
74 // empty preferences. This will result in them losing their settings.
75 // We keep the old file for possible support and debugging assistance
76 // as well as to detect if they're seeing these errors repeatedly.
77 // TODO(erikkay) Instead, use the last known good file.
78 base::FilePath bad
= path
.ReplaceExtension(kBadExtension
);
80 // If they've ever had a parse error before, put them in another bucket.
81 // TODO(erikkay) if we keep this error checking for very long, we may
82 // want to differentiate between recent and long ago errors.
83 bool bad_existed
= base::PathExists(bad
);
84 base::Move(path
, bad
);
85 return bad_existed
? PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT
86 : PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE
;
88 } else if (!value
->IsType(base::Value::TYPE_DICTIONARY
)) {
89 return PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE
;
91 return PersistentPrefStore::PREF_READ_ERROR_NONE
;
94 // Records a sample for |size| in the Settings.JsonDataReadSizeKilobytes
95 // histogram suffixed with the base name of the JSON file under |path|.
96 void RecordJsonDataSizeHistogram(const base::FilePath
& path
, size_t size
) {
97 std::string spaceless_basename
;
98 base::ReplaceChars(path
.BaseName().MaybeAsASCII(), " ", "_",
101 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS
102 // macro adapted to allow for a dynamically suffixed histogram name.
103 // Note: The factory creates and owns the histogram.
104 base::HistogramBase
* histogram
= base::Histogram::FactoryGet(
105 "Settings.JsonDataReadSizeKilobytes." + spaceless_basename
, 1, 10000, 50,
106 base::HistogramBase::kUmaTargetedHistogramFlag
);
107 histogram
->Add(static_cast<int>(size
) / 1024);
110 scoped_ptr
<JsonPrefStore::ReadResult
> ReadPrefsFromDisk(
111 const base::FilePath
& path
,
112 const base::FilePath
& alternate_path
) {
113 if (!base::PathExists(path
) && !alternate_path
.empty() &&
114 base::PathExists(alternate_path
)) {
115 base::Move(alternate_path
, path
);
119 std::string error_msg
;
120 scoped_ptr
<JsonPrefStore::ReadResult
> read_result(
121 new JsonPrefStore::ReadResult
);
122 JSONFileValueDeserializer
deserializer(path
);
123 read_result
->value
.reset(deserializer
.Deserialize(&error_code
, &error_msg
));
125 HandleReadErrors(read_result
->value
.get(), path
, error_code
, error_msg
);
126 read_result
->no_dir
= !base::PathExists(path
.DirName());
128 if (read_result
->error
== PersistentPrefStore::PREF_READ_ERROR_NONE
)
129 RecordJsonDataSizeHistogram(path
, deserializer
.get_last_read_size());
131 return read_result
.Pass();
137 scoped_refptr
<base::SequencedTaskRunner
> JsonPrefStore::GetTaskRunnerForFile(
138 const base::FilePath
& filename
,
139 base::SequencedWorkerPool
* worker_pool
) {
140 std::string
token("json_pref_store-");
141 token
.append(filename
.AsUTF8Unsafe());
142 return worker_pool
->GetSequencedTaskRunnerWithShutdownBehavior(
143 worker_pool
->GetNamedSequenceToken(token
),
144 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
147 JsonPrefStore::JsonPrefStore(
148 const base::FilePath
& filename
,
149 const scoped_refptr
<base::SequencedTaskRunner
>& sequenced_task_runner
,
150 scoped_ptr
<PrefFilter
> pref_filter
)
152 sequenced_task_runner_(sequenced_task_runner
),
153 prefs_(new base::DictionaryValue()),
155 writer_(filename
, sequenced_task_runner
),
156 pref_filter_(pref_filter
.Pass()),
158 filtering_in_progress_(false),
159 read_error_(PREF_READ_ERROR_NONE
) {
160 DCHECK(!path_
.empty());
163 JsonPrefStore::JsonPrefStore(
164 const base::FilePath
& filename
,
165 const base::FilePath
& alternate_filename
,
166 const scoped_refptr
<base::SequencedTaskRunner
>& sequenced_task_runner
,
167 scoped_ptr
<PrefFilter
> pref_filter
)
169 alternate_path_(alternate_filename
),
170 sequenced_task_runner_(sequenced_task_runner
),
171 prefs_(new base::DictionaryValue()),
173 writer_(filename
, sequenced_task_runner
),
174 pref_filter_(pref_filter
.Pass()),
176 filtering_in_progress_(false),
177 read_error_(PREF_READ_ERROR_NONE
) {
178 DCHECK(!path_
.empty());
181 bool JsonPrefStore::GetValue(const std::string
& key
,
182 const base::Value
** result
) const {
183 DCHECK(CalledOnValidThread());
185 base::Value
* tmp
= NULL
;
186 if (!prefs_
->Get(key
, &tmp
))
194 void JsonPrefStore::AddObserver(PrefStore::Observer
* observer
) {
195 DCHECK(CalledOnValidThread());
197 observers_
.AddObserver(observer
);
200 void JsonPrefStore::RemoveObserver(PrefStore::Observer
* observer
) {
201 DCHECK(CalledOnValidThread());
203 observers_
.RemoveObserver(observer
);
206 bool JsonPrefStore::HasObservers() const {
207 DCHECK(CalledOnValidThread());
209 return observers_
.might_have_observers();
212 bool JsonPrefStore::IsInitializationComplete() const {
213 DCHECK(CalledOnValidThread());
218 bool JsonPrefStore::GetMutableValue(const std::string
& key
,
219 base::Value
** result
) {
220 DCHECK(CalledOnValidThread());
222 return prefs_
->Get(key
, result
);
225 void JsonPrefStore::SetValue(const std::string
& key
, base::Value
* value
) {
226 DCHECK(CalledOnValidThread());
229 scoped_ptr
<base::Value
> new_value(value
);
230 base::Value
* old_value
= NULL
;
231 prefs_
->Get(key
, &old_value
);
232 if (!old_value
|| !value
->Equals(old_value
)) {
233 prefs_
->Set(key
, new_value
.release());
234 ReportValueChanged(key
);
238 void JsonPrefStore::SetValueSilently(const std::string
& key
,
239 base::Value
* value
) {
240 DCHECK(CalledOnValidThread());
243 scoped_ptr
<base::Value
> new_value(value
);
244 base::Value
* old_value
= NULL
;
245 prefs_
->Get(key
, &old_value
);
246 if (!old_value
|| !value
->Equals(old_value
)) {
247 prefs_
->Set(key
, new_value
.release());
249 writer_
.ScheduleWrite(this);
253 void JsonPrefStore::RemoveValue(const std::string
& key
) {
254 DCHECK(CalledOnValidThread());
256 if (prefs_
->RemovePath(key
, NULL
))
257 ReportValueChanged(key
);
260 void JsonPrefStore::RemoveValueSilently(const std::string
& key
) {
261 DCHECK(CalledOnValidThread());
263 prefs_
->RemovePath(key
, NULL
);
265 writer_
.ScheduleWrite(this);
268 bool JsonPrefStore::ReadOnly() const {
269 DCHECK(CalledOnValidThread());
274 PersistentPrefStore::PrefReadError
JsonPrefStore::GetReadError() const {
275 DCHECK(CalledOnValidThread());
280 PersistentPrefStore::PrefReadError
JsonPrefStore::ReadPrefs() {
281 DCHECK(CalledOnValidThread());
283 OnFileRead(ReadPrefsFromDisk(path_
, alternate_path_
));
284 return filtering_in_progress_
? PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE
288 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate
* error_delegate
) {
289 DCHECK(CalledOnValidThread());
291 initialized_
= false;
292 error_delegate_
.reset(error_delegate
);
294 // Weakly binds the read task so that it doesn't kick in during shutdown.
295 base::PostTaskAndReplyWithResult(
296 sequenced_task_runner_
.get(),
298 base::Bind(&ReadPrefsFromDisk
, path_
, alternate_path_
),
299 base::Bind(&JsonPrefStore::OnFileRead
, AsWeakPtr()));
302 void JsonPrefStore::CommitPendingWrite() {
303 DCHECK(CalledOnValidThread());
305 if (writer_
.HasPendingWrite() && !read_only_
)
306 writer_
.DoScheduledWrite();
309 void JsonPrefStore::ReportValueChanged(const std::string
& key
) {
310 DCHECK(CalledOnValidThread());
313 pref_filter_
->FilterUpdate(key
);
315 FOR_EACH_OBSERVER(PrefStore::Observer
, observers_
, OnPrefValueChanged(key
));
318 writer_
.ScheduleWrite(this);
321 void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback(
322 const base::Closure
& on_next_successful_write
) {
323 DCHECK(CalledOnValidThread());
325 writer_
.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write
);
328 void JsonPrefStore::OnFileRead(scoped_ptr
<ReadResult
> read_result
) {
329 DCHECK(CalledOnValidThread());
333 scoped_ptr
<base::DictionaryValue
> unfiltered_prefs(new base::DictionaryValue
);
335 read_error_
= read_result
->error
;
337 bool initialization_successful
= !read_result
->no_dir
;
339 if (initialization_successful
) {
340 switch (read_error_
) {
341 case PREF_READ_ERROR_ACCESS_DENIED
:
342 case PREF_READ_ERROR_FILE_OTHER
:
343 case PREF_READ_ERROR_FILE_LOCKED
:
344 case PREF_READ_ERROR_JSON_TYPE
:
345 case PREF_READ_ERROR_FILE_NOT_SPECIFIED
:
348 case PREF_READ_ERROR_NONE
:
349 DCHECK(read_result
->value
.get());
350 unfiltered_prefs
.reset(
351 static_cast<base::DictionaryValue
*>(read_result
->value
.release()));
353 case PREF_READ_ERROR_NO_FILE
:
354 // If the file just doesn't exist, maybe this is first run. In any case
355 // there's no harm in writing out default prefs in this case.
357 case PREF_READ_ERROR_JSON_PARSE
:
358 case PREF_READ_ERROR_JSON_REPEAT
:
360 case PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE
:
361 // This is a special error code to be returned by ReadPrefs when it
362 // can't complete synchronously, it should never be returned by the read
366 case PREF_READ_ERROR_LEVELDB_IO
:
367 case PREF_READ_ERROR_LEVELDB_CORRUPTION_READ_ONLY
:
368 case PREF_READ_ERROR_LEVELDB_CORRUPTION
:
369 // These are specific to LevelDBPrefStore.
371 case PREF_READ_ERROR_MAX_ENUM
:
378 filtering_in_progress_
= true;
379 const PrefFilter::PostFilterOnLoadCallback
post_filter_on_load_callback(
381 &JsonPrefStore::FinalizeFileRead
, AsWeakPtr(),
382 initialization_successful
));
383 pref_filter_
->FilterOnLoad(post_filter_on_load_callback
,
384 unfiltered_prefs
.Pass());
386 FinalizeFileRead(initialization_successful
, unfiltered_prefs
.Pass(), false);
390 JsonPrefStore::~JsonPrefStore() {
391 CommitPendingWrite();
394 bool JsonPrefStore::SerializeData(std::string
* output
) {
395 DCHECK(CalledOnValidThread());
398 pref_filter_
->FilterSerializeData(prefs_
.get());
400 JSONStringValueSerializer
serializer(output
);
401 // Not pretty-printing prefs shrinks pref file size by ~30%. To obtain
402 // readable prefs for debugging purposes, you can dump your prefs into any
403 // command-line or online JSON pretty printing tool.
404 serializer
.set_pretty_print(false);
405 return serializer
.Serialize(*prefs_
);
408 void JsonPrefStore::FinalizeFileRead(bool initialization_successful
,
409 scoped_ptr
<base::DictionaryValue
> prefs
,
410 bool schedule_write
) {
411 DCHECK(CalledOnValidThread());
413 filtering_in_progress_
= false;
415 if (!initialization_successful
) {
416 FOR_EACH_OBSERVER(PrefStore::Observer
,
418 OnInitializationCompleted(false));
422 prefs_
= prefs
.Pass();
426 if (schedule_write
&& !read_only_
)
427 writer_
.ScheduleWrite(this);
429 if (error_delegate_
&& read_error_
!= PREF_READ_ERROR_NONE
)
430 error_delegate_
->OnError(read_error_
);
432 FOR_EACH_OBSERVER(PrefStore::Observer
,
434 OnInitializationCompleted(true));