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

Unified Diff: chrome/browser/sync/glue/bookmark_model_worker.cc

Issue 160598: Add files to browser/sync. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 4 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: chrome/browser/sync/glue/bookmark_model_worker.cc
===================================================================
--- chrome/browser/sync/glue/bookmark_model_worker.cc (revision 0)
+++ chrome/browser/sync/glue/bookmark_model_worker.cc (revision 0)
@@ -0,0 +1,114 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifdef CHROME_PERSONALIZATION
+
+#include "chrome/browser/sync/glue/bookmark_model_worker.h"
+
+#include "base/message_loop.h"
+#include "base/waitable_event.h"
+
+namespace browser_sync {
+
+void BookmarkModelWorker::CallDoWorkFromModelSafeThreadAndWait(
+ ModelSafeWorkerInterface::Visitor* visitor) {
+ // It is possible this gets called when we are in the STOPPING state, because
+ // the UI loop has initiated shutdown but the syncer hasn't got the memo yet.
+ // This is fine, the work will get scheduled and run normally or run by our
+ // code handling this case in Stop().
+ DCHECK_NE(state_, STOPPED);
+ if (state_ == STOPPED)
+ return;
+ if (MessageLoop::current() == bookmark_model_loop_) {
+ DLOG(WARNING) << "CallDoWorkFromModelSafeThreadAndWait called from "
+ << "bookmark_model_loop_. Probably a nested invocation?";
+ visitor->DoWork();
+ return;
+ }
+
+ // Create an unsignaled event to wait on.
+ base::WaitableEvent work_done(false, false);
+ {
+ // We lock only to avoid PostTask'ing a NULL pending_work_ (because it
+ // could get Run() in Stop() and call OnTaskCompleted before we post).
+ // The task is owned by the message loop as per usual.
+ AutoLock lock(pending_work_lock_);
+ DCHECK(!pending_work_);
+ pending_work_ = new CallDoWorkAndSignalTask(visitor, &work_done, this);
+ bookmark_model_loop_->PostTask(FROM_HERE, pending_work_);
+ }
+ syncapi_event_.Signal(); // Notify that the syncapi produced work for us.
+ work_done.Wait();
+}
+
+BookmarkModelWorker::~BookmarkModelWorker() {
+ DCHECK_EQ(state_, STOPPED);
+}
+
+void BookmarkModelWorker::OnSyncerShutdownComplete() {
+ // The SyncerThread has terminated and we are no longer needed by syncapi.
+ // The UI loop initiated shutdown and is (or will be) waiting in Stop().
+ // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending
+ // on where we timeslice the UI thread in Stop; but we can't be STOPPED,
+ // because that would imply NotifySyncapiShutdownComplete already signaled.
+ DCHECK_NE(state_, STOPPED);
+
+ syncapi_has_shutdown_ = true;
+ syncapi_event_.Signal();
+}
+
+void BookmarkModelWorker::Stop() {
+ DCHECK_EQ(MessageLoop::current(), bookmark_model_loop_);
+ DCHECK_EQ(state_, WORKING);
+
+ // We're on our own now, the beloved UI MessageLoop is no longer running.
+ // Any tasks scheduled or to be scheduled on the UI MessageLoop will not run.
+ state_ = RUNNING_MANUAL_SHUTDOWN_PUMP;
+
+ // Drain any final task manually until the SyncerThread tells us it has
+ // totally finished. Note we use a 'while' loop and not 'if'. The main subtle
+ // reason for this is that syncapi_event could be signaled the first time we
+ // come through due to an old CallDoWork call, and we need to keep looping
+ // until the SyncerThread either calls it again or tells us it is done. There
+ // should only ever be 0 or 1 tasks Run() here, however.
+ while (!syncapi_has_shutdown_) {
+ {
+ AutoLock lock(pending_work_lock_);
+ if (pending_work_)
+ pending_work_->Run();
+ }
+ syncapi_event_.Wait(); // Signaled either by new task, or SyncerThread
+ // termination.
+ }
+
+ state_ = STOPPED;
+}
+
+void BookmarkModelWorker::CallDoWorkAndSignalTask::Run() {
+ if (!visitor_) {
+ // This can happen during tests or cases where there are more than just the
+ // default BookmarkModelWorker in existence and it gets destroyed before
+ // the main UI loop has terminated. There is no easy way to assert the
+ // loop is running / not running at the moment, so we just provide cancel
+ // semantics here and short-circuit.
+ // TODO(timsteele): Maybe we should have the message loop destruction
+ // observer fire when the loop has ended, just a bit before it
+ // actually gets destroyed.
+ return;
+ }
+ visitor_->DoWork();
+
+ // Sever ties with visitor_ to allow the sanity-checking above that we don't
+ // get run twice.
+ visitor_ = NULL;
+
+ // Notify the BookmarkModelWorker that scheduled us that we have run
+ // successfully.
+ scheduler_->OnTaskCompleted();
+ work_done_->Signal(); // Unblock the syncer thread that scheduled us.
+}
+
+} // namespace browser_sync
+
+#endif // CHROME_PERSONALIZATION
Property changes on: chrome\browser\sync\glue\bookmark_model_worker.cc
___________________________________________________________________
Added: svn:eol-style
+ LF
« no previous file with comments | « chrome/browser/sync/glue/bookmark_model_worker.h ('k') | chrome/browser/sync/glue/bookmark_model_worker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698