 Chromium Code Reviews
 Chromium Code Reviews Issue 2867223003:
  Send a message to the client if bad It2Me policies are read.  (Closed)
    
  
    Issue 2867223003:
  Send a message to the client if bad It2Me policies are read.  (Closed) 
  | 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.is_null()); | 
| 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.is_null()); | 
| 
joedow
2017/05/09 18:09:47
nit: DCHECK(callback);
 
Jamie
2017/05/09 18:19:33
Done, here and above.
 | |
| 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 491 if (pending_connect_) { | 512 if (pending_connect_) { | 
| 492 base::ResetAndReturn(&pending_connect_).Run(); | 513 base::ResetAndReturn(&pending_connect_).Run(); | 
| 493 } | 514 } | 
| 494 } | 515 } | 
| 495 | 516 | 
| 496 if (it2me_host_.get()) { | 517 if (it2me_host_.get()) { | 
| 497 it2me_host_->OnPolicyUpdate(std::move(policies)); | 518 it2me_host_->OnPolicyUpdate(std::move(policies)); | 
| 498 } | 519 } | 
| 499 } | 520 } | 
| 500 | 521 | 
| 522 void It2MeNativeMessagingHost::OnPolicyError() { | |
| 523 LOG(ERROR) << "Malformed policies detected."; | |
| 524 policy_received_ = true; | |
| 525 | |
| 526 if (it2me_host_) { | |
| 527 // If there is already a connection, close it and notify the webapp. | |
| 528 it2me_host_->Disconnect(); | |
| 529 it2me_host_ = nullptr; | |
| 530 SendPolicyErrorAndExit(); | |
| 531 | |
| 
joedow
2017/05/09 18:09:47
nit: remove newline
 
Jamie
2017/05/09 18:19:33
Done.
 | |
| 532 } else if (pending_connect_) { | |
| 533 // If there is no connection, run the pending connection callback if there | |
| 534 // is one, but otherwise do nothing. The policy error will be sent when a | |
| 535 // connection is made; doing so beforehand would break assumptions made by | |
| 536 // the Chrome app. | |
| 537 base::ResetAndReturn(&pending_connect_).Run(); | |
| 538 } | |
| 539 } | |
| 540 | |
| 501 #if defined(OS_WIN) | 541 #if defined(OS_WIN) | 
| 502 | 542 | 
| 503 bool It2MeNativeMessagingHost::DelegateToElevatedHost( | 543 bool It2MeNativeMessagingHost::DelegateToElevatedHost( | 
| 504 std::unique_ptr<base::DictionaryValue> message) { | 544 std::unique_ptr<base::DictionaryValue> message) { | 
| 505 DCHECK(task_runner()->BelongsToCurrentThread()); | 545 DCHECK(task_runner()->BelongsToCurrentThread()); | 
| 506 DCHECK(needs_elevation_); | 546 DCHECK(needs_elevation_); | 
| 507 | 547 | 
| 508 if (!elevated_host_) { | 548 if (!elevated_host_) { | 
| 509 base::FilePath binary_path = | 549 base::FilePath binary_path = | 
| 510 base::CommandLine::ForCurrentProcess()->GetProgram(); | 550 base::CommandLine::ForCurrentProcess()->GetProgram(); | 
| (...skipping 21 matching lines...) Expand all Loading... | |
| 532 | 572 | 
| 533 bool It2MeNativeMessagingHost::DelegateToElevatedHost( | 573 bool It2MeNativeMessagingHost::DelegateToElevatedHost( | 
| 534 std::unique_ptr<base::DictionaryValue> message) { | 574 std::unique_ptr<base::DictionaryValue> message) { | 
| 535 NOTREACHED(); | 575 NOTREACHED(); | 
| 536 return false; | 576 return false; | 
| 537 } | 577 } | 
| 538 | 578 | 
| 539 #endif // !defined(OS_WIN) | 579 #endif // !defined(OS_WIN) | 
| 540 | 580 | 
| 541 } // namespace remoting | 581 } // namespace remoting | 
| OLD | NEW |