OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 8 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "content/browser/browser_thread.h" | 10 #include "content/browser/browser_thread.h" |
11 | 11 |
12 namespace browser_sync { | 12 namespace browser_sync { |
13 | 13 |
14 void UIModelWorker::DoWorkAndWaitUntilDone(Callback0::Type* work) { | 14 UnrecoverableErrorInfo UIModelWorker::DoWorkAndWaitUntilDone( |
| 15 const WorkCallback& work) { |
15 // In most cases, this method is called in WORKING state. It is possible this | 16 // In most cases, this method is called in WORKING state. It is possible this |
16 // gets called when we are in the RUNNING_MANUAL_SHUTDOWN_PUMP state, because | 17 // gets called when we are in the RUNNING_MANUAL_SHUTDOWN_PUMP state, because |
17 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. | 18 // 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 // This is fine, the work will get scheduled and run normally or run by our |
19 // code handling this case in Stop(). Note there _no_ way we can be in here | 20 // code handling this case in Stop(). Note there _no_ way we can be in here |
20 // with state_ = STOPPED, so it is safe to read / compare in this case. | 21 // with state_ = STOPPED, so it is safe to read / compare in this case. |
21 CHECK_NE(ANNOTATE_UNPROTECTED_READ(state_), STOPPED); | 22 CHECK_NE(ANNOTATE_UNPROTECTED_READ(state_), STOPPED); |
22 | 23 UnrecoverableErrorInfo error_info; |
23 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 24 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
24 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " | 25 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " |
25 << "ui_loop_. Probably a nested invocation?"; | 26 << "ui_loop_. Probably a nested invocation?"; |
26 work->Run(); | 27 return work.Run(); |
27 return; | |
28 } | 28 } |
29 | 29 |
30 // Create an unsignaled event to wait on. | 30 // Create an unsignaled event to wait on. |
31 base::WaitableEvent work_done(false, false); | 31 base::WaitableEvent work_done(false, false); |
32 { | 32 { |
33 // We lock only to avoid PostTask'ing a NULL pending_work_ (because it | 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). | 34 // could get Run() in Stop() and call OnTaskCompleted before we post). |
35 // The task is owned by the message loop as per usual. | 35 // The task is owned by the message loop as per usual. |
36 base::AutoLock lock(lock_); | 36 base::AutoLock lock(lock_); |
37 DCHECK(!pending_work_); | 37 DCHECK(!pending_work_); |
38 pending_work_ = new CallDoWorkAndSignalTask(work, &work_done, this); | 38 UnrecoverableErrorInfo error_info; |
| 39 pending_work_ = new CallDoWorkAndSignalTask(work, &work_done, this, |
| 40 &error_info); |
39 if (!BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, pending_work_)) { | 41 if (!BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, pending_work_)) { |
40 LOG(WARNING) << "Could not post work to UI loop."; | 42 LOG(WARNING) << "Could not post work to UI loop."; |
41 pending_work_ = NULL; | 43 pending_work_ = NULL; |
42 syncapi_event_.Signal(); | 44 syncapi_event_.Signal(); |
43 return; | 45 return error_info; |
44 } | 46 } |
45 } | 47 } |
46 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. | 48 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. |
47 work_done.Wait(); | 49 work_done.Wait(); |
| 50 return error_info; |
48 } | 51 } |
49 | 52 |
50 UIModelWorker::UIModelWorker() | 53 UIModelWorker::UIModelWorker() |
51 : state_(WORKING), | 54 : state_(WORKING), |
52 pending_work_(NULL), | 55 pending_work_(NULL), |
53 syncapi_has_shutdown_(false), | 56 syncapi_has_shutdown_(false), |
54 syncapi_event_(&lock_) { | 57 syncapi_event_(&lock_) { |
55 } | 58 } |
56 | 59 |
57 UIModelWorker::~UIModelWorker() { | 60 UIModelWorker::~UIModelWorker() { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 } | 95 } |
93 | 96 |
94 state_ = STOPPED; | 97 state_ = STOPPED; |
95 } | 98 } |
96 | 99 |
97 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | 100 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
98 return GROUP_UI; | 101 return GROUP_UI; |
99 } | 102 } |
100 | 103 |
101 void UIModelWorker::CallDoWorkAndSignalTask::Run() { | 104 void UIModelWorker::CallDoWorkAndSignalTask::Run() { |
102 if (!work_) { | 105 if (work_.is_null()) { |
103 // This can happen during tests or cases where there are more than just the | 106 // This can happen during tests or cases where there are more than just the |
104 // default UIModelWorker in existence and it gets destroyed before | 107 // default UIModelWorker in existence and it gets destroyed before |
105 // the main UI loop has terminated. There is no easy way to assert the | 108 // the main UI loop has terminated. There is no easy way to assert the |
106 // loop is running / not running at the moment, so we just provide cancel | 109 // loop is running / not running at the moment, so we just provide cancel |
107 // semantics here and short-circuit. | 110 // semantics here and short-circuit. |
108 // TODO(timsteele): Maybe we should have the message loop destruction | 111 // TODO(timsteele): Maybe we should have the message loop destruction |
109 // observer fire when the loop has ended, just a bit before it | 112 // observer fire when the loop has ended, just a bit before it |
110 // actually gets destroyed. | 113 // actually gets destroyed. |
111 return; | 114 return; |
112 } | 115 } |
113 work_->Run(); | 116 *error_info_ = work_.Run(); |
114 | 117 |
115 // Sever ties with work_ to allow the sanity-checking above that we don't | 118 // Sever ties with work_ to allow the sanity-checking above that we don't |
116 // get run twice. | 119 // get run twice. |
117 work_ = NULL; | 120 work_.Reset(); |
118 | 121 |
119 // Notify the UIModelWorker that scheduled us that we have run | 122 // Notify the UIModelWorker that scheduled us that we have run |
120 // successfully. | 123 // successfully. |
121 scheduler_->OnTaskCompleted(); | 124 scheduler_->OnTaskCompleted(); |
122 work_done_->Signal(); // Unblock the syncer thread that scheduled us. | 125 work_done_->Signal(); // Unblock the syncer thread that scheduled us. |
123 } | 126 } |
124 | 127 |
125 } // namespace browser_sync | 128 } // namespace browser_sync |
OLD | NEW |