| 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 // This file implements a standalone host process for Me2Me. | 5 // This file implements a standalone host process for Me2Me. |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/debug/alias.h" | 13 #include "base/debug/alias.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/synchronization/waitable_event.h" | |
| 23 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
| 24 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 25 #include "crypto/nss_util.h" | 24 #include "crypto/nss_util.h" |
| 26 #include "ipc/ipc_channel.h" | 25 #include "ipc/ipc_channel.h" |
| 27 #include "ipc/ipc_channel_proxy.h" | 26 #include "ipc/ipc_channel_proxy.h" |
| 28 #include "ipc/ipc_listener.h" | 27 #include "ipc/ipc_listener.h" |
| 29 #include "media/base/media.h" | 28 #include "media/base/media.h" |
| 30 #include "net/base/network_change_notifier.h" | 29 #include "net/base/network_change_notifier.h" |
| 31 #include "net/socket/client_socket_factory.h" | 30 #include "net/socket/client_socket_factory.h" |
| 32 #include "net/socket/ssl_server_socket.h" | 31 #include "net/socket/ssl_server_socket.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 | 140 |
| 142 namespace remoting { | 141 namespace remoting { |
| 143 | 142 |
| 144 class HostProcess | 143 class HostProcess |
| 145 : public ConfigWatcher::Delegate, | 144 : public ConfigWatcher::Delegate, |
| 146 public HeartbeatSender::Listener, | 145 public HeartbeatSender::Listener, |
| 147 public HostChangeNotificationListener::Listener, | 146 public HostChangeNotificationListener::Listener, |
| 148 public IPC::Listener, | 147 public IPC::Listener, |
| 149 public base::RefCountedThreadSafe<HostProcess> { | 148 public base::RefCountedThreadSafe<HostProcess> { |
| 150 public: | 149 public: |
| 151 HostProcess(scoped_ptr<ChromotingHostContext> context, | 150 HostProcess(scoped_refptr<ChromotingHostContext> context, |
| 152 int* exit_code_out); | 151 int* exit_code_out); |
| 153 | 152 |
| 154 // ConfigWatcher::Delegate interface. | 153 // ConfigWatcher::Delegate interface. |
| 155 virtual void OnConfigUpdated(const std::string& serialized_config) override; | 154 virtual void OnConfigUpdated(const std::string& serialized_config) override; |
| 156 virtual void OnConfigWatcherError() override; | 155 virtual void OnConfigWatcherError() override; |
| 157 | 156 |
| 158 // IPC::Listener implementation. | 157 // IPC::Listener implementation. |
| 159 virtual bool OnMessageReceived(const IPC::Message& message) override; | 158 virtual bool OnMessageReceived(const IPC::Message& message) override; |
| 160 virtual void OnChannelError() override; | 159 virtual void OnChannelError() override; |
| 161 | 160 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 | 252 |
| 254 void RestartHost(); | 253 void RestartHost(); |
| 255 | 254 |
| 256 // Stops the host and shuts down the process with the specified |exit_code|. | 255 // Stops the host and shuts down the process with the specified |exit_code|. |
| 257 void ShutdownHost(HostExitCodes exit_code); | 256 void ShutdownHost(HostExitCodes exit_code); |
| 258 | 257 |
| 259 void ScheduleHostShutdown(); | 258 void ScheduleHostShutdown(); |
| 260 | 259 |
| 261 void ShutdownOnNetworkThread(); | 260 void ShutdownOnNetworkThread(); |
| 262 | 261 |
| 262 void PostPolicyWatcherShutdown(); |
| 263 |
| 263 // Crashes the process in response to a daemon's request. The daemon passes | 264 // Crashes the process in response to a daemon's request. The daemon passes |
| 264 // the location of the code that detected the fatal error resulted in this | 265 // the location of the code that detected the fatal error resulted in this |
| 265 // request. | 266 // request. |
| 266 void OnCrash(const std::string& function_name, | 267 void OnCrash(const std::string& function_name, |
| 267 const std::string& file_name, | 268 const std::string& file_name, |
| 268 const int& line_number); | 269 const int& line_number); |
| 269 | 270 |
| 270 scoped_ptr<ChromotingHostContext> context_; | 271 scoped_refptr<ChromotingHostContext> context_; |
| 271 | 272 |
| 272 // Created on the UI thread but used from the network thread. | 273 // Created on the UI thread but used from the network thread. |
| 273 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 274 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 274 | 275 |
| 275 // Accessed on the UI thread. | 276 // Accessed on the UI thread. |
| 276 scoped_ptr<IPC::ChannelProxy> daemon_channel_; | 277 scoped_ptr<IPC::ChannelProxy> daemon_channel_; |
| 277 | 278 |
| 278 // XMPP server/remoting bot configuration (initialized from the command line). | 279 // XMPP server/remoting bot configuration (initialized from the command line). |
| 279 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; | 280 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; |
| 280 std::string directory_bot_jid_; | 281 std::string directory_bot_jid_; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 #if defined(REMOTING_MULTI_PROCESS) | 339 #if defined(REMOTING_MULTI_PROCESS) |
| 339 DesktopSessionConnector* desktop_session_connector_; | 340 DesktopSessionConnector* desktop_session_connector_; |
| 340 #endif // defined(REMOTING_MULTI_PROCESS) | 341 #endif // defined(REMOTING_MULTI_PROCESS) |
| 341 | 342 |
| 342 int* exit_code_out_; | 343 int* exit_code_out_; |
| 343 bool signal_parent_; | 344 bool signal_parent_; |
| 344 | 345 |
| 345 scoped_ptr<PairingRegistry::Delegate> pairing_registry_delegate_; | 346 scoped_ptr<PairingRegistry::Delegate> pairing_registry_delegate_; |
| 346 }; | 347 }; |
| 347 | 348 |
| 348 HostProcess::HostProcess(scoped_ptr<ChromotingHostContext> context, | 349 HostProcess::HostProcess(scoped_refptr<ChromotingHostContext> context, |
| 349 int* exit_code_out) | 350 int* exit_code_out) |
| 350 : context_(context.Pass()), | 351 : context_(context), |
| 351 state_(HOST_INITIALIZING), | 352 state_(HOST_INITIALIZING), |
| 352 use_service_account_(false), | 353 use_service_account_(false), |
| 353 enable_vp9_(false), | 354 enable_vp9_(false), |
| 354 frame_recorder_buffer_size_(0), | 355 frame_recorder_buffer_size_(0), |
| 355 host_username_match_required_(false), | 356 host_username_match_required_(false), |
| 356 allow_nat_traversal_(true), | 357 allow_nat_traversal_(true), |
| 357 allow_relay_(true), | 358 allow_relay_(true), |
| 358 min_udp_port_(0), | 359 min_udp_port_(0), |
| 359 max_udp_port_(0), | 360 max_udp_port_(0), |
| 360 allow_pairing_(true), | 361 allow_pairing_(true), |
| 361 curtain_required_(false), | 362 curtain_required_(false), |
| 362 enable_gnubby_auth_(false), | 363 enable_gnubby_auth_(false), |
| 363 enable_window_capture_(false), | 364 enable_window_capture_(false), |
| 364 window_id_(0), | 365 window_id_(0), |
| 365 #if defined(REMOTING_MULTI_PROCESS) | 366 #if defined(REMOTING_MULTI_PROCESS) |
| 366 desktop_session_connector_(NULL), | 367 desktop_session_connector_(NULL), |
| 367 #endif // defined(REMOTING_MULTI_PROCESS) | 368 #endif // defined(REMOTING_MULTI_PROCESS) |
| 368 self_(this), | 369 self_(this), |
| 369 exit_code_out_(exit_code_out), | 370 exit_code_out_(exit_code_out), |
| 370 signal_parent_(false) { | 371 signal_parent_(false) { |
| 371 StartOnUiThread(); | 372 StartOnUiThread(); |
| 372 } | 373 } |
| 373 | 374 |
| 374 HostProcess::~HostProcess() { | 375 HostProcess::~HostProcess() { |
| 375 // Verify that UI components have been torn down. | 376 // Verify that UI components have been torn down. |
| 376 DCHECK(!config_watcher_); | 377 DCHECK(!config_watcher_); |
| 377 DCHECK(!daemon_channel_); | 378 DCHECK(!daemon_channel_); |
| 378 DCHECK(!desktop_environment_factory_); | 379 DCHECK(!desktop_environment_factory_); |
| 379 | |
| 380 // We might be getting deleted on one of the threads the |host_context| owns, | |
| 381 // so we need to post it back to the caller thread to safely join & delete the | |
| 382 // threads it contains. This will go away when we move to AutoThread. | |
| 383 // |context_release()| will null |context_| before the method is invoked, so | |
| 384 // we need to pull out the task-runner on which to call DeleteSoon first. | |
| 385 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | |
| 386 context_->ui_task_runner(); | |
| 387 task_runner->DeleteSoon(FROM_HERE, context_.release()); | |
| 388 } | 380 } |
| 389 | 381 |
| 390 bool HostProcess::InitWithCommandLine(const base::CommandLine* cmd_line) { | 382 bool HostProcess::InitWithCommandLine(const base::CommandLine* cmd_line) { |
| 391 #if defined(REMOTING_MULTI_PROCESS) | 383 #if defined(REMOTING_MULTI_PROCESS) |
| 392 // Parse the handle value and convert it to a handle/file descriptor. | 384 // Parse the handle value and convert it to a handle/file descriptor. |
| 393 std::string channel_name = | 385 std::string channel_name = |
| 394 cmd_line->GetSwitchValueASCII(kDaemonPipeSwitchName); | 386 cmd_line->GetSwitchValueASCII(kDaemonPipeSwitchName); |
| 395 | 387 |
| 396 int pipe_handle = 0; | 388 int pipe_handle = 0; |
| 397 if (channel_name.empty() || | 389 if (channel_name.empty() || |
| (...skipping 1006 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1404 oauth_token_getter_.reset(); | 1396 oauth_token_getter_.reset(); |
| 1405 signal_strategy_.reset(); | 1397 signal_strategy_.reset(); |
| 1406 network_change_notifier_.reset(); | 1398 network_change_notifier_.reset(); |
| 1407 | 1399 |
| 1408 if (state_ == HOST_STOPPING_TO_RESTART) { | 1400 if (state_ == HOST_STOPPING_TO_RESTART) { |
| 1409 StartHost(); | 1401 StartHost(); |
| 1410 } else if (state_ == HOST_STOPPING) { | 1402 } else if (state_ == HOST_STOPPING) { |
| 1411 state_ = HOST_STOPPED; | 1403 state_ = HOST_STOPPED; |
| 1412 | 1404 |
| 1413 if (policy_watcher_.get()) { | 1405 if (policy_watcher_.get()) { |
| 1414 base::WaitableEvent done_event(true, false); | 1406 policy_watcher_->StopWatching( |
| 1415 policy_watcher_->StopWatching(&done_event); | 1407 base::Bind(&HostProcess::PostPolicyWatcherShutdown, this)); |
| 1416 done_event.Wait(); | 1408 } else { |
| 1417 policy_watcher_.reset(); | 1409 PostPolicyWatcherShutdown(); |
| 1418 } | 1410 } |
| 1419 | |
| 1420 config_watcher_.reset(); | |
| 1421 | |
| 1422 // Complete the rest of shutdown on the main thread. | |
| 1423 context_->ui_task_runner()->PostTask( | |
| 1424 FROM_HERE, | |
| 1425 base::Bind(&HostProcess::ShutdownOnUiThread, this)); | |
| 1426 } else { | 1411 } else { |
| 1427 // This method is only called in STOPPING_TO_RESTART and STOPPING states. | 1412 // This method is only called in STOPPING_TO_RESTART and STOPPING states. |
| 1428 NOTREACHED(); | 1413 NOTREACHED(); |
| 1429 } | 1414 } |
| 1430 } | 1415 } |
| 1431 | 1416 |
| 1417 void HostProcess::PostPolicyWatcherShutdown() { |
| 1418 policy_watcher_.reset(); |
| 1419 |
| 1420 // Complete the rest of shutdown on the main thread. |
| 1421 context_->ui_task_runner()->PostTask( |
| 1422 FROM_HERE, base::Bind(&HostProcess::ShutdownOnUiThread, this)); |
| 1423 } |
| 1424 |
| 1432 void HostProcess::OnCrash(const std::string& function_name, | 1425 void HostProcess::OnCrash(const std::string& function_name, |
| 1433 const std::string& file_name, | 1426 const std::string& file_name, |
| 1434 const int& line_number) { | 1427 const int& line_number) { |
| 1435 char message[1024]; | 1428 char message[1024]; |
| 1436 base::snprintf(message, sizeof(message), | 1429 base::snprintf(message, sizeof(message), |
| 1437 "Requested by %s at %s, line %d.", | 1430 "Requested by %s at %s, line %d.", |
| 1438 function_name.c_str(), file_name.c_str(), line_number); | 1431 function_name.c_str(), file_name.c_str(), line_number); |
| 1439 base::debug::Alias(message); | 1432 base::debug::Alias(message); |
| 1440 | 1433 |
| 1441 // The daemon requested us to crash the process. | 1434 // The daemon requested us to crash the process. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1455 | 1448 |
| 1456 // Enable support for SSL server sockets, which must be done while still | 1449 // Enable support for SSL server sockets, which must be done while still |
| 1457 // single-threaded. | 1450 // single-threaded. |
| 1458 net::EnableSSLServerSockets(); | 1451 net::EnableSSLServerSockets(); |
| 1459 | 1452 |
| 1460 // Ensures runtime specific CPU features are initialized. | 1453 // Ensures runtime specific CPU features are initialized. |
| 1461 media::InitializeCPUSpecificMediaFeatures(); | 1454 media::InitializeCPUSpecificMediaFeatures(); |
| 1462 | 1455 |
| 1463 // Create the main message loop and start helper threads. | 1456 // Create the main message loop and start helper threads. |
| 1464 base::MessageLoopForUI message_loop; | 1457 base::MessageLoopForUI message_loop; |
| 1465 scoped_ptr<ChromotingHostContext> context = | 1458 scoped_refptr<ChromotingHostContext> context = |
| 1466 ChromotingHostContext::Create(new AutoThreadTaskRunner( | 1459 ChromotingHostContext::Create(new AutoThreadTaskRunner( |
| 1467 message_loop.message_loop_proxy(), base::MessageLoop::QuitClosure())); | 1460 message_loop.message_loop_proxy(), base::MessageLoop::QuitClosure())); |
| 1468 if (!context) | 1461 if (!context.get()) |
| 1469 return kInitializationFailed; | 1462 return kInitializationFailed; |
| 1470 | 1463 |
| 1471 // Create & start the HostProcess using these threads. | 1464 // Create & start the HostProcess using these threads. |
| 1472 // TODO(wez): The HostProcess holds a reference to itself until Shutdown(). | 1465 // TODO(wez): The HostProcess holds a reference to itself until Shutdown(). |
| 1473 // Remove this hack as part of the multi-process refactoring. | 1466 // Remove this hack as part of the multi-process refactoring. |
| 1474 int exit_code = kSuccessExitCode; | 1467 int exit_code = kSuccessExitCode; |
| 1475 new HostProcess(context.Pass(), &exit_code); | 1468 new HostProcess(context, &exit_code); |
| 1476 | 1469 |
| 1477 // Run the main (also UI) message loop until the host no longer needs it. | 1470 // Run the main (also UI) message loop until the host no longer needs it. |
| 1478 message_loop.Run(); | 1471 message_loop.Run(); |
| 1479 | 1472 |
| 1480 return exit_code; | 1473 return exit_code; |
| 1481 } | 1474 } |
| 1482 | 1475 |
| 1483 } // namespace remoting | 1476 } // namespace remoting |
| OLD | NEW |