| 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 <stddef.h> |
| 8 |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 8 #include <utility> | 10 #include <utility> |
| 9 | 11 |
| 10 #include "base/bind.h" | 12 #include "base/bind.h" |
| 11 #include "base/callback.h" | 13 #include "base/callback.h" |
| 12 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 14 #include "base/json/json_file_value_serializer.h" | 16 #include "base/json/json_file_value_serializer.h" |
| 15 #include "base/json/json_string_value_serializer.h" | 17 #include "base/json/json_string_value_serializer.h" |
| 18 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 17 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
| 18 #include "base/prefs/pref_filter.h" | 21 #include "base/prefs/pref_filter.h" |
| 19 #include "base/sequenced_task_runner.h" | 22 #include "base/sequenced_task_runner.h" |
| 20 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 22 #include "base/task_runner_util.h" | 25 #include "base/task_runner_util.h" |
| 23 #include "base/threading/sequenced_worker_pool.h" | 26 #include "base/threading/sequenced_worker_pool.h" |
| 24 #include "base/time/default_clock.h" | 27 #include "base/time/default_clock.h" |
| 25 #include "base/values.h" | 28 #include "base/values.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 | 214 |
| 212 bool JsonPrefStore::GetMutableValue(const std::string& key, | 215 bool JsonPrefStore::GetMutableValue(const std::string& key, |
| 213 base::Value** result) { | 216 base::Value** result) { |
| 214 DCHECK(CalledOnValidThread()); | 217 DCHECK(CalledOnValidThread()); |
| 215 | 218 |
| 216 return prefs_->Get(key, result); | 219 return prefs_->Get(key, result); |
| 217 } | 220 } |
| 218 | 221 |
| 219 void JsonPrefStore::SetValue(const std::string& key, | 222 void JsonPrefStore::SetValue(const std::string& key, |
| 220 scoped_ptr<base::Value> value, | 223 scoped_ptr<base::Value> value, |
| 221 uint32 flags) { | 224 uint32_t flags) { |
| 222 DCHECK(CalledOnValidThread()); | 225 DCHECK(CalledOnValidThread()); |
| 223 | 226 |
| 224 DCHECK(value); | 227 DCHECK(value); |
| 225 base::Value* old_value = nullptr; | 228 base::Value* old_value = nullptr; |
| 226 prefs_->Get(key, &old_value); | 229 prefs_->Get(key, &old_value); |
| 227 if (!old_value || !value->Equals(old_value)) { | 230 if (!old_value || !value->Equals(old_value)) { |
| 228 prefs_->Set(key, std::move(value)); | 231 prefs_->Set(key, std::move(value)); |
| 229 ReportValueChanged(key, flags); | 232 ReportValueChanged(key, flags); |
| 230 } | 233 } |
| 231 } | 234 } |
| 232 | 235 |
| 233 void JsonPrefStore::SetValueSilently(const std::string& key, | 236 void JsonPrefStore::SetValueSilently(const std::string& key, |
| 234 scoped_ptr<base::Value> value, | 237 scoped_ptr<base::Value> value, |
| 235 uint32 flags) { | 238 uint32_t flags) { |
| 236 DCHECK(CalledOnValidThread()); | 239 DCHECK(CalledOnValidThread()); |
| 237 | 240 |
| 238 DCHECK(value); | 241 DCHECK(value); |
| 239 base::Value* old_value = nullptr; | 242 base::Value* old_value = nullptr; |
| 240 prefs_->Get(key, &old_value); | 243 prefs_->Get(key, &old_value); |
| 241 if (!old_value || !value->Equals(old_value)) { | 244 if (!old_value || !value->Equals(old_value)) { |
| 242 prefs_->Set(key, std::move(value)); | 245 prefs_->Set(key, std::move(value)); |
| 243 ScheduleWrite(flags); | 246 ScheduleWrite(flags); |
| 244 } | 247 } |
| 245 } | 248 } |
| 246 | 249 |
| 247 void JsonPrefStore::RemoveValue(const std::string& key, uint32 flags) { | 250 void JsonPrefStore::RemoveValue(const std::string& key, uint32_t flags) { |
| 248 DCHECK(CalledOnValidThread()); | 251 DCHECK(CalledOnValidThread()); |
| 249 | 252 |
| 250 if (prefs_->RemovePath(key, nullptr)) | 253 if (prefs_->RemovePath(key, nullptr)) |
| 251 ReportValueChanged(key, flags); | 254 ReportValueChanged(key, flags); |
| 252 } | 255 } |
| 253 | 256 |
| 254 void JsonPrefStore::RemoveValueSilently(const std::string& key, uint32 flags) { | 257 void JsonPrefStore::RemoveValueSilently(const std::string& key, |
| 258 uint32_t flags) { |
| 255 DCHECK(CalledOnValidThread()); | 259 DCHECK(CalledOnValidThread()); |
| 256 | 260 |
| 257 prefs_->RemovePath(key, nullptr); | 261 prefs_->RemovePath(key, nullptr); |
| 258 ScheduleWrite(flags); | 262 ScheduleWrite(flags); |
| 259 } | 263 } |
| 260 | 264 |
| 261 bool JsonPrefStore::ReadOnly() const { | 265 bool JsonPrefStore::ReadOnly() const { |
| 262 DCHECK(CalledOnValidThread()); | 266 DCHECK(CalledOnValidThread()); |
| 263 | 267 |
| 264 return read_only_; | 268 return read_only_; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 | 305 |
| 302 if (writer_.HasPendingWrite() && !read_only_) | 306 if (writer_.HasPendingWrite() && !read_only_) |
| 303 writer_.DoScheduledWrite(); | 307 writer_.DoScheduledWrite(); |
| 304 } | 308 } |
| 305 | 309 |
| 306 void JsonPrefStore::SchedulePendingLossyWrites() { | 310 void JsonPrefStore::SchedulePendingLossyWrites() { |
| 307 if (pending_lossy_write_) | 311 if (pending_lossy_write_) |
| 308 writer_.ScheduleWrite(this); | 312 writer_.ScheduleWrite(this); |
| 309 } | 313 } |
| 310 | 314 |
| 311 void JsonPrefStore::ReportValueChanged(const std::string& key, uint32 flags) { | 315 void JsonPrefStore::ReportValueChanged(const std::string& key, uint32_t flags) { |
| 312 DCHECK(CalledOnValidThread()); | 316 DCHECK(CalledOnValidThread()); |
| 313 | 317 |
| 314 if (pref_filter_) | 318 if (pref_filter_) |
| 315 pref_filter_->FilterUpdate(key); | 319 pref_filter_->FilterUpdate(key); |
| 316 | 320 |
| 317 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 321 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 318 | 322 |
| 319 ScheduleWrite(flags); | 323 ScheduleWrite(flags); |
| 320 } | 324 } |
| 321 | 325 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 431 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
| 428 error_delegate_->OnError(read_error_); | 432 error_delegate_->OnError(read_error_); |
| 429 | 433 |
| 430 FOR_EACH_OBSERVER(PrefStore::Observer, | 434 FOR_EACH_OBSERVER(PrefStore::Observer, |
| 431 observers_, | 435 observers_, |
| 432 OnInitializationCompleted(true)); | 436 OnInitializationCompleted(true)); |
| 433 | 437 |
| 434 return; | 438 return; |
| 435 } | 439 } |
| 436 | 440 |
| 437 void JsonPrefStore::ScheduleWrite(uint32 flags) { | 441 void JsonPrefStore::ScheduleWrite(uint32_t flags) { |
| 438 if (read_only_) | 442 if (read_only_) |
| 439 return; | 443 return; |
| 440 | 444 |
| 441 if (flags & LOSSY_PREF_WRITE_FLAG) | 445 if (flags & LOSSY_PREF_WRITE_FLAG) |
| 442 pending_lossy_write_ = true; | 446 pending_lossy_write_ = true; |
| 443 else | 447 else |
| 444 writer_.ScheduleWrite(this); | 448 writer_.ScheduleWrite(this); |
| 445 } | 449 } |
| 446 | 450 |
| 447 // NOTE: This value should NOT be changed without renaming the histogram | 451 // NOTE: This value should NOT be changed without renaming the histogram |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 return; | 492 return; |
| 489 | 493 |
| 490 // If the time since the last report exceeds the report interval, report all | 494 // If the time since the last report exceeds the report interval, report all |
| 491 // the writes since the last report. They must have all occurred in the same | 495 // the writes since the last report. They must have all occurred in the same |
| 492 // report interval. | 496 // report interval. |
| 493 base::HistogramBase* histogram = GetHistogram(); | 497 base::HistogramBase* histogram = GetHistogram(); |
| 494 histogram->Add(writes_since_last_report_); | 498 histogram->Add(writes_since_last_report_); |
| 495 | 499 |
| 496 // There may be several report intervals that elapsed that don't have any | 500 // There may be several report intervals that elapsed that don't have any |
| 497 // writes in them. Report these too. | 501 // writes in them. Report these too. |
| 498 int64 total_num_intervals_elapsed = | 502 int64_t total_num_intervals_elapsed = |
| 499 (time_since_last_report / report_interval_); | 503 (time_since_last_report / report_interval_); |
| 500 for (int64 i = 0; i < total_num_intervals_elapsed - 1; ++i) | 504 for (int64_t i = 0; i < total_num_intervals_elapsed - 1; ++i) |
| 501 histogram->Add(0); | 505 histogram->Add(0); |
| 502 | 506 |
| 503 writes_since_last_report_ = 0; | 507 writes_since_last_report_ = 0; |
| 504 last_report_time_ += total_num_intervals_elapsed * report_interval_; | 508 last_report_time_ += total_num_intervals_elapsed * report_interval_; |
| 505 } | 509 } |
| 506 | 510 |
| 507 base::HistogramBase* JsonPrefStore::WriteCountHistogram::GetHistogram() { | 511 base::HistogramBase* JsonPrefStore::WriteCountHistogram::GetHistogram() { |
| 508 std::string spaceless_basename; | 512 std::string spaceless_basename; |
| 509 base::ReplaceChars(path_.BaseName().MaybeAsASCII(), " ", "_", | 513 base::ReplaceChars(path_.BaseName().MaybeAsASCII(), " ", "_", |
| 510 &spaceless_basename); | 514 &spaceless_basename); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 524 DCHECK_EQ(31, num_buckets); | 528 DCHECK_EQ(31, num_buckets); |
| 525 | 529 |
| 526 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 530 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
| 527 // macro adapted to allow for a dynamically suffixed histogram name. | 531 // macro adapted to allow for a dynamically suffixed histogram name. |
| 528 // Note: The factory creates and owns the histogram. | 532 // Note: The factory creates and owns the histogram. |
| 529 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 533 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| 530 histogram_name, min_value, max_value, num_buckets, | 534 histogram_name, min_value, max_value, num_buckets, |
| 531 base::HistogramBase::kUmaTargetedHistogramFlag); | 535 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 532 return histogram; | 536 return histogram; |
| 533 } | 537 } |
| OLD | NEW |