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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 // Also called internally, from the network thread. | 107 // Also called internally, from the network thread. |
108 void Disconnect(); | 108 void Disconnect(); |
109 | 109 |
110 // Request a NAT policy notification. | 110 // Request a NAT policy notification. |
111 void RequestNatPolicy(); | 111 void RequestNatPolicy(); |
112 | 112 |
113 // remoting::HostStatusObserver implementation. | 113 // remoting::HostStatusObserver implementation. |
114 virtual void OnAccessDenied(const std::string& jid) OVERRIDE; | 114 virtual void OnAccessDenied(const std::string& jid) OVERRIDE; |
115 virtual void OnClientAuthenticated(const std::string& jid) OVERRIDE; | 115 virtual void OnClientAuthenticated(const std::string& jid) OVERRIDE; |
116 virtual void OnClientDisconnected(const std::string& jid) OVERRIDE; | 116 virtual void OnClientDisconnected(const std::string& jid) OVERRIDE; |
117 virtual void OnShutdown() OVERRIDE; | |
118 | 117 |
119 private: | 118 private: |
120 friend class base::RefCountedThreadSafe<It2MeImpl>; | 119 friend class base::RefCountedThreadSafe<It2MeImpl>; |
121 | 120 |
122 virtual ~It2MeImpl(); | 121 virtual ~It2MeImpl(); |
123 | 122 |
124 // Updates state of the host. Can be called only on the network thread. | 123 // Updates state of the host. Can be called only on the network thread. |
125 void SetState(State state); | 124 void SetState(State state); |
126 | 125 |
127 // Returns true if the host is connected. | 126 // Returns true if the host is connected. |
128 bool IsConnected() const; | 127 bool IsConnected() const; |
129 | 128 |
130 // Called by Connect() to check for policies and start connection process. | 129 // Called by Connect() to check for policies and start connection process. |
131 void ReadPolicyAndConnect(const std::string& uid, | 130 void ReadPolicyAndConnect(const std::string& uid, |
132 const std::string& auth_token, | 131 const std::string& auth_token, |
133 const std::string& auth_service); | 132 const std::string& auth_service); |
134 | 133 |
135 // Called by ReadPolicyAndConnect once policies have been read. | 134 // Called by ReadPolicyAndConnect once policies have been read. |
136 void FinishConnect(const std::string& uid, | 135 void FinishConnect(const std::string& uid, |
137 const std::string& auth_token, | 136 const std::string& auth_token, |
138 const std::string& auth_service); | 137 const std::string& auth_service); |
139 | 138 |
140 // Called when the support host registration completes. | 139 // Called when the support host registration completes. |
141 void OnReceivedSupportID(bool success, | 140 void OnReceivedSupportID(bool success, |
142 const std::string& support_id, | 141 const std::string& support_id, |
143 const base::TimeDelta& lifetime); | 142 const base::TimeDelta& lifetime); |
144 | 143 |
145 // Called when ChromotingHost::Shutdown() has completed. | 144 // Shuts down |host_| on the network thread and posts ShutdownOnUiThread() |
146 void OnShutdownFinished(); | 145 // to shut down UI thread resources. |
146 void ShutdownOnNetworkThread(); | |
147 | |
148 // Shuts down |desktop_environment_factory_| and |policy_watcher_| on | |
149 // the UI thread. | |
150 void ShutdownOnUiThread(); | |
147 | 151 |
148 // Called when initial policies are read, and when they change. | 152 // Called when initial policies are read, and when they change. |
149 void OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies); | 153 void OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies); |
150 | 154 |
151 // Handlers for NAT traversal and host domain policies. | 155 // Handlers for NAT traversal and host domain policies. |
152 void UpdateNatPolicy(bool nat_traversal_enabled); | 156 void UpdateNatPolicy(bool nat_traversal_enabled); |
153 void UpdateHostDomainPolicy(const std::string& host_domain); | 157 void UpdateHostDomainPolicy(const std::string& host_domain); |
154 | 158 |
155 // Caller supplied fields. | 159 // Caller supplied fields. |
156 scoped_ptr<ChromotingHostContext> host_context_; | 160 scoped_ptr<ChromotingHostContext> host_context_; |
157 scoped_refptr<base::SingleThreadTaskRunner> plugin_task_runner_; | 161 scoped_refptr<base::SingleThreadTaskRunner> plugin_task_runner_; |
158 base::WeakPtr<HostNPScriptObject> script_object_; | 162 base::WeakPtr<HostNPScriptObject> script_object_; |
159 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; | 163 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; |
160 std::string directory_bot_jid_; | 164 std::string directory_bot_jid_; |
161 | 165 |
162 State state_; | 166 State state_; |
163 | 167 |
164 scoped_refptr<RsaKeyPair> host_key_pair_; | 168 scoped_refptr<RsaKeyPair> host_key_pair_; |
165 scoped_ptr<SignalStrategy> signal_strategy_; | 169 scoped_ptr<SignalStrategy> signal_strategy_; |
166 scoped_ptr<RegisterSupportHostRequest> register_request_; | 170 scoped_ptr<RegisterSupportHostRequest> register_request_; |
167 scoped_ptr<LogToServer> log_to_server_; | 171 scoped_ptr<LogToServer> log_to_server_; |
168 scoped_ptr<DesktopEnvironmentFactory> desktop_environment_factory_; | 172 scoped_ptr<DesktopEnvironmentFactory> desktop_environment_factory_; |
169 scoped_ptr<HostEventLogger> host_event_logger_; | 173 scoped_ptr<HostEventLogger> host_event_logger_; |
170 | 174 |
171 scoped_refptr<ChromotingHost> host_; | 175 scoped_ptr<ChromotingHost> host_; |
172 int failed_login_attempts_; | 176 int failed_login_attempts_; |
173 | 177 |
174 scoped_ptr<policy_hack::PolicyWatcher> policy_watcher_; | 178 scoped_ptr<policy_hack::PolicyWatcher> policy_watcher_; |
175 | 179 |
176 // Host the current nat traversal policy setting. | 180 // Host the current nat traversal policy setting. |
177 bool nat_traversal_enabled_; | 181 bool nat_traversal_enabled_; |
178 | 182 |
179 // The host domain policy setting. | 183 // The host domain policy setting. |
180 std::string required_host_domain_; | 184 std::string required_host_domain_; |
181 | 185 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 void HostNPScriptObject::It2MeImpl::Disconnect() { | 251 void HostNPScriptObject::It2MeImpl::Disconnect() { |
248 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { | 252 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { |
249 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); | 253 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); |
250 host_context_->network_task_runner()->PostTask( | 254 host_context_->network_task_runner()->PostTask( |
251 FROM_HERE, base::Bind(&It2MeImpl::Disconnect, this)); | 255 FROM_HERE, base::Bind(&It2MeImpl::Disconnect, this)); |
252 return; | 256 return; |
253 } | 257 } |
254 | 258 |
255 switch (state_) { | 259 switch (state_) { |
256 case kDisconnected: | 260 case kDisconnected: |
257 OnShutdownFinished(); | 261 ShutdownOnNetworkThread(); |
258 return; | 262 return; |
259 | 263 |
260 case kStarting: | 264 case kStarting: |
261 SetState(kDisconnecting); | 265 SetState(kDisconnecting); |
262 SetState(kDisconnected); | 266 SetState(kDisconnected); |
263 OnShutdownFinished(); | 267 ShutdownOnNetworkThread(); |
264 return; | 268 return; |
265 | 269 |
266 case kDisconnecting: | 270 case kDisconnecting: |
267 return; | 271 return; |
268 | 272 |
269 default: | 273 default: |
270 SetState(kDisconnecting); | 274 SetState(kDisconnecting); |
Sergey Ulanov
2013/04/30 18:26:46
nit: we don't need disconnecting state anymore bec
alexeypa (please no reviews)
2013/04/30 18:39:56
The disconnecting state is still needed because we
Sergey Ulanov
2013/04/30 18:55:19
Right, but potentially we could just always post a
| |
271 | 275 |
272 if (!host_) { | 276 if (!host_) { |
273 OnShutdownFinished(); | 277 SetState(kDisconnected); |
278 ShutdownOnNetworkThread(); | |
274 return; | 279 return; |
275 } | 280 } |
276 | 281 |
277 // ChromotingHost::Shutdown() may destroy SignalStrategy | 282 // Deleting the host destroys SignalStrategy synchronously, but |
278 // synchronously, but SignalStrategy::Listener handlers are not | 283 // SignalStrategy::Listener handlers are not allowed to destroy |
279 // allowed to destroy SignalStrategy, so post task to call | 284 // SignalStrategy, so post task to destroy the host later. |
280 // Shutdown() later. | |
281 host_context_->network_task_runner()->PostTask( | 285 host_context_->network_task_runner()->PostTask( |
282 FROM_HERE, base::Bind( | 286 FROM_HERE, base::Bind(&It2MeImpl::ShutdownOnNetworkThread, this)); |
283 &ChromotingHost::Shutdown, host_, | |
284 base::Bind(&It2MeImpl::OnShutdownFinished, this))); | |
285 return; | 287 return; |
286 } | 288 } |
287 } | 289 } |
288 | 290 |
289 void HostNPScriptObject::It2MeImpl::RequestNatPolicy() { | 291 void HostNPScriptObject::It2MeImpl::RequestNatPolicy() { |
290 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { | 292 if (!host_context_->network_task_runner()->BelongsToCurrentThread()) { |
291 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); | 293 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); |
292 host_context_->network_task_runner()->PostTask( | 294 host_context_->network_task_runner()->PostTask( |
293 FROM_HERE, base::Bind(&It2MeImpl::RequestNatPolicy, this)); | 295 FROM_HERE, base::Bind(&It2MeImpl::RequestNatPolicy, this)); |
294 return; | 296 return; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
362 NetworkSettings network_settings( | 364 NetworkSettings network_settings( |
363 nat_traversal_enabled_ ? | 365 nat_traversal_enabled_ ? |
364 NetworkSettings::NAT_TRAVERSAL_ENABLED : | 366 NetworkSettings::NAT_TRAVERSAL_ENABLED : |
365 NetworkSettings::NAT_TRAVERSAL_DISABLED); | 367 NetworkSettings::NAT_TRAVERSAL_DISABLED); |
366 if (!nat_traversal_enabled_) { | 368 if (!nat_traversal_enabled_) { |
367 network_settings.min_port = NetworkSettings::kDefaultMinPort; | 369 network_settings.min_port = NetworkSettings::kDefaultMinPort; |
368 network_settings.max_port = NetworkSettings::kDefaultMaxPort; | 370 network_settings.max_port = NetworkSettings::kDefaultMaxPort; |
369 } | 371 } |
370 | 372 |
371 // Create the host. | 373 // Create the host. |
372 host_ = new ChromotingHost( | 374 host_.reset(new ChromotingHost( |
373 signal_strategy_.get(), | 375 signal_strategy_.get(), |
374 desktop_environment_factory_.get(), | 376 desktop_environment_factory_.get(), |
375 CreateHostSessionManager(network_settings, | 377 CreateHostSessionManager(network_settings, |
376 host_context_->url_request_context_getter()), | 378 host_context_->url_request_context_getter()), |
377 host_context_->audio_task_runner(), | 379 host_context_->audio_task_runner(), |
378 host_context_->input_task_runner(), | 380 host_context_->input_task_runner(), |
379 host_context_->video_capture_task_runner(), | 381 host_context_->video_capture_task_runner(), |
380 host_context_->video_encode_task_runner(), | 382 host_context_->video_encode_task_runner(), |
381 host_context_->network_task_runner(), | 383 host_context_->network_task_runner(), |
382 host_context_->ui_task_runner()); | 384 host_context_->ui_task_runner())); |
383 host_->AddStatusObserver(this); | 385 host_->AddStatusObserver(this); |
384 log_to_server_.reset( | 386 log_to_server_.reset( |
385 new LogToServer(host_->AsWeakPtr(), ServerLogEntry::IT2ME, | 387 new LogToServer(host_->AsWeakPtr(), ServerLogEntry::IT2ME, |
386 signal_strategy_.get(), directory_bot_jid_)); | 388 signal_strategy_.get(), directory_bot_jid_)); |
387 | 389 |
388 // Disable audio by default. | 390 // Disable audio by default. |
389 // TODO(sergeyu): Add UI to enable it. | 391 // TODO(sergeyu): Add UI to enable it. |
390 scoped_ptr<protocol::CandidateSessionConfig> protocol_config = | 392 scoped_ptr<protocol::CandidateSessionConfig> protocol_config = |
391 protocol::CandidateSessionConfig::CreateDefault(); | 393 protocol::CandidateSessionConfig::CreateDefault(); |
392 protocol::CandidateSessionConfig::DisableAudioChannel(protocol_config.get()); | 394 protocol::CandidateSessionConfig::DisableAudioChannel(protocol_config.get()); |
393 host_->set_protocol_config(protocol_config.Pass()); | 395 host_->set_protocol_config(protocol_config.Pass()); |
394 | 396 |
395 // Create event logger. | 397 // Create event logger. |
396 host_event_logger_ = | 398 host_event_logger_ = |
397 HostEventLogger::Create(host_->AsWeakPtr(), kApplicationName); | 399 HostEventLogger::Create(host_->AsWeakPtr(), kApplicationName); |
398 | 400 |
399 // Connect signaling and start the host. | 401 // Connect signaling and start the host. |
400 signal_strategy_->Connect(); | 402 signal_strategy_->Connect(); |
401 host_->Start(uid); | 403 host_->Start(uid); |
402 | 404 |
403 SetState(kRequestedAccessCode); | 405 SetState(kRequestedAccessCode); |
404 return; | 406 return; |
405 } | 407 } |
406 | 408 |
407 void HostNPScriptObject::It2MeImpl::OnShutdownFinished() { | 409 void HostNPScriptObject::It2MeImpl::ShutdownOnNetworkThread() { |
408 if (!host_context_->ui_task_runner()->BelongsToCurrentThread()) { | 410 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); |
409 host_context_->ui_task_runner()->PostTask( | 411 DCHECK(state_ == kDisconnecting || state_ == kDisconnected); |
410 FROM_HERE, base::Bind(&It2MeImpl::OnShutdownFinished, this)); | 412 |
411 return; | 413 if (state_ == kDisconnecting) { |
414 host_event_logger_.reset(); | |
415 host_->RemoveStatusObserver(this); | |
Sergey Ulanov
2013/04/30 18:26:46
Don't need this anymore, I think.
alexeypa (please no reviews)
2013/04/30 18:39:56
ObserverList<>::~ObserverList DCHECKs that all obs
| |
416 host_.reset(); | |
417 | |
418 register_request_.reset(); | |
419 log_to_server_.reset(); | |
420 signal_strategy_.reset(); | |
421 SetState(kDisconnected); | |
412 } | 422 } |
413 | 423 |
414 // Note that OnShutdownFinished() may be called more than once. | 424 host_context_->ui_task_runner()->PostTask( |
425 FROM_HERE, base::Bind(&It2MeImpl::ShutdownOnUiThread, this)); | |
426 } | |
427 | |
428 void HostNPScriptObject::It2MeImpl::ShutdownOnUiThread() { | |
429 DCHECK(host_context_->ui_task_runner()->BelongsToCurrentThread()); | |
415 | 430 |
416 // Destroy the DesktopEnvironmentFactory, to free thread references. | 431 // Destroy the DesktopEnvironmentFactory, to free thread references. |
417 desktop_environment_factory_.reset(); | 432 desktop_environment_factory_.reset(); |
418 | 433 |
419 // Stop listening for policy updates. | 434 // Stop listening for policy updates. |
420 if (policy_watcher_.get()) { | 435 if (policy_watcher_.get()) { |
421 base::WaitableEvent policy_watcher_stopped_(true, false); | 436 base::WaitableEvent policy_watcher_stopped_(true, false); |
422 policy_watcher_->StopWatching(&policy_watcher_stopped_); | 437 policy_watcher_->StopWatching(&policy_watcher_stopped_); |
423 policy_watcher_stopped_.Wait(); | 438 policy_watcher_stopped_.Wait(); |
424 policy_watcher_.reset(); | 439 policy_watcher_.reset(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); | 486 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); |
472 | 487 |
473 // Pass the client user name to the script object before changing state. | 488 // Pass the client user name to the script object before changing state. |
474 plugin_task_runner_->PostTask( | 489 plugin_task_runner_->PostTask( |
475 FROM_HERE, base::Bind(&HostNPScriptObject::StoreClientUsername, | 490 FROM_HERE, base::Bind(&HostNPScriptObject::StoreClientUsername, |
476 script_object_, std::string())); | 491 script_object_, std::string())); |
477 | 492 |
478 Disconnect(); | 493 Disconnect(); |
479 } | 494 } |
480 | 495 |
481 void HostNPScriptObject::It2MeImpl::OnShutdown() { | |
482 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); | |
483 | |
484 register_request_.reset(); | |
485 log_to_server_.reset(); | |
486 signal_strategy_.reset(); | |
487 host_event_logger_.reset(); | |
488 host_->RemoveStatusObserver(this); | |
489 host_ = NULL; | |
490 | |
491 if (state_ != kDisconnected) { | |
492 SetState(kDisconnected); | |
493 } | |
494 } | |
495 | |
496 void HostNPScriptObject::It2MeImpl::OnPolicyUpdate( | 496 void HostNPScriptObject::It2MeImpl::OnPolicyUpdate( |
497 scoped_ptr<base::DictionaryValue> policies) { | 497 scoped_ptr<base::DictionaryValue> policies) { |
498 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); | 498 DCHECK(host_context_->network_task_runner()->BelongsToCurrentThread()); |
499 | 499 |
500 bool nat_policy; | 500 bool nat_policy; |
501 if (policies->GetBoolean(policy_hack::PolicyWatcher::kNatPolicyName, | 501 if (policies->GetBoolean(policy_hack::PolicyWatcher::kNatPolicyName, |
502 &nat_policy)) { | 502 &nat_policy)) { |
503 UpdateNatPolicy(nat_policy); | 503 UpdateNatPolicy(nat_policy); |
504 } | 504 } |
505 std::string host_domain; | 505 std::string host_domain; |
(...skipping 1076 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1582 } | 1582 } |
1583 | 1583 |
1584 void HostNPScriptObject::SetException(const std::string& exception_string) { | 1584 void HostNPScriptObject::SetException(const std::string& exception_string) { |
1585 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); | 1585 DCHECK(plugin_task_runner_->BelongsToCurrentThread()); |
1586 | 1586 |
1587 g_npnetscape_funcs->setexception(parent_, exception_string.c_str()); | 1587 g_npnetscape_funcs->setexception(parent_, exception_string.c_str()); |
1588 LOG(INFO) << exception_string; | 1588 LOG(INFO) << exception_string; |
1589 } | 1589 } |
1590 | 1590 |
1591 } // namespace remoting | 1591 } // namespace remoting |
OLD | NEW |