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 15 matching lines...) Expand all Loading... |
26 #include "base/time/default_clock.h" | 26 #include "base/time/default_clock.h" |
27 #include "base/values.h" | 27 #include "base/values.h" |
28 #include "components/prefs/pref_filter.h" | 28 #include "components/prefs/pref_filter.h" |
29 | 29 |
30 // Result returned from internal read tasks. | 30 // Result returned from internal read tasks. |
31 struct JsonPrefStore::ReadResult { | 31 struct JsonPrefStore::ReadResult { |
32 public: | 32 public: |
33 ReadResult(); | 33 ReadResult(); |
34 ~ReadResult(); | 34 ~ReadResult(); |
35 | 35 |
36 scoped_ptr<base::Value> value; | 36 std::unique_ptr<base::Value> value; |
37 PrefReadError error; | 37 PrefReadError error; |
38 bool no_dir; | 38 bool no_dir; |
39 | 39 |
40 private: | 40 private: |
41 DISALLOW_COPY_AND_ASSIGN(ReadResult); | 41 DISALLOW_COPY_AND_ASSIGN(ReadResult); |
42 }; | 42 }; |
43 | 43 |
44 JsonPrefStore::ReadResult::ReadResult() | 44 JsonPrefStore::ReadResult::ReadResult() |
45 : error(PersistentPrefStore::PREF_READ_ERROR_NONE), no_dir(false) { | 45 : error(PersistentPrefStore::PREF_READ_ERROR_NONE), no_dir(false) { |
46 } | 46 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 103 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
104 // macro adapted to allow for a dynamically suffixed histogram name. | 104 // macro adapted to allow for a dynamically suffixed histogram name. |
105 // Note: The factory creates and owns the histogram. | 105 // Note: The factory creates and owns the histogram. |
106 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 106 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
107 "Settings.JsonDataReadSizeKilobytes." + spaceless_basename, 1, 10000, 50, | 107 "Settings.JsonDataReadSizeKilobytes." + spaceless_basename, 1, 10000, 50, |
108 base::HistogramBase::kUmaTargetedHistogramFlag); | 108 base::HistogramBase::kUmaTargetedHistogramFlag); |
109 histogram->Add(static_cast<int>(size) / 1024); | 109 histogram->Add(static_cast<int>(size) / 1024); |
110 } | 110 } |
111 | 111 |
112 scoped_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( | 112 std::unique_ptr<JsonPrefStore::ReadResult> ReadPrefsFromDisk( |
113 const base::FilePath& path, | 113 const base::FilePath& path, |
114 const base::FilePath& alternate_path) { | 114 const base::FilePath& alternate_path) { |
115 if (!base::PathExists(path) && !alternate_path.empty() && | 115 if (!base::PathExists(path) && !alternate_path.empty() && |
116 base::PathExists(alternate_path)) { | 116 base::PathExists(alternate_path)) { |
117 base::Move(alternate_path, path); | 117 base::Move(alternate_path, path); |
118 } | 118 } |
119 | 119 |
120 int error_code; | 120 int error_code; |
121 std::string error_msg; | 121 std::string error_msg; |
122 scoped_ptr<JsonPrefStore::ReadResult> read_result( | 122 std::unique_ptr<JsonPrefStore::ReadResult> read_result( |
123 new JsonPrefStore::ReadResult); | 123 new JsonPrefStore::ReadResult); |
124 JSONFileValueDeserializer deserializer(path); | 124 JSONFileValueDeserializer deserializer(path); |
125 read_result->value = deserializer.Deserialize(&error_code, &error_msg); | 125 read_result->value = deserializer.Deserialize(&error_code, &error_msg); |
126 read_result->error = | 126 read_result->error = |
127 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); | 127 HandleReadErrors(read_result->value.get(), path, error_code, error_msg); |
128 read_result->no_dir = !base::PathExists(path.DirName()); | 128 read_result->no_dir = !base::PathExists(path.DirName()); |
129 | 129 |
130 if (read_result->error == PersistentPrefStore::PREF_READ_ERROR_NONE) | 130 if (read_result->error == PersistentPrefStore::PREF_READ_ERROR_NONE) |
131 RecordJsonDataSizeHistogram(path, deserializer.get_last_read_size()); | 131 RecordJsonDataSizeHistogram(path, deserializer.get_last_read_size()); |
132 | 132 |
133 return read_result; | 133 return read_result; |
134 } | 134 } |
135 | 135 |
136 } // namespace | 136 } // namespace |
137 | 137 |
138 // static | 138 // static |
139 scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( | 139 scoped_refptr<base::SequencedTaskRunner> JsonPrefStore::GetTaskRunnerForFile( |
140 const base::FilePath& filename, | 140 const base::FilePath& filename, |
141 base::SequencedWorkerPool* worker_pool) { | 141 base::SequencedWorkerPool* worker_pool) { |
142 std::string token("json_pref_store-"); | 142 std::string token("json_pref_store-"); |
143 token.append(filename.AsUTF8Unsafe()); | 143 token.append(filename.AsUTF8Unsafe()); |
144 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | 144 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
145 worker_pool->GetNamedSequenceToken(token), | 145 worker_pool->GetNamedSequenceToken(token), |
146 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | 146 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
147 } | 147 } |
148 | 148 |
149 JsonPrefStore::JsonPrefStore( | 149 JsonPrefStore::JsonPrefStore( |
150 const base::FilePath& pref_filename, | 150 const base::FilePath& pref_filename, |
151 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 151 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
152 scoped_ptr<PrefFilter> pref_filter) | 152 std::unique_ptr<PrefFilter> pref_filter) |
153 : JsonPrefStore(pref_filename, | 153 : JsonPrefStore(pref_filename, |
154 base::FilePath(), | 154 base::FilePath(), |
155 sequenced_task_runner, | 155 sequenced_task_runner, |
156 std::move(pref_filter)) {} | 156 std::move(pref_filter)) {} |
157 | 157 |
158 JsonPrefStore::JsonPrefStore( | 158 JsonPrefStore::JsonPrefStore( |
159 const base::FilePath& pref_filename, | 159 const base::FilePath& pref_filename, |
160 const base::FilePath& pref_alternate_filename, | 160 const base::FilePath& pref_alternate_filename, |
161 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 161 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
162 scoped_ptr<PrefFilter> pref_filter) | 162 std::unique_ptr<PrefFilter> pref_filter) |
163 : path_(pref_filename), | 163 : path_(pref_filename), |
164 alternate_path_(pref_alternate_filename), | 164 alternate_path_(pref_alternate_filename), |
165 sequenced_task_runner_(sequenced_task_runner), | 165 sequenced_task_runner_(sequenced_task_runner), |
166 prefs_(new base::DictionaryValue()), | 166 prefs_(new base::DictionaryValue()), |
167 read_only_(false), | 167 read_only_(false), |
168 writer_(pref_filename, sequenced_task_runner), | 168 writer_(pref_filename, sequenced_task_runner), |
169 pref_filter_(std::move(pref_filter)), | 169 pref_filter_(std::move(pref_filter)), |
170 initialized_(false), | 170 initialized_(false), |
171 filtering_in_progress_(false), | 171 filtering_in_progress_(false), |
172 pending_lossy_write_(false), | 172 pending_lossy_write_(false), |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 } | 213 } |
214 | 214 |
215 bool JsonPrefStore::GetMutableValue(const std::string& key, | 215 bool JsonPrefStore::GetMutableValue(const std::string& key, |
216 base::Value** result) { | 216 base::Value** result) { |
217 DCHECK(CalledOnValidThread()); | 217 DCHECK(CalledOnValidThread()); |
218 | 218 |
219 return prefs_->Get(key, result); | 219 return prefs_->Get(key, result); |
220 } | 220 } |
221 | 221 |
222 void JsonPrefStore::SetValue(const std::string& key, | 222 void JsonPrefStore::SetValue(const std::string& key, |
223 scoped_ptr<base::Value> value, | 223 std::unique_ptr<base::Value> value, |
224 uint32_t flags) { | 224 uint32_t flags) { |
225 DCHECK(CalledOnValidThread()); | 225 DCHECK(CalledOnValidThread()); |
226 | 226 |
227 DCHECK(value); | 227 DCHECK(value); |
228 base::Value* old_value = nullptr; | 228 base::Value* old_value = nullptr; |
229 prefs_->Get(key, &old_value); | 229 prefs_->Get(key, &old_value); |
230 if (!old_value || !value->Equals(old_value)) { | 230 if (!old_value || !value->Equals(old_value)) { |
231 prefs_->Set(key, std::move(value)); | 231 prefs_->Set(key, std::move(value)); |
232 ReportValueChanged(key, flags); | 232 ReportValueChanged(key, flags); |
233 } | 233 } |
234 } | 234 } |
235 | 235 |
236 void JsonPrefStore::SetValueSilently(const std::string& key, | 236 void JsonPrefStore::SetValueSilently(const std::string& key, |
237 scoped_ptr<base::Value> value, | 237 std::unique_ptr<base::Value> value, |
238 uint32_t flags) { | 238 uint32_t flags) { |
239 DCHECK(CalledOnValidThread()); | 239 DCHECK(CalledOnValidThread()); |
240 | 240 |
241 DCHECK(value); | 241 DCHECK(value); |
242 base::Value* old_value = nullptr; | 242 base::Value* old_value = nullptr; |
243 prefs_->Get(key, &old_value); | 243 prefs_->Get(key, &old_value); |
244 if (!old_value || !value->Equals(old_value)) { | 244 if (!old_value || !value->Equals(old_value)) { |
245 prefs_->Set(key, std::move(value)); | 245 prefs_->Set(key, std::move(value)); |
246 ScheduleWrite(flags); | 246 ScheduleWrite(flags); |
247 } | 247 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 const base::Closure& on_next_successful_write) { | 327 const base::Closure& on_next_successful_write) { |
328 DCHECK(CalledOnValidThread()); | 328 DCHECK(CalledOnValidThread()); |
329 | 329 |
330 writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); | 330 writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); |
331 } | 331 } |
332 | 332 |
333 void JsonPrefStore::ClearMutableValues() { | 333 void JsonPrefStore::ClearMutableValues() { |
334 NOTIMPLEMENTED(); | 334 NOTIMPLEMENTED(); |
335 } | 335 } |
336 | 336 |
337 void JsonPrefStore::OnFileRead(scoped_ptr<ReadResult> read_result) { | 337 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { |
338 DCHECK(CalledOnValidThread()); | 338 DCHECK(CalledOnValidThread()); |
339 | 339 |
340 DCHECK(read_result); | 340 DCHECK(read_result); |
341 | 341 |
342 scoped_ptr<base::DictionaryValue> unfiltered_prefs(new base::DictionaryValue); | 342 std::unique_ptr<base::DictionaryValue> unfiltered_prefs( |
| 343 new base::DictionaryValue); |
343 | 344 |
344 read_error_ = read_result->error; | 345 read_error_ = read_result->error; |
345 | 346 |
346 bool initialization_successful = !read_result->no_dir; | 347 bool initialization_successful = !read_result->no_dir; |
347 | 348 |
348 if (initialization_successful) { | 349 if (initialization_successful) { |
349 switch (read_error_) { | 350 switch (read_error_) { |
350 case PREF_READ_ERROR_ACCESS_DENIED: | 351 case PREF_READ_ERROR_ACCESS_DENIED: |
351 case PREF_READ_ERROR_FILE_OTHER: | 352 case PREF_READ_ERROR_FILE_OTHER: |
352 case PREF_READ_ERROR_FILE_LOCKED: | 353 case PREF_READ_ERROR_FILE_LOCKED: |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 pref_filter_->FilterSerializeData(prefs_.get()); | 405 pref_filter_->FilterSerializeData(prefs_.get()); |
405 | 406 |
406 JSONStringValueSerializer serializer(output); | 407 JSONStringValueSerializer serializer(output); |
407 // Not pretty-printing prefs shrinks pref file size by ~30%. To obtain | 408 // Not pretty-printing prefs shrinks pref file size by ~30%. To obtain |
408 // readable prefs for debugging purposes, you can dump your prefs into any | 409 // readable prefs for debugging purposes, you can dump your prefs into any |
409 // command-line or online JSON pretty printing tool. | 410 // command-line or online JSON pretty printing tool. |
410 serializer.set_pretty_print(false); | 411 serializer.set_pretty_print(false); |
411 return serializer.Serialize(*prefs_); | 412 return serializer.Serialize(*prefs_); |
412 } | 413 } |
413 | 414 |
414 void JsonPrefStore::FinalizeFileRead(bool initialization_successful, | 415 void JsonPrefStore::FinalizeFileRead( |
415 scoped_ptr<base::DictionaryValue> prefs, | 416 bool initialization_successful, |
416 bool schedule_write) { | 417 std::unique_ptr<base::DictionaryValue> prefs, |
| 418 bool schedule_write) { |
417 DCHECK(CalledOnValidThread()); | 419 DCHECK(CalledOnValidThread()); |
418 | 420 |
419 filtering_in_progress_ = false; | 421 filtering_in_progress_ = false; |
420 | 422 |
421 if (!initialization_successful) { | 423 if (!initialization_successful) { |
422 FOR_EACH_OBSERVER(PrefStore::Observer, | 424 FOR_EACH_OBSERVER(PrefStore::Observer, |
423 observers_, | 425 observers_, |
424 OnInitializationCompleted(false)); | 426 OnInitializationCompleted(false)); |
425 return; | 427 return; |
426 } | 428 } |
(...skipping 26 matching lines...) Expand all Loading... |
453 } | 455 } |
454 | 456 |
455 // NOTE: This value should NOT be changed without renaming the histogram | 457 // NOTE: This value should NOT be changed without renaming the histogram |
456 // otherwise it will create incompatible buckets. | 458 // otherwise it will create incompatible buckets. |
457 const int32_t | 459 const int32_t |
458 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins = 5; | 460 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins = 5; |
459 | 461 |
460 JsonPrefStore::WriteCountHistogram::WriteCountHistogram( | 462 JsonPrefStore::WriteCountHistogram::WriteCountHistogram( |
461 const base::TimeDelta& commit_interval, | 463 const base::TimeDelta& commit_interval, |
462 const base::FilePath& path) | 464 const base::FilePath& path) |
463 : WriteCountHistogram(commit_interval, | 465 : WriteCountHistogram( |
464 path, | 466 commit_interval, |
465 scoped_ptr<base::Clock>(new base::DefaultClock)) { | 467 path, |
466 } | 468 std::unique_ptr<base::Clock>(new base::DefaultClock)) {} |
467 | 469 |
468 JsonPrefStore::WriteCountHistogram::WriteCountHistogram( | 470 JsonPrefStore::WriteCountHistogram::WriteCountHistogram( |
469 const base::TimeDelta& commit_interval, | 471 const base::TimeDelta& commit_interval, |
470 const base::FilePath& path, | 472 const base::FilePath& path, |
471 scoped_ptr<base::Clock> clock) | 473 std::unique_ptr<base::Clock> clock) |
472 : commit_interval_(commit_interval), | 474 : commit_interval_(commit_interval), |
473 path_(path), | 475 path_(path), |
474 clock_(clock.release()), | 476 clock_(clock.release()), |
475 report_interval_( | 477 report_interval_( |
476 base::TimeDelta::FromMinutes(kHistogramWriteReportIntervalMins)), | 478 base::TimeDelta::FromMinutes(kHistogramWriteReportIntervalMins)), |
477 last_report_time_(clock_->Now()), | 479 last_report_time_(clock_->Now()), |
478 writes_since_last_report_(0) { | 480 writes_since_last_report_(0) {} |
479 } | |
480 | 481 |
481 JsonPrefStore::WriteCountHistogram::~WriteCountHistogram() { | 482 JsonPrefStore::WriteCountHistogram::~WriteCountHistogram() { |
482 ReportOutstandingWrites(); | 483 ReportOutstandingWrites(); |
483 } | 484 } |
484 | 485 |
485 void JsonPrefStore::WriteCountHistogram::RecordWriteOccured() { | 486 void JsonPrefStore::WriteCountHistogram::RecordWriteOccured() { |
486 ReportOutstandingWrites(); | 487 ReportOutstandingWrites(); |
487 | 488 |
488 ++writes_since_last_report_; | 489 ++writes_since_last_report_; |
489 } | 490 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 DCHECK_EQ(31, num_buckets); | 533 DCHECK_EQ(31, num_buckets); |
533 | 534 |
534 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 535 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
535 // macro adapted to allow for a dynamically suffixed histogram name. | 536 // macro adapted to allow for a dynamically suffixed histogram name. |
536 // Note: The factory creates and owns the histogram. | 537 // Note: The factory creates and owns the histogram. |
537 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 538 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
538 histogram_name, min_value, max_value, num_buckets, | 539 histogram_name, min_value, max_value, num_buckets, |
539 base::HistogramBase::kUmaTargetedHistogramFlag); | 540 base::HistogramBase::kUmaTargetedHistogramFlag); |
540 return histogram; | 541 return histogram; |
541 } | 542 } |
OLD | NEW |