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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 sequenced_task_runner_(sequenced_task_runner), | 166 sequenced_task_runner_(sequenced_task_runner), |
167 prefs_(new base::DictionaryValue()), | 167 prefs_(new base::DictionaryValue()), |
168 read_only_(false), | 168 read_only_(false), |
169 writer_(pref_filename, sequenced_task_runner), | 169 writer_(pref_filename, sequenced_task_runner), |
170 pref_filter_(std::move(pref_filter)), | 170 pref_filter_(std::move(pref_filter)), |
171 initialized_(false), | 171 initialized_(false), |
172 filtering_in_progress_(false), | 172 filtering_in_progress_(false), |
173 pending_lossy_write_(false), | 173 pending_lossy_write_(false), |
174 read_error_(PREF_READ_ERROR_NONE), | 174 read_error_(PREF_READ_ERROR_NONE), |
175 has_pending_successful_write_reply_(false), | 175 has_pending_successful_write_reply_(false), |
176 has_pending_write_callback_(false), | 176 has_pending_write_callbacks_(false), |
177 write_count_histogram_(writer_.commit_interval(), path_) { | 177 write_count_histogram_(writer_.commit_interval(), path_) { |
178 DCHECK(!path_.empty()); | 178 DCHECK(!path_.empty()); |
179 } | 179 } |
180 | 180 |
181 bool JsonPrefStore::GetValue(const std::string& key, | 181 bool JsonPrefStore::GetValue(const std::string& key, |
182 const base::Value** result) const { | 182 const base::Value** result) const { |
183 DCHECK(CalledOnValidThread()); | 183 DCHECK(CalledOnValidThread()); |
184 | 184 |
185 base::Value* tmp = nullptr; | 185 base::Value* tmp = nullptr; |
186 if (!prefs_->Get(key, &tmp)) | 186 if (!prefs_->Get(key, &tmp)) |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 | 323 |
324 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 324 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
325 | 325 |
326 ScheduleWrite(flags); | 326 ScheduleWrite(flags); |
327 } | 327 } |
328 | 328 |
329 void JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback( | 329 void JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback( |
330 bool write_success) { | 330 bool write_success) { |
331 DCHECK(CalledOnValidThread()); | 331 DCHECK(CalledOnValidThread()); |
332 | 332 |
333 has_pending_write_callback_ = false; | 333 has_pending_write_callbacks_ = false; |
334 if (has_pending_successful_write_reply_) { | 334 if (has_pending_successful_write_reply_) { |
335 has_pending_successful_write_reply_ = false; | 335 has_pending_successful_write_reply_ = false; |
336 if (write_success) { | 336 if (write_success) { |
337 on_next_successful_write_reply_.Run(); | 337 on_next_successful_write_reply_.Run(); |
338 } else { | 338 } else { |
339 RegisterOnNextSuccessfulWriteReply(on_next_successful_write_reply_); | 339 RegisterOnNextSuccessfulWriteReply(on_next_successful_write_reply_); |
340 } | 340 } |
341 } | 341 } |
342 } | 342 } |
343 | 343 |
(...skipping 13 matching lines...) Expand all Loading... |
357 } | 357 } |
358 | 358 |
359 void JsonPrefStore::RegisterOnNextSuccessfulWriteReply( | 359 void JsonPrefStore::RegisterOnNextSuccessfulWriteReply( |
360 const base::Closure& on_next_successful_write_reply) { | 360 const base::Closure& on_next_successful_write_reply) { |
361 DCHECK(CalledOnValidThread()); | 361 DCHECK(CalledOnValidThread()); |
362 DCHECK(!has_pending_successful_write_reply_); | 362 DCHECK(!has_pending_successful_write_reply_); |
363 | 363 |
364 has_pending_successful_write_reply_ = true; | 364 has_pending_successful_write_reply_ = true; |
365 on_next_successful_write_reply_ = on_next_successful_write_reply; | 365 on_next_successful_write_reply_ = on_next_successful_write_reply; |
366 | 366 |
367 // If there already is a pending callback, avoid erasing it; the reply will | 367 // If there are pending callbacks, avoid erasing them; the reply will be used |
368 // be used as we set |on_next_successful_write_reply_|. Otherwise, setup a | 368 // as we set |on_next_successful_write_reply_|. Otherwise, setup a reply with |
369 // reply with an empty callback. | 369 // an empty callback. |
370 if (!has_pending_write_callback_) { | 370 if (!has_pending_write_callbacks_) { |
371 writer_.RegisterOnNextWriteCallback(base::Bind( | 371 writer_.RegisterOnNextWriteCallbacks( |
372 &PostWriteCallback, | 372 base::Closure(), |
373 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, | 373 base::Bind( |
374 AsWeakPtr()), | 374 &PostWriteCallback, |
375 base::Callback<void(bool success)>(), | 375 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, |
376 base::SequencedTaskRunnerHandle::Get())); | 376 AsWeakPtr()), |
| 377 base::Callback<void(bool success)>(), |
| 378 base::SequencedTaskRunnerHandle::Get())); |
377 } | 379 } |
378 } | 380 } |
379 | 381 |
380 void JsonPrefStore::RegisterOnNextWriteSynchronousCallback( | 382 void JsonPrefStore::RegisterOnNextWriteSynchronousCallbacks( |
381 const base::Callback<void(bool success)>& on_next_write_callback) { | 383 OnWriteCallbackPair callbacks) { |
382 DCHECK(CalledOnValidThread()); | 384 DCHECK(CalledOnValidThread()); |
383 DCHECK(!has_pending_write_callback_); | 385 DCHECK(!has_pending_write_callbacks_); |
384 | 386 |
385 has_pending_write_callback_ = true; | 387 has_pending_write_callbacks_ = true; |
386 | 388 |
387 writer_.RegisterOnNextWriteCallback(base::Bind( | 389 writer_.RegisterOnNextWriteCallbacks( |
388 &PostWriteCallback, | 390 callbacks.first, |
389 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, | 391 base::Bind( |
390 AsWeakPtr()), | 392 &PostWriteCallback, |
391 on_next_write_callback, base::SequencedTaskRunnerHandle::Get())); | 393 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, |
| 394 AsWeakPtr()), |
| 395 callbacks.second, base::SequencedTaskRunnerHandle::Get())); |
392 } | 396 } |
393 | 397 |
394 void JsonPrefStore::ClearMutableValues() { | 398 void JsonPrefStore::ClearMutableValues() { |
395 NOTIMPLEMENTED(); | 399 NOTIMPLEMENTED(); |
396 } | 400 } |
397 | 401 |
398 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { | 402 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { |
399 DCHECK(CalledOnValidThread()); | 403 DCHECK(CalledOnValidThread()); |
400 | 404 |
401 DCHECK(read_result); | 405 DCHECK(read_result); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 CommitPendingWrite(); | 459 CommitPendingWrite(); |
456 } | 460 } |
457 | 461 |
458 bool JsonPrefStore::SerializeData(std::string* output) { | 462 bool JsonPrefStore::SerializeData(std::string* output) { |
459 DCHECK(CalledOnValidThread()); | 463 DCHECK(CalledOnValidThread()); |
460 | 464 |
461 pending_lossy_write_ = false; | 465 pending_lossy_write_ = false; |
462 | 466 |
463 write_count_histogram_.RecordWriteOccured(); | 467 write_count_histogram_.RecordWriteOccured(); |
464 | 468 |
465 if (pref_filter_) | 469 if (pref_filter_) { |
466 pref_filter_->FilterSerializeData(prefs_.get()); | 470 OnWriteCallbackPair callbacks = |
| 471 pref_filter_->FilterSerializeData(prefs_.get()); |
| 472 if (!callbacks.first.is_null() || !callbacks.second.is_null()) |
| 473 RegisterOnNextWriteSynchronousCallbacks(callbacks); |
| 474 } |
467 | 475 |
468 JSONStringValueSerializer serializer(output); | 476 JSONStringValueSerializer serializer(output); |
469 // Not pretty-printing prefs shrinks pref file size by ~30%. To obtain | 477 // Not pretty-printing prefs shrinks pref file size by ~30%. To obtain |
470 // readable prefs for debugging purposes, you can dump your prefs into any | 478 // readable prefs for debugging purposes, you can dump your prefs into any |
471 // command-line or online JSON pretty printing tool. | 479 // command-line or online JSON pretty printing tool. |
472 serializer.set_pretty_print(false); | 480 serializer.set_pretty_print(false); |
473 return serializer.Serialize(*prefs_); | 481 return serializer.Serialize(*prefs_); |
474 } | 482 } |
475 | 483 |
476 void JsonPrefStore::FinalizeFileRead( | 484 void JsonPrefStore::FinalizeFileRead( |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 DCHECK_EQ(31, num_buckets); | 602 DCHECK_EQ(31, num_buckets); |
595 | 603 |
596 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 604 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
597 // macro adapted to allow for a dynamically suffixed histogram name. | 605 // macro adapted to allow for a dynamically suffixed histogram name. |
598 // Note: The factory creates and owns the histogram. | 606 // Note: The factory creates and owns the histogram. |
599 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 607 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
600 histogram_name, min_value, max_value, num_buckets, | 608 histogram_name, min_value, max_value, num_buckets, |
601 base::HistogramBase::kUmaTargetedHistogramFlag); | 609 base::HistogramBase::kUmaTargetedHistogramFlag); |
602 return histogram; | 610 return histogram; |
603 } | 611 } |
OLD | NEW |