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/it2me/it2me_native_messaging_host.h" | 5 #include "remoting/host/it2me/it2me_native_messaging_host.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 {kConnecting, "CONNECTING"}, | 54 {kConnecting, "CONNECTING"}, |
55 }; | 55 }; |
56 | 56 |
57 #if defined(OS_WIN) | 57 #if defined(OS_WIN) |
58 const base::FilePath::CharType kBaseHostBinaryName[] = | 58 const base::FilePath::CharType kBaseHostBinaryName[] = |
59 FILE_PATH_LITERAL("remote_assistance_host.exe"); | 59 FILE_PATH_LITERAL("remote_assistance_host.exe"); |
60 const base::FilePath::CharType kElevatedHostBinaryName[] = | 60 const base::FilePath::CharType kElevatedHostBinaryName[] = |
61 FILE_PATH_LITERAL("remote_assistance_host_uiaccess.exe"); | 61 FILE_PATH_LITERAL("remote_assistance_host_uiaccess.exe"); |
62 #endif // defined(OS_WIN) | 62 #endif // defined(OS_WIN) |
63 | 63 |
64 // Helper function to run |callback| on the correct thread using |task_runner|. | 64 // Helper functions to run |callback| asynchronously on the correct thread |
| 65 // using |task_runner|. |
65 void PolicyUpdateCallback( | 66 void PolicyUpdateCallback( |
66 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 67 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
67 remoting::PolicyWatcher::PolicyUpdatedCallback callback, | 68 remoting::PolicyWatcher::PolicyUpdatedCallback callback, |
68 std::unique_ptr<base::DictionaryValue> policies) { | 69 std::unique_ptr<base::DictionaryValue> policies) { |
69 DCHECK(!callback.is_null()); | 70 DCHECK(callback); |
70 | |
71 // Always post the task so the execution is consistent (always asynchronous). | |
72 task_runner->PostTask(FROM_HERE, | 71 task_runner->PostTask(FROM_HERE, |
73 base::Bind(callback, base::Passed(&policies))); | 72 base::Bind(callback, base::Passed(&policies))); |
74 } | 73 } |
75 | 74 |
76 // Called when malformed policies are detected. | 75 void PolicyErrorCallback( |
77 void OnPolicyError() { | 76 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
78 // TODO(joedow): Report the policy error to the user. crbug.com/433009 | 77 remoting::PolicyWatcher::PolicyErrorCallback callback) { |
79 NOTIMPLEMENTED(); | 78 DCHECK(callback); |
| 79 task_runner->PostTask(FROM_HERE, callback); |
80 } | 80 } |
81 | 81 |
82 } // namespace | 82 } // namespace |
83 | 83 |
84 It2MeNativeMessagingHost::It2MeNativeMessagingHost( | 84 It2MeNativeMessagingHost::It2MeNativeMessagingHost( |
85 bool needs_elevation, | 85 bool needs_elevation, |
86 std::unique_ptr<PolicyWatcher> policy_watcher, | 86 std::unique_ptr<PolicyWatcher> policy_watcher, |
87 std::unique_ptr<ChromotingHostContext> context, | 87 std::unique_ptr<ChromotingHostContext> context, |
88 std::unique_ptr<It2MeHostFactory> factory) | 88 std::unique_ptr<It2MeHostFactory> factory) |
89 : needs_elevation_(needs_elevation), | 89 : needs_elevation_(needs_elevation), |
90 host_context_(std::move(context)), | 90 host_context_(std::move(context)), |
91 factory_(std::move(factory)), | 91 factory_(std::move(factory)), |
92 policy_watcher_(std::move(policy_watcher)), | 92 policy_watcher_(std::move(policy_watcher)), |
93 weak_factory_(this) { | 93 weak_factory_(this) { |
94 weak_ptr_ = weak_factory_.GetWeakPtr(); | 94 weak_ptr_ = weak_factory_.GetWeakPtr(); |
95 | 95 |
96 // The policy watcher runs on the |file_task_runner| but we want to run the | 96 // The policy watcher runs on the |file_task_runner| but we want to run the |
97 // update code on |task_runner| so we use a shim to post the callback to the | 97 // callbacks on |task_runner| so we use a shim to post them to it. |
98 // preferred task runner. | |
99 PolicyWatcher::PolicyUpdatedCallback update_callback = | 98 PolicyWatcher::PolicyUpdatedCallback update_callback = |
100 base::Bind(&It2MeNativeMessagingHost::OnPolicyUpdate, weak_ptr_); | 99 base::Bind(&It2MeNativeMessagingHost::OnPolicyUpdate, weak_ptr_); |
| 100 PolicyWatcher::PolicyErrorCallback error_callback = |
| 101 base::Bind(&It2MeNativeMessagingHost::OnPolicyError, weak_ptr_); |
101 policy_watcher_->StartWatching( | 102 policy_watcher_->StartWatching( |
102 base::Bind(&PolicyUpdateCallback, task_runner(), update_callback), | 103 base::Bind(&PolicyUpdateCallback, task_runner(), update_callback), |
103 base::Bind(&OnPolicyError)); | 104 base::Bind(&PolicyErrorCallback, task_runner(), error_callback)); |
104 } | 105 } |
105 | 106 |
106 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { | 107 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { |
107 DCHECK(task_runner()->BelongsToCurrentThread()); | 108 DCHECK(task_runner()->BelongsToCurrentThread()); |
108 | 109 |
109 if (it2me_host_.get()) { | 110 if (it2me_host_.get()) { |
110 it2me_host_->Disconnect(); | 111 it2me_host_->Disconnect(); |
111 it2me_host_ = nullptr; | 112 it2me_host_ = nullptr; |
112 } | 113 } |
113 } | 114 } |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 std::string directory_bot_jid = service_urls->directory_bot_jid(); | 308 std::string directory_bot_jid = service_urls->directory_bot_jid(); |
308 | 309 |
309 #if !defined(NDEBUG) | 310 #if !defined(NDEBUG) |
310 if (!message->GetString("directoryBotJid", &directory_bot_jid)) { | 311 if (!message->GetString("directoryBotJid", &directory_bot_jid)) { |
311 SendErrorAndExit(std::move(response), | 312 SendErrorAndExit(std::move(response), |
312 "'directoryBotJid' not found in request."); | 313 "'directoryBotJid' not found in request."); |
313 return; | 314 return; |
314 } | 315 } |
315 #endif // !defined(NDEBUG) | 316 #endif // !defined(NDEBUG) |
316 | 317 |
| 318 std::unique_ptr<base::DictionaryValue> policies = |
| 319 policy_watcher_->GetCurrentPolicies(); |
| 320 if (policies->size() == 0) { |
| 321 // At this point policies have been read, so if there are none set then |
| 322 // it indicates an error. Since this can be fixed by end users it has a |
| 323 // dedicated message type rather than the generic "error" so that the |
| 324 // right error message can be displayed. |
| 325 SendPolicyErrorAndExit(); |
| 326 return; |
| 327 } |
| 328 |
317 // Create the It2Me host and start connecting. | 329 // Create the It2Me host and start connecting. |
318 it2me_host_ = factory_->CreateIt2MeHost(host_context_->Copy(), weak_ptr_, | 330 it2me_host_ = factory_->CreateIt2MeHost(host_context_->Copy(), weak_ptr_, |
319 std::move(signal_strategy), username, | 331 std::move(signal_strategy), username, |
320 directory_bot_jid); | 332 directory_bot_jid); |
321 it2me_host_->OnPolicyUpdate(policy_watcher_->GetCurrentPolicies()); | 333 it2me_host_->OnPolicyUpdate(std::move(policies)); |
322 it2me_host_->Connect(); | 334 it2me_host_->Connect(); |
323 | 335 |
324 SendMessageToClient(std::move(response)); | 336 SendMessageToClient(std::move(response)); |
325 } | 337 } |
326 | 338 |
327 void It2MeNativeMessagingHost::ProcessDisconnect( | 339 void It2MeNativeMessagingHost::ProcessDisconnect( |
328 std::unique_ptr<base::DictionaryValue> message, | 340 std::unique_ptr<base::DictionaryValue> message, |
329 std::unique_ptr<base::DictionaryValue> response) { | 341 std::unique_ptr<base::DictionaryValue> response) { |
330 DCHECK(task_runner()->BelongsToCurrentThread()); | 342 DCHECK(task_runner()->BelongsToCurrentThread()); |
331 DCHECK(policy_received_); | 343 DCHECK(policy_received_); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 LOG(ERROR) << description; | 390 LOG(ERROR) << description; |
379 | 391 |
380 response->SetString("type", "error"); | 392 response->SetString("type", "error"); |
381 response->SetString("description", description); | 393 response->SetString("description", description); |
382 SendMessageToClient(std::move(response)); | 394 SendMessageToClient(std::move(response)); |
383 | 395 |
384 // Trigger a host shutdown by sending an empty message. | 396 // Trigger a host shutdown by sending an empty message. |
385 client_->CloseChannel(std::string()); | 397 client_->CloseChannel(std::string()); |
386 } | 398 } |
387 | 399 |
| 400 void It2MeNativeMessagingHost::SendPolicyErrorAndExit() const { |
| 401 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 402 |
| 403 auto message = base::MakeUnique<base::DictionaryValue>(); |
| 404 message->SetString("type", "policyError"); |
| 405 SendMessageToClient(std::move(message)); |
| 406 client_->CloseChannel(std::string()); |
| 407 } |
| 408 |
388 void It2MeNativeMessagingHost::OnStateChanged( | 409 void It2MeNativeMessagingHost::OnStateChanged( |
389 It2MeHostState state, | 410 It2MeHostState state, |
390 const std::string& error_message) { | 411 const std::string& error_message) { |
391 DCHECK(task_runner()->BelongsToCurrentThread()); | 412 DCHECK(task_runner()->BelongsToCurrentThread()); |
392 | 413 |
393 state_ = state; | 414 state_ = state; |
394 | 415 |
395 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 416 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
396 | 417 |
397 message->SetString("type", "hostStateChanged"); | 418 message->SetString("type", "hostStateChanged"); |
(...skipping 22 matching lines...) Expand all Loading... |
420 message->SetString("description", error_message); | 441 message->SetString("description", error_message); |
421 break; | 442 break; |
422 | 443 |
423 default: | 444 default: |
424 break; | 445 break; |
425 } | 446 } |
426 | 447 |
427 SendMessageToClient(std::move(message)); | 448 SendMessageToClient(std::move(message)); |
428 } | 449 } |
429 | 450 |
| 451 void It2MeNativeMessagingHost::SetPolicyErrorClosureForTesting( |
| 452 const base::Closure& closure) { |
| 453 policy_error_closure_for_testing_ = closure; |
| 454 } |
| 455 |
430 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { | 456 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { |
431 DCHECK(task_runner()->BelongsToCurrentThread()); | 457 DCHECK(task_runner()->BelongsToCurrentThread()); |
432 | 458 |
433 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 459 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
434 | 460 |
435 message->SetString("type", "natPolicyChanged"); | 461 message->SetString("type", "natPolicyChanged"); |
436 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); | 462 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); |
437 SendMessageToClient(std::move(message)); | 463 SendMessageToClient(std::move(message)); |
438 } | 464 } |
439 | 465 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 if (pending_connect_) { | 517 if (pending_connect_) { |
492 base::ResetAndReturn(&pending_connect_).Run(); | 518 base::ResetAndReturn(&pending_connect_).Run(); |
493 } | 519 } |
494 } | 520 } |
495 | 521 |
496 if (it2me_host_.get()) { | 522 if (it2me_host_.get()) { |
497 it2me_host_->OnPolicyUpdate(std::move(policies)); | 523 it2me_host_->OnPolicyUpdate(std::move(policies)); |
498 } | 524 } |
499 } | 525 } |
500 | 526 |
| 527 void It2MeNativeMessagingHost::OnPolicyError() { |
| 528 LOG(ERROR) << "Malformed policies detected."; |
| 529 policy_received_ = true; |
| 530 |
| 531 if (policy_error_closure_for_testing_) { |
| 532 policy_error_closure_for_testing_.Run(); |
| 533 } |
| 534 |
| 535 if (it2me_host_) { |
| 536 // If there is already a connection, close it and notify the webapp. |
| 537 it2me_host_->Disconnect(); |
| 538 it2me_host_ = nullptr; |
| 539 SendPolicyErrorAndExit(); |
| 540 } else if (pending_connect_) { |
| 541 // If there is no connection, run the pending connection callback if there |
| 542 // is one, but otherwise do nothing. The policy error will be sent when a |
| 543 // connection is made; doing so beforehand would break assumptions made by |
| 544 // the Chrome app. |
| 545 base::ResetAndReturn(&pending_connect_).Run(); |
| 546 } |
| 547 } |
| 548 |
501 #if defined(OS_WIN) | 549 #if defined(OS_WIN) |
502 | 550 |
503 bool It2MeNativeMessagingHost::DelegateToElevatedHost( | 551 bool It2MeNativeMessagingHost::DelegateToElevatedHost( |
504 std::unique_ptr<base::DictionaryValue> message) { | 552 std::unique_ptr<base::DictionaryValue> message) { |
505 DCHECK(task_runner()->BelongsToCurrentThread()); | 553 DCHECK(task_runner()->BelongsToCurrentThread()); |
506 DCHECK(needs_elevation_); | 554 DCHECK(needs_elevation_); |
507 | 555 |
508 if (!elevated_host_) { | 556 if (!elevated_host_) { |
509 base::FilePath binary_path = | 557 base::FilePath binary_path = |
510 base::CommandLine::ForCurrentProcess()->GetProgram(); | 558 base::CommandLine::ForCurrentProcess()->GetProgram(); |
(...skipping 21 matching lines...) Expand all Loading... |
532 | 580 |
533 bool It2MeNativeMessagingHost::DelegateToElevatedHost( | 581 bool It2MeNativeMessagingHost::DelegateToElevatedHost( |
534 std::unique_ptr<base::DictionaryValue> message) { | 582 std::unique_ptr<base::DictionaryValue> message) { |
535 NOTREACHED(); | 583 NOTREACHED(); |
536 return false; | 584 return false; |
537 } | 585 } |
538 | 586 |
539 #endif // !defined(OS_WIN) | 587 #endif // !defined(OS_WIN) |
540 | 588 |
541 } // namespace remoting | 589 } // namespace remoting |
OLD | NEW |