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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 : path_(pref_filename), | 164 : path_(pref_filename), |
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), |
174 has_pending_successful_write_reply_(false), | |
175 has_pending_write_callback_(false), | |
173 read_error_(PREF_READ_ERROR_NONE), | 176 read_error_(PREF_READ_ERROR_NONE), |
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; |
(...skipping 133 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()) { | |
gab
2016/09/20 17:30:12
nit: no {} for single-line conditional + single-li
proberge
2016/09/20 18:03:28
Done.
| |
351 on_next_write_callback.Run(write_success); | |
352 } | |
353 | |
354 // We can't run |on_next_write_reply| on the current thread. Bounce back to | |
355 // the |reply_task_runner| which is the correct sequenced thread. | |
356 reply_task_runner->PostTask(FROM_HERE, | |
357 base::Bind(on_next_write_reply, write_success)); | |
358 } | |
359 | |
360 void JsonPrefStore::RegisterOnNextSuccessfulWriteReply( | |
361 const base::Closure& on_next_successful_write_reply) { | |
362 DCHECK(CalledOnValidThread()); | |
363 DCHECK(!has_pending_successful_write_reply_); | |
364 | |
365 has_pending_successful_write_reply_ = true; | |
366 on_next_successful_write_reply_ = on_next_successful_write_reply; | |
367 | |
368 // Register |PostWriteCallback| to be called when the preference file is | |
369 // written to disk. |on_next_successful_write_reply| will be run on the | |
370 // current sequence while |on_next_write_callback| will be run, if set by | |
371 // |RegisterOnNextWriteSynchronousCallback|, on the file writer's sequence to | |
372 // ensure that | |
373 // it is run even if Chrome is shutting down. | |
gab
2016/09/20 17:30:12
I meant more something like:
// If there already
proberge
2016/09/20 18:03:28
Done.
| |
374 if (!has_pending_write_callback_) { | |
375 writer_.RegisterOnNextWriteCallback(base::Bind( | |
376 &PostWriteCallback, | |
377 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, | |
378 AsWeakPtr()), | |
379 base::Callback<void(bool success)>(), | |
380 base::SequencedTaskRunnerHandle::Get())); | |
381 } | |
382 } | |
383 | |
384 void JsonPrefStore::RegisterOnNextWriteSynchronousCallback( | |
385 const base::Callback<void(bool success)>& on_next_write_callback) { | |
386 DCHECK(CalledOnValidThread()); | |
387 DCHECK(!has_pending_write_callback_); | |
388 | |
389 has_pending_write_callback_ = true; | |
390 | |
391 writer_.RegisterOnNextWriteCallback(base::Bind( | |
392 &PostWriteCallback, | |
393 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, | |
394 AsWeakPtr()), | |
395 on_next_write_callback, base::SequencedTaskRunnerHandle::Get())); | |
331 } | 396 } |
332 | 397 |
333 void JsonPrefStore::ClearMutableValues() { | 398 void JsonPrefStore::ClearMutableValues() { |
334 NOTIMPLEMENTED(); | 399 NOTIMPLEMENTED(); |
335 } | 400 } |
336 | 401 |
337 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { | 402 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { |
338 DCHECK(CalledOnValidThread()); | 403 DCHECK(CalledOnValidThread()); |
339 | 404 |
340 DCHECK(read_result); | 405 DCHECK(read_result); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
533 DCHECK_EQ(31, num_buckets); | 598 DCHECK_EQ(31, num_buckets); |
534 | 599 |
535 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 600 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
536 // macro adapted to allow for a dynamically suffixed histogram name. | 601 // macro adapted to allow for a dynamically suffixed histogram name. |
537 // Note: The factory creates and owns the histogram. | 602 // Note: The factory creates and owns the histogram. |
538 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 603 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
539 histogram_name, min_value, max_value, num_buckets, | 604 histogram_name, min_value, max_value, num_buckets, |
540 base::HistogramBase::kUmaTargetedHistogramFlag); | 605 base::HistogramBase::kUmaTargetedHistogramFlag); |
541 return histogram; | 606 return histogram; |
542 } | 607 } |
OLD | NEW |