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" |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 const char kWindowIdSwitchName[] = "window-id"; | 142 const char kWindowIdSwitchName[] = "window-id"; |
143 | 143 |
144 // Maximum time to wait for clean shutdown to occur, before forcing termination | 144 // Maximum time to wait for clean shutdown to occur, before forcing termination |
145 // of the process. | 145 // of the process. |
146 const int kShutdownTimeoutSeconds = 15; | 146 const int kShutdownTimeoutSeconds = 15; |
147 | 147 |
148 // Maximum time to wait for reporting host-offline-reason to the service, | 148 // Maximum time to wait for reporting host-offline-reason to the service, |
149 // before continuing normal process shutdown. | 149 // before continuing normal process shutdown. |
150 const int kHostOfflineReasonTimeoutSeconds = 10; | 150 const int kHostOfflineReasonTimeoutSeconds = 10; |
151 | 151 |
152 // Host offline reasons not associated with shutting down the host process | |
153 // and therefore not expressible through HostExitCodes enum. | |
154 const char kHostOfflineReasonPolicyReadError[] = "POLICY_READ_ERROR"; | |
155 const char kHostOfflineReasonPolicyChangeRequiresRestart[] = | |
156 "POLICY_CHANGE_REQUIRES_RESTART"; | |
157 | |
158 } // namespace | 152 } // namespace |
159 | 153 |
160 namespace remoting { | 154 namespace remoting { |
161 | 155 |
162 class HostProcess : public ConfigWatcher::Delegate, | 156 class HostProcess : public ConfigWatcher::Delegate, |
163 public HostSignalingManager::Listener, | 157 public HostSignalingManager::Listener, |
164 public HostChangeNotificationListener::Listener, | 158 public HostChangeNotificationListener::Listener, |
165 public IPC::Listener, | 159 public IPC::Listener, |
166 public base::RefCountedThreadSafe<HostProcess> { | 160 public base::RefCountedThreadSafe<HostProcess> { |
167 public: | 161 public: |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 scoped_ptr<HostSignalingManager> CreateHostSignalingManager(); | 270 scoped_ptr<HostSignalingManager> CreateHostSignalingManager(); |
277 | 271 |
278 void StartHostIfReady(); | 272 void StartHostIfReady(); |
279 void StartHost(); | 273 void StartHost(); |
280 | 274 |
281 // Overrides for HostSignalingManager::Listener interface. | 275 // Overrides for HostSignalingManager::Listener interface. |
282 void OnHeartbeatSuccessful() override; | 276 void OnHeartbeatSuccessful() override; |
283 void OnUnknownHostIdError() override; | 277 void OnUnknownHostIdError() override; |
284 void OnAuthFailed() override; | 278 void OnAuthFailed() override; |
285 | 279 |
286 void RestartHost(const std::string& host_offline_reason); | 280 void RestartHost(HostExitCodes exit_code); |
287 void ShutdownHost(HostExitCodes exit_code); | 281 void ShutdownHost(HostExitCodes exit_code); |
288 | 282 |
289 // Helper methods doing the work needed by RestartHost and ShutdownHost. | 283 // Helper methods doing the work needed by RestartHost and ShutdownHost. |
290 void GoOffline(const std::string& host_offline_reason); | 284 void GoOffline(HostExitCodes exit_code); |
291 void OnHostOfflineReasonAck(bool success); | 285 void OnHostOfflineReasonAck(bool success); |
292 | 286 |
293 #if defined(OS_WIN) | 287 #if defined(OS_WIN) |
294 // Initializes the pairing registry on Windows. This should be invoked on the | 288 // Initializes the pairing registry on Windows. This should be invoked on the |
295 // network thread. | 289 // network thread. |
296 void InitializePairingRegistry( | 290 void InitializePairingRegistry( |
297 IPC::PlatformFileForTransit privileged_key, | 291 IPC::PlatformFileForTransit privileged_key, |
298 IPC::PlatformFileForTransit unprivileged_key); | 292 IPC::PlatformFileForTransit unprivileged_key); |
299 #endif // defined(OS_WIN) | 293 #endif // defined(OS_WIN) |
300 | 294 |
(...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1007 restart_required |= OnHostTokenUrlPolicyUpdate(policies.get()); | 1001 restart_required |= OnHostTokenUrlPolicyUpdate(policies.get()); |
1008 restart_required |= OnPairingPolicyUpdate(policies.get()); | 1002 restart_required |= OnPairingPolicyUpdate(policies.get()); |
1009 restart_required |= OnGnubbyAuthPolicyUpdate(policies.get()); | 1003 restart_required |= OnGnubbyAuthPolicyUpdate(policies.get()); |
1010 | 1004 |
1011 policy_state_ = POLICY_LOADED; | 1005 policy_state_ = POLICY_LOADED; |
1012 | 1006 |
1013 if (state_ == HOST_STARTING) { | 1007 if (state_ == HOST_STARTING) { |
1014 StartHostIfReady(); | 1008 StartHostIfReady(); |
1015 } else if (state_ == HOST_STARTED) { | 1009 } else if (state_ == HOST_STARTED) { |
1016 if (restart_required) | 1010 if (restart_required) |
1017 RestartHost(kHostOfflineReasonPolicyChangeRequiresRestart); | 1011 RestartHost(kPolicyChangeRequiresRestart); |
1018 } | 1012 } |
1019 } | 1013 } |
1020 | 1014 |
1021 void HostProcess::OnPolicyError() { | 1015 void HostProcess::OnPolicyError() { |
1022 if (!context_->network_task_runner()->BelongsToCurrentThread()) { | 1016 if (!context_->network_task_runner()->BelongsToCurrentThread()) { |
1023 context_->network_task_runner()->PostTask( | 1017 context_->network_task_runner()->PostTask( |
1024 FROM_HERE, base::Bind(&HostProcess::OnPolicyError, this)); | 1018 FROM_HERE, base::Bind(&HostProcess::OnPolicyError, this)); |
1025 return; | 1019 return; |
1026 } | 1020 } |
1027 | 1021 |
1028 if (policy_state_ != POLICY_ERROR_REPORTED) { | 1022 if (policy_state_ != POLICY_ERROR_REPORTED) { |
1029 policy_state_ = POLICY_ERROR_REPORT_PENDING; | 1023 policy_state_ = POLICY_ERROR_REPORT_PENDING; |
1030 if ((state_ == HOST_STARTED) || | 1024 if ((state_ == HOST_STARTED) || |
1031 (state_ == HOST_STARTING && !serialized_config_.empty())) { | 1025 (state_ == HOST_STARTING && !serialized_config_.empty())) { |
1032 ReportPolicyErrorAndRestartHost(); | 1026 ReportPolicyErrorAndRestartHost(); |
1033 } | 1027 } |
1034 } | 1028 } |
1035 } | 1029 } |
1036 | 1030 |
1037 void HostProcess::ReportPolicyErrorAndRestartHost() { | 1031 void HostProcess::ReportPolicyErrorAndRestartHost() { |
1038 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | 1032 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
1039 DCHECK(!serialized_config_.empty()); | 1033 DCHECK(!serialized_config_.empty()); |
1040 | 1034 |
1041 DCHECK_EQ(policy_state_, POLICY_ERROR_REPORT_PENDING); | 1035 DCHECK_EQ(policy_state_, POLICY_ERROR_REPORT_PENDING); |
1042 policy_state_ = POLICY_ERROR_REPORTED; | 1036 policy_state_ = POLICY_ERROR_REPORTED; |
1043 | 1037 |
1044 LOG(INFO) << "Restarting the host due to policy errors."; | 1038 LOG(INFO) << "Restarting the host due to policy errors."; |
1045 RestartHost(kHostOfflineReasonPolicyReadError); | 1039 RestartHost(kPolicyReadError); |
1046 } | 1040 } |
1047 | 1041 |
1048 void HostProcess::ApplyHostDomainPolicy() { | 1042 void HostProcess::ApplyHostDomainPolicy() { |
1049 if (state_ != HOST_STARTED) | 1043 if (state_ != HOST_STARTED) |
1050 return; | 1044 return; |
1051 | 1045 |
1052 HOST_LOG << "Policy sets host domain: " << host_domain_; | 1046 HOST_LOG << "Policy sets host domain: " << host_domain_; |
1053 | 1047 |
1054 if (!host_domain_.empty()) { | 1048 if (!host_domain_.empty()) { |
1055 // If the user does not have a Google email, their client JID will not be | 1049 // If the user does not have a Google email, their client JID will not be |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1406 CreateAuthenticatorFactory(); | 1400 CreateAuthenticatorFactory(); |
1407 | 1401 |
1408 ApplyHostDomainPolicy(); | 1402 ApplyHostDomainPolicy(); |
1409 ApplyUsernamePolicy(); | 1403 ApplyUsernamePolicy(); |
1410 } | 1404 } |
1411 | 1405 |
1412 void HostProcess::OnAuthFailed() { | 1406 void HostProcess::OnAuthFailed() { |
1413 ShutdownHost(kInvalidOauthCredentialsExitCode); | 1407 ShutdownHost(kInvalidOauthCredentialsExitCode); |
1414 } | 1408 } |
1415 | 1409 |
1416 void HostProcess::RestartHost(const std::string& host_offline_reason) { | 1410 void HostProcess::RestartHost(HostExitCodes exit_code) { |
1417 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | 1411 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
1418 DCHECK(!host_offline_reason.empty()); | |
1419 | 1412 |
1420 SetState(HOST_GOING_OFFLINE_TO_RESTART); | 1413 SetState(HOST_GOING_OFFLINE_TO_RESTART); |
1421 GoOffline(host_offline_reason); | 1414 GoOffline(exit_code); |
1422 } | 1415 } |
1423 | 1416 |
1424 void HostProcess::ShutdownHost(HostExitCodes exit_code) { | 1417 void HostProcess::ShutdownHost(HostExitCodes exit_code) { |
1425 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | 1418 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
1426 | 1419 |
1427 *exit_code_out_ = exit_code; | 1420 *exit_code_out_ = exit_code; |
1428 | 1421 |
1429 switch (state_) { | 1422 switch (state_) { |
1430 case HOST_STARTING: | 1423 case HOST_STARTING: |
1431 case HOST_STARTED: | 1424 case HOST_STARTED: |
1432 SetState(HOST_GOING_OFFLINE_TO_STOP); | 1425 SetState(HOST_GOING_OFFLINE_TO_STOP); |
1433 GoOffline(ExitCodeToString(exit_code)); | 1426 GoOffline(exit_code); |
1434 break; | 1427 break; |
1435 | 1428 |
1436 case HOST_GOING_OFFLINE_TO_RESTART: | 1429 case HOST_GOING_OFFLINE_TO_RESTART: |
1437 SetState(HOST_GOING_OFFLINE_TO_STOP); | 1430 SetState(HOST_GOING_OFFLINE_TO_STOP); |
1438 break; | 1431 break; |
1439 | 1432 |
1440 case HOST_GOING_OFFLINE_TO_STOP: | 1433 case HOST_GOING_OFFLINE_TO_STOP: |
1441 case HOST_STOPPED: | 1434 case HOST_STOPPED: |
1442 // Host is already stopped or being stopped. No action is required. | 1435 // Host is already stopped or being stopped. No action is required. |
1443 break; | 1436 break; |
1444 } | 1437 } |
1445 } | 1438 } |
1446 | 1439 |
1447 void HostProcess::GoOffline(const std::string& host_offline_reason) { | 1440 void HostProcess::GoOffline(HostExitCodes exit_code) { |
1448 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | 1441 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
1449 DCHECK(!host_offline_reason.empty()); | |
1450 DCHECK((state_ == HOST_GOING_OFFLINE_TO_STOP) || | 1442 DCHECK((state_ == HOST_GOING_OFFLINE_TO_STOP) || |
1451 (state_ == HOST_GOING_OFFLINE_TO_RESTART)); | 1443 (state_ == HOST_GOING_OFFLINE_TO_RESTART)); |
1452 | 1444 |
| 1445 std::string host_offline_reason = ExitCodeToString(exit_code); |
1453 // Shut down everything except the HostSignalingManager. | 1446 // Shut down everything except the HostSignalingManager. |
| 1447 host_->SetExitCode(exit_code); |
1454 host_.reset(); | 1448 host_.reset(); |
1455 host_event_logger_.reset(); | 1449 host_event_logger_.reset(); |
1456 host_status_logger_.reset(); | 1450 host_status_logger_.reset(); |
1457 host_change_notification_listener_.reset(); | 1451 host_change_notification_listener_.reset(); |
1458 | 1452 |
1459 // Before shutting down HostSignalingManager, send the |host_offline_reason| | 1453 // Before shutting down HostSignalingManager, send the |host_offline_reason| |
1460 // if possible (i.e. if we have the config). | 1454 // if possible (i.e. if we have the config). |
1461 if (!serialized_config_.empty()) { | 1455 if (!serialized_config_.empty()) { |
1462 if (!host_signaling_manager_) { | 1456 if (!host_signaling_manager_) { |
1463 host_signaling_manager_ = CreateHostSignalingManager(); | 1457 host_signaling_manager_ = CreateHostSignalingManager(); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1560 base::TimeDelta::FromSeconds(kShutdownTimeoutSeconds)); | 1554 base::TimeDelta::FromSeconds(kShutdownTimeoutSeconds)); |
1561 new HostProcess(context.Pass(), &exit_code, &shutdown_watchdog); | 1555 new HostProcess(context.Pass(), &exit_code, &shutdown_watchdog); |
1562 | 1556 |
1563 // Run the main (also UI) message loop until the host no longer needs it. | 1557 // Run the main (also UI) message loop until the host no longer needs it. |
1564 message_loop.Run(); | 1558 message_loop.Run(); |
1565 | 1559 |
1566 return exit_code; | 1560 return exit_code; |
1567 } | 1561 } |
1568 | 1562 |
1569 } // namespace remoting | 1563 } // namespace remoting |
OLD | NEW |