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 "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 | 13 |
14 using content::BrowserThread; | 14 using content::BrowserThread; |
15 | 15 |
16 namespace browser_sync { | 16 namespace browser_sync { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // A simple callback to signal a waitable event after running a closure. | 20 // A simple callback to signal a waitable event after running a closure. |
21 void CallDoWorkAndSignalCallback(const WorkCallback& work, | 21 void CallDoWorkAndSignalCallback(const WorkCallback& work, |
22 base::WaitableEvent* work_done, | 22 base::WaitableEvent* work_done, |
23 UIModelWorker* const scheduler, | 23 UIModelWorker* const scheduler, |
24 UnrecoverableErrorInfo* error_info) { | 24 SyncerError* error_info) { |
25 if (work.is_null()) { | 25 if (work.is_null()) { |
26 // This can happen during tests or cases where there are more than just the | 26 // This can happen during tests or cases where there are more than just the |
27 // default UIModelWorker in existence and it gets destroyed before | 27 // default UIModelWorker in existence and it gets destroyed before |
28 // the main UI loop has terminated. There is no easy way to assert the | 28 // the main UI loop has terminated. There is no easy way to assert the |
29 // loop is running / not running at the moment, so we just provide cancel | 29 // loop is running / not running at the moment, so we just provide cancel |
30 // semantics here and short-circuit. | 30 // semantics here and short-circuit. |
31 // TODO(timsteele): Maybe we should have the message loop destruction | 31 // TODO(timsteele): Maybe we should have the message loop destruction |
32 // observer fire when the loop has ended, just a bit before it | 32 // observer fire when the loop has ended, just a bit before it |
33 // actually gets destroyed. | 33 // actually gets destroyed. |
34 return; | 34 return; |
35 } | 35 } |
36 | 36 |
37 *error_info = work.Run(); | 37 *error_info = work.Run(); |
38 | 38 |
39 // Notify the UIModelWorker that scheduled us that we have run | 39 // Notify the UIModelWorker that scheduled us that we have run |
40 // successfully. | 40 // successfully. |
41 scheduler->OnTaskCompleted(); | 41 scheduler->OnTaskCompleted(); |
42 work_done->Signal(); // Unblock the syncer thread that scheduled us. | 42 work_done->Signal(); // Unblock the syncer thread that scheduled us. |
43 } | 43 } |
44 | 44 |
45 } // namespace | 45 } // namespace |
46 | 46 |
47 UnrecoverableErrorInfo UIModelWorker::DoWorkAndWaitUntilDone( | 47 SyncerError UIModelWorker::DoWorkAndWaitUntilDone( |
48 const WorkCallback& work) { | 48 const WorkCallback& work) { |
49 // In most cases, this method is called in WORKING state. It is possible this | 49 // In most cases, this method is called in WORKING state. It is possible this |
50 // gets called when we are in the RUNNING_MANUAL_SHUTDOWN_PUMP state, because | 50 // gets called when we are in the RUNNING_MANUAL_SHUTDOWN_PUMP state, because |
51 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. | 51 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. |
52 // This is fine, the work will get scheduled and run normally or run by our | 52 // This is fine, the work will get scheduled and run normally or run by our |
53 // code handling this case in Stop(). Note there _no_ way we can be in here | 53 // code handling this case in Stop(). Note there _no_ way we can be in here |
54 // with state_ = STOPPED, so it is safe to read / compare in this case. | 54 // with state_ = STOPPED, so it is safe to read / compare in this case. |
55 CHECK_NE(ANNOTATE_UNPROTECTED_READ(state_), STOPPED); | 55 CHECK_NE(ANNOTATE_UNPROTECTED_READ(state_), STOPPED); |
56 UnrecoverableErrorInfo error_info; | 56 SyncerError error_info; |
57 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 57 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
58 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " | 58 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " |
59 << "ui_loop_. Probably a nested invocation?"; | 59 << "ui_loop_. Probably a nested invocation?"; |
60 return work.Run(); | 60 return work.Run(); |
61 } | 61 } |
62 | 62 |
63 // Create an unsignaled event to wait on. | 63 // Create an unsignaled event to wait on. |
64 base::WaitableEvent work_done(false, false); | 64 base::WaitableEvent work_done(false, false); |
65 { | 65 { |
66 // We lock only to avoid PostTask'ing a NULL pending_work_ (because it | 66 // We lock only to avoid PostTask'ing a NULL pending_work_ (because it |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 126 } |
127 | 127 |
128 state_ = STOPPED; | 128 state_ = STOPPED; |
129 } | 129 } |
130 | 130 |
131 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | 131 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
132 return GROUP_UI; | 132 return GROUP_UI; |
133 } | 133 } |
134 | 134 |
135 } // namespace browser_sync | 135 } // namespace browser_sync |
OLD | NEW |