| 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" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 base::Move(path, bad); | 84 base::Move(path, bad); |
| 85 return bad_existed ? PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT | 85 return bad_existed ? PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT |
| 86 : PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE; | 86 : PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE; |
| 87 } | 87 } |
| 88 } else if (!value->IsType(base::Value::TYPE_DICTIONARY)) { | 88 } else if (!value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 89 return PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; | 89 return PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; |
| 90 } | 90 } |
| 91 return PersistentPrefStore::PREF_READ_ERROR_NONE; | 91 return PersistentPrefStore::PREF_READ_ERROR_NONE; |
| 92 } | 92 } |
| 93 | 93 |
| 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(), " ", "_", |
| 99 &spaceless_basename); |
| 100 |
| 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); |
| 108 } |
| 109 |
| 94 scoped_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( | 110 scoped_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( |
| 95 const base::FilePath& path, | 111 const base::FilePath& path, |
| 96 const base::FilePath& alternate_path) { | 112 const base::FilePath& alternate_path) { |
| 97 if (!base::PathExists(path) && !alternate_path.empty() && | 113 if (!base::PathExists(path) && !alternate_path.empty() && |
| 98 base::PathExists(alternate_path)) { | 114 base::PathExists(alternate_path)) { |
| 99 base::Move(alternate_path, path); | 115 base::Move(alternate_path, path); |
| 100 } | 116 } |
| 101 | 117 |
| 102 int error_code; | 118 int error_code; |
| 103 std::string error_msg; | 119 std::string error_msg; |
| 104 scoped_ptr<JsonPrefStore::ReadResult> read_result( | 120 scoped_ptr<JsonPrefStore::ReadResult> read_result( |
| 105 new JsonPrefStore::ReadResult); | 121 new JsonPrefStore::ReadResult); |
| 106 JSONFileValueSerializer serializer(path); | 122 JSONFileValueSerializer serializer(path); |
| 107 read_result->value.reset(serializer.Deserialize(&error_code, &error_msg)); | 123 read_result->value.reset(serializer.Deserialize(&error_code, &error_msg)); |
| 108 read_result->error = | 124 read_result->error = |
| 109 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); | 125 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); |
| 110 read_result->no_dir = !base::PathExists(path.DirName()); | 126 read_result->no_dir = !base::PathExists(path.DirName()); |
| 127 |
| 128 if (read_result->error == PersistentPrefStore::PREF_READ_ERROR_NONE) |
| 129 RecordJsonDataSizeHistogram(path, serializer.get_last_read_size()); |
| 130 |
| 111 return read_result.Pass(); | 131 return read_result.Pass(); |
| 112 } | 132 } |
| 113 | 133 |
| 114 } // namespace | 134 } // namespace |
| 115 | 135 |
| 116 // static | 136 // static |
| 117 scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( | 137 scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( |
| 118 const base::FilePath& filename, | 138 const base::FilePath& filename, |
| 119 base::SequencedWorkerPool* worker_pool) { | 139 base::SequencedWorkerPool* worker_pool) { |
| 120 std::string token("json_pref_store-"); | 140 std::string token("json_pref_store-"); |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 } | 392 } |
| 373 | 393 |
| 374 bool JsonPrefStore::SerializeData(std::string* output) { | 394 bool JsonPrefStore::SerializeData(std::string* output) { |
| 375 DCHECK(CalledOnValidThread()); | 395 DCHECK(CalledOnValidThread()); |
| 376 | 396 |
| 377 if (pref_filter_) | 397 if (pref_filter_) |
| 378 pref_filter_->FilterSerializeData(prefs_.get()); | 398 pref_filter_->FilterSerializeData(prefs_.get()); |
| 379 | 399 |
| 380 JSONStringValueSerializer serializer(output); | 400 JSONStringValueSerializer serializer(output); |
| 381 serializer.set_pretty_print(true); | 401 serializer.set_pretty_print(true); |
| 382 bool result = serializer.Serialize(*prefs_); | 402 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 } | 403 } |
| 404 | 404 |
| 405 void JsonPrefStore::FinalizeFileRead(bool initialization_successful, | 405 void JsonPrefStore::FinalizeFileRead(bool initialization_successful, |
| 406 scoped_ptr<base::DictionaryValue> prefs, | 406 scoped_ptr<base::DictionaryValue> prefs, |
| 407 bool schedule_write) { | 407 bool schedule_write) { |
| 408 DCHECK(CalledOnValidThread()); | 408 DCHECK(CalledOnValidThread()); |
| 409 | 409 |
| 410 filtering_in_progress_ = false; | 410 filtering_in_progress_ = false; |
| 411 | 411 |
| 412 if (!initialization_successful) { | 412 if (!initialization_successful) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 425 | 425 |
| 426 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 426 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
| 427 error_delegate_->OnError(read_error_); | 427 error_delegate_->OnError(read_error_); |
| 428 | 428 |
| 429 FOR_EACH_OBSERVER(PrefStore::Observer, | 429 FOR_EACH_OBSERVER(PrefStore::Observer, |
| 430 observers_, | 430 observers_, |
| 431 OnInitializationCompleted(true)); | 431 OnInitializationCompleted(true)); |
| 432 | 432 |
| 433 return; | 433 return; |
| 434 } | 434 } |
| OLD | NEW |