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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 // Call ConfigUpdatedDelayed after a short delay, so that this object won't | 136 // Call ConfigUpdatedDelayed after a short delay, so that this object won't |
137 // try to read the updated configuration file before it has been | 137 // try to read the updated configuration file before it has been |
138 // completely written. | 138 // completely written. |
139 // If the writer moves the new configuration file into place atomically, | 139 // If the writer moves the new configuration file into place atomically, |
140 // this delay may not be necessary. | 140 // this delay may not be necessary. |
141 config_updated_timer_->Reset(); | 141 config_updated_timer_->Reset(); |
142 } | 142 } |
143 | 143 |
144 void ConfigUpdatedDelayed() { | 144 void ConfigUpdatedDelayed() { |
145 if (LoadConfig()) { | 145 if (LoadConfig()) { |
146 context_->network_message_loop()->PostTask( | 146 context_->network_task_runner()->PostTask( |
147 FROM_HERE, | 147 FROM_HERE, |
148 base::Bind(&HostProcess::CreateAuthenticatorFactory, | 148 base::Bind(&HostProcess::CreateAuthenticatorFactory, |
149 base::Unretained(this))); | 149 base::Unretained(this))); |
150 } else { | 150 } else { |
151 LOG(ERROR) << "Invalid configuration."; | 151 LOG(ERROR) << "Invalid configuration."; |
152 } | 152 } |
153 } | 153 } |
154 | 154 |
155 #if defined(OS_WIN) | 155 #if defined(OS_WIN) |
156 class ConfigChangedDelegate : public base::files::FilePathWatcher::Delegate { | 156 class ConfigChangedDelegate : public base::files::FilePathWatcher::Delegate { |
157 public: | 157 public: |
158 ConfigChangedDelegate(base::MessageLoopProxy* message_loop, | 158 ConfigChangedDelegate( |
159 const base::Closure& callback) | 159 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
160 : message_loop_(message_loop), | 160 const base::Closure& callback) |
| 161 : task_runner_(task_runner), |
161 callback_(callback) { | 162 callback_(callback) { |
162 } | 163 } |
163 | 164 |
164 void OnFilePathChanged(const FilePath& path) OVERRIDE { | 165 void OnFilePathChanged(const FilePath& path) OVERRIDE { |
165 message_loop_->PostTask(FROM_HERE, callback_); | 166 task_runner_->PostTask(FROM_HERE, callback_); |
166 } | 167 } |
167 | 168 |
168 void OnFilePathError(const FilePath& path) OVERRIDE { | 169 void OnFilePathError(const FilePath& path) OVERRIDE { |
169 } | 170 } |
170 | 171 |
171 private: | 172 private: |
172 scoped_refptr<base::MessageLoopProxy> message_loop_; | 173 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
173 base::Closure callback_; | 174 base::Closure callback_; |
174 | 175 |
175 DISALLOW_COPY_AND_ASSIGN(ConfigChangedDelegate); | 176 DISALLOW_COPY_AND_ASSIGN(ConfigChangedDelegate); |
176 }; | 177 }; |
177 #endif // defined(OS_WIN) | 178 #endif // defined(OS_WIN) |
178 | 179 |
179 void ListenForConfigChanges() { | 180 void ListenForConfigChanges() { |
180 #if defined(OS_MACOSX) | 181 #if defined(OS_MACOSX) |
181 remoting::RegisterHupSignalHandler( | 182 remoting::RegisterHupSignalHandler( |
182 base::Bind(&HostProcess::ConfigUpdatedDelayed, base::Unretained(this))); | 183 base::Bind(&HostProcess::ConfigUpdatedDelayed, base::Unretained(this))); |
(...skipping 22 matching lines...) Expand all Loading... |
205 return kInvalidHostConfigurationExitCode; | 206 return kInvalidHostConfigurationExitCode; |
206 } | 207 } |
207 | 208 |
208 #if defined(OS_MACOSX) || defined(OS_WIN) | 209 #if defined(OS_MACOSX) || defined(OS_WIN) |
209 host_user_interface_.reset(new HostUserInterface(context_.get())); | 210 host_user_interface_.reset(new HostUserInterface(context_.get())); |
210 #endif | 211 #endif |
211 | 212 |
212 StartWatchingNatPolicy(); | 213 StartWatchingNatPolicy(); |
213 | 214 |
214 #if defined(OS_MACOSX) || defined(OS_WIN) | 215 #if defined(OS_MACOSX) || defined(OS_WIN) |
215 context_->file_message_loop()->PostTask( | 216 context_->file_task_runner()->PostTask( |
216 FROM_HERE, | 217 FROM_HERE, |
217 base::Bind(&HostProcess::ListenForConfigChanges, | 218 base::Bind(&HostProcess::ListenForConfigChanges, |
218 base::Unretained(this))); | 219 base::Unretained(this))); |
219 #endif | 220 #endif |
220 message_loop_.Run(); | 221 message_loop_.Run(); |
221 | 222 |
222 #if defined(OS_MACOSX) || defined(OS_WIN) | 223 #if defined(OS_MACOSX) || defined(OS_WIN) |
223 host_user_interface_.reset(); | 224 host_user_interface_.reset(); |
224 #endif | 225 #endif |
225 | 226 |
226 base::WaitableEvent done_event(true, false); | 227 base::WaitableEvent done_event(true, false); |
227 nat_policy_->StopWatching(&done_event); | 228 nat_policy_->StopWatching(&done_event); |
228 done_event.Wait(); | 229 done_event.Wait(); |
229 nat_policy_.reset(); | 230 nat_policy_.reset(); |
230 | 231 |
231 return exit_code_; | 232 return exit_code_; |
232 } | 233 } |
233 | 234 |
234 // Overridden from HeartbeatSender::Listener | 235 // Overridden from HeartbeatSender::Listener |
235 virtual void OnUnknownHostIdError() OVERRIDE { | 236 virtual void OnUnknownHostIdError() OVERRIDE { |
236 LOG(ERROR) << "Host ID not found."; | 237 LOG(ERROR) << "Host ID not found."; |
237 Shutdown(kInvalidHostIdExitCode); | 238 Shutdown(kInvalidHostIdExitCode); |
238 } | 239 } |
239 | 240 |
240 private: | 241 private: |
241 void StartWatchingNatPolicy() { | 242 void StartWatchingNatPolicy() { |
242 nat_policy_.reset( | 243 nat_policy_.reset( |
243 policy_hack::NatPolicy::Create(context_->file_message_loop())); | 244 policy_hack::NatPolicy::Create(context_->file_task_runner())); |
244 nat_policy_->StartWatching( | 245 nat_policy_->StartWatching( |
245 base::Bind(&HostProcess::OnNatPolicyUpdate, base::Unretained(this))); | 246 base::Bind(&HostProcess::OnNatPolicyUpdate, base::Unretained(this))); |
246 } | 247 } |
247 | 248 |
248 // Read Host config from disk, returning true if successful. | 249 // Read Host config from disk, returning true if successful. |
249 bool LoadConfig() { | 250 bool LoadConfig() { |
250 JsonHostConfig host_config(host_config_path_); | 251 JsonHostConfig host_config(host_config_path_); |
251 JsonHostConfig auth_config(auth_config_path_); | 252 JsonHostConfig auth_config(auth_config_path_); |
252 | 253 |
253 FilePath failed_path; | 254 FilePath failed_path; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 &xmpp_auth_service_)) { | 305 &xmpp_auth_service_)) { |
305 // For the me2me host, we default to ClientLogin token for chromiumsync | 306 // For the me2me host, we default to ClientLogin token for chromiumsync |
306 // because earlier versions of the host had no HTTP stack with which to | 307 // because earlier versions of the host had no HTTP stack with which to |
307 // request an OAuth2 access token. | 308 // request an OAuth2 access token. |
308 xmpp_auth_service_ = kChromotingTokenDefaultServiceName; | 309 xmpp_auth_service_ = kChromotingTokenDefaultServiceName; |
309 } | 310 } |
310 return true; | 311 return true; |
311 } | 312 } |
312 | 313 |
313 void OnNatPolicyUpdate(bool nat_traversal_enabled) { | 314 void OnNatPolicyUpdate(bool nat_traversal_enabled) { |
314 if (!context_->network_message_loop()->BelongsToCurrentThread()) { | 315 if (!context_->network_task_runner()->BelongsToCurrentThread()) { |
315 context_->network_message_loop()->PostTask(FROM_HERE, base::Bind( | 316 context_->network_task_runner()->PostTask(FROM_HERE, base::Bind( |
316 &HostProcess::OnNatPolicyUpdate, base::Unretained(this), | 317 &HostProcess::OnNatPolicyUpdate, base::Unretained(this), |
317 nat_traversal_enabled)); | 318 nat_traversal_enabled)); |
318 return; | 319 return; |
319 } | 320 } |
320 | 321 |
321 bool policy_changed = allow_nat_traversal_ != nat_traversal_enabled; | 322 bool policy_changed = allow_nat_traversal_ != nat_traversal_enabled; |
322 allow_nat_traversal_ = nat_traversal_enabled; | 323 allow_nat_traversal_ = nat_traversal_enabled; |
323 | 324 |
324 if (host_) { | 325 if (host_) { |
325 // Restart the host if the policy has changed while the host was | 326 // Restart the host if the policy has changed while the host was |
326 // online. | 327 // online. |
327 if (policy_changed) | 328 if (policy_changed) |
328 RestartHost(); | 329 RestartHost(); |
329 } else { | 330 } else { |
330 // Just start the host otherwise. | 331 // Just start the host otherwise. |
331 StartHost(); | 332 StartHost(); |
332 } | 333 } |
333 } | 334 } |
334 | 335 |
335 void StartHost() { | 336 void StartHost() { |
336 DCHECK(context_->network_message_loop()->BelongsToCurrentThread()); | 337 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
337 DCHECK(!host_); | 338 DCHECK(!host_); |
338 | 339 |
339 if (!signal_strategy_.get()) { | 340 if (!signal_strategy_.get()) { |
340 signal_strategy_.reset( | 341 signal_strategy_.reset( |
341 new XmppSignalStrategy(context_->jingle_thread(), xmpp_login_, | 342 new XmppSignalStrategy(context_->jingle_thread(), xmpp_login_, |
342 xmpp_auth_token_, xmpp_auth_service_)); | 343 xmpp_auth_token_, xmpp_auth_service_)); |
343 | 344 |
344 signaling_connector_.reset(new SignalingConnector( | 345 signaling_connector_.reset(new SignalingConnector( |
345 signal_strategy_.get(), | 346 signal_strategy_.get(), |
346 base::Bind(&HostProcess::OnAuthFailed, base::Unretained(this)))); | 347 base::Bind(&HostProcess::OnAuthFailed, base::Unretained(this)))); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 | 415 |
415 // Invoked when the user uses the Disconnect windows to terminate | 416 // Invoked when the user uses the Disconnect windows to terminate |
416 // the sessions. | 417 // the sessions. |
417 void OnDisconnectRequested() { | 418 void OnDisconnectRequested() { |
418 DCHECK(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); | 419 DCHECK(message_loop_.message_loop_proxy()->BelongsToCurrentThread()); |
419 | 420 |
420 host_->DisconnectAllClients(); | 421 host_->DisconnectAllClients(); |
421 } | 422 } |
422 | 423 |
423 void RestartHost() { | 424 void RestartHost() { |
424 DCHECK(context_->network_message_loop()->BelongsToCurrentThread()); | 425 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
425 | 426 |
426 if (restarting_ || shutting_down_) | 427 if (restarting_ || shutting_down_) |
427 return; | 428 return; |
428 | 429 |
429 restarting_ = true; | 430 restarting_ = true; |
430 host_->Shutdown(base::Bind( | 431 host_->Shutdown(base::Bind( |
431 &HostProcess::RestartOnHostShutdown, base::Unretained(this))); | 432 &HostProcess::RestartOnHostShutdown, base::Unretained(this))); |
432 } | 433 } |
433 | 434 |
434 void RestartOnHostShutdown() { | 435 void RestartOnHostShutdown() { |
435 DCHECK(context_->network_message_loop()->BelongsToCurrentThread()); | 436 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
436 | 437 |
437 if (shutting_down_) | 438 if (shutting_down_) |
438 return; | 439 return; |
439 | 440 |
440 restarting_ = false; | 441 restarting_ = false; |
441 host_ = NULL; | 442 host_ = NULL; |
442 log_to_server_.reset(); | 443 log_to_server_.reset(); |
443 host_event_logger_.reset(); | 444 host_event_logger_.reset(); |
444 heartbeat_sender_.reset(); | 445 heartbeat_sender_.reset(); |
445 | 446 |
446 StartHost(); | 447 StartHost(); |
447 } | 448 } |
448 | 449 |
449 void Shutdown(int exit_code) { | 450 void Shutdown(int exit_code) { |
450 DCHECK(context_->network_message_loop()->BelongsToCurrentThread()); | 451 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
451 | 452 |
452 if (shutting_down_) | 453 if (shutting_down_) |
453 return; | 454 return; |
454 | 455 |
455 shutting_down_ = true; | 456 shutting_down_ = true; |
456 exit_code_ = exit_code; | 457 exit_code_ = exit_code; |
457 host_->Shutdown(base::Bind( | 458 host_->Shutdown(base::Bind( |
458 &HostProcess::OnShutdownFinished, base::Unretained(this))); | 459 &HostProcess::OnShutdownFinished, base::Unretained(this))); |
459 } | 460 } |
460 | 461 |
461 void OnShutdownFinished() { | 462 void OnShutdownFinished() { |
462 DCHECK(context_->network_message_loop()->BelongsToCurrentThread()); | 463 DCHECK(context_->network_task_runner()->BelongsToCurrentThread()); |
463 | 464 |
464 // Destroy networking objects while we are on the network thread. | 465 // Destroy networking objects while we are on the network thread. |
465 host_ = NULL; | 466 host_ = NULL; |
466 host_event_logger_.reset(); | 467 host_event_logger_.reset(); |
467 log_to_server_.reset(); | 468 log_to_server_.reset(); |
468 heartbeat_sender_.reset(); | 469 heartbeat_sender_.reset(); |
469 signaling_connector_.reset(); | 470 signaling_connector_.reset(); |
470 signal_strategy_.reset(); | 471 signal_strategy_.reset(); |
471 | 472 |
472 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 473 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 | 582 |
582 // Mark the process as DPI-aware, so Windows won't scale coordinates in APIs. | 583 // Mark the process as DPI-aware, so Windows won't scale coordinates in APIs. |
583 SetProcessDPIAware(); | 584 SetProcessDPIAware(); |
584 | 585 |
585 // CommandLine::Init() ignores the passed |argc| and |argv| on Windows getting | 586 // CommandLine::Init() ignores the passed |argc| and |argv| on Windows getting |
586 // the command line from GetCommandLineW(), so we can safely pass NULL here. | 587 // the command line from GetCommandLineW(), so we can safely pass NULL here. |
587 return main(0, NULL); | 588 return main(0, NULL); |
588 } | 589 } |
589 | 590 |
590 #endif // defined(OS_WIN) | 591 #endif // defined(OS_WIN) |
OLD | NEW |