OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/prefs/json_pref_store.h" | 5 #include "base/prefs/json_pref_store.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/files/file_path.h" | |
12 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
13 #include "base/json/json_file_value_serializer.h" | 12 #include "base/json/json_file_value_serializer.h" |
14 #include "base/json/json_string_value_serializer.h" | 13 #include "base/json/json_string_value_serializer.h" |
15 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
16 #include "base/metrics/histogram.h" | |
17 #include "base/prefs/pref_filter.h" | 15 #include "base/prefs/pref_filter.h" |
18 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
19 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
20 #include "base/task_runner_util.h" | 18 #include "base/task_runner_util.h" |
21 #include "base/threading/sequenced_worker_pool.h" | 19 #include "base/threading/sequenced_worker_pool.h" |
22 #include "base/values.h" | 20 #include "base/values.h" |
23 | 21 |
24 // Result returned from internal read tasks. | 22 // Result returned from internal read tasks. |
25 struct JsonPrefStore::ReadResult { | 23 struct JsonPrefStore::ReadResult { |
26 public: | 24 public: |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 } | 90 } |
93 | 91 |
94 scoped_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( | 92 scoped_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( |
95 const base::FilePath& path, | 93 const base::FilePath& path, |
96 const base::FilePath& alternate_path) { | 94 const base::FilePath& alternate_path) { |
97 if (!base::PathExists(path) && !alternate_path.empty() && | 95 if (!base::PathExists(path) && !alternate_path.empty() && |
98 base::PathExists(alternate_path)) { | 96 base::PathExists(alternate_path)) { |
99 base::Move(alternate_path, path); | 97 base::Move(alternate_path, path); |
100 } | 98 } |
101 | 99 |
| 100 std::string histogram_suffix; |
| 101 base::ReplaceChars(path.BaseName().MaybeAsASCII(), " ", "_", |
| 102 &histogram_suffix); |
| 103 |
102 int error_code; | 104 int error_code; |
103 std::string error_msg; | 105 std::string error_msg; |
104 scoped_ptr<JsonPrefStore::ReadResult> read_result( | 106 scoped_ptr<JsonPrefStore::ReadResult> read_result( |
105 new JsonPrefStore::ReadResult); | 107 new JsonPrefStore::ReadResult); |
106 JSONFileValueSerializer serializer(path); | 108 JSONFileValueSerializer serializer(path, histogram_suffix); |
107 read_result->value.reset(serializer.Deserialize(&error_code, &error_msg)); | 109 read_result->value.reset(serializer.Deserialize(&error_code, &error_msg)); |
108 read_result->error = | 110 read_result->error = |
109 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); | 111 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); |
110 read_result->no_dir = !base::PathExists(path.DirName()); | 112 read_result->no_dir = !base::PathExists(path.DirName()); |
111 return read_result.Pass(); | 113 return read_result.Pass(); |
112 } | 114 } |
113 | 115 |
114 } // namespace | 116 } // namespace |
115 | 117 |
116 // static | 118 // static |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 } | 374 } |
373 | 375 |
374 bool JsonPrefStore::SerializeData(std::string* output) { | 376 bool JsonPrefStore::SerializeData(std::string* output) { |
375 DCHECK(CalledOnValidThread()); | 377 DCHECK(CalledOnValidThread()); |
376 | 378 |
377 if (pref_filter_) | 379 if (pref_filter_) |
378 pref_filter_->FilterSerializeData(prefs_.get()); | 380 pref_filter_->FilterSerializeData(prefs_.get()); |
379 | 381 |
380 JSONStringValueSerializer serializer(output); | 382 JSONStringValueSerializer serializer(output); |
381 serializer.set_pretty_print(true); | 383 serializer.set_pretty_print(true); |
382 bool result = serializer.Serialize(*prefs_); | 384 return serializer.Serialize(*prefs_); |
383 | |
384 if (result) { | |
385 std::string spaceless_basename; | |
386 base::ReplaceChars(path_.BaseName().MaybeAsASCII(), " ", "_", | |
387 &spaceless_basename); | |
388 | |
389 // The histogram below is an expansion of the UMA_HISTOGRAM_COUNTS_10000 | |
390 // macro adapted to allow for a dynamically suffixed histogram name. | |
391 // Note: The factory creates and owns the histogram. | |
392 base::HistogramBase* histogram = | |
393 base::LinearHistogram::FactoryGet( | |
394 "Settings.JsonDataSizeKilobytes." + spaceless_basename, | |
395 1, | |
396 10000, | |
397 50, | |
398 base::HistogramBase::kUmaTargetedHistogramFlag); | |
399 histogram->Add(static_cast<int>(output->size()) / 1024); | |
400 } | |
401 | |
402 return result; | |
403 } | 385 } |
404 | 386 |
405 void JsonPrefStore::FinalizeFileRead(bool initialization_successful, | 387 void JsonPrefStore::FinalizeFileRead(bool initialization_successful, |
406 scoped_ptr<base::DictionaryValue> prefs, | 388 scoped_ptr<base::DictionaryValue> prefs, |
407 bool schedule_write) { | 389 bool schedule_write) { |
408 DCHECK(CalledOnValidThread()); | 390 DCHECK(CalledOnValidThread()); |
409 | 391 |
410 filtering_in_progress_ = false; | 392 filtering_in_progress_ = false; |
411 | 393 |
412 if (!initialization_successful) { | 394 if (!initialization_successful) { |
(...skipping 12 matching lines...) Expand all Loading... |
425 | 407 |
426 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 408 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
427 error_delegate_->OnError(read_error_); | 409 error_delegate_->OnError(read_error_); |
428 | 410 |
429 FOR_EACH_OBSERVER(PrefStore::Observer, | 411 FOR_EACH_OBSERVER(PrefStore::Observer, |
430 observers_, | 412 observers_, |
431 OnInitializationCompleted(true)); | 413 OnInitializationCompleted(true)); |
432 | 414 |
433 return; | 415 return; |
434 } | 416 } |
OLD | NEW |