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" | 11 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/json/json_file_value_serializer.h" | 13 #include "base/json/json_file_value_serializer.h" |
14 #include "base/json/json_string_value_serializer.h" | 14 #include "base/json/json_string_value_serializer.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
17 #include "base/prefs/pref_filter.h" | 17 #include "base/prefs/pref_filter.h" |
18 #include "base/sequenced_task_runner.h" | 18 #include "base/sequenced_task_runner.h" |
19 #include "base/strings/string_number_conversions.h" | |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
20 #include "base/task_runner_util.h" | 21 #include "base/task_runner_util.h" |
21 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
22 #include "base/values.h" | 23 #include "base/values.h" |
23 | 24 |
24 // Result returned from internal read tasks. | 25 // Result returned from internal read tasks. |
25 struct JsonPrefStore::ReadResult { | 26 struct JsonPrefStore::ReadResult { |
26 public: | 27 public: |
27 ReadResult(); | 28 ReadResult(); |
28 ~ReadResult(); | 29 ~ReadResult(); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 150 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
150 scoped_ptr<PrefFilter> pref_filter) | 151 scoped_ptr<PrefFilter> pref_filter) |
151 : path_(filename), | 152 : path_(filename), |
152 sequenced_task_runner_(sequenced_task_runner), | 153 sequenced_task_runner_(sequenced_task_runner), |
153 prefs_(new base::DictionaryValue()), | 154 prefs_(new base::DictionaryValue()), |
154 read_only_(false), | 155 read_only_(false), |
155 writer_(filename, sequenced_task_runner), | 156 writer_(filename, sequenced_task_runner), |
156 pref_filter_(pref_filter.Pass()), | 157 pref_filter_(pref_filter.Pass()), |
157 initialized_(false), | 158 initialized_(false), |
158 filtering_in_progress_(false), | 159 filtering_in_progress_(false), |
159 read_error_(PREF_READ_ERROR_NONE) { | 160 read_error_(PREF_READ_ERROR_NONE), |
161 write_count_histogram_(writer_.commit_interval(), path_) { | |
160 DCHECK(!path_.empty()); | 162 DCHECK(!path_.empty()); |
161 } | 163 } |
162 | 164 |
163 JsonPrefStore::JsonPrefStore( | 165 JsonPrefStore::JsonPrefStore( |
164 const base::FilePath& filename, | 166 const base::FilePath& filename, |
165 const base::FilePath& alternate_filename, | 167 const base::FilePath& alternate_filename, |
166 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 168 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
167 scoped_ptr<PrefFilter> pref_filter) | 169 scoped_ptr<PrefFilter> pref_filter) |
168 : path_(filename), | 170 : path_(filename), |
169 alternate_path_(alternate_filename), | 171 alternate_path_(alternate_filename), |
170 sequenced_task_runner_(sequenced_task_runner), | 172 sequenced_task_runner_(sequenced_task_runner), |
171 prefs_(new base::DictionaryValue()), | 173 prefs_(new base::DictionaryValue()), |
172 read_only_(false), | 174 read_only_(false), |
173 writer_(filename, sequenced_task_runner), | 175 writer_(filename, sequenced_task_runner), |
174 pref_filter_(pref_filter.Pass()), | 176 pref_filter_(pref_filter.Pass()), |
175 initialized_(false), | 177 initialized_(false), |
176 filtering_in_progress_(false), | 178 filtering_in_progress_(false), |
177 read_error_(PREF_READ_ERROR_NONE) { | 179 read_error_(PREF_READ_ERROR_NONE), |
180 write_count_histogram_(writer_.commit_interval(), path_) { | |
178 DCHECK(!path_.empty()); | 181 DCHECK(!path_.empty()); |
179 } | 182 } |
180 | 183 |
181 bool JsonPrefStore::GetValue(const std::string& key, | 184 bool JsonPrefStore::GetValue(const std::string& key, |
182 const base::Value** result) const { | 185 const base::Value** result) const { |
183 DCHECK(CalledOnValidThread()); | 186 DCHECK(CalledOnValidThread()); |
184 | 187 |
185 base::Value* tmp = NULL; | 188 base::Value* tmp = NULL; |
186 if (!prefs_->Get(key, &tmp)) | 189 if (!prefs_->Get(key, &tmp)) |
187 return false; | 190 return false; |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
387 } | 390 } |
388 } | 391 } |
389 | 392 |
390 JsonPrefStore::~JsonPrefStore() { | 393 JsonPrefStore::~JsonPrefStore() { |
391 CommitPendingWrite(); | 394 CommitPendingWrite(); |
392 } | 395 } |
393 | 396 |
394 bool JsonPrefStore::SerializeData(std::string* output) { | 397 bool JsonPrefStore::SerializeData(std::string* output) { |
395 DCHECK(CalledOnValidThread()); | 398 DCHECK(CalledOnValidThread()); |
396 | 399 |
400 write_count_histogram_.RecordWriteOccured(); | |
401 | |
397 if (pref_filter_) | 402 if (pref_filter_) |
398 pref_filter_->FilterSerializeData(prefs_.get()); | 403 pref_filter_->FilterSerializeData(prefs_.get()); |
399 | 404 |
400 JSONStringValueSerializer serializer(output); | 405 JSONStringValueSerializer serializer(output); |
401 // Not pretty-printing prefs shrinks pref file size by ~30%. To obtain | 406 // 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 | 407 // readable prefs for debugging purposes, you can dump your prefs into any |
403 // command-line or online JSON pretty printing tool. | 408 // command-line or online JSON pretty printing tool. |
404 serializer.set_pretty_print(false); | 409 serializer.set_pretty_print(false); |
405 return serializer.Serialize(*prefs_); | 410 return serializer.Serialize(*prefs_); |
406 } | 411 } |
(...skipping 21 matching lines...) Expand all Loading... | |
428 | 433 |
429 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 434 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
430 error_delegate_->OnError(read_error_); | 435 error_delegate_->OnError(read_error_); |
431 | 436 |
432 FOR_EACH_OBSERVER(PrefStore::Observer, | 437 FOR_EACH_OBSERVER(PrefStore::Observer, |
433 observers_, | 438 observers_, |
434 OnInitializationCompleted(true)); | 439 OnInitializationCompleted(true)); |
435 | 440 |
436 return; | 441 return; |
437 } | 442 } |
443 | |
444 const int32_t | |
445 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins = 5; | |
446 | |
447 JsonPrefStore::WriteCountHistogram::WriteCountHistogram( | |
448 const base::TimeDelta& commit_interval, | |
449 const base::FilePath& path) | |
450 : WriteCountHistogram(commit_interval, path, base::Bind(&base::Time::Now)) { | |
451 } | |
452 | |
453 JsonPrefStore::WriteCountHistogram::WriteCountHistogram( | |
454 const base::TimeDelta& commit_interval, | |
455 const base::FilePath& path, | |
456 const base::Callback<base::Time(void)>& get_time_func) | |
457 : commit_interval_(commit_interval), | |
458 path_(path), | |
459 get_time_func_(get_time_func), | |
460 report_interval_( | |
461 base::TimeDelta::FromMinutes(kHistogramWriteReportIntervalMins)), | |
462 last_report_time_(get_time_func.Run()), | |
463 writes_since_last_report_(0) { | |
464 } | |
465 | |
466 JsonPrefStore::WriteCountHistogram::~WriteCountHistogram() { | |
467 ReportOutstandingWrites(); | |
468 } | |
469 | |
470 void JsonPrefStore::WriteCountHistogram::RecordWriteOccured() { | |
471 ReportOutstandingWrites(); | |
472 | |
473 writes_since_last_report_++; | |
Alexei Svitkine (slow)
2015/04/24 16:34:47
Nit: ++writes
raymes
2015/04/27 03:27:54
Done.
| |
474 } | |
475 | |
476 void JsonPrefStore::WriteCountHistogram::ReportOutstandingWrites() { | |
477 base::Time current_time = get_time_func_.Run(); | |
478 base::TimeDelta time_since_last_report = current_time - last_report_time_; | |
479 base::HistogramBase* histogram = GetHistogram(); | |
480 | |
481 // If the time since the last report exceeds the report interval, we report | |
482 // all of the fully elapsed intervals up to the current time. | |
483 while (time_since_last_report > report_interval_) { | |
484 histogram->Add(writes_since_last_report_); | |
485 | |
486 writes_since_last_report_ = 0; | |
487 last_report_time_ += report_interval_; | |
488 time_since_last_report = current_time - last_report_time_; | |
489 } | |
490 } | |
491 | |
492 base::HistogramBase* JsonPrefStore::WriteCountHistogram::GetHistogram() { | |
493 std::string spaceless_basename; | |
494 base::ReplaceChars(path_.BaseName().MaybeAsASCII(), " ", "_", | |
495 &spaceless_basename); | |
496 std::string histogram_name = | |
497 "Settings.JsonDataWriteCount." + spaceless_basename; | |
498 | |
499 // The min value for a histogram is 1. The max value is the maximum number of | |
500 // writes that can occur in the window being recorded. The number of buckets | |
501 // used is the max value (plus the underflow/overflow buckets). | |
502 int32_t min_value = 1; | |
503 int32_t max_value = report_interval_ / commit_interval_; | |
504 int32_t num_buckets = max_value + 1; | |
505 | |
506 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | |
507 // macro adapted to allow for a dynamically suffixed histogram name. | |
508 // Note: The factory creates and owns the histogram. | |
509 base::HistogramBase* histogram = base::Histogram::FactoryGet( | |
510 histogram_name, min_value, max_value, num_buckets, | |
511 base::HistogramBase::kUmaTargetedHistogramFlag); | |
512 return histogram; | |
513 } | |
OLD | NEW |