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

Side by Side Diff: remoting/host/setup/native_messaging_host.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: fix the license 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
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/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 LOG(ERROR) << "'config' dictionary not found"; 55 LOG(ERROR) << "'config' dictionary not found";
56 } 56 }
57 return result.Pass(); 57 return result.Pass();
58 } 58 }
59 59
60 } // namespace 60 } // namespace
61 61
62 namespace remoting { 62 namespace remoting {
63 63
64 NativeMessagingHost::NativeMessagingHost( 64 NativeMessagingHost::NativeMessagingHost(
65 scoped_ptr<DaemonController> daemon_controller, 65 scoped_refptr<DaemonController> daemon_controller,
66 scoped_refptr<protocol::PairingRegistry> pairing_registry, 66 scoped_refptr<protocol::PairingRegistry> pairing_registry,
67 scoped_ptr<OAuthClient> oauth_client, 67 scoped_ptr<OAuthClient> oauth_client,
68 base::PlatformFile input, 68 base::PlatformFile input,
69 base::PlatformFile output, 69 base::PlatformFile output,
70 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, 70 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
71 const base::Closure& quit_closure) 71 const base::Closure& quit_closure)
72 : caller_task_runner_(caller_task_runner), 72 : caller_task_runner_(caller_task_runner),
73 quit_closure_(quit_closure), 73 quit_closure_(quit_closure),
74 native_messaging_reader_(input), 74 native_messaging_reader_(input),
75 native_messaging_writer_(output), 75 native_messaging_writer_(output),
76 daemon_controller_(daemon_controller.Pass()), 76 daemon_controller_(daemon_controller),
77 pairing_registry_(pairing_registry), 77 pairing_registry_(pairing_registry),
78 oauth_client_(oauth_client.Pass()), 78 oauth_client_(oauth_client.Pass()),
79 pending_requests_(0), 79 pending_requests_(0),
80 shutdown_(false), 80 shutdown_(false),
81 weak_factory_(this) { 81 weak_factory_(this) {
82 weak_ptr_ = weak_factory_.GetWeakPtr(); 82 weak_ptr_ = weak_factory_.GetWeakPtr();
83 } 83 }
84 84
85 NativeMessagingHost::~NativeMessagingHost() {} 85 NativeMessagingHost::~NativeMessagingHost() {}
86 86
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 } 263 }
264 264
265 bool NativeMessagingHost::ProcessUpdateDaemonConfig( 265 bool NativeMessagingHost::ProcessUpdateDaemonConfig(
266 const base::DictionaryValue& message, 266 const base::DictionaryValue& message,
267 scoped_ptr<base::DictionaryValue> response) { 267 scoped_ptr<base::DictionaryValue> response) {
268 scoped_ptr<base::DictionaryValue> config_dict = 268 scoped_ptr<base::DictionaryValue> config_dict =
269 ConfigDictionaryFromMessage(message); 269 ConfigDictionaryFromMessage(message);
270 if (!config_dict) 270 if (!config_dict)
271 return false; 271 return false;
272 272
273 // base::Unretained() is safe because this object owns |daemon_controller_|
274 // which owns the thread that will run the callback.
275 daemon_controller_->UpdateConfig( 273 daemon_controller_->UpdateConfig(
276 config_dict.Pass(), 274 config_dict.Pass(),
277 base::Bind(&NativeMessagingHost::SendAsyncResult, base::Unretained(this), 275 base::Bind(&NativeMessagingHost::SendAsyncResult, weak_ptr_,
278 base::Passed(&response))); 276 base::Passed(&response)));
279 return true; 277 return true;
280 } 278 }
281 279
282 bool NativeMessagingHost::ProcessGetDaemonConfig( 280 bool NativeMessagingHost::ProcessGetDaemonConfig(
283 const base::DictionaryValue& message, 281 const base::DictionaryValue& message,
284 scoped_ptr<base::DictionaryValue> response) { 282 scoped_ptr<base::DictionaryValue> response) {
285 daemon_controller_->GetConfig( 283 daemon_controller_->GetConfig(
286 base::Bind(&NativeMessagingHost::SendConfigResponse, 284 base::Bind(&NativeMessagingHost::SendConfigResponse, weak_ptr_,
287 base::Unretained(this), base::Passed(&response))); 285 base::Passed(&response)));
288 return true; 286 return true;
289 } 287 }
290 288
291 bool NativeMessagingHost::ProcessGetPairedClients( 289 bool NativeMessagingHost::ProcessGetPairedClients(
292 const base::DictionaryValue& message, 290 const base::DictionaryValue& message,
293 scoped_ptr<base::DictionaryValue> response) { 291 scoped_ptr<base::DictionaryValue> response) {
294 if (pairing_registry_) { 292 if (pairing_registry_) {
295 pairing_registry_->GetAllPairings( 293 pairing_registry_->GetAllPairings(
296 base::Bind(&NativeMessagingHost::SendPairedClientsResponse, weak_ptr_, 294 base::Bind(&NativeMessagingHost::SendPairedClientsResponse, weak_ptr_,
297 base::Passed(&response))); 295 base::Passed(&response)));
298 } else { 296 } else {
299 scoped_ptr<base::ListValue> no_paired_clients(new base::ListValue); 297 scoped_ptr<base::ListValue> no_paired_clients(new base::ListValue);
300 SendPairedClientsResponse(response.Pass(), no_paired_clients.Pass()); 298 SendPairedClientsResponse(response.Pass(), no_paired_clients.Pass());
301 } 299 }
302 return true; 300 return true;
303 } 301 }
304 302
305 bool NativeMessagingHost::ProcessGetUsageStatsConsent( 303 bool NativeMessagingHost::ProcessGetUsageStatsConsent(
306 const base::DictionaryValue& message, 304 const base::DictionaryValue& message,
307 scoped_ptr<base::DictionaryValue> response) { 305 scoped_ptr<base::DictionaryValue> response) {
308 daemon_controller_->GetUsageStatsConsent( 306 daemon_controller_->GetUsageStatsConsent(
309 base::Bind(&NativeMessagingHost::SendUsageStatsConsentResponse, 307 base::Bind(&NativeMessagingHost::SendUsageStatsConsentResponse,
310 base::Unretained(this), base::Passed(&response))); 308 weak_ptr_, base::Passed(&response)));
311 return true; 309 return true;
312 } 310 }
313 311
314 bool NativeMessagingHost::ProcessStartDaemon( 312 bool NativeMessagingHost::ProcessStartDaemon(
315 const base::DictionaryValue& message, 313 const base::DictionaryValue& message,
316 scoped_ptr<base::DictionaryValue> response) { 314 scoped_ptr<base::DictionaryValue> response) {
317 bool consent; 315 bool consent;
318 if (!message.GetBoolean("consent", &consent)) { 316 if (!message.GetBoolean("consent", &consent)) {
319 LOG(ERROR) << "'consent' not found."; 317 LOG(ERROR) << "'consent' not found.";
320 return false; 318 return false;
321 } 319 }
322 320
323 scoped_ptr<base::DictionaryValue> config_dict = 321 scoped_ptr<base::DictionaryValue> config_dict =
324 ConfigDictionaryFromMessage(message); 322 ConfigDictionaryFromMessage(message);
325 if (!config_dict) 323 if (!config_dict)
326 return false; 324 return false;
327 325
328 daemon_controller_->SetConfigAndStart( 326 daemon_controller_->SetConfigAndStart(
329 config_dict.Pass(), consent, 327 config_dict.Pass(), consent,
330 base::Bind(&NativeMessagingHost::SendAsyncResult, base::Unretained(this), 328 base::Bind(&NativeMessagingHost::SendAsyncResult, weak_ptr_,
331 base::Passed(&response))); 329 base::Passed(&response)));
332 return true; 330 return true;
333 } 331 }
334 332
335 bool NativeMessagingHost::ProcessStopDaemon( 333 bool NativeMessagingHost::ProcessStopDaemon(
336 const base::DictionaryValue& message, 334 const base::DictionaryValue& message,
337 scoped_ptr<base::DictionaryValue> response) { 335 scoped_ptr<base::DictionaryValue> response) {
338 daemon_controller_->Stop( 336 daemon_controller_->Stop(
339 base::Bind(&NativeMessagingHost::SendAsyncResult, base::Unretained(this), 337 base::Bind(&NativeMessagingHost::SendAsyncResult, weak_ptr_,
340 base::Passed(&response))); 338 base::Passed(&response)));
341 return true; 339 return true;
342 } 340 }
343 341
344 bool NativeMessagingHost::ProcessGetDaemonState( 342 bool NativeMessagingHost::ProcessGetDaemonState(
345 const base::DictionaryValue& message, 343 const base::DictionaryValue& message,
346 scoped_ptr<base::DictionaryValue> response) { 344 scoped_ptr<base::DictionaryValue> response) {
347 DaemonController::State state = daemon_controller_->GetState(); 345 DaemonController::State state = daemon_controller_->GetState();
348 switch (state) { 346 switch (state) {
349 case DaemonController::STATE_NOT_IMPLEMENTED: 347 case DaemonController::STATE_NOT_IMPLEMENTED:
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 437
440 void NativeMessagingHost::SendPairedClientsResponse( 438 void NativeMessagingHost::SendPairedClientsResponse(
441 scoped_ptr<base::DictionaryValue> response, 439 scoped_ptr<base::DictionaryValue> response,
442 scoped_ptr<base::ListValue> pairings) { 440 scoped_ptr<base::ListValue> pairings) {
443 response->Set("pairedClients", pairings.release()); 441 response->Set("pairedClients", pairings.release());
444 SendResponse(response.Pass()); 442 SendResponse(response.Pass());
445 } 443 }
446 444
447 void NativeMessagingHost::SendUsageStatsConsentResponse( 445 void NativeMessagingHost::SendUsageStatsConsentResponse(
448 scoped_ptr<base::DictionaryValue> response, 446 scoped_ptr<base::DictionaryValue> response,
449 bool supported, 447 const DaemonController::UsageStatsConsent& consent) {
450 bool allowed, 448 response->SetBoolean("supported", consent.supported);
451 bool set_by_policy) { 449 response->SetBoolean("allowed", consent.allowed);
452 response->SetBoolean("supported", supported); 450 response->SetBoolean("setByPolicy", consent.set_by_policy);
453 response->SetBoolean("allowed", allowed);
454 response->SetBoolean("setByPolicy", set_by_policy);
455 SendResponse(response.Pass()); 451 SendResponse(response.Pass());
456 } 452 }
457 453
458 void NativeMessagingHost::SendAsyncResult( 454 void NativeMessagingHost::SendAsyncResult(
459 scoped_ptr<base::DictionaryValue> response, 455 scoped_ptr<base::DictionaryValue> response,
460 DaemonController::AsyncResult result) { 456 DaemonController::AsyncResult result) {
461 switch (result) { 457 switch (result) {
462 case DaemonController::RESULT_OK: 458 case DaemonController::RESULT_OK:
463 response->SetString("result", "OK"); 459 response->SetString("result", "OK");
464 break; 460 break;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 oauth_client.Pass(), 514 oauth_client.Pass(),
519 read_file, write_file, 515 read_file, write_file,
520 message_loop.message_loop_proxy(), 516 message_loop.message_loop_proxy(),
521 run_loop.QuitClosure()); 517 run_loop.QuitClosure());
522 host.Start(); 518 host.Start();
523 run_loop.Run(); 519 run_loop.Run();
524 return kSuccessExitCode; 520 return kSuccessExitCode;
525 } 521 }
526 522
527 } // namespace remoting 523 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/setup/native_messaging_host.h ('k') | remoting/host/setup/native_messaging_host_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698