| 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 "components/prefs/json_pref_store.h" | 5 #include "components/prefs/json_pref_store.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 104 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
| 105 // macro adapted to allow for a dynamically suffixed histogram name. | 105 // macro adapted to allow for a dynamically suffixed histogram name. |
| 106 // Note: The factory creates and owns the histogram. | 106 // Note: The factory creates and owns the histogram. |
| 107 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 107 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| 108 "Settings.JsonDataReadSizeKilobytes." + spaceless_basename, 1, 10000, 50, | 108 "Settings.JsonDataReadSizeKilobytes." + spaceless_basename, 1, 10000, 50, |
| 109 base::HistogramBase::kUmaTargetedHistogramFlag); | 109 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 110 histogram->Add(static_cast<int>(size) / 1024); | 110 histogram->Add(static_cast<int>(size) / 1024); |
| 111 } | 111 } |
| 112 | 112 |
| 113 std::unique_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( | 113 std::unique_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( |
| 114 const base::FilePath& path, | 114 const base::FilePath& path) { |
| 115 const base::FilePath& alternate_path) { | |
| 116 if (!base::PathExists(path) && !alternate_path.empty() && | |
| 117 base::PathExists(alternate_path)) { | |
| 118 base::Move(alternate_path, path); | |
| 119 } | |
| 120 | |
| 121 int error_code; | 115 int error_code; |
| 122 std::string error_msg; | 116 std::string error_msg; |
| 123 std::unique_ptr<JsonPrefStore::ReadResult> read_result( | 117 std::unique_ptr<JsonPrefStore::ReadResult> read_result( |
| 124 new JsonPrefStore::ReadResult); | 118 new JsonPrefStore::ReadResult); |
| 125 JSONFileValueDeserializer deserializer(path); | 119 JSONFileValueDeserializer deserializer(path); |
| 126 read_result->value = deserializer.Deserialize(&error_code, &error_msg); | 120 read_result->value = deserializer.Deserialize(&error_code, &error_msg); |
| 127 read_result->error = | 121 read_result->error = |
| 128 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); | 122 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); |
| 129 read_result->no_dir = !base::PathExists(path.DirName()); | 123 read_result->no_dir = !base::PathExists(path.DirName()); |
| 130 | 124 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 144 token.append(filename.AsUTF8Unsafe()); | 138 token.append(filename.AsUTF8Unsafe()); |
| 145 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | 139 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 146 worker_pool->GetNamedSequenceToken(token), | 140 worker_pool->GetNamedSequenceToken(token), |
| 147 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | 141 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 148 } | 142 } |
| 149 | 143 |
| 150 JsonPrefStore::JsonPrefStore( | 144 JsonPrefStore::JsonPrefStore( |
| 151 const base::FilePath& pref_filename, | 145 const base::FilePath& pref_filename, |
| 152 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 146 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
| 153 std::unique_ptr<PrefFilter> pref_filter) | 147 std::unique_ptr<PrefFilter> pref_filter) |
| 154 : JsonPrefStore(pref_filename, | |
| 155 base::FilePath(), | |
| 156 sequenced_task_runner, | |
| 157 std::move(pref_filter)) {} | |
| 158 | |
| 159 JsonPrefStore::JsonPrefStore( | |
| 160 const base::FilePath& pref_filename, | |
| 161 const base::FilePath& pref_alternate_filename, | |
| 162 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | |
| 163 std::unique_ptr<PrefFilter> pref_filter) | |
| 164 : path_(pref_filename), | 148 : path_(pref_filename), |
| 165 alternate_path_(pref_alternate_filename), | |
| 166 sequenced_task_runner_(sequenced_task_runner), | 149 sequenced_task_runner_(sequenced_task_runner), |
| 167 prefs_(new base::DictionaryValue()), | 150 prefs_(new base::DictionaryValue()), |
| 168 read_only_(false), | 151 read_only_(false), |
| 169 writer_(pref_filename, sequenced_task_runner), | 152 writer_(pref_filename, sequenced_task_runner), |
| 170 pref_filter_(std::move(pref_filter)), | 153 pref_filter_(std::move(pref_filter)), |
| 171 initialized_(false), | 154 initialized_(false), |
| 172 filtering_in_progress_(false), | 155 filtering_in_progress_(false), |
| 173 pending_lossy_write_(false), | 156 pending_lossy_write_(false), |
| 174 read_error_(PREF_READ_ERROR_NONE), | 157 read_error_(PREF_READ_ERROR_NONE), |
| 175 has_pending_write_reply_(false), | 158 has_pending_write_reply_(false), |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 | 259 |
| 277 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { | 260 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { |
| 278 DCHECK(CalledOnValidThread()); | 261 DCHECK(CalledOnValidThread()); |
| 279 | 262 |
| 280 return read_error_; | 263 return read_error_; |
| 281 } | 264 } |
| 282 | 265 |
| 283 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { | 266 PersistentPrefStore::PrefReadError JsonPrefStore::ReadPrefs() { |
| 284 DCHECK(CalledOnValidThread()); | 267 DCHECK(CalledOnValidThread()); |
| 285 | 268 |
| 286 OnFileRead(ReadPrefsFromDisk(path_, alternate_path_)); | 269 OnFileRead(ReadPrefsFromDisk(path_)); |
| 287 return filtering_in_progress_ ? PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE | 270 return filtering_in_progress_ ? PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE |
| 288 : read_error_; | 271 : read_error_; |
| 289 } | 272 } |
| 290 | 273 |
| 291 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { | 274 void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { |
| 292 DCHECK(CalledOnValidThread()); | 275 DCHECK(CalledOnValidThread()); |
| 293 | 276 |
| 294 initialized_ = false; | 277 initialized_ = false; |
| 295 error_delegate_.reset(error_delegate); | 278 error_delegate_.reset(error_delegate); |
| 296 | 279 |
| 297 // Weakly binds the read task so that it doesn't kick in during shutdown. | 280 // Weakly binds the read task so that it doesn't kick in during shutdown. |
| 298 base::PostTaskAndReplyWithResult( | 281 base::PostTaskAndReplyWithResult( |
| 299 sequenced_task_runner_.get(), | 282 sequenced_task_runner_.get(), FROM_HERE, |
| 300 FROM_HERE, | 283 base::Bind(&ReadPrefsFromDisk, path_), |
| 301 base::Bind(&ReadPrefsFromDisk, path_, alternate_path_), | |
| 302 base::Bind(&JsonPrefStore::OnFileRead, AsWeakPtr())); | 284 base::Bind(&JsonPrefStore::OnFileRead, AsWeakPtr())); |
| 303 } | 285 } |
| 304 | 286 |
| 305 void JsonPrefStore::CommitPendingWrite() { | 287 void JsonPrefStore::CommitPendingWrite() { |
| 306 DCHECK(CalledOnValidThread()); | 288 DCHECK(CalledOnValidThread()); |
| 307 | 289 |
| 308 // Schedule a write for any lossy writes that are outstanding to ensure that | 290 // Schedule a write for any lossy writes that are outstanding to ensure that |
| 309 // they get flushed when this function is called. | 291 // they get flushed when this function is called. |
| 310 SchedulePendingLossyWrites(); | 292 SchedulePendingLossyWrites(); |
| 311 | 293 |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 DCHECK_EQ(31, num_buckets); | 585 DCHECK_EQ(31, num_buckets); |
| 604 | 586 |
| 605 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 587 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
| 606 // macro adapted to allow for a dynamically suffixed histogram name. | 588 // macro adapted to allow for a dynamically suffixed histogram name. |
| 607 // Note: The factory creates and owns the histogram. | 589 // Note: The factory creates and owns the histogram. |
| 608 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 590 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| 609 histogram_name, min_value, max_value, num_buckets, | 591 histogram_name, min_value, max_value, num_buckets, |
| 610 base::HistogramBase::kUmaTargetedHistogramFlag); | 592 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 611 return histogram; | 593 return histogram; |
| 612 } | 594 } |
| OLD | NEW |