| 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_host.h" | 5 #include "remoting/host/it2me/it2me_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| 10 #include "net/socket/client_socket_factory.h" | 11 #include "net/socket/client_socket_factory.h" |
| 11 #include "remoting/base/auto_thread.h" | 12 #include "remoting/base/auto_thread.h" |
| 12 #include "remoting/base/logging.h" | 13 #include "remoting/base/logging.h" |
| 13 #include "remoting/base/rsa_key_pair.h" | 14 #include "remoting/base/rsa_key_pair.h" |
| 14 #include "remoting/host/chromoting_host.h" | 15 #include "remoting/host/chromoting_host.h" |
| 15 #include "remoting/host/chromoting_host_context.h" | 16 #include "remoting/host/chromoting_host_context.h" |
| 16 #include "remoting/host/host_event_logger.h" | 17 #include "remoting/host/host_event_logger.h" |
| 17 #include "remoting/host/host_secret.h" | 18 #include "remoting/host/host_secret.h" |
| 18 #include "remoting/host/host_status_logger.h" | 19 #include "remoting/host/host_status_logger.h" |
| 19 #include "remoting/host/it2me_desktop_environment.h" | 20 #include "remoting/host/it2me_desktop_environment.h" |
| 20 #include "remoting/host/policy_hack/policy_watcher.h" | 21 #include "remoting/host/policy_hack/policy_watcher.h" |
| 21 #include "remoting/host/register_support_host_request.h" | 22 #include "remoting/host/register_support_host_request.h" |
| 22 #include "remoting/host/session_manager_factory.h" | 23 #include "remoting/host/session_manager_factory.h" |
| 23 #include "remoting/protocol/it2me_host_authenticator_factory.h" | 24 #include "remoting/protocol/it2me_host_authenticator_factory.h" |
| 24 #include "remoting/protocol/network_settings.h" | 25 #include "remoting/protocol/network_settings.h" |
| 25 #include "remoting/signaling/server_log_entry.h" | 26 #include "remoting/signaling/server_log_entry.h" |
| 26 | 27 |
| 27 namespace remoting { | 28 namespace remoting { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 // This is used for tagging system event logs. | 32 // This is used for tagging system event logs. |
| 32 const char kApplicationName[] = "chromoting"; | 33 const char kApplicationName[] = "chromoting"; |
| 33 const int kMaxLoginAttempts = 5; | 34 const int kMaxLoginAttempts = 5; |
| 34 | 35 |
| 35 } // namespace | 36 } // namespace |
| 36 | 37 |
| 37 It2MeHost::It2MeHost( | 38 It2MeHost::It2MeHost( |
| 38 scoped_ptr<ChromotingHostContext> host_context, | 39 ChromotingHostContext* host_context, |
| 39 scoped_ptr<policy_hack::PolicyWatcher> policy_watcher, | 40 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 40 base::WeakPtr<It2MeHost::Observer> observer, | 41 base::WeakPtr<It2MeHost::Observer> observer, |
| 41 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, | 42 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, |
| 42 const std::string& directory_bot_jid) | 43 const std::string& directory_bot_jid) |
| 43 : host_context_(host_context.Pass()), | 44 : host_context_(host_context), |
| 44 task_runner_(host_context_->ui_task_runner()), | 45 task_runner_(task_runner), |
| 45 observer_(observer), | 46 observer_(observer), |
| 46 xmpp_server_config_(xmpp_server_config), | 47 xmpp_server_config_(xmpp_server_config), |
| 47 directory_bot_jid_(directory_bot_jid), | 48 directory_bot_jid_(directory_bot_jid), |
| 48 state_(kDisconnected), | 49 state_(kDisconnected), |
| 49 failed_login_attempts_(0), | 50 failed_login_attempts_(0), |
| 50 policy_watcher_(policy_watcher.Pass()), | |
| 51 nat_traversal_enabled_(false), | 51 nat_traversal_enabled_(false), |
| 52 policy_received_(false) { | 52 policy_received_(false) { |
| 53 DCHECK(task_runner_->BelongsToCurrentThread()); | 53 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void It2MeHost::Connect() { | 56 void It2MeHost::Connect() { |
| 57 if (!host_context_->ui_task_runner()->BelongsToCurrentThread()) { | 57 if (!host_context_->ui_task_runner()->BelongsToCurrentThread()) { |
| 58 DCHECK(task_runner_->BelongsToCurrentThread()); | 58 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 59 host_context_->ui_task_runner()->PostTask( | 59 host_context_->ui_task_runner()->PostTask( |
| 60 FROM_HERE, base::Bind(&It2MeHost::Connect, this)); | 60 FROM_HERE, base::Bind(&It2MeHost::Connect, this)); |
| 61 return; | 61 return; |
| 62 } | 62 } |
| 63 | 63 |
| 64 desktop_environment_factory_.reset(new It2MeDesktopEnvironmentFactory( | 64 desktop_environment_factory_.reset(new It2MeDesktopEnvironmentFactory( |
| 65 host_context_->network_task_runner(), | 65 host_context_->network_task_runner(), |
| 66 host_context_->input_task_runner(), | 66 host_context_->input_task_runner(), |
| 67 host_context_->ui_task_runner())); | 67 host_context_->ui_task_runner())); |
| 68 | 68 |
| 69 // Start monitoring configured policies. | 69 // Start monitoring configured policies. |
| 70 policy_watcher_->StartWatching(base::Bind(&It2MeHost::OnPolicyUpdate, this)); | 70 policy_watcher_.reset( |
| 71 policy_hack::PolicyWatcher::Create(host_context_->network_task_runner())); |
| 72 policy_watcher_->StartWatching( |
| 73 base::Bind(&It2MeHost::OnPolicyUpdate, this)); |
| 71 | 74 |
| 72 // Switch to the network thread to start the actual connection. | 75 // Switch to the network thread to start the actual connection. |
| 73 host_context_->network_task_runner()->PostTask( | 76 host_context_->network_task_runner()->PostTask( |
| 74 FROM_HERE, base::Bind(&It2MeHost::ReadPolicyAndConnect, this)); | 77 FROM_HERE, base::Bind(&It2MeHost::ReadPolicyAndConnect, this)); |
| 75 } | 78 } |
| 76 | 79 |
| 77 void It2MeHost::Disconnect() { | 80 void It2MeHost::Disconnect() { |
| 78 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { | 81 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { |
| 79 DCHECK(task_runner_->BelongsToCurrentThread()); | 82 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 80 host_context_->network_task_runner()->PostTask( | 83 host_context_->network_task_runner()->PostTask( |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 } | 250 } |
| 248 | 251 |
| 249 void It2MeHost::ShutdownOnUiThread() { | 252 void It2MeHost::ShutdownOnUiThread() { |
| 250 DCHECK(host_context_->ui_task_runner()->BelongsToCurrentThread()); | 253 DCHECK(host_context_->ui_task_runner()->BelongsToCurrentThread()); |
| 251 | 254 |
| 252 // Destroy the DesktopEnvironmentFactory, to free thread references. | 255 // Destroy the DesktopEnvironmentFactory, to free thread references. |
| 253 desktop_environment_factory_.reset(); | 256 desktop_environment_factory_.reset(); |
| 254 | 257 |
| 255 // Stop listening for policy updates. | 258 // Stop listening for policy updates. |
| 256 if (policy_watcher_.get()) { | 259 if (policy_watcher_.get()) { |
| 257 policy_watcher_->StopWatching( | 260 base::WaitableEvent policy_watcher_stopped_(true, false); |
| 258 base::Bind(&It2MeHost::OnPolicyWatcherShutdown, this)); | 261 policy_watcher_->StopWatching(&policy_watcher_stopped_); |
| 259 return; | 262 policy_watcher_stopped_.Wait(); |
| 263 policy_watcher_.reset(); |
| 260 } | 264 } |
| 261 } | 265 } |
| 262 | 266 |
| 263 void It2MeHost::OnPolicyWatcherShutdown() { | |
| 264 policy_watcher_.reset(); | |
| 265 } | |
| 266 | |
| 267 void It2MeHost::OnAccessDenied(const std::string& jid) { | 267 void It2MeHost::OnAccessDenied(const std::string& jid) { |
| 268 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); | 268 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); |
| 269 | 269 |
| 270 ++failed_login_attempts_; | 270 ++failed_login_attempts_; |
| 271 if (failed_login_attempts_ == kMaxLoginAttempts) { | 271 if (failed_login_attempts_ == kMaxLoginAttempts) { |
| 272 Disconnect(); | 272 Disconnect(); |
| 273 } | 273 } |
| 274 } | 274 } |
| 275 | 275 |
| 276 void It2MeHost::OnClientAuthenticated(const std::string& jid) { | 276 void It2MeHost::OnClientAuthenticated(const std::string& jid) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 304 SetState(kConnected); | 304 SetState(kConnected); |
| 305 } | 305 } |
| 306 | 306 |
| 307 void It2MeHost::OnClientDisconnected(const std::string& jid) { | 307 void It2MeHost::OnClientDisconnected(const std::string& jid) { |
| 308 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); | 308 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); |
| 309 | 309 |
| 310 Disconnect(); | 310 Disconnect(); |
| 311 } | 311 } |
| 312 | 312 |
| 313 void It2MeHost::OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies) { | 313 void It2MeHost::OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies) { |
| 314 // The policy watcher runs on the |ui_task_runner| on ChromeOS and the | 314 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); |
| 315 // |network_task_runner| on other platforms. | |
| 316 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { | |
| 317 host_context_->network_task_runner()->PostTask( | |
| 318 FROM_HERE, | |
| 319 base::Bind(&It2MeHost::OnPolicyUpdate, this, base::Passed(&policies))); | |
| 320 return; | |
| 321 } | |
| 322 | 315 |
| 323 bool nat_policy; | 316 bool nat_policy; |
| 324 if (policies->GetBoolean(policy_hack::PolicyWatcher::kNatPolicyName, | 317 if (policies->GetBoolean(policy_hack::PolicyWatcher::kNatPolicyName, |
| 325 &nat_policy)) { | 318 &nat_policy)) { |
| 326 UpdateNatPolicy(nat_policy); | 319 UpdateNatPolicy(nat_policy); |
| 327 } | 320 } |
| 328 std::string host_domain; | 321 std::string host_domain; |
| 329 if (policies->GetString(policy_hack::PolicyWatcher::kHostDomainPolicyName, | 322 if (policies->GetString(policy_hack::PolicyWatcher::kHostDomainPolicyName, |
| 330 &host_domain)) { | 323 &host_domain)) { |
| 331 UpdateHostDomainPolicy(host_domain); | 324 UpdateHostDomainPolicy(host_domain); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 host_->SetAuthenticatorFactory(factory.Pass()); | 452 host_->SetAuthenticatorFactory(factory.Pass()); |
| 460 | 453 |
| 461 // Pass the Access Code to the script object before changing state. | 454 // Pass the Access Code to the script object before changing state. |
| 462 task_runner_->PostTask( | 455 task_runner_->PostTask( |
| 463 FROM_HERE, base::Bind(&It2MeHost::Observer::OnStoreAccessCode, | 456 FROM_HERE, base::Bind(&It2MeHost::Observer::OnStoreAccessCode, |
| 464 observer_, access_code, lifetime)); | 457 observer_, access_code, lifetime)); |
| 465 | 458 |
| 466 SetState(kReceivedAccessCode); | 459 SetState(kReceivedAccessCode); |
| 467 } | 460 } |
| 468 | 461 |
| 469 It2MeHostFactory::It2MeHostFactory() : policy_service_(nullptr) { | 462 It2MeHostFactory::It2MeHostFactory() {} |
| 470 } | |
| 471 | 463 |
| 472 It2MeHostFactory::~It2MeHostFactory() {} | 464 It2MeHostFactory::~It2MeHostFactory() {} |
| 473 | 465 |
| 474 void It2MeHostFactory::set_policy_service( | |
| 475 policy::PolicyService* policy_service) { | |
| 476 DCHECK(policy_service); | |
| 477 DCHECK(!policy_service_) << "|policy_service| can only be set once."; | |
| 478 policy_service_ = policy_service; | |
| 479 } | |
| 480 | |
| 481 scoped_refptr<It2MeHost> It2MeHostFactory::CreateIt2MeHost( | 466 scoped_refptr<It2MeHost> It2MeHostFactory::CreateIt2MeHost( |
| 482 scoped_ptr<ChromotingHostContext> context, | 467 ChromotingHostContext* context, |
| 468 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 483 base::WeakPtr<It2MeHost::Observer> observer, | 469 base::WeakPtr<It2MeHost::Observer> observer, |
| 484 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, | 470 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, |
| 485 const std::string& directory_bot_jid) { | 471 const std::string& directory_bot_jid) { |
| 486 scoped_ptr<policy_hack::PolicyWatcher> policy_watcher = | 472 return new It2MeHost( |
| 487 policy_hack::PolicyWatcher::Create(policy_service_, | 473 context, task_runner, observer, xmpp_server_config, directory_bot_jid); |
| 488 context->network_task_runner()); | |
| 489 return new It2MeHost(context.Pass(), policy_watcher.Pass(), observer, | |
| 490 xmpp_server_config, directory_bot_jid); | |
| 491 } | 474 } |
| 492 | 475 |
| 493 } // namespace remoting | 476 } // namespace remoting |
| OLD | NEW |