| OLD | NEW |
| 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 "chrome/browser/sync/glue/ui_model_worker.h" | 5 #include "chrome/browser/sync/glue/ui_model_worker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 | 14 |
| 15 using content::BrowserThread; | 15 using content::BrowserThread; |
| 16 | 16 |
| 17 namespace browser_sync { | 17 namespace browser_sync { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // A simple callback to signal a waitable event after running a closure. | 21 // A simple callback to signal a waitable event after running a closure. |
| 22 void CallDoWorkAndSignalCallback(const csync::WorkCallback& work, | 22 void CallDoWorkAndSignalCallback(const syncer::WorkCallback& work, |
| 23 base::WaitableEvent* work_done, | 23 base::WaitableEvent* work_done, |
| 24 UIModelWorker* const scheduler, | 24 UIModelWorker* const scheduler, |
| 25 csync::SyncerError* error_info) { | 25 syncer::SyncerError* error_info) { |
| 26 if (work.is_null()) { | 26 if (work.is_null()) { |
| 27 // This can happen during tests or cases where there are more than just the | 27 // This can happen during tests or cases where there are more than just the |
| 28 // default UIModelWorker in existence and it gets destroyed before | 28 // default UIModelWorker in existence and it gets destroyed before |
| 29 // the main UI loop has terminated. There is no easy way to assert the | 29 // the main UI loop has terminated. There is no easy way to assert the |
| 30 // loop is running / not running at the moment, so we just provide cancel | 30 // loop is running / not running at the moment, so we just provide cancel |
| 31 // semantics here and short-circuit. | 31 // semantics here and short-circuit. |
| 32 // TODO(timsteele): Maybe we should have the message loop destruction | 32 // TODO(timsteele): Maybe we should have the message loop destruction |
| 33 // observer fire when the loop has ended, just a bit before it | 33 // observer fire when the loop has ended, just a bit before it |
| 34 // actually gets destroyed. | 34 // actually gets destroyed. |
| 35 return; | 35 return; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 69 |
| 70 // http://crbug.com/19757 | 70 // http://crbug.com/19757 |
| 71 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 71 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 72 // Wait for either a new task or SyncerThread termination. | 72 // Wait for either a new task or SyncerThread termination. |
| 73 syncapi_event_.Wait(); | 73 syncapi_event_.Wait(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 state_ = STOPPED; | 76 state_ = STOPPED; |
| 77 } | 77 } |
| 78 | 78 |
| 79 csync::SyncerError UIModelWorker::DoWorkAndWaitUntilDone( | 79 syncer::SyncerError UIModelWorker::DoWorkAndWaitUntilDone( |
| 80 const csync::WorkCallback& work) { | 80 const syncer::WorkCallback& work) { |
| 81 // In most cases, this method is called in WORKING state. It is possible this | 81 // In most cases, this method is called in WORKING state. It is possible this |
| 82 // gets called when we are in the RUNNING_MANUAL_SHUTDOWN_PUMP state, because | 82 // gets called when we are in the RUNNING_MANUAL_SHUTDOWN_PUMP state, because |
| 83 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. | 83 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. |
| 84 // This is fine, the work will get scheduled and run normally or run by our | 84 // This is fine, the work will get scheduled and run normally or run by our |
| 85 // code handling this case in Stop(). Note there _no_ way we can be in here | 85 // code handling this case in Stop(). Note there _no_ way we can be in here |
| 86 // with state_ = STOPPED, so it is safe to read / compare in this case. | 86 // with state_ = STOPPED, so it is safe to read / compare in this case. |
| 87 CHECK_NE(ANNOTATE_UNPROTECTED_READ(state_), STOPPED); | 87 CHECK_NE(ANNOTATE_UNPROTECTED_READ(state_), STOPPED); |
| 88 csync::SyncerError error_info; | 88 syncer::SyncerError error_info; |
| 89 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 89 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 90 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " | 90 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " |
| 91 << "ui_loop_. Probably a nested invocation?"; | 91 << "ui_loop_. Probably a nested invocation?"; |
| 92 return work.Run(); | 92 return work.Run(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Create an unsignaled event to wait on. | 95 // Create an unsignaled event to wait on. |
| 96 base::WaitableEvent work_done(false, false); | 96 base::WaitableEvent work_done(false, false); |
| 97 { | 97 { |
| 98 // We lock only to avoid PostTask'ing a NULL pending_work_ (because it | 98 // We lock only to avoid PostTask'ing a NULL pending_work_ (because it |
| 99 // could get Run() in Stop() and call OnTaskCompleted before we post). | 99 // could get Run() in Stop() and call OnTaskCompleted before we post). |
| 100 // The task is owned by the message loop as per usual. | 100 // The task is owned by the message loop as per usual. |
| 101 base::AutoLock lock(lock_); | 101 base::AutoLock lock(lock_); |
| 102 DCHECK(pending_work_.is_null()); | 102 DCHECK(pending_work_.is_null()); |
| 103 pending_work_ = base::Bind(&CallDoWorkAndSignalCallback, work, &work_done, | 103 pending_work_ = base::Bind(&CallDoWorkAndSignalCallback, work, &work_done, |
| 104 base::Unretained(this), &error_info); | 104 base::Unretained(this), &error_info); |
| 105 if (!BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, pending_work_)) { | 105 if (!BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, pending_work_)) { |
| 106 LOG(WARNING) << "Could not post work to UI loop."; | 106 LOG(WARNING) << "Could not post work to UI loop."; |
| 107 pending_work_.Reset(); | 107 pending_work_.Reset(); |
| 108 syncapi_event_.Signal(); | 108 syncapi_event_.Signal(); |
| 109 return error_info; | 109 return error_info; |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. | 112 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. |
| 113 work_done.Wait(); | 113 work_done.Wait(); |
| 114 return error_info; | 114 return error_info; |
| 115 } | 115 } |
| 116 | 116 |
| 117 csync::ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | 117 syncer::ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
| 118 return csync::GROUP_UI; | 118 return syncer::GROUP_UI; |
| 119 } | 119 } |
| 120 | 120 |
| 121 void UIModelWorker::OnSyncerShutdownComplete() { | 121 void UIModelWorker::OnSyncerShutdownComplete() { |
| 122 base::AutoLock lock(lock_); | 122 base::AutoLock lock(lock_); |
| 123 // The SyncerThread has terminated and we are no longer needed by syncapi. | 123 // The SyncerThread has terminated and we are no longer needed by syncapi. |
| 124 // The UI loop initiated shutdown and is (or will be) waiting in Stop(). | 124 // The UI loop initiated shutdown and is (or will be) waiting in Stop(). |
| 125 // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending | 125 // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending |
| 126 // on where we timeslice the UI thread in Stop; but we can't be STOPPED, | 126 // on where we timeslice the UI thread in Stop; but we can't be STOPPED, |
| 127 // because that would imply OnSyncerShutdownComplete already signaled. | 127 // because that would imply OnSyncerShutdownComplete already signaled. |
| 128 DCHECK_NE(state_, STOPPED); | 128 DCHECK_NE(state_, STOPPED); |
| 129 | 129 |
| 130 syncapi_has_shutdown_ = true; | 130 syncapi_has_shutdown_ = true; |
| 131 syncapi_event_.Signal(); | 131 syncapi_event_.Signal(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 UIModelWorker::~UIModelWorker() { | 134 UIModelWorker::~UIModelWorker() { |
| 135 DCHECK_EQ(state_, STOPPED); | 135 DCHECK_EQ(state_, STOPPED); |
| 136 } | 136 } |
| 137 | 137 |
| 138 } // namespace browser_sync | 138 } // namespace browser_sync |
| OLD | NEW |