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