Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: components/prefs/json_pref_store.cc

Issue 2299523003: Add synchronous callback support to important_file_writer.cc (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Try move logic from ImportantFileWriter to JsonPrefStore Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 // Static
gab 2016/09/19 18:01:30 "static" (lower-case)
proberge 2016/09/19 21:24:25 Done.
327 const base::Closure& on_next_successful_write) { 330 void JsonPrefStore::PostWriteCallback(
328 DCHECK(CalledOnValidThread()); 331 base::WeakPtr<JsonPrefStore> calling_store,
332 const base::Callback<void(bool success)>& on_next_write,
333 scoped_refptr<base::SequencedTaskRunner> reply_task_runner,
334 bool write_success) {
335 if (!on_next_write.is_null()) {
336 on_next_write.Run(write_success);
337 }
329 338
330 writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); 339 // The calling JsonPrefStore can be null when the current profile is being
340 // destroyed (for example during shutdown).
341 if (calling_store) {
342 calling_store->has_pending_write_callback_ = false;
gab 2016/09/19 18:01:30 Member state can only be modified on the owning se
proberge 2016/09/19 21:24:25 Hmm. Not sure if this is better than locks. PTAL.
gab 2016/09/20 01:27:41 Yes, much better than locks :-), well done!
343
344 if (calling_store->has_pending_successful_write_reply_) {
345 calling_store->has_pending_successful_write_reply_ = false;
346 if (write_success) {
347 reply_task_runner->PostTask(
348 FROM_HERE, calling_store->on_next_successful_write_reply_);
349 } else {
350 calling_store->RegisterOnNextSuccessfulWriteReply(
351 calling_store->on_next_successful_write_reply_);
352 }
353 }
354 }
355 }
356
357 void JsonPrefStore::RegisterOnNextSuccessfulWriteReply(
358 const base::Closure& on_next_successful_write_reply_reply) {
gab 2016/09/19 18:01:30 s/on_next_successful_write_reply_reply/on_next_suc
proberge 2016/09/19 21:24:25 Done.
359 DCHECK(!has_pending_successful_write_reply_);
360
361 has_pending_successful_write_reply_ = true;
362 on_next_successful_write_reply_ = on_next_successful_write_reply_reply;
363
364 if (!has_pending_write_callback_) {
365 writer_.RegisterOnNextWriteCallback(
366 base::Bind(&PostWriteCallback, this->AsWeakPtr(),
367 base::Callback<void(bool success)>(),
368 base::SequencedTaskRunnerHandle::Get()));
369 }
370 }
371
372 void JsonPrefStore::RegisterOnNextWriteCallback(
373 const base::Callback<void(bool success)>& on_next_write_callback) {
374 DCHECK(!has_pending_write_callback_);
375
376 has_pending_write_callback_ = true;
377
378 writer_.RegisterOnNextWriteCallback(
379 base::Bind(&PostWriteCallback, this->AsWeakPtr(), on_next_write_callback,
380 base::SequencedTaskRunnerHandle::Get()));
331 } 381 }
332 382
333 void JsonPrefStore::ClearMutableValues() { 383 void JsonPrefStore::ClearMutableValues() {
334 NOTIMPLEMENTED(); 384 NOTIMPLEMENTED();
335 } 385 }
336 386
337 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { 387 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) {
338 DCHECK(CalledOnValidThread()); 388 DCHECK(CalledOnValidThread());
339 389
340 DCHECK(read_result); 390 DCHECK(read_result);
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 DCHECK_EQ(31, num_buckets); 583 DCHECK_EQ(31, num_buckets);
534 584
535 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS 585 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS
536 // macro adapted to allow for a dynamically suffixed histogram name. 586 // macro adapted to allow for a dynamically suffixed histogram name.
537 // Note: The factory creates and owns the histogram. 587 // Note: The factory creates and owns the histogram.
538 base::HistogramBase* histogram = base::Histogram::FactoryGet( 588 base::HistogramBase* histogram = base::Histogram::FactoryGet(
539 histogram_name, min_value, max_value, num_buckets, 589 histogram_name, min_value, max_value, num_buckets,
540 base::HistogramBase::kUmaTargetedHistogramFlag); 590 base::HistogramBase::kUmaTargetedHistogramFlag);
541 return histogram; 591 return histogram;
542 } 592 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698