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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/prefs/json_pref_store.cc
diff --git a/components/prefs/json_pref_store.cc b/components/prefs/json_pref_store.cc
index 779c2009730366aba4d8fae689d340b2fb81ad71..f8bca6a6047482b00a29616c1bdf385c229e18f8 100644
--- a/components/prefs/json_pref_store.cc
+++ b/components/prefs/json_pref_store.cc
@@ -22,6 +22,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task_runner_util.h"
+#include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/time/default_clock.h"
#include "base/values.h"
@@ -170,6 +171,8 @@ JsonPrefStore::JsonPrefStore(
initialized_(false),
filtering_in_progress_(false),
pending_lossy_write_(false),
+ has_pending_successful_write_reply_(false),
+ has_pending_write_callback_(false),
read_error_(PREF_READ_ERROR_NONE),
write_count_histogram_(writer_.commit_interval(), path_) {
DCHECK(!path_.empty());
@@ -323,11 +326,68 @@ void JsonPrefStore::ReportValueChanged(const std::string& key, uint32_t flags) {
ScheduleWrite(flags);
}
-void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback(
- const base::Closure& on_next_successful_write) {
- DCHECK(CalledOnValidThread());
+// static
+void JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback(
+ base::WeakPtr<JsonPrefStore> calling_store,
+ bool write_success) {
+ if (!calling_store)
+ return;
+
+ DCHECK(calling_store->CalledOnValidThread());
+
+ calling_store->has_pending_write_callback_ = false;
+ if (calling_store->has_pending_successful_write_reply_) {
+ calling_store->has_pending_successful_write_reply_ = false;
+ if (write_success) {
+ calling_store->on_next_successful_write_reply_.Run();
+ } else {
+ calling_store->RegisterOnNextSuccessfulWriteReply(
+ calling_store->on_next_successful_write_reply_);
+ }
+ }
+}
+
+// static
+void JsonPrefStore::PostWriteCallback(
+ 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.
+ const base::Callback<void(bool success)>& on_next_write,
+ scoped_refptr<base::SequencedTaskRunner> reply_task_runner,
+ bool write_success) {
+ if (!on_next_write.is_null()) {
+ on_next_write.Run(write_success);
+ }
+
+ // We can't use |calling_store| on the current thread. Bounce back to the
+ // |reply_task_runner| which is the correct sequenced thread.
+ reply_task_runner->PostTask(
+ FROM_HERE, base::Bind(&RunOrScheduleNextSuccessfulWriteCallback,
+ calling_store, write_success));
+}
+
+void JsonPrefStore::RegisterOnNextSuccessfulWriteReply(
+ 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.
+ DCHECK(!has_pending_successful_write_reply_);
+
+ has_pending_successful_write_reply_ = true;
+ on_next_successful_write_reply_ = on_next_successful_write_reply;
+
+ 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.
+ writer_.RegisterOnNextWriteCallback(
+ base::Bind(&PostWriteCallback, this->AsWeakPtr(),
+ base::Callback<void(bool success)>(),
+ base::SequencedTaskRunnerHandle::Get()));
+ }
+}
+
+void JsonPrefStore::RegisterOnNextWriteCallback(
+ 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.
+ DCHECK(!has_pending_write_callback_);
+
+ has_pending_write_callback_ = true;
- writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write);
+ writer_.RegisterOnNextWriteCallback(
+ 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.
+ base::SequencedTaskRunnerHandle::Get()));
}
void JsonPrefStore::ClearMutableValues() {

Powered by Google App Engine
This is Rietveld 408576698