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

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: Address comments on #13 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
327 const base::Closure& on_next_successful_write) { 330 void JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback(
328 DCHECK(CalledOnValidThread()); 331 base::WeakPtr<JsonPrefStore> calling_store,
332 bool write_success) {
333 if (!calling_store)
334 return;
329 335
330 writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); 336 DCHECK(calling_store->CalledOnValidThread());
337
338 calling_store->has_pending_write_callback_ = false;
339 if (calling_store->has_pending_successful_write_reply_) {
340 calling_store->has_pending_successful_write_reply_ = false;
341 if (write_success) {
342 calling_store->on_next_successful_write_reply_.Run();
343 } else {
344 calling_store->RegisterOnNextSuccessfulWriteReply(
345 calling_store->on_next_successful_write_reply_);
346 }
347 }
348 }
349
350 // static
351 void JsonPrefStore::PostWriteCallback(
352 base::WeakPtr<JsonPrefStore> calling_store,
gab 2016/09/20 01:27:42 Since this method should be using member state, it
proberge 2016/09/20 15:15:53 Done.
353 const base::Callback<void(bool success)>& on_next_write,
354 scoped_refptr<base::SequencedTaskRunner> reply_task_runner,
355 bool write_success) {
356 if (!on_next_write.is_null()) {
357 on_next_write.Run(write_success);
358 }
359
360 // We can't use |calling_store| on the current thread. Bounce back to the
361 // |reply_task_runner| which is the correct sequenced thread.
362 reply_task_runner->PostTask(
363 FROM_HERE, base::Bind(&RunOrScheduleNextSuccessfulWriteCallback,
364 calling_store, write_success));
365 }
366
367 void JsonPrefStore::RegisterOnNextSuccessfulWriteReply(
368 const base::Closure& on_next_successful_write_reply) {
gab 2016/09/20 01:27:41 DCHECK(CalledOnValidThread());
proberge 2016/09/20 15:15:52 Done.
369 DCHECK(!has_pending_successful_write_reply_);
370
371 has_pending_successful_write_reply_ = true;
372 on_next_successful_write_reply_ = on_next_successful_write_reply;
373
374 if (!has_pending_write_callback_) {
gab 2016/09/20 01:27:42 Add a meta comment here (or elsewhere but this see
proberge 2016/09/20 15:15:53 Done.
375 writer_.RegisterOnNextWriteCallback(
376 base::Bind(&PostWriteCallback, this->AsWeakPtr(),
377 base::Callback<void(bool success)>(),
378 base::SequencedTaskRunnerHandle::Get()));
379 }
380 }
381
382 void JsonPrefStore::RegisterOnNextWriteCallback(
383 const base::Callback<void(bool success)>& on_next_write_callback) {
gab 2016/09/20 01:27:42 DCHECK(CalledOnValidThread());
proberge 2016/09/20 15:15:52 Done.
384 DCHECK(!has_pending_write_callback_);
385
386 has_pending_write_callback_ = true;
387
388 writer_.RegisterOnNextWriteCallback(
389 base::Bind(&PostWriteCallback, this->AsWeakPtr(), on_next_write_callback,
gab 2016/09/20 01:27:41 AsWeakPtr() (no need to explicitly add "this->")
proberge 2016/09/20 15:15:53 Done.
390 base::SequencedTaskRunnerHandle::Get()));
331 } 391 }
332 392
333 void JsonPrefStore::ClearMutableValues() { 393 void JsonPrefStore::ClearMutableValues() {
334 NOTIMPLEMENTED(); 394 NOTIMPLEMENTED();
335 } 395 }
336 396
337 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { 397 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) {
338 DCHECK(CalledOnValidThread()); 398 DCHECK(CalledOnValidThread());
339 399
340 DCHECK(read_result); 400 DCHECK(read_result);
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 DCHECK_EQ(31, num_buckets); 593 DCHECK_EQ(31, num_buckets);
534 594
535 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS 595 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS
536 // macro adapted to allow for a dynamically suffixed histogram name. 596 // macro adapted to allow for a dynamically suffixed histogram name.
537 // Note: The factory creates and owns the histogram. 597 // Note: The factory creates and owns the histogram.
538 base::HistogramBase* histogram = base::Histogram::FactoryGet( 598 base::HistogramBase* histogram = base::Histogram::FactoryGet(
539 histogram_name, min_value, max_value, num_buckets, 599 histogram_name, min_value, max_value, num_buckets,
540 base::HistogramBase::kUmaTargetedHistogramFlag); 600 base::HistogramBase::kUmaTargetedHistogramFlag);
541 return histogram; 601 return histogram;
542 } 602 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698