OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "remoting/host/setup/native_messaging_host.h" | 5 #include "remoting/host/setup/native_messaging_host.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 const base::Closure& quit_closure) | 54 const base::Closure& quit_closure) |
55 : caller_task_runner_(caller_task_runner), | 55 : caller_task_runner_(caller_task_runner), |
56 quit_closure_(quit_closure), | 56 quit_closure_(quit_closure), |
57 native_messaging_reader_(input), | 57 native_messaging_reader_(input), |
58 native_messaging_writer_(output), | 58 native_messaging_writer_(output), |
59 daemon_controller_(DaemonController::Create()), | 59 daemon_controller_(DaemonController::Create()), |
60 weak_factory_(this) { | 60 weak_factory_(this) { |
61 weak_ptr_ = weak_factory_.GetWeakPtr(); | 61 weak_ptr_ = weak_factory_.GetWeakPtr(); |
62 } | 62 } |
63 | 63 |
64 NativeMessagingHost::~NativeMessagingHost() { | 64 NativeMessagingHost::~NativeMessagingHost() {} |
65 } | |
66 | 65 |
67 void NativeMessagingHost::Start() { | 66 void NativeMessagingHost::Start() { |
68 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 67 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
69 | 68 |
70 native_messaging_reader_.Start( | 69 native_messaging_reader_.Start( |
71 base::Bind(&NativeMessagingHost::ProcessMessage, weak_ptr_), | 70 base::Bind(&NativeMessagingHost::ProcessMessage, weak_ptr_), |
72 base::Bind(&NativeMessagingHost::Shutdown, weak_ptr_)); | 71 base::Bind(&NativeMessagingHost::Shutdown, weak_ptr_)); |
73 } | 72 } |
74 | 73 |
75 void NativeMessagingHost::Shutdown() { | 74 void NativeMessagingHost::Shutdown() { |
76 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 75 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
77 if (!quit_closure_.is_null()) { | 76 if (!quit_closure_.is_null()) { |
78 caller_task_runner_->PostTask(FROM_HERE, quit_closure_); | 77 caller_task_runner_->PostTask(FROM_HERE, quit_closure_); |
79 quit_closure_.Reset(); | 78 quit_closure_.Reset(); |
80 } | 79 } |
81 } | 80 } |
82 | 81 |
| 82 void NativeMessagingHost::SetDaemonControllerForTest( |
| 83 scoped_ptr<DaemonController> daemon_controller) { |
| 84 daemon_controller_ = daemon_controller.Pass(); |
| 85 } |
| 86 |
83 void NativeMessagingHost::ProcessMessage(scoped_ptr<base::Value> message) { | 87 void NativeMessagingHost::ProcessMessage(scoped_ptr<base::Value> message) { |
84 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 88 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 89 |
| 90 // Don't process any more messages if Shutdown() has been called. |
| 91 if (quit_closure_.is_null()) |
| 92 return; |
| 93 |
85 const base::DictionaryValue* message_dict; | 94 const base::DictionaryValue* message_dict; |
86 if (!message->GetAsDictionary(&message_dict)) { | 95 if (!message->GetAsDictionary(&message_dict)) { |
87 LOG(ERROR) << "Expected DictionaryValue"; | 96 LOG(ERROR) << "Expected DictionaryValue"; |
88 Shutdown(); | 97 Shutdown(); |
| 98 return; |
89 } | 99 } |
90 | 100 |
91 scoped_ptr<base::DictionaryValue> response_dict(new base::DictionaryValue()); | 101 scoped_ptr<base::DictionaryValue> response_dict(new base::DictionaryValue()); |
92 | 102 |
93 // If the client supplies an ID, it will expect it in the response. This | 103 // If the client supplies an ID, it will expect it in the response. This |
94 // might be a string or a number, so cope with both. | 104 // might be a string or a number, so cope with both. |
95 const base::Value* id; | 105 const base::Value* id; |
96 if (message_dict->Get("id", &id)) | 106 if (message_dict->Get("id", &id)) |
97 response_dict->Set("id", id->DeepCopy()); | 107 response_dict->Set("id", id->DeepCopy()); |
98 | 108 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 daemon_controller_->UpdateConfig( | 201 daemon_controller_->UpdateConfig( |
192 config_dict.Pass(), | 202 config_dict.Pass(), |
193 base::Bind(&NativeMessagingHost::SendAsyncResult, base::Unretained(this), | 203 base::Bind(&NativeMessagingHost::SendAsyncResult, base::Unretained(this), |
194 base::Passed(&response))); | 204 base::Passed(&response))); |
195 return true; | 205 return true; |
196 } | 206 } |
197 | 207 |
198 bool NativeMessagingHost::ProcessGetDaemonConfig( | 208 bool NativeMessagingHost::ProcessGetDaemonConfig( |
199 const base::DictionaryValue& message, | 209 const base::DictionaryValue& message, |
200 scoped_ptr<base::DictionaryValue> response) { | 210 scoped_ptr<base::DictionaryValue> response) { |
201 daemon_controller_->GetConfig(base::Bind( | 211 daemon_controller_->GetConfig( |
202 &NativeMessagingHost::SendConfigResponse, | 212 base::Bind(&NativeMessagingHost::SendConfigResponse, |
203 base::Unretained(this), base::Passed(&response))); | 213 base::Unretained(this), base::Passed(&response))); |
204 return true; | 214 return true; |
205 } | 215 } |
206 | 216 |
207 bool NativeMessagingHost::ProcessGetUsageStatsConsent( | 217 bool NativeMessagingHost::ProcessGetUsageStatsConsent( |
208 const base::DictionaryValue& message, | 218 const base::DictionaryValue& message, |
209 scoped_ptr<base::DictionaryValue> response) { | 219 scoped_ptr<base::DictionaryValue> response) { |
210 daemon_controller_->GetUsageStatsConsent(base::Bind( | 220 daemon_controller_->GetUsageStatsConsent( |
211 &NativeMessagingHost::SendUsageStatsConsentResponse, | 221 base::Bind(&NativeMessagingHost::SendUsageStatsConsentResponse, |
212 base::Unretained(this), base::Passed(&response))); | 222 base::Unretained(this), base::Passed(&response))); |
213 return true; | 223 return true; |
214 } | 224 } |
215 | 225 |
216 bool NativeMessagingHost::ProcessStartDaemon( | 226 bool NativeMessagingHost::ProcessStartDaemon( |
217 const base::DictionaryValue& message, | 227 const base::DictionaryValue& message, |
218 scoped_ptr<base::DictionaryValue> response) { | 228 scoped_ptr<base::DictionaryValue> response) { |
219 bool consent; | 229 bool consent; |
220 if (!message.GetBoolean("consent", &consent)) { | 230 if (!message.GetBoolean("consent", &consent)) { |
221 LOG(ERROR) << "'consent' not found."; | 231 LOG(ERROR) << "'consent' not found."; |
222 return false; | 232 return false; |
223 } | 233 } |
224 | 234 |
225 scoped_ptr<base::DictionaryValue> config_dict = | 235 scoped_ptr<base::DictionaryValue> config_dict = |
226 ConfigDictionaryFromMessage(message); | 236 ConfigDictionaryFromMessage(message); |
227 if (!config_dict) | 237 if (!config_dict) |
228 return false; | 238 return false; |
229 | 239 |
230 daemon_controller_->SetConfigAndStart( | 240 daemon_controller_->SetConfigAndStart( |
231 config_dict.Pass(), consent, | 241 config_dict.Pass(), consent, |
232 base::Bind(&NativeMessagingHost::SendAsyncResult, base::Unretained(this), | 242 base::Bind(&NativeMessagingHost::SendAsyncResult, base::Unretained(this), |
233 base::Passed(&response))); | 243 base::Passed(&response))); |
234 return true; | 244 return true; |
235 } | 245 } |
236 | 246 |
237 bool NativeMessagingHost::ProcessStopDaemon( | 247 bool NativeMessagingHost::ProcessStopDaemon( |
238 const base::DictionaryValue& message, | 248 const base::DictionaryValue& message, |
239 scoped_ptr<base::DictionaryValue> response) { | 249 scoped_ptr<base::DictionaryValue> response) { |
240 daemon_controller_->Stop(base::Bind( | 250 daemon_controller_->Stop( |
241 &NativeMessagingHost::SendAsyncResult, base::Unretained(this), | 251 base::Bind(&NativeMessagingHost::SendAsyncResult, base::Unretained(this), |
242 base::Passed(&response))); | 252 base::Passed(&response))); |
243 return true; | 253 return true; |
244 } | 254 } |
245 | 255 |
246 bool NativeMessagingHost::ProcessGetDaemonState( | 256 bool NativeMessagingHost::ProcessGetDaemonState( |
247 const base::DictionaryValue& message, | 257 const base::DictionaryValue& message, |
248 scoped_ptr<base::DictionaryValue> response) { | 258 scoped_ptr<base::DictionaryValue> response) { |
249 // TODO(lambroslambrou): Send the state as a string instead of an integer, | 259 // TODO(lambroslambrou): Send the state as a string instead of an integer, |
250 // and update the web-app accordingly. | 260 // and update the web-app accordingly. |
251 DaemonController::State state = daemon_controller_->GetState(); | 261 DaemonController::State state = daemon_controller_->GetState(); |
252 response->SetInteger("state", state); | 262 response->SetInteger("state", state); |
253 SendResponse(response.Pass()); | 263 SendResponse(response.Pass()); |
254 return true; | 264 return true; |
255 } | 265 } |
256 | 266 |
257 void NativeMessagingHost::SendResponse( | 267 void NativeMessagingHost::SendResponse( |
258 scoped_ptr<base::DictionaryValue> response) { | 268 scoped_ptr<base::DictionaryValue> response) { |
259 if (!caller_task_runner_->BelongsToCurrentThread()) { | 269 if (!caller_task_runner_->BelongsToCurrentThread()) { |
260 caller_task_runner_->PostTask(FROM_HERE, base::Bind( | 270 caller_task_runner_->PostTask( |
261 &NativeMessagingHost::SendResponse, weak_ptr_, | 271 FROM_HERE, base::Bind(&NativeMessagingHost::SendResponse, weak_ptr_, |
262 base::Passed(&response))); | 272 base::Passed(&response))); |
263 return; | 273 return; |
264 } | 274 } |
265 | 275 |
266 if (!native_messaging_writer_.WriteMessage(*response)) | 276 if (!native_messaging_writer_.WriteMessage(*response)) |
267 Shutdown(); | 277 Shutdown(); |
268 } | 278 } |
269 | 279 |
270 void NativeMessagingHost::SendConfigResponse( | 280 void NativeMessagingHost::SendConfigResponse( |
271 scoped_ptr<base::DictionaryValue> response, | 281 scoped_ptr<base::DictionaryValue> response, |
272 scoped_ptr<base::DictionaryValue> config) { | 282 scoped_ptr<base::DictionaryValue> config) { |
(...skipping 20 matching lines...) Expand all Loading... |
293 void NativeMessagingHost::SendAsyncResult( | 303 void NativeMessagingHost::SendAsyncResult( |
294 scoped_ptr<base::DictionaryValue> response, | 304 scoped_ptr<base::DictionaryValue> response, |
295 DaemonController::AsyncResult result) { | 305 DaemonController::AsyncResult result) { |
296 // TODO(lambroslambrou): Send the result as a string instead of an integer, | 306 // TODO(lambroslambrou): Send the result as a string instead of an integer, |
297 // and update the web-app accordingly. See http://crbug.com/232135. | 307 // and update the web-app accordingly. See http://crbug.com/232135. |
298 response->SetInteger("result", result); | 308 response->SetInteger("result", result); |
299 SendResponse(response.Pass()); | 309 SendResponse(response.Pass()); |
300 } | 310 } |
301 | 311 |
302 } // namespace remoting | 312 } // namespace remoting |
OLD | NEW |