Chromium Code Reviews| 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 220 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_ptr<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. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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()); | |
|
Jamie
2014/10/14 01:18:42
Why has this been deleted?
kelvinp
2014/10/15 23:03:10
Good catch
| |
| 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 517 LOG(ERROR) << "Failed to apply the configuration."; | 509 LOG(ERROR) << "Failed to apply the configuration."; |
| 518 ShutdownHost(kInvalidHostConfigurationExitCode); | 510 ShutdownHost(kInvalidHostConfigurationExitCode); |
| 519 return; | 511 return; |
| 520 } | 512 } |
| 521 | 513 |
| 522 if (state_ == HOST_INITIALIZING) { | 514 if (state_ == HOST_INITIALIZING) { |
| 523 // TODO(sergeyu): Currently OnPolicyUpdate() assumes that host config is | 515 // TODO(sergeyu): Currently OnPolicyUpdate() assumes that host config is |
| 524 // already loaded so PolicyWatcher has to be started here. Separate policy | 516 // already loaded so PolicyWatcher has to be started here. Separate policy |
| 525 // loading from policy verifications and move |policy_watcher_| | 517 // loading from policy verifications and move |policy_watcher_| |
| 526 // initialization to StartOnNetworkThread(). | 518 // initialization to StartOnNetworkThread(). |
| 527 policy_watcher_.reset( | 519 policy_watcher_.reset(policy_hack::PolicyWatcher::Create( |
| 528 policy_hack::PolicyWatcher::Create(context_->file_task_runner())); | 520 context_.get(), context_->file_task_runner())); |
| 529 policy_watcher_->StartWatching( | 521 policy_watcher_->StartWatching( |
| 530 base::Bind(&HostProcess::OnPolicyUpdate, base::Unretained(this))); | 522 base::Bind(&HostProcess::OnPolicyUpdate, base::Unretained(this))); |
| 531 } else { | 523 } else { |
| 532 // Reapply policies that could be affected by a new config. | 524 // Reapply policies that could be affected by a new config. |
| 533 ApplyHostDomainPolicy(); | 525 ApplyHostDomainPolicy(); |
| 534 ApplyUsernamePolicy(); | 526 ApplyUsernamePolicy(); |
| 535 | 527 |
| 536 if (state_ == HOST_STARTED) { | 528 if (state_ == HOST_STARTED) { |
| 537 // TODO(sergeyu): Here we assume that PIN is the only part of the config | 529 // TODO(sergeyu): Here we assume that PIN is the only part of the config |
| 538 // that may change while the service is running. Change ApplyConfig() to | 530 // that may change while the service is running. Change ApplyConfig() to |
| (...skipping 865 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() { | |
|
rmsousa
2014/10/14 00:18:25
nit: OnPolicyWatcherShutdown.
"Post" is an ambiguo
kelvinp
2014/10/15 23:03:10
Done.
| |
| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1474 int exit_code = kSuccessExitCode; | 1467 int exit_code = kSuccessExitCode; |
| 1475 new HostProcess(context.Pass(), &exit_code); | 1468 new HostProcess(context.Pass(), &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 |