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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 const char kWindowIdSwitchName[] = "window-id"; | 140 const char kWindowIdSwitchName[] = "window-id"; |
141 | 141 |
142 // Maximum time to wait for clean shutdown to occur, before forcing termination | 142 // Maximum time to wait for clean shutdown to occur, before forcing termination |
143 // of the process. | 143 // of the process. |
144 const int kShutdownTimeoutSeconds = 15; | 144 const int kShutdownTimeoutSeconds = 15; |
145 | 145 |
146 // Maximum time to wait for reporting host-offline-reason to the service, | 146 // Maximum time to wait for reporting host-offline-reason to the service, |
147 // before continuing normal process shutdown. | 147 // before continuing normal process shutdown. |
148 const int kHostOfflineReasonTimeoutSeconds = 10; | 148 const int kHostOfflineReasonTimeoutSeconds = 10; |
149 | 149 |
150 // Host offline reasons not associated with shutting down the host process | |
151 // and therefore not expressible through HostExitCodes enum. | |
152 const char kHostOfflineReasonPolicyReadError[] = "POLICY_READ_ERROR"; | |
153 const char kHostOfflineReasonPolicyChangeRequiresRestart[] = | |
154 "POLICY_CHANGE_REQUIRES_RESTART"; | |
155 | |
150 } // namespace | 156 } // namespace |
151 | 157 |
152 namespace remoting { | 158 namespace remoting { |
153 | 159 |
154 class HostProcess : public ConfigWatcher::Delegate, | 160 class HostProcess : public ConfigWatcher::Delegate, |
155 public HostSignalingManager::Listener, | 161 public HostSignalingManager::Listener, |
156 public HostChangeNotificationListener::Listener, | 162 public HostChangeNotificationListener::Listener, |
157 public IPC::Listener, | 163 public IPC::Listener, |
158 public base::RefCountedThreadSafe<HostProcess> { | 164 public base::RefCountedThreadSafe<HostProcess> { |
159 public: | 165 public: |
(...skipping 16 matching lines...) Expand all Loading... | |
176 void OnHostDeleted() override; | 182 void OnHostDeleted() override; |
177 | 183 |
178 // Handler of the ChromotingDaemonNetworkMsg_InitializePairingRegistry IPC | 184 // Handler of the ChromotingDaemonNetworkMsg_InitializePairingRegistry IPC |
179 // message. | 185 // message. |
180 void OnInitializePairingRegistry( | 186 void OnInitializePairingRegistry( |
181 IPC::PlatformFileForTransit privileged_key, | 187 IPC::PlatformFileForTransit privileged_key, |
182 IPC::PlatformFileForTransit unprivileged_key); | 188 IPC::PlatformFileForTransit unprivileged_key); |
183 | 189 |
184 private: | 190 private: |
185 enum HostState { | 191 enum HostState { |
186 // Host process has just been started. Waiting for config and policies to be | 192 // Waiting for valid config and policies to be read from the disk. |
187 // read from the disk. | 193 // Either the host process has just been started, or it is trying to start |
194 // again after temporarily going offline due to policy change or error. | |
188 HOST_INITIALIZING, | 195 HOST_INITIALIZING, |
Wez
2015/02/14 00:49:15
Suggest renaming this to HOST_STARTING since it ca
Łukasz Anforowicz
2015/02/17 21:02:27
Done.
| |
189 | 196 |
190 // Host is started and running. | 197 // Host is started and running. |
191 HOST_STARTED, | 198 HOST_STARTED, |
192 | 199 |
193 // Host is being stopped and will need to be started again. | 200 // Host is sending offline reason, before trying to (re)initialize again. |
194 HOST_STOPPING_TO_RESTART, | 201 // Used when |
202 // 1) policy change (or a config change in the future) requires a restart. | |
Wez
2015/02/14 00:49:15
How can we detect a config change in the future...
Łukasz Anforowicz
2015/02/17 21:02:27
I meant that a config change can theoretically als
| |
203 // 2) policy error puts the host offline until we have a valid policy again. | |
204 HOST_GOING_OFFLINE_TO_RESTART, | |
Wez
2015/02/14 00:49:15
Why not simply HOST_RESTARTING or HOST_STOPPING_TO
Łukasz Anforowicz
2015/02/17 21:02:27
I like your suggestion for its brevity, but I thin
| |
195 | 205 |
196 // Host is being stopped. | 206 // Host is sending offline reason, before shutting down. |
197 HOST_STOPPING, | 207 HOST_GOING_OFFLINE_TO_STOP, |
Wez
2015/02/14 00:49:15
Why not simply HOST_STOPPING as it was before?
Łukasz Anforowicz
2015/02/17 21:02:27
Please see my other comment.
| |
198 | 208 |
199 // Host has been stopped. | 209 // Host has been stopped (host process will end soon). |
200 HOST_STOPPED, | 210 HOST_STOPPED, |
201 | 211 |
Łukasz Anforowicz
2015/02/13 20:41:00
Summary of the changes I made to "allowed state tr
| |
202 // Allowed state transitions: | 212 // Allowed state transitions (enforced via DCHECKs in SetState method): |
203 // INITIALIZING->STARTED | 213 // INITIALIZING->STARTED (once we have valid config + policy) |
204 // INITIALIZING->STOPPED | 214 // INITIALIZING->GOING_OFFLINE_TO_STOP |
205 // STARTED->STOPPING_TO_RESTART | 215 // INITIALIZING->GOING_OFFLINE_TO_RESTART |
206 // STARTED->STOPPING | 216 // STARTED->GOING_OFFLINE_TO_STOP |
207 // STOPPING_TO_RESTART->STARTED | 217 // STARTED->GOING_OFFLINE_TO_RESTART |
208 // STOPPING_TO_RESTART->STOPPING | 218 // GOING_OFFLINE_TO_RESTART->GOING_OFFLINE_TO_STOP |
209 // STOPPING->STOPPED | 219 // GOING_OFFLINE_TO_RESTART->INITIALIZING (after OfflineReasonAck) |
210 // STOPPED->STARTED | 220 // GOING_OFFLINE_TO_STOP->STOPPED (after OfflineReasonAck) |
211 // | 221 // |
212 // |host_| must be nullptr in INITIALIZING and STOPPED states and not | 222 // |host_| must be not-null in STARTED state and nullptr in all other states |
213 // nullptr in all other states. | 223 // (although this invariant can be temporarily violated when doing |
224 // synchronous processing on the networking thread). | |
225 }; | |
226 | |
227 void SetState(HostState target_state) { | |
228 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | |
229 | |
230 // DCHECKs below enforce state allowed transitions listed in HostState. | |
231 switch (state_) { | |
232 case HOST_INITIALIZING: | |
233 DCHECK((target_state == HOST_STARTED) || | |
234 (target_state == HOST_GOING_OFFLINE_TO_STOP) || | |
235 (target_state == HOST_GOING_OFFLINE_TO_RESTART)); | |
236 break; | |
237 case HOST_STARTED: | |
238 DCHECK((target_state == HOST_GOING_OFFLINE_TO_STOP) || | |
239 (target_state == HOST_GOING_OFFLINE_TO_RESTART)); | |
240 break; | |
241 case HOST_GOING_OFFLINE_TO_RESTART: | |
242 DCHECK((target_state == HOST_GOING_OFFLINE_TO_STOP) || | |
243 (target_state == HOST_INITIALIZING)); | |
244 break; | |
245 case HOST_GOING_OFFLINE_TO_STOP: | |
246 DCHECK_EQ(target_state, HOST_STOPPED); | |
247 break; | |
248 case HOST_STOPPED: | |
249 default: | |
250 NOTREACHED(); // HOST_STOPPED is a terminal state. | |
251 break; | |
252 } | |
253 state_ = target_state; | |
254 } | |
255 | |
256 enum PolicyState { | |
257 // Cannot start the host, because a valid policy has not been read yet. | |
258 POLICY_INITIALIZING, | |
259 | |
260 // Policy was loaded successfully. | |
261 POLICY_LOADED, | |
262 | |
263 // Policy error was detected, and we haven't yet sent out a | |
264 // host-offline-reason (i.e. because we haven't yet read the config). | |
265 POLICY_ERROR_REPORT_PENDING, | |
266 | |
267 // Policy error was detected, and we have sent out a host-offline-reason. | |
268 POLICY_ERROR_REPORTED, | |
214 }; | 269 }; |
215 | 270 |
216 friend class base::RefCountedThreadSafe<HostProcess>; | 271 friend class base::RefCountedThreadSafe<HostProcess>; |
217 ~HostProcess() override; | 272 ~HostProcess() override; |
218 | 273 |
219 void StartOnNetworkThread(); | 274 void StartOnNetworkThread(); |
220 | 275 |
221 #if defined(OS_POSIX) | 276 #if defined(OS_POSIX) |
222 // Callback passed to RegisterSignalHandler() to handle SIGTERM events. | 277 // Callback passed to RegisterSignalHandler() to handle SIGTERM events. |
223 void SigTermHandler(int signal_number); | 278 void SigTermHandler(int signal_number); |
(...skipping 14 matching lines...) Expand all Loading... | |
238 | 293 |
239 // Tear down resources that run on the UI thread. | 294 // Tear down resources that run on the UI thread. |
240 void ShutdownOnUiThread(); | 295 void ShutdownOnUiThread(); |
241 | 296 |
242 // Applies the host config, returning true if successful. | 297 // Applies the host config, returning true if successful. |
243 bool ApplyConfig(const base::DictionaryValue& config); | 298 bool ApplyConfig(const base::DictionaryValue& config); |
244 | 299 |
245 // Handles policy updates, by calling On*PolicyUpdate methods. | 300 // Handles policy updates, by calling On*PolicyUpdate methods. |
246 void OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies); | 301 void OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies); |
247 void OnPolicyError(); | 302 void OnPolicyError(); |
303 void ReportPolicyErrorAndRestartHost(); | |
248 void ApplyHostDomainPolicy(); | 304 void ApplyHostDomainPolicy(); |
249 void ApplyUsernamePolicy(); | 305 void ApplyUsernamePolicy(); |
250 bool OnHostDomainPolicyUpdate(base::DictionaryValue* policies); | 306 bool OnHostDomainPolicyUpdate(base::DictionaryValue* policies); |
251 bool OnUsernamePolicyUpdate(base::DictionaryValue* policies); | 307 bool OnUsernamePolicyUpdate(base::DictionaryValue* policies); |
252 bool OnNatPolicyUpdate(base::DictionaryValue* policies); | 308 bool OnNatPolicyUpdate(base::DictionaryValue* policies); |
253 bool OnRelayPolicyUpdate(base::DictionaryValue* policies); | 309 bool OnRelayPolicyUpdate(base::DictionaryValue* policies); |
254 bool OnUdpPortPolicyUpdate(base::DictionaryValue* policies); | 310 bool OnUdpPortPolicyUpdate(base::DictionaryValue* policies); |
255 bool OnCurtainPolicyUpdate(base::DictionaryValue* policies); | 311 bool OnCurtainPolicyUpdate(base::DictionaryValue* policies); |
256 bool OnHostTalkGadgetPrefixPolicyUpdate(base::DictionaryValue* policies); | 312 bool OnHostTalkGadgetPrefixPolicyUpdate(base::DictionaryValue* policies); |
257 bool OnHostTokenUrlPolicyUpdate(base::DictionaryValue* policies); | 313 bool OnHostTokenUrlPolicyUpdate(base::DictionaryValue* policies); |
258 bool OnPairingPolicyUpdate(base::DictionaryValue* policies); | 314 bool OnPairingPolicyUpdate(base::DictionaryValue* policies); |
259 bool OnGnubbyAuthPolicyUpdate(base::DictionaryValue* policies); | 315 bool OnGnubbyAuthPolicyUpdate(base::DictionaryValue* policies); |
260 | 316 |
261 scoped_ptr<HostSignalingManager> CreateHostSignalingManager(); | 317 scoped_ptr<HostSignalingManager> CreateHostSignalingManager(); |
262 | 318 |
263 void StartHostIfReady(); | 319 void StartHostIfReady(); |
264 void StartHost(); | 320 void StartHost(); |
265 | 321 |
266 // Overrides for HostSignalingManager::Listener interface. | 322 // Overrides for HostSignalingManager::Listener interface. |
267 void OnHeartbeatSuccessful() override; | 323 void OnHeartbeatSuccessful() override; |
268 void OnUnknownHostIdError() override; | 324 void OnUnknownHostIdError() override; |
269 void OnAuthFailed() override; | 325 void OnAuthFailed() override; |
270 | 326 |
271 void RestartHost(); | 327 void RestartHost(const std::string& host_offline_reason); |
272 | |
273 // Stops the host and shuts down the process with the specified |exit_code|. | |
274 void ShutdownHost(HostExitCodes exit_code); | 328 void ShutdownHost(HostExitCodes exit_code); |
275 | 329 |
276 // Private helper used by ShutdownHost method to initiate sending of | 330 // Helper methods doing the work needed by RestartHost and ShutdownHost. |
277 // host-offline-reason before continuing shutdown. | 331 void GoOffline(const std::string& host_offline_reason); |
278 void SendOfflineReasonAndShutdownOnNetworkThread(HostExitCodes exit_code); | 332 void OnHostOfflineReasonAck(bool success); |
279 | |
280 void ShutdownOnNetworkThread(); | |
281 | 333 |
282 #if defined(OS_WIN) | 334 #if defined(OS_WIN) |
283 // Initializes the pairing registry on Windows. This should be invoked on the | 335 // Initializes the pairing registry on Windows. This should be invoked on the |
284 // network thread. | 336 // network thread. |
285 void InitializePairingRegistry( | 337 void InitializePairingRegistry( |
286 IPC::PlatformFileForTransit privileged_key, | 338 IPC::PlatformFileForTransit privileged_key, |
287 IPC::PlatformFileForTransit unprivileged_key); | 339 IPC::PlatformFileForTransit unprivileged_key); |
288 #endif // defined(OS_WIN) | 340 #endif // defined(OS_WIN) |
289 | 341 |
290 // Crashes the process in response to a daemon's request. The daemon passes | 342 // Crashes the process in response to a daemon's request. The daemon passes |
(...skipping 27 matching lines...) Expand all Loading... | |
318 scoped_refptr<RsaKeyPair> key_pair_; | 370 scoped_refptr<RsaKeyPair> key_pair_; |
319 std::string oauth_refresh_token_; | 371 std::string oauth_refresh_token_; |
320 std::string serialized_config_; | 372 std::string serialized_config_; |
321 std::string host_owner_; | 373 std::string host_owner_; |
322 std::string host_owner_email_; | 374 std::string host_owner_email_; |
323 bool use_service_account_; | 375 bool use_service_account_; |
324 bool enable_vp9_; | 376 bool enable_vp9_; |
325 int64_t frame_recorder_buffer_size_; | 377 int64_t frame_recorder_buffer_size_; |
326 | 378 |
327 scoped_ptr<PolicyWatcher> policy_watcher_; | 379 scoped_ptr<PolicyWatcher> policy_watcher_; |
328 bool policies_loaded_; | 380 PolicyState policy_state_; |
329 std::string host_domain_; | 381 std::string host_domain_; |
330 bool host_username_match_required_; | 382 bool host_username_match_required_; |
331 bool allow_nat_traversal_; | 383 bool allow_nat_traversal_; |
332 bool allow_relay_; | 384 bool allow_relay_; |
333 uint16 min_udp_port_; | 385 uint16 min_udp_port_; |
334 uint16 max_udp_port_; | 386 uint16 max_udp_port_; |
335 std::string talkgadget_prefix_; | 387 std::string talkgadget_prefix_; |
336 bool allow_pairing_; | 388 bool allow_pairing_; |
337 | 389 |
338 bool curtain_required_; | 390 bool curtain_required_; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
374 }; | 426 }; |
375 | 427 |
376 HostProcess::HostProcess(scoped_ptr<ChromotingHostContext> context, | 428 HostProcess::HostProcess(scoped_ptr<ChromotingHostContext> context, |
377 int* exit_code_out, | 429 int* exit_code_out, |
378 ShutdownWatchdog* shutdown_watchdog) | 430 ShutdownWatchdog* shutdown_watchdog) |
379 : context_(context.Pass()), | 431 : context_(context.Pass()), |
380 state_(HOST_INITIALIZING), | 432 state_(HOST_INITIALIZING), |
381 use_service_account_(false), | 433 use_service_account_(false), |
382 enable_vp9_(false), | 434 enable_vp9_(false), |
383 frame_recorder_buffer_size_(0), | 435 frame_recorder_buffer_size_(0), |
384 policies_loaded_(false), | 436 policy_state_(POLICY_INITIALIZING), |
385 host_username_match_required_(false), | 437 host_username_match_required_(false), |
386 allow_nat_traversal_(true), | 438 allow_nat_traversal_(true), |
387 allow_relay_(true), | 439 allow_relay_(true), |
388 min_udp_port_(0), | 440 min_udp_port_(0), |
389 max_udp_port_(0), | 441 max_udp_port_(0), |
390 allow_pairing_(true), | 442 allow_pairing_(true), |
391 curtain_required_(false), | 443 curtain_required_(false), |
392 enable_gnubby_auth_(false), | 444 enable_gnubby_auth_(false), |
393 enable_window_capture_(false), | 445 enable_window_capture_(false), |
394 window_id_(0), | 446 window_id_(0), |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 | 599 |
548 if (!ApplyConfig(*config)) { | 600 if (!ApplyConfig(*config)) { |
549 LOG(ERROR) << "Failed to apply the configuration."; | 601 LOG(ERROR) << "Failed to apply the configuration."; |
550 ShutdownHost(kInvalidHostConfigurationExitCode); | 602 ShutdownHost(kInvalidHostConfigurationExitCode); |
551 return; | 603 return; |
552 } | 604 } |
553 | 605 |
554 if (state_ == HOST_INITIALIZING) { | 606 if (state_ == HOST_INITIALIZING) { |
555 StartHostIfReady(); | 607 StartHostIfReady(); |
556 } else if (state_ == HOST_STARTED) { | 608 } else if (state_ == HOST_STARTED) { |
557 DCHECK(policies_loaded_); | |
558 | |
559 // Reapply policies that could be affected by a new config. | 609 // Reapply policies that could be affected by a new config. |
610 DCHECK_EQ(policy_state_, POLICY_LOADED); | |
560 ApplyHostDomainPolicy(); | 611 ApplyHostDomainPolicy(); |
561 ApplyUsernamePolicy(); | 612 ApplyUsernamePolicy(); |
562 | 613 |
563 // TODO(sergeyu): Here we assume that PIN is the only part of the config | 614 // TODO(sergeyu): Here we assume that PIN is the only part of the config |
564 // that may change while the service is running. Change ApplyConfig() to | 615 // that may change while the service is running. Change ApplyConfig() to |
565 // detect other changes in the config and restart host if necessary here. | 616 // detect other changes in the config and restart host if necessary here. |
566 CreateAuthenticatorFactory(); | 617 CreateAuthenticatorFactory(); |
567 } | 618 } |
568 } | 619 } |
569 | 620 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
779 FROM_HERE, | 830 FROM_HERE, |
780 base::Bind(&HostProcess::StartOnNetworkThread, this)); | 831 base::Bind(&HostProcess::StartOnNetworkThread, this)); |
781 } | 832 } |
782 | 833 |
783 void HostProcess::ShutdownOnUiThread() { | 834 void HostProcess::ShutdownOnUiThread() { |
784 DCHECK(context_->ui_task_runner()->BelongsToCurrentThread()); | 835 DCHECK(context_->ui_task_runner()->BelongsToCurrentThread()); |
785 | 836 |
786 // Tear down resources that need to be torn down on the UI thread. | 837 // Tear down resources that need to be torn down on the UI thread. |
787 daemon_channel_.reset(); | 838 daemon_channel_.reset(); |
788 desktop_environment_factory_.reset(); | 839 desktop_environment_factory_.reset(); |
789 | |
790 policy_watcher_.reset(); | 840 policy_watcher_.reset(); |
791 | 841 |
792 // It is now safe for the HostProcess to be deleted. | 842 // It is now safe for the HostProcess to be deleted. |
793 self_ = nullptr; | 843 self_ = nullptr; |
794 | 844 |
795 #if defined(OS_LINUX) | 845 #if defined(OS_LINUX) |
796 // Cause the global AudioPipeReader to be freed, otherwise the audio | 846 // Cause the global AudioPipeReader to be freed, otherwise the audio |
797 // thread will remain in-use and prevent the process from exiting. | 847 // thread will remain in-use and prevent the process from exiting. |
798 // TODO(wez): DesktopEnvironmentFactory should own the pipe reader. | 848 // TODO(wez): DesktopEnvironmentFactory should own the pipe reader. |
799 // See crbug.com/161373 and crbug.com/104544. | 849 // See crbug.com/161373 and crbug.com/104544. |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
974 // Note: UsernamePolicyUpdate must run after OnCurtainPolicyUpdate. | 1024 // Note: UsernamePolicyUpdate must run after OnCurtainPolicyUpdate. |
975 restart_required |= OnUsernamePolicyUpdate(policies.get()); | 1025 restart_required |= OnUsernamePolicyUpdate(policies.get()); |
976 restart_required |= OnNatPolicyUpdate(policies.get()); | 1026 restart_required |= OnNatPolicyUpdate(policies.get()); |
977 restart_required |= OnRelayPolicyUpdate(policies.get()); | 1027 restart_required |= OnRelayPolicyUpdate(policies.get()); |
978 restart_required |= OnUdpPortPolicyUpdate(policies.get()); | 1028 restart_required |= OnUdpPortPolicyUpdate(policies.get()); |
979 restart_required |= OnHostTalkGadgetPrefixPolicyUpdate(policies.get()); | 1029 restart_required |= OnHostTalkGadgetPrefixPolicyUpdate(policies.get()); |
980 restart_required |= OnHostTokenUrlPolicyUpdate(policies.get()); | 1030 restart_required |= OnHostTokenUrlPolicyUpdate(policies.get()); |
981 restart_required |= OnPairingPolicyUpdate(policies.get()); | 1031 restart_required |= OnPairingPolicyUpdate(policies.get()); |
982 restart_required |= OnGnubbyAuthPolicyUpdate(policies.get()); | 1032 restart_required |= OnGnubbyAuthPolicyUpdate(policies.get()); |
983 | 1033 |
984 policies_loaded_ = true; | 1034 policy_state_ = POLICY_LOADED; |
985 | 1035 |
986 if (state_ == HOST_INITIALIZING) { | 1036 if (state_ == HOST_INITIALIZING) { |
987 StartHostIfReady(); | 1037 StartHostIfReady(); |
988 } else if (state_ == HOST_STARTED) { | 1038 } else if (state_ == HOST_STARTED) { |
989 if (restart_required) | 1039 if (restart_required) |
990 RestartHost(); | 1040 RestartHost(kHostOfflineReasonPolicyChangeRequiresRestart); |
991 } | 1041 } |
992 } | 1042 } |
993 | 1043 |
994 void HostProcess::OnPolicyError() { | 1044 void HostProcess::OnPolicyError() { |
995 if (!context_->network_task_runner()->BelongsToCurrentThread()) { | 1045 if (!context_->network_task_runner()->BelongsToCurrentThread()) { |
996 context_->network_task_runner()->PostTask( | 1046 context_->network_task_runner()->PostTask( |
997 FROM_HERE, base::Bind(&HostProcess::OnPolicyError, this)); | 1047 FROM_HERE, base::Bind(&HostProcess::OnPolicyError, this)); |
998 return; | 1048 return; |
999 } | 1049 } |
1000 | 1050 |
1001 ShutdownHost(kInvalidHostConfigurationExitCode); | 1051 if (policy_state_ != POLICY_ERROR_REPORTED) { |
1052 policy_state_ = POLICY_ERROR_REPORT_PENDING; | |
1053 if (((state_ == HOST_INITIALIZING) || (state_ == HOST_STARTED)) && | |
1054 (!serialized_config_.empty())) { | |
1055 ReportPolicyErrorAndRestartHost(); | |
1056 } | |
1057 } | |
1058 } | |
1059 | |
1060 void HostProcess::ReportPolicyErrorAndRestartHost() { | |
1061 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | |
1062 | |
1063 DCHECK_EQ(policy_state_, POLICY_ERROR_REPORT_PENDING); | |
1064 policy_state_ = POLICY_ERROR_REPORTED; | |
1065 | |
1066 LOG(INFO) << "Restarting the host due to policy errors."; | |
1067 RestartHost(kHostOfflineReasonPolicyReadError); | |
1002 } | 1068 } |
1003 | 1069 |
1004 void HostProcess::ApplyHostDomainPolicy() { | 1070 void HostProcess::ApplyHostDomainPolicy() { |
1005 if (state_ != HOST_STARTED) | 1071 if (state_ != HOST_STARTED) |
1006 return; | 1072 return; |
1007 | 1073 |
1008 HOST_LOG << "Policy sets host domain: " << host_domain_; | 1074 HOST_LOG << "Policy sets host domain: " << host_domain_; |
1009 | 1075 |
1010 if (!host_domain_.empty()) { | 1076 if (!host_domain_.empty()) { |
1011 // If the user does not have a Google email, their client JID will not be | 1077 // If the user does not have a Google email, their client JID will not be |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1293 } | 1359 } |
1294 | 1360 |
1295 scoped_ptr<HostSignalingManager> HostProcess::CreateHostSignalingManager() { | 1361 scoped_ptr<HostSignalingManager> HostProcess::CreateHostSignalingManager() { |
1296 DCHECK(!host_id_.empty()); // |ApplyConfig| should already have been run. | 1362 DCHECK(!host_id_.empty()); // |ApplyConfig| should already have been run. |
1297 | 1363 |
1298 scoped_ptr<OAuthTokenGetter::OAuthCredentials> oauth_credentials( | 1364 scoped_ptr<OAuthTokenGetter::OAuthCredentials> oauth_credentials( |
1299 new OAuthTokenGetter::OAuthCredentials(xmpp_server_config_.username, | 1365 new OAuthTokenGetter::OAuthCredentials(xmpp_server_config_.username, |
1300 oauth_refresh_token_, | 1366 oauth_refresh_token_, |
1301 use_service_account_)); | 1367 use_service_account_)); |
1302 | 1368 |
1303 return HostSignalingManager::Create(this, context_->network_task_runner(), | 1369 return HostSignalingManager::Create( |
1304 context_->url_request_context_getter(), | 1370 this, context_->url_request_context_getter(), xmpp_server_config_, |
1305 xmpp_server_config_, talkgadget_prefix_, | 1371 talkgadget_prefix_, host_id_, key_pair_, directory_bot_jid_, |
1306 host_id_, key_pair_, directory_bot_jid_, | 1372 oauth_credentials.Pass()); |
1307 oauth_credentials.Pass()); | |
1308 } | 1373 } |
1309 | 1374 |
1310 void HostProcess::StartHostIfReady() { | 1375 void HostProcess::StartHostIfReady() { |
1311 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | 1376 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
1312 DCHECK_EQ(state_, HOST_INITIALIZING); | 1377 DCHECK_EQ(state_, HOST_INITIALIZING); |
1313 | 1378 |
1314 // Start the host if both the config and the policies are loaded. | 1379 // Start the host if both the config and the policies are loaded. |
1315 if (!serialized_config_.empty() && policies_loaded_) | 1380 if (!serialized_config_.empty()) { |
1316 StartHost(); | 1381 if (policy_state_ == POLICY_LOADED) { |
1382 StartHost(); | |
1383 } else if (policy_state_ == POLICY_ERROR_REPORT_PENDING) { | |
1384 ReportPolicyErrorAndRestartHost(); | |
1385 } | |
1386 } | |
1317 } | 1387 } |
1318 | 1388 |
1319 void HostProcess::StartHost() { | 1389 void HostProcess::StartHost() { |
1320 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | 1390 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
1321 DCHECK(!host_); | 1391 DCHECK(!host_); |
1322 DCHECK(!host_signaling_manager_); | 1392 DCHECK(!host_signaling_manager_); |
1323 | 1393 |
1324 DCHECK(state_ == HOST_INITIALIZING || state_ == HOST_STOPPING_TO_RESTART || | 1394 SetState(HOST_STARTED); |
1325 state_ == HOST_STOPPED) | |
1326 << "state_ = " << state_; | |
1327 state_ = HOST_STARTED; | |
1328 | 1395 |
1329 host_signaling_manager_ = CreateHostSignalingManager(); | 1396 host_signaling_manager_ = CreateHostSignalingManager(); |
1330 | 1397 |
1331 uint32 network_flags = 0; | 1398 uint32 network_flags = 0; |
1332 if (allow_nat_traversal_) { | 1399 if (allow_nat_traversal_) { |
1333 network_flags = NetworkSettings::NAT_TRAVERSAL_STUN | | 1400 network_flags = NetworkSettings::NAT_TRAVERSAL_STUN | |
1334 NetworkSettings::NAT_TRAVERSAL_OUTGOING; | 1401 NetworkSettings::NAT_TRAVERSAL_OUTGOING; |
1335 if (allow_relay_) | 1402 if (allow_relay_) |
1336 network_flags |= NetworkSettings::NAT_TRAVERSAL_RELAY; | 1403 network_flags |= NetworkSettings::NAT_TRAVERSAL_RELAY; |
1337 } | 1404 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1402 CreateAuthenticatorFactory(); | 1469 CreateAuthenticatorFactory(); |
1403 | 1470 |
1404 ApplyHostDomainPolicy(); | 1471 ApplyHostDomainPolicy(); |
1405 ApplyUsernamePolicy(); | 1472 ApplyUsernamePolicy(); |
1406 } | 1473 } |
1407 | 1474 |
1408 void HostProcess::OnAuthFailed() { | 1475 void HostProcess::OnAuthFailed() { |
1409 ShutdownHost(kInvalidOauthCredentialsExitCode); | 1476 ShutdownHost(kInvalidOauthCredentialsExitCode); |
1410 } | 1477 } |
1411 | 1478 |
1412 void HostProcess::RestartHost() { | 1479 void HostProcess::RestartHost(const std::string& host_offline_reason) { |
1413 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | 1480 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
1414 DCHECK_EQ(state_, HOST_STARTED); | 1481 DCHECK(!host_offline_reason.empty()); |
1415 | 1482 |
1416 state_ = HOST_STOPPING_TO_RESTART; | 1483 SetState(HOST_GOING_OFFLINE_TO_RESTART); |
1417 ShutdownOnNetworkThread(); | 1484 GoOffline(host_offline_reason); |
1418 } | |
1419 | |
1420 void HostProcess::SendOfflineReasonAndShutdownOnNetworkThread( | |
1421 HostExitCodes exit_code) { | |
1422 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | |
1423 DCHECK(host_signaling_manager_); | |
1424 host_signaling_manager_.release()->SendHostOfflineReasonAndDelete( | |
1425 ExitCodeToString(exit_code), | |
1426 base::TimeDelta::FromSeconds(kHostOfflineReasonTimeoutSeconds)); | |
1427 ShutdownOnNetworkThread(); | |
1428 } | 1485 } |
1429 | 1486 |
1430 void HostProcess::ShutdownHost(HostExitCodes exit_code) { | 1487 void HostProcess::ShutdownHost(HostExitCodes exit_code) { |
1431 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | 1488 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
1432 | 1489 |
1433 *exit_code_out_ = exit_code; | 1490 *exit_code_out_ = exit_code; |
1434 | 1491 |
1435 switch (state_) { | 1492 switch (state_) { |
1436 case HOST_INITIALIZING: | 1493 case HOST_INITIALIZING: |
1437 state_ = HOST_STOPPING; | 1494 case HOST_STARTED: |
1438 DCHECK(!host_signaling_manager_); | 1495 SetState(HOST_GOING_OFFLINE_TO_STOP); |
1439 host_signaling_manager_ = CreateHostSignalingManager(); | 1496 GoOffline(ExitCodeToString(exit_code)); |
1440 SendOfflineReasonAndShutdownOnNetworkThread(exit_code); | |
1441 break; | 1497 break; |
1442 | 1498 |
1443 case HOST_STARTED: | 1499 case HOST_GOING_OFFLINE_TO_RESTART: |
1444 state_ = HOST_STOPPING; | 1500 SetState(HOST_GOING_OFFLINE_TO_STOP); |
1445 SendOfflineReasonAndShutdownOnNetworkThread(exit_code); | |
1446 break; | 1501 break; |
1447 | 1502 |
1448 case HOST_STOPPING_TO_RESTART: | 1503 case HOST_GOING_OFFLINE_TO_STOP: |
1449 state_ = HOST_STOPPING; | |
1450 break; | |
1451 | |
1452 case HOST_STOPPING: | |
1453 case HOST_STOPPED: | 1504 case HOST_STOPPED: |
1454 // Host is already stopped or being stopped. No action is required. | 1505 // Host is already stopped or being stopped. No action is required. |
1455 break; | 1506 break; |
1456 } | 1507 } |
1457 } | 1508 } |
1458 | 1509 |
1459 void HostProcess::ShutdownOnNetworkThread() { | 1510 void HostProcess::GoOffline(const std::string& host_offline_reason) { |
1460 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | 1511 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
1512 DCHECK(!host_offline_reason.empty()); | |
1513 DCHECK((state_ == HOST_GOING_OFFLINE_TO_STOP) || | |
1514 (state_ == HOST_GOING_OFFLINE_TO_RESTART)); | |
1461 | 1515 |
1516 // Shut down everything except the HostSignalingManager. | |
1462 host_.reset(); | 1517 host_.reset(); |
1463 host_event_logger_.reset(); | 1518 host_event_logger_.reset(); |
1464 host_status_logger_.reset(); | 1519 host_status_logger_.reset(); |
1465 host_signaling_manager_.reset(); | |
1466 host_change_notification_listener_.reset(); | 1520 host_change_notification_listener_.reset(); |
1467 | 1521 |
1468 if (state_ == HOST_STOPPING_TO_RESTART) { | 1522 // Before shutting down HostSignalingManager, send the |host_offline_reason| |
1469 StartHost(); | 1523 // if possible (i.e. if we have the config). |
1470 } else if (state_ == HOST_STOPPING) { | 1524 if (!serialized_config_.empty()) { |
1471 state_ = HOST_STOPPED; | 1525 if (!host_signaling_manager_) { |
1526 host_signaling_manager_ = CreateHostSignalingManager(); | |
1527 } | |
1528 | |
1529 host_signaling_manager_->SendHostOfflineReason( | |
1530 host_offline_reason, | |
1531 base::TimeDelta::FromSeconds(kHostOfflineReasonTimeoutSeconds), | |
1532 base::Bind(&HostProcess::OnHostOfflineReasonAck, this)); | |
1533 return; // Shutdown will resume after OnHostOfflineReasonAck. | |
1534 } | |
1535 | |
1536 // Continue the shutdown without sending the host offline reason. | |
1537 HOST_LOG << "SendHostOfflineReason( " << host_offline_reason << ") " | |
1538 << "not possible without a config..."; | |
1539 OnHostOfflineReasonAck(false); | |
1540 } | |
1541 | |
1542 void HostProcess::OnHostOfflineReasonAck(bool success) { | |
Wez
2015/02/14 00:49:15
nit: It's weird for this to be OnHostOfflineReason
Łukasz Anforowicz
2015/02/17 21:02:27
This (presence of OnXxx method, without DoXxx meth
| |
1543 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); | |
1544 DCHECK(!host_); // Assert that the host is really offline at this point. | |
1545 | |
1546 HOST_LOG << "SendHostOfflineReason " << (success ? "succeeded." : "failed."); | |
1547 host_signaling_manager_.reset(); | |
1548 | |
1549 if (state_ == HOST_GOING_OFFLINE_TO_RESTART) { | |
1550 SetState(HOST_INITIALIZING); | |
1551 StartHostIfReady(); | |
1552 } else if (state_ == HOST_GOING_OFFLINE_TO_STOP) { | |
1553 SetState(HOST_STOPPED); | |
1472 | 1554 |
1473 shutdown_watchdog_->SetExitCode(*exit_code_out_); | 1555 shutdown_watchdog_->SetExitCode(*exit_code_out_); |
1474 shutdown_watchdog_->Arm(); | 1556 shutdown_watchdog_->Arm(); |
1475 | 1557 |
1476 config_watcher_.reset(); | 1558 config_watcher_.reset(); |
1477 | 1559 |
1478 // Complete the rest of shutdown on the main thread. | 1560 // Complete the rest of shutdown on the main thread. |
1479 context_->ui_task_runner()->PostTask( | 1561 context_->ui_task_runner()->PostTask( |
1480 FROM_HERE, base::Bind(&HostProcess::ShutdownOnUiThread, this)); | 1562 FROM_HERE, base::Bind(&HostProcess::ShutdownOnUiThread, this)); |
1481 } else { | 1563 } else { |
1482 // This method is only called in STOPPING_TO_RESTART and STOPPING states. | |
1483 NOTREACHED(); | 1564 NOTREACHED(); |
1484 } | 1565 } |
1485 } | 1566 } |
1486 | 1567 |
1487 void HostProcess::OnCrash(const std::string& function_name, | 1568 void HostProcess::OnCrash(const std::string& function_name, |
1488 const std::string& file_name, | 1569 const std::string& file_name, |
1489 const int& line_number) { | 1570 const int& line_number) { |
1490 char message[1024]; | 1571 char message[1024]; |
1491 base::snprintf(message, sizeof(message), | 1572 base::snprintf(message, sizeof(message), |
1492 "Requested by %s at %s, line %d.", | 1573 "Requested by %s at %s, line %d.", |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1542 base::TimeDelta::FromSeconds(kShutdownTimeoutSeconds)); | 1623 base::TimeDelta::FromSeconds(kShutdownTimeoutSeconds)); |
1543 new HostProcess(context.Pass(), &exit_code, &shutdown_watchdog); | 1624 new HostProcess(context.Pass(), &exit_code, &shutdown_watchdog); |
1544 | 1625 |
1545 // Run the main (also UI) message loop until the host no longer needs it. | 1626 // Run the main (also UI) message loop until the host no longer needs it. |
1546 message_loop.Run(); | 1627 message_loop.Run(); |
1547 | 1628 |
1548 return exit_code; | 1629 return exit_code; |
1549 } | 1630 } |
1550 | 1631 |
1551 } // namespace remoting | 1632 } // namespace remoting |
OLD | NEW |