| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/sync/glue/bookmark_model_worker.h" | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "base/waitable_event.h" | |
| 9 | |
| 10 namespace browser_sync { | |
| 11 | |
| 12 void BookmarkModelWorker::CallDoWorkFromModelSafeThreadAndWait( | |
| 13 ModelSafeWorkerInterface::Visitor* visitor) { | |
| 14 // It is possible this gets called when we are in the STOPPING state, because | |
| 15 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. | |
| 16 // This is fine, the work will get scheduled and run normally or run by our | |
| 17 // code handling this case in Stop(). | |
| 18 DCHECK_NE(state_, STOPPED); | |
| 19 if (state_ == STOPPED) | |
| 20 return; | |
| 21 if (MessageLoop::current() == bookmark_model_loop_) { | |
| 22 DLOG(WARNING) << "CallDoWorkFromModelSafeThreadAndWait called from " | |
| 23 << "bookmark_model_loop_. Probably a nested invocation?"; | |
| 24 visitor->DoWork(); | |
| 25 return; | |
| 26 } | |
| 27 | |
| 28 // Create an unsignaled event to wait on. | |
| 29 base::WaitableEvent work_done(false, false); | |
| 30 { | |
| 31 // We lock only to avoid PostTask'ing a NULL pending_work_ (because it | |
| 32 // could get Run() in Stop() and call OnTaskCompleted before we post). | |
| 33 // The task is owned by the message loop as per usual. | |
| 34 AutoLock lock(lock_); | |
| 35 DCHECK(!pending_work_); | |
| 36 pending_work_ = new CallDoWorkAndSignalTask(visitor, &work_done, this); | |
| 37 bookmark_model_loop_->PostTask(FROM_HERE, pending_work_); | |
| 38 } | |
| 39 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. | |
| 40 work_done.Wait(); | |
| 41 } | |
| 42 | |
| 43 BookmarkModelWorker::~BookmarkModelWorker() { | |
| 44 DCHECK_EQ(state_, STOPPED); | |
| 45 } | |
| 46 | |
| 47 void BookmarkModelWorker::OnSyncerShutdownComplete() { | |
| 48 AutoLock lock(lock_); | |
| 49 // The SyncerThread has terminated and we are no longer needed by syncapi. | |
| 50 // The UI loop initiated shutdown and is (or will be) waiting in Stop(). | |
| 51 // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending | |
| 52 // on where we timeslice the UI thread in Stop; but we can't be STOPPED, | |
| 53 // because that would imply OnSyncerShutdownComplete already signaled. | |
| 54 DCHECK_NE(state_, STOPPED); | |
| 55 | |
| 56 syncapi_has_shutdown_ = true; | |
| 57 syncapi_event_.Signal(); | |
| 58 } | |
| 59 | |
| 60 void BookmarkModelWorker::Stop() { | |
| 61 DCHECK_EQ(MessageLoop::current(), bookmark_model_loop_); | |
| 62 | |
| 63 AutoLock lock(lock_); | |
| 64 DCHECK_EQ(state_, WORKING); | |
| 65 | |
| 66 // We're on our own now, the beloved UI MessageLoop is no longer running. | |
| 67 // Any tasks scheduled or to be scheduled on the UI MessageLoop will not run. | |
| 68 state_ = RUNNING_MANUAL_SHUTDOWN_PUMP; | |
| 69 | |
| 70 // Drain any final tasks manually until the SyncerThread tells us it has | |
| 71 // totally finished. There should only ever be 0 or 1 tasks Run() here. | |
| 72 while (!syncapi_has_shutdown_) { | |
| 73 if (pending_work_) | |
| 74 pending_work_->Run(); // OnTaskCompleted will set pending_work_ to NULL. | |
| 75 | |
| 76 // Wait for either a new task or SyncerThread termination. | |
| 77 syncapi_event_.Wait(); | |
| 78 } | |
| 79 | |
| 80 state_ = STOPPED; | |
| 81 } | |
| 82 | |
| 83 void BookmarkModelWorker::CallDoWorkAndSignalTask::Run() { | |
| 84 if (!visitor_) { | |
| 85 // This can happen during tests or cases where there are more than just the | |
| 86 // default BookmarkModelWorker in existence and it gets destroyed before | |
| 87 // the main UI loop has terminated. There is no easy way to assert the | |
| 88 // loop is running / not running at the moment, so we just provide cancel | |
| 89 // semantics here and short-circuit. | |
| 90 // TODO(timsteele): Maybe we should have the message loop destruction | |
| 91 // observer fire when the loop has ended, just a bit before it | |
| 92 // actually gets destroyed. | |
| 93 return; | |
| 94 } | |
| 95 visitor_->DoWork(); | |
| 96 | |
| 97 // Sever ties with visitor_ to allow the sanity-checking above that we don't | |
| 98 // get run twice. | |
| 99 visitor_ = NULL; | |
| 100 | |
| 101 // Notify the BookmarkModelWorker that scheduled us that we have run | |
| 102 // successfully. | |
| 103 scheduler_->OnTaskCompleted(); | |
| 104 work_done_->Signal(); // Unblock the syncer thread that scheduled us. | |
| 105 } | |
| 106 | |
| 107 } // namespace browser_sync | |
| OLD | NEW |