Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: remoting/host/setup/daemon_controller.cc

Issue 23606019: Refactor the daemon controller so that the callbacks are called on the caller thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 #include "remoting/host/setup/daemon_controller.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/values.h"
12 #include "remoting/base/auto_thread.h"
13 #include "remoting/base/auto_thread_task_runner.h"
14
15 namespace remoting {
16
17 // Name of the Daemon Controller's worker thread.
18 const char kDaemonControllerThreadName[] = "Daemon Controller thread";
19
20 DaemonController::DaemonController(scoped_ptr<Delegate> delegate)
21 : caller_task_runner_(base::ThreadTaskRunnerHandle::Get()),
22 delegate_(delegate.Pass()) {
23 // Launch the delegate thread.
24 delegate_thread_.reset(new AutoThread(kDaemonControllerThreadName));
25 #if defined(OS_WIN)
26 delegate_thread_->SetComInitType(AutoThread::COM_INIT_STA);
27 delegate_task_runner_ =
28 delegate_thread_->StartWithType(base::MessageLoop::TYPE_UI);
29 #else
30 delegate_task_runner_ =
31 delegate_thread_->StartWithType(base::MessageLoop::TYPE_DEFAULT);
32 #endif
33 }
34
35 DaemonController::State DaemonController::GetState() {
36 DCHECK(caller_task_runner_->BelongsToCurrentThread());
37 return delegate_->GetState();
38 }
39
40 void DaemonController::GetConfig(const GetConfigCallback& done) {
41 DCHECK(caller_task_runner_->BelongsToCurrentThread());
42
43 DaemonController::GetConfigCallback wrapped_done = base::Bind(
44 &DaemonController::InvokeConfigCallbackAndScheduleNext, this, done);
45 base::Closure request = base::Bind(
46 &DaemonController::DoGetConfig, this, wrapped_done);
47 ServiceOrQueueRequest(request);
48 }
49
50 void DaemonController::SetConfigAndStart(
51 scoped_ptr<base::DictionaryValue> config,
52 bool consent,
53 const CompletionCallback& done) {
54 DCHECK(caller_task_runner_->BelongsToCurrentThread());
55
56 DaemonController::CompletionCallback wrapped_done = base::Bind(
57 &DaemonController::InvokeCompletionCallbackAndScheduleNext, this, done);
58 base::Closure request = base::Bind(
59 &DaemonController::DoSetConfigAndStart, this, base::Passed(&config),
60 consent, wrapped_done);
61 ServiceOrQueueRequest(request);
62 }
63
64 void DaemonController::UpdateConfig(scoped_ptr<base::DictionaryValue> config,
65 const CompletionCallback& done) {
66 DCHECK(caller_task_runner_->BelongsToCurrentThread());
67
68 DaemonController::CompletionCallback wrapped_done = base::Bind(
69 &DaemonController::InvokeCompletionCallbackAndScheduleNext, this, done);
70 base::Closure request = base::Bind(
71 &DaemonController::DoUpdateConfig, this, base::Passed(&config),
72 wrapped_done);
73 ServiceOrQueueRequest(request);
74 }
75
76 void DaemonController::Stop(const CompletionCallback& done) {
77 DCHECK(caller_task_runner_->BelongsToCurrentThread());
78
79 DaemonController::CompletionCallback wrapped_done = base::Bind(
80 &DaemonController::InvokeCompletionCallbackAndScheduleNext, this, done);
81 base::Closure request = base::Bind(
82 &DaemonController::DoStop, this, wrapped_done);
83 ServiceOrQueueRequest(request);
84 }
85
86 void DaemonController::SetWindow(void* window_handle) {
87 DCHECK(caller_task_runner_->BelongsToCurrentThread());
88
89 base::Closure done = base::Bind(&DaemonController::ScheduleNext, this);
90 base::Closure request = base::Bind(
91 &DaemonController::DoSetWindow, this, window_handle, done);
92 ServiceOrQueueRequest(request);
93 }
94
95 void DaemonController::GetVersion(const GetVersionCallback& done) {
96 DCHECK(caller_task_runner_->BelongsToCurrentThread());
97
98 DaemonController::GetVersionCallback wrapped_done = base::Bind(
99 &DaemonController::InvokeVersionCallbackAndScheduleNext, this, done);
100 base::Closure request = base::Bind(
101 &DaemonController::DoGetVersion, this, wrapped_done);
102 ServiceOrQueueRequest(request);
103 }
104
105 void DaemonController::GetUsageStatsConsent(
106 const GetUsageStatsConsentCallback& done) {
107 DCHECK(caller_task_runner_->BelongsToCurrentThread());
108
109 DaemonController::GetUsageStatsConsentCallback wrapped_done = base::Bind(
110 &DaemonController::InvokeConsentCallbackAndScheduleNext, this, done);
111 base::Closure request = base::Bind(
112 &DaemonController::DoGetUsageStatsConsent, this, wrapped_done);
113 ServiceOrQueueRequest(request);
114 }
115
116 DaemonController::~DaemonController() {
117 // Make sure |delegate_| is deleted on the background thread.
118 delegate_task_runner_->DeleteSoon(FROM_HERE, delegate_.release());
119
120 // Stop the thread.
121 delegate_task_runner_ = NULL;
122 caller_task_runner_->DeleteSoon(FROM_HERE, delegate_thread_.release());
123 }
124
125 void DaemonController::DoGetConfig(const GetConfigCallback& done) {
126 if (!delegate_task_runner_->BelongsToCurrentThread()) {
Sergey Ulanov 2013/09/09 18:12:31 Here and in other Do*() methods you can replace it
alexeypa (please no reviews) 2013/09/09 19:30:57 Only if DaemonController::ServiceNextRequest() pos
127 delegate_task_runner_->PostTask(
128 FROM_HERE, base::Bind(&DaemonController::DoGetConfig, this, done));
129 return;
130 }
131
132 scoped_ptr<base::DictionaryValue> config = delegate_->GetConfig();
133 caller_task_runner_->PostTask(FROM_HERE,
134 base::Bind(done, base::Passed(&config)));
135 }
136
137 void DaemonController::DoSetConfigAndStart(
138 scoped_ptr<base::DictionaryValue> config,
139 bool consent,
140 const CompletionCallback& done) {
141 if (!delegate_task_runner_->BelongsToCurrentThread()) {
142 delegate_task_runner_->PostTask(
143 FROM_HERE, base::Bind(&DaemonController::DoSetConfigAndStart, this,
144 base::Passed(&config), consent, done));
145 return;
146 }
147
148 delegate_->SetConfigAndStart(config.Pass(), consent, done);
149 }
150
151 void DaemonController::DoUpdateConfig(
152 scoped_ptr<base::DictionaryValue> config,
153 const CompletionCallback& done) {
154 if (!delegate_task_runner_->BelongsToCurrentThread()) {
155 delegate_task_runner_->PostTask(
156 FROM_HERE, base::Bind(&DaemonController::DoUpdateConfig, this,
157 base::Passed(&config), done));
158 return;
159 }
160
161 delegate_->UpdateConfig(config.Pass(), done);
162 }
163
164 void DaemonController::DoStop(const CompletionCallback& done) {
165 if (!delegate_task_runner_->BelongsToCurrentThread()) {
166 delegate_task_runner_->PostTask(
167 FROM_HERE, base::Bind(&DaemonController::DoStop, this, done));
168 return;
169 }
170
171 delegate_->Stop(done);
172 }
173
174 void DaemonController::DoSetWindow(void* window_handle,
175 const base::Closure& done) {
176 if (!delegate_task_runner_->BelongsToCurrentThread()) {
177 delegate_task_runner_->PostTask(
178 FROM_HERE, base::Bind(&DaemonController::DoSetWindow, this,
179 window_handle, done));
180 return;
181 }
182
183 delegate_->SetWindow(window_handle);
184 caller_task_runner_->PostTask(FROM_HERE, done);
185 }
186
187 void DaemonController::DoGetVersion(const GetVersionCallback& done) {
188 if (!delegate_task_runner_->BelongsToCurrentThread()) {
189 delegate_task_runner_->PostTask(
190 FROM_HERE, base::Bind(&DaemonController::DoGetVersion, this, done));
191 return;
192 }
193
194 std::string version = delegate_->GetVersion();
195 caller_task_runner_->PostTask(FROM_HERE, base::Bind(done, version));
196 }
197
198 void DaemonController::DoGetUsageStatsConsent(
199 const GetUsageStatsConsentCallback& done) {
200 if (!delegate_task_runner_->BelongsToCurrentThread()) {
201 delegate_task_runner_->PostTask(
202 FROM_HERE, base::Bind(&DaemonController::DoGetUsageStatsConsent, this,
203 done));
204 return;
205 }
206
207 DaemonController::UsageStatsConsent consent =
208 delegate_->GetUsageStatsConsent();
209 caller_task_runner_->PostTask(FROM_HERE, base::Bind(done, consent));
210 }
211
212 void DaemonController::InvokeCompletionCallbackAndScheduleNext(
213 const CompletionCallback& done,
214 AsyncResult result) {
215 if (!caller_task_runner_->BelongsToCurrentThread()) {
216 caller_task_runner_->PostTask(
217 FROM_HERE,
218 base::Bind(&DaemonController::InvokeCompletionCallbackAndScheduleNext,
219 this, done, result));
220 return;
221 }
222
223 done.Run(result);
224 ScheduleNext();
225 }
226
227 void DaemonController::InvokeConfigCallbackAndScheduleNext(
228 const GetConfigCallback& done,
229 scoped_ptr<base::DictionaryValue> config) {
230 DCHECK(caller_task_runner_->BelongsToCurrentThread());
231
232 done.Run(config.Pass());
233 ScheduleNext();
234 }
235
236 void DaemonController::InvokeConsentCallbackAndScheduleNext(
237 const GetUsageStatsConsentCallback& done,
238 const UsageStatsConsent& consent) {
239 DCHECK(caller_task_runner_->BelongsToCurrentThread());
240
241 done.Run(consent);
242 ScheduleNext();
243 }
244
245 void DaemonController::InvokeVersionCallbackAndScheduleNext(
246 const GetVersionCallback& done,
247 const std::string& version) {
248 DCHECK(caller_task_runner_->BelongsToCurrentThread());
249
250 done.Run(version);
251 ScheduleNext();
252 }
253
254 void DaemonController::ScheduleNext() {
255 DCHECK(caller_task_runner_->BelongsToCurrentThread());
256
257 pending_requests_.pop();
258 ServiceNextRequest();
259 }
260
261 void DaemonController::ServiceOrQueueRequest(const base::Closure& request) {
262 bool servicing_request = !pending_requests_.empty();
263 pending_requests_.push(request);
264 if (!servicing_request)
265 ServiceNextRequest();
266 }
267
268 void DaemonController::ServiceNextRequest() {
269 if (!pending_requests_.empty())
270 pending_requests_.front().Run();
271 }
272
273 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698