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> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/json/json_file_value_serializer.h" | 16 #include "base/json/json_file_value_serializer.h" |
17 #include "base/json/json_string_value_serializer.h" | 17 #include "base/json/json_string_value_serializer.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
20 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
21 #include "base/sequenced_task_runner.h" | 21 #include "base/sequenced_task_runner.h" |
22 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
23 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
24 #include "base/task_runner_util.h" | 24 #include "base/task_runner_util.h" |
| 25 #include "base/threading/sequenced_task_runner_handle.h" |
25 #include "base/threading/sequenced_worker_pool.h" | 26 #include "base/threading/sequenced_worker_pool.h" |
26 #include "base/time/default_clock.h" | 27 #include "base/time/default_clock.h" |
27 #include "base/values.h" | 28 #include "base/values.h" |
28 #include "components/prefs/pref_filter.h" | 29 #include "components/prefs/pref_filter.h" |
29 | 30 |
30 // Result returned from internal read tasks. | 31 // Result returned from internal read tasks. |
31 struct JsonPrefStore::ReadResult { | 32 struct JsonPrefStore::ReadResult { |
32 public: | 33 public: |
33 ReadResult(); | 34 ReadResult(); |
34 ~ReadResult(); | 35 ~ReadResult(); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 alternate_path_(pref_alternate_filename), | 165 alternate_path_(pref_alternate_filename), |
165 sequenced_task_runner_(sequenced_task_runner), | 166 sequenced_task_runner_(sequenced_task_runner), |
166 prefs_(new base::DictionaryValue()), | 167 prefs_(new base::DictionaryValue()), |
167 read_only_(false), | 168 read_only_(false), |
168 writer_(pref_filename, sequenced_task_runner), | 169 writer_(pref_filename, sequenced_task_runner), |
169 pref_filter_(std::move(pref_filter)), | 170 pref_filter_(std::move(pref_filter)), |
170 initialized_(false), | 171 initialized_(false), |
171 filtering_in_progress_(false), | 172 filtering_in_progress_(false), |
172 pending_lossy_write_(false), | 173 pending_lossy_write_(false), |
173 read_error_(PREF_READ_ERROR_NONE), | 174 read_error_(PREF_READ_ERROR_NONE), |
| 175 has_pending_successful_write_reply_(false), |
| 176 has_pending_write_callback_(false), |
174 write_count_histogram_(writer_.commit_interval(), path_) { | 177 write_count_histogram_(writer_.commit_interval(), path_) { |
175 DCHECK(!path_.empty()); | 178 DCHECK(!path_.empty()); |
176 } | 179 } |
177 | 180 |
178 bool JsonPrefStore::GetValue(const std::string& key, | 181 bool JsonPrefStore::GetValue(const std::string& key, |
179 const base::Value** result) const { | 182 const base::Value** result) const { |
180 DCHECK(CalledOnValidThread()); | 183 DCHECK(CalledOnValidThread()); |
181 | 184 |
182 base::Value* tmp = nullptr; | 185 base::Value* tmp = nullptr; |
183 if (!prefs_->Get(key, &tmp)) | 186 if (!prefs_->Get(key, &tmp)) |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 DCHECK(CalledOnValidThread()); | 319 DCHECK(CalledOnValidThread()); |
317 | 320 |
318 if (pref_filter_) | 321 if (pref_filter_) |
319 pref_filter_->FilterUpdate(key); | 322 pref_filter_->FilterUpdate(key); |
320 | 323 |
321 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 324 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
322 | 325 |
323 ScheduleWrite(flags); | 326 ScheduleWrite(flags); |
324 } | 327 } |
325 | 328 |
326 void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback( | 329 void JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback( |
327 const base::Closure& on_next_successful_write) { | 330 bool write_success) { |
328 DCHECK(CalledOnValidThread()); | 331 DCHECK(CalledOnValidThread()); |
329 | 332 |
330 writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); | 333 has_pending_write_callback_ = false; |
| 334 if (has_pending_successful_write_reply_) { |
| 335 has_pending_successful_write_reply_ = false; |
| 336 if (write_success) { |
| 337 on_next_successful_write_reply_.Run(); |
| 338 } else { |
| 339 RegisterOnNextSuccessfulWriteReply(on_next_successful_write_reply_); |
| 340 } |
| 341 } |
| 342 } |
| 343 |
| 344 // static |
| 345 void JsonPrefStore::PostWriteCallback( |
| 346 const base::Callback<void(bool success)>& on_next_write_reply, |
| 347 const base::Callback<void(bool success)>& on_next_write_callback, |
| 348 scoped_refptr<base::SequencedTaskRunner> reply_task_runner, |
| 349 bool write_success) { |
| 350 if (!on_next_write_callback.is_null()) |
| 351 on_next_write_callback.Run(write_success); |
| 352 |
| 353 // We can't run |on_next_write_reply| on the current thread. Bounce back to |
| 354 // the |reply_task_runner| which is the correct sequenced thread. |
| 355 reply_task_runner->PostTask(FROM_HERE, |
| 356 base::Bind(on_next_write_reply, write_success)); |
| 357 } |
| 358 |
| 359 void JsonPrefStore::RegisterOnNextSuccessfulWriteReply( |
| 360 const base::Closure& on_next_successful_write_reply) { |
| 361 DCHECK(CalledOnValidThread()); |
| 362 DCHECK(!has_pending_successful_write_reply_); |
| 363 |
| 364 has_pending_successful_write_reply_ = true; |
| 365 on_next_successful_write_reply_ = on_next_successful_write_reply; |
| 366 |
| 367 // If there already is a pending callback, avoid erasing it; the reply will |
| 368 // be used as we set |on_next_successful_write_reply_|. Otherwise, setup a |
| 369 // reply with an empty callback. |
| 370 if (!has_pending_write_callback_) { |
| 371 writer_.RegisterOnNextWriteCallback(base::Bind( |
| 372 &PostWriteCallback, |
| 373 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, |
| 374 AsWeakPtr()), |
| 375 base::Callback<void(bool success)>(), |
| 376 base::SequencedTaskRunnerHandle::Get())); |
| 377 } |
| 378 } |
| 379 |
| 380 void JsonPrefStore::RegisterOnNextWriteSynchronousCallback( |
| 381 const base::Callback<void(bool success)>& on_next_write_callback) { |
| 382 DCHECK(CalledOnValidThread()); |
| 383 DCHECK(!has_pending_write_callback_); |
| 384 |
| 385 has_pending_write_callback_ = true; |
| 386 |
| 387 writer_.RegisterOnNextWriteCallback(base::Bind( |
| 388 &PostWriteCallback, |
| 389 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, |
| 390 AsWeakPtr()), |
| 391 on_next_write_callback, base::SequencedTaskRunnerHandle::Get())); |
331 } | 392 } |
332 | 393 |
333 void JsonPrefStore::ClearMutableValues() { | 394 void JsonPrefStore::ClearMutableValues() { |
334 NOTIMPLEMENTED(); | 395 NOTIMPLEMENTED(); |
335 } | 396 } |
336 | 397 |
337 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { | 398 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { |
338 DCHECK(CalledOnValidThread()); | 399 DCHECK(CalledOnValidThread()); |
339 | 400 |
340 DCHECK(read_result); | 401 DCHECK(read_result); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 DCHECK_EQ(31, num_buckets); | 594 DCHECK_EQ(31, num_buckets); |
534 | 595 |
535 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 596 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
536 // macro adapted to allow for a dynamically suffixed histogram name. | 597 // macro adapted to allow for a dynamically suffixed histogram name. |
537 // Note: The factory creates and owns the histogram. | 598 // Note: The factory creates and owns the histogram. |
538 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 599 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
539 histogram_name, min_value, max_value, num_buckets, | 600 histogram_name, min_value, max_value, num_buckets, |
540 base::HistogramBase::kUmaTargetedHistogramFlag); | 601 base::HistogramBase::kUmaTargetedHistogramFlag); |
541 return histogram; | 602 return histogram; |
542 } | 603 } |
OLD | NEW |