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