| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/plugin/host_script_object.h" | 5 #include "remoting/host/plugin/host_script_object.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 // Called by ReadPolicyAndConnect once policies have been read. | 135 // Called by ReadPolicyAndConnect once policies have been read. |
| 136 void FinishConnect(const std::string& uid, | 136 void FinishConnect(const std::string& uid, |
| 137 const std::string& auth_token, | 137 const std::string& auth_token, |
| 138 const std::string& auth_service); | 138 const std::string& auth_service); |
| 139 | 139 |
| 140 // Called when the support host registration completes. | 140 // Called when the support host registration completes. |
| 141 void OnReceivedSupportID(bool success, | 141 void OnReceivedSupportID(bool success, |
| 142 const std::string& support_id, | 142 const std::string& support_id, |
| 143 const base::TimeDelta& lifetime); | 143 const base::TimeDelta& lifetime); |
| 144 | 144 |
| 145 // Called when ChromotingHost::Shutdown() has completed. | 145 // Shuts down |host_| on the network thread and posts ShutdownOnUiThread() |
| 146 void OnShutdownFinished(); | 146 // to shut down UI thread resources. |
| 147 void ShutdownOnNetworkThread(); |
| 148 |
| 149 // Shuts down |desktop_environment_factory_| and |policy_watcher_| on |
| 150 // the UI thread. |
| 151 void ShutdownOnUiThread(); |
| 147 | 152 |
| 148 // Called when initial policies are read, and when they change. | 153 // Called when initial policies are read, and when they change. |
| 149 void OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies); | 154 void OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies); |
| 150 | 155 |
| 151 // Handlers for NAT traversal and host domain policies. | 156 // Handlers for NAT traversal and host domain policies. |
| 152 void UpdateNatPolicy(bool nat_traversal_enabled); | 157 void UpdateNatPolicy(bool nat_traversal_enabled); |
| 153 void UpdateHostDomainPolicy(const std::string& host_domain); | 158 void UpdateHostDomainPolicy(const std::string& host_domain); |
| 154 | 159 |
| 155 // Caller supplied fields. | 160 // Caller supplied fields. |
| 156 scoped_ptr<ChromotingHostContext> host_context_; | 161 scoped_ptr<ChromotingHostContext> host_context_; |
| 157 scoped_refptr<base::SingleThreadTaskRunner> plugin_task_runner_; | 162 scoped_refptr<base::SingleThreadTaskRunner> plugin_task_runner_; |
| 158 base::WeakPtr<HostNPScriptObject> script_object_; | 163 base::WeakPtr<HostNPScriptObject> script_object_; |
| 159 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; | 164 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; |
| 160 std::string directory_bot_jid_; | 165 std::string directory_bot_jid_; |
| 161 | 166 |
| 162 State state_; | 167 State state_; |
| 163 | 168 |
| 164 scoped_refptr<RsaKeyPair> host_key_pair_; | 169 scoped_refptr<RsaKeyPair> host_key_pair_; |
| 165 scoped_ptr<SignalStrategy> signal_strategy_; | 170 scoped_ptr<SignalStrategy> signal_strategy_; |
| 166 scoped_ptr<RegisterSupportHostRequest> register_request_; | 171 scoped_ptr<RegisterSupportHostRequest> register_request_; |
| 167 scoped_ptr<LogToServer> log_to_server_; | 172 scoped_ptr<LogToServer> log_to_server_; |
| 168 scoped_ptr<DesktopEnvironmentFactory> desktop_environment_factory_; | 173 scoped_ptr<DesktopEnvironmentFactory> desktop_environment_factory_; |
| 169 scoped_ptr<HostEventLogger> host_event_logger_; | 174 scoped_ptr<HostEventLogger> host_event_logger_; |
| 170 | 175 |
| 171 scoped_refptr<ChromotingHost> host_; | 176 scoped_ptr<ChromotingHost> host_; |
| 172 int failed_login_attempts_; | 177 int failed_login_attempts_; |
| 173 | 178 |
| 174 scoped_ptr<policy_hack::PolicyWatcher> policy_watcher_; | 179 scoped_ptr<policy_hack::PolicyWatcher> policy_watcher_; |
| 175 | 180 |
| 176 // Host the current nat traversal policy setting. | 181 // Host the current nat traversal policy setting. |
| 177 bool nat_traversal_enabled_; | 182 bool nat_traversal_enabled_; |
| 178 | 183 |
| 179 // The host domain policy setting. | 184 // The host domain policy setting. |
| 180 std::string required_host_domain_; | 185 std::string required_host_domain_; |
| 181 | 186 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 void HostNPScriptObject::It2MeImpl::Disconnect() { | 252 void HostNPScriptObject::It2MeImpl::Disconnect() { |
| 248 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { | 253 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { |
| 249 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); | 254 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); |
| 250 host_context_->network_task_runner()->PostTask( | 255 host_context_->network_task_runner()->PostTask( |
| 251 FROM_HERE, base::Bind(&It2MeImpl::Disconnect, this)); | 256 FROM_HERE, base::Bind(&It2MeImpl::Disconnect, this)); |
| 252 return; | 257 return; |
| 253 } | 258 } |
| 254 | 259 |
| 255 switch (state_) { | 260 switch (state_) { |
| 256 case kDisconnected: | 261 case kDisconnected: |
| 257 OnShutdownFinished(); | 262 ShutdownOnNetworkThread(); |
| 258 return; | 263 return; |
| 259 | 264 |
| 260 case kStarting: | 265 case kStarting: |
| 261 SetState(kDisconnecting); | 266 SetState(kDisconnecting); |
| 262 SetState(kDisconnected); | 267 SetState(kDisconnected); |
| 263 OnShutdownFinished(); | 268 ShutdownOnNetworkThread(); |
| 264 return; | 269 return; |
| 265 | 270 |
| 266 case kDisconnecting: | 271 case kDisconnecting: |
| 267 return; | 272 return; |
| 268 | 273 |
| 269 default: | 274 default: |
| 270 SetState(kDisconnecting); | 275 SetState(kDisconnecting); |
| 271 | 276 |
| 272 if (!host_) { | 277 if (!host_) { |
| 273 OnShutdownFinished(); | 278 SetState(kDisconnected); |
| 279 ShutdownOnNetworkThread(); |
| 274 return; | 280 return; |
| 275 } | 281 } |
| 276 | 282 |
| 277 // ChromotingHost::Shutdown() may destroy SignalStrategy | 283 // Deleting the host destroys SignalStrategy synchronously, but |
| 278 // synchronously, but SignalStrategy::Listener handlers are not | 284 // SignalStrategy::Listener handlers are not allowed to destroy |
| 279 // allowed to destroy SignalStrategy, so post task to call | 285 // SignalStrategy, so post task to destroy the host later. |
| 280 // Shutdown() later. | |
| 281 host_context_->network_task_runner()->PostTask( | 286 host_context_->network_task_runner()->PostTask( |
| 282 FROM_HERE, base::Bind( | 287 FROM_HERE, base::Bind(&It2MeImpl::ShutdownOnNetworkThread, this)); |
| 283 &ChromotingHost::Shutdown, host_, | |
| 284 base::Bind(&It2MeImpl::OnShutdownFinished, this))); | |
| 285 return; | 288 return; |
| 286 } | 289 } |
| 287 } | 290 } |
| 288 | 291 |
| 289 void HostNPScriptObject::It2MeImpl::RequestNatPolicy() { | 292 void HostNPScriptObject::It2MeImpl::RequestNatPolicy() { |
| 290 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { | 293 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { |
| 291 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); | 294 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); |
| 292 host_context_->network_task_runner()->PostTask( | 295 host_context_->network_task_runner()->PostTask( |
| 293 FROM_HERE, base::Bind(&It2MeImpl::RequestNatPolicy, this)); | 296 FROM_HERE, base::Bind(&It2MeImpl::RequestNatPolicy, this)); |
| 294 return; | 297 return; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 NetworkSettings network_settings( | 365 NetworkSettings network_settings( |
| 363 nat_traversal_enabled_ ? | 366 nat_traversal_enabled_ ? |
| 364 NetworkSettings::NAT_TRAVERSAL_ENABLED : | 367 NetworkSettings::NAT_TRAVERSAL_ENABLED : |
| 365 NetworkSettings::NAT_TRAVERSAL_DISABLED); | 368 NetworkSettings::NAT_TRAVERSAL_DISABLED); |
| 366 if (!nat_traversal_enabled_) { | 369 if (!nat_traversal_enabled_) { |
| 367 network_settings.min_port = NetworkSettings::kDefaultMinPort; | 370 network_settings.min_port = NetworkSettings::kDefaultMinPort; |
| 368 network_settings.max_port = NetworkSettings::kDefaultMaxPort; | 371 network_settings.max_port = NetworkSettings::kDefaultMaxPort; |
| 369 } | 372 } |
| 370 | 373 |
| 371 // Create the host. | 374 // Create the host. |
| 372 host_ = new ChromotingHost( | 375 host_.reset(new ChromotingHost( |
| 373 signal_strategy_.get(), | 376 signal_strategy_.get(), |
| 374 desktop_environment_factory_.get(), | 377 desktop_environment_factory_.get(), |
| 375 CreateHostSessionManager(network_settings, | 378 CreateHostSessionManager(network_settings, |
| 376 host_context_->url_request_context_getter()), | 379 host_context_->url_request_context_getter()), |
| 377 host_context_->audio_task_runner(), | 380 host_context_->audio_task_runner(), |
| 378 host_context_->input_task_runner(), | 381 host_context_->input_task_runner(), |
| 379 host_context_->video_capture_task_runner(), | 382 host_context_->video_capture_task_runner(), |
| 380 host_context_->video_encode_task_runner(), | 383 host_context_->video_encode_task_runner(), |
| 381 host_context_->network_task_runner(), | 384 host_context_->network_task_runner(), |
| 382 host_context_->ui_task_runner()); | 385 host_context_->ui_task_runner())); |
| 383 host_->AddStatusObserver(this); | 386 host_->AddStatusObserver(this); |
| 384 log_to_server_.reset( | 387 log_to_server_.reset( |
| 385 new LogToServer(host_->AsWeakPtr(), ServerLogEntry::IT2ME, | 388 new LogToServer(host_->AsWeakPtr(), ServerLogEntry::IT2ME, |
| 386 signal_strategy_.get(), directory_bot_jid_)); | 389 signal_strategy_.get(), directory_bot_jid_)); |
| 387 | 390 |
| 388 // Disable audio by default. | 391 // Disable audio by default. |
| 389 // TODO(sergeyu): Add UI to enable it. | 392 // TODO(sergeyu): Add UI to enable it. |
| 390 scoped_ptr<protocol::CandidateSessionConfig> protocol_config = | 393 scoped_ptr<protocol::CandidateSessionConfig> protocol_config = |
| 391 protocol::CandidateSessionConfig::CreateDefault(); | 394 protocol::CandidateSessionConfig::CreateDefault(); |
| 392 protocol::CandidateSessionConfig::DisableAudioChannel(protocol_config.get()); | 395 protocol::CandidateSessionConfig::DisableAudioChannel(protocol_config.get()); |
| 393 host_->set_protocol_config(protocol_config.Pass()); | 396 host_->set_protocol_config(protocol_config.Pass()); |
| 394 | 397 |
| 395 // Create event logger. | 398 // Create event logger. |
| 396 host_event_logger_ = | 399 host_event_logger_ = |
| 397 HostEventLogger::Create(host_->AsWeakPtr(), kApplicationName); | 400 HostEventLogger::Create(host_->AsWeakPtr(), kApplicationName); |
| 398 | 401 |
| 399 // Connect signaling and start the host. | 402 // Connect signaling and start the host. |
| 400 signal_strategy_->Connect(); | 403 signal_strategy_->Connect(); |
| 401 host_->Start(uid); | 404 host_->Start(uid); |
| 402 | 405 |
| 403 SetState(kRequestedAccessCode); | 406 SetState(kRequestedAccessCode); |
| 404 return; | 407 return; |
| 405 } | 408 } |
| 406 | 409 |
| 407 void HostNPScriptObject::It2MeImpl::OnShutdownFinished() { | 410 void HostNPScriptObject::It2MeImpl::ShutdownOnNetworkThread() { |
| 408 if (!host_context_->ui_task_runner()->BelongsToCurrentThread()) { | 411 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); |
| 409 host_context_->ui_task_runner()->PostTask( | 412 DCHECK(state_ == kDisconnecting || state_ == kDisconnected); |
| 410 FROM_HERE, base::Bind(&It2MeImpl::OnShutdownFinished, this)); | 413 |
| 411 return; | 414 if (state_ == kDisconnecting) { |
| 415 host_.reset(); |
| 416 SetState(kDisconnected); |
| 412 } | 417 } |
| 413 | 418 |
| 414 // Note that OnShutdownFinished() may be called more than once. | 419 host_context_->ui_task_runner()->PostTask( |
| 420 FROM_HERE, base::Bind(&It2MeImpl::ShutdownOnUiThread, this)); |
| 421 } |
| 422 |
| 423 void HostNPScriptObject::It2MeImpl::ShutdownOnUiThread() { |
| 424 DCHECK(host_context_->ui_task_runner()->BelongsToCurrentThread()); |
| 415 | 425 |
| 416 // Destroy the DesktopEnvironmentFactory, to free thread references. | 426 // Destroy the DesktopEnvironmentFactory, to free thread references. |
| 417 desktop_environment_factory_.reset(); | 427 desktop_environment_factory_.reset(); |
| 418 | 428 |
| 419 // Stop listening for policy updates. | 429 // Stop listening for policy updates. |
| 420 if (policy_watcher_.get()) { | 430 if (policy_watcher_.get()) { |
| 421 base::WaitableEvent policy_watcher_stopped_(true, false); | 431 base::WaitableEvent policy_watcher_stopped_(true, false); |
| 422 policy_watcher_->StopWatching(&policy_watcher_stopped_); | 432 policy_watcher_->StopWatching(&policy_watcher_stopped_); |
| 423 policy_watcher_stopped_.Wait(); | 433 policy_watcher_stopped_.Wait(); |
| 424 policy_watcher_.reset(); | 434 policy_watcher_.reset(); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 } | 489 } |
| 480 | 490 |
| 481 void HostNPScriptObject::It2MeImpl::OnShutdown() { | 491 void HostNPScriptObject::It2MeImpl::OnShutdown() { |
| 482 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); | 492 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); |
| 483 | 493 |
| 484 register_request_.reset(); | 494 register_request_.reset(); |
| 485 log_to_server_.reset(); | 495 log_to_server_.reset(); |
| 486 signal_strategy_.reset(); | 496 signal_strategy_.reset(); |
| 487 host_event_logger_.reset(); | 497 host_event_logger_.reset(); |
| 488 host_->RemoveStatusObserver(this); | 498 host_->RemoveStatusObserver(this); |
| 489 host_ = NULL; | |
| 490 | |
| 491 if (state_ != kDisconnected) { | |
| 492 SetState(kDisconnected); | |
| 493 } | |
| 494 } | 499 } |
| 495 | 500 |
| 496 void HostNPScriptObject::It2MeImpl::OnPolicyUpdate( | 501 void HostNPScriptObject::It2MeImpl::OnPolicyUpdate( |
| 497 scoped_ptr<base::DictionaryValue> policies) { | 502 scoped_ptr<base::DictionaryValue> policies) { |
| 498 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); | 503 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); |
| 499 | 504 |
| 500 bool nat_policy; | 505 bool nat_policy; |
| 501 if (policies->GetBoolean(policy_hack::PolicyWatcher::kNatPolicyName, | 506 if (policies->GetBoolean(policy_hack::PolicyWatcher::kNatPolicyName, |
| 502 &nat_policy)) { | 507 &nat_policy)) { |
| 503 UpdateNatPolicy(nat_policy); | 508 UpdateNatPolicy(nat_policy); |
| (...skipping 1074 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1578 } | 1583 } |
| 1579 | 1584 |
| 1580 void HostNPScriptObject::SetException(const std::string& exception_string) { | 1585 void HostNPScriptObject::SetException(const std::string& exception_string) { |
| 1581 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); | 1586 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); |
| 1582 | 1587 |
| 1583 g_npnetscape_funcs->setexception(parent_, exception_string.c_str()); | 1588 g_npnetscape_funcs->setexception(parent_, exception_string.c_str()); |
| 1584 LOG(INFO) << exception_string; | 1589 LOG(INFO) << exception_string; |
| 1585 } | 1590 } |
| 1586 | 1591 |
| 1587 } // namespace remoting | 1592 } // namespace remoting |
| OLD | NEW |