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 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/at_exit.h" | 13 #include "base/at_exit.h" |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/file_path.h" | 17 #include "base/file_path.h" |
18 #include "base/file_util.h" | 18 #include "base/file_util.h" |
| 19 #include "base/files/file_path_watcher.h" |
19 #include "base/logging.h" | 20 #include "base/logging.h" |
20 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
21 #include "base/message_loop.h" | 22 #include "base/message_loop.h" |
22 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
23 #include "build/build_config.h" | 24 #include "build/build_config.h" |
24 #include "crypto/nss_util.h" | 25 #include "crypto/nss_util.h" |
25 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
26 #include "remoting/base/constants.h" | 27 #include "remoting/base/constants.h" |
27 #include "remoting/host/branding.h" | 28 #include "remoting/host/branding.h" |
28 #include "remoting/host/capturer.h" | 29 #include "remoting/host/capturer.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 class HostProcess : public OAuthClient::Delegate { | 77 class HostProcess : public OAuthClient::Delegate { |
77 public: | 78 public: |
78 HostProcess() | 79 HostProcess() |
79 : message_loop_(MessageLoop::TYPE_UI), | 80 : message_loop_(MessageLoop::TYPE_UI), |
80 allow_nat_traversal_(true), | 81 allow_nat_traversal_(true), |
81 restarting_(false) { | 82 restarting_(false) { |
82 context_.reset( | 83 context_.reset( |
83 new ChromotingHostContext(message_loop_.message_loop_proxy())); | 84 new ChromotingHostContext(message_loop_.message_loop_proxy())); |
84 context_->Start(); | 85 context_->Start(); |
85 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); | 86 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); |
| 87 config_updated_timer_.reset(new base::DelayTimer<HostProcess>( |
| 88 FROM_HERE, base::TimeDelta::FromSeconds(2), this, |
| 89 &HostProcess::ConfigUpdated)); |
86 } | 90 } |
87 | 91 |
88 void InitWithCommandLine(const CommandLine* cmd_line) { | 92 void InitWithCommandLine(const CommandLine* cmd_line) { |
89 FilePath default_config_dir = remoting::GetConfigDir(); | 93 FilePath default_config_dir = remoting::GetConfigDir(); |
90 if (cmd_line->HasSwitch(kAuthConfigSwitchName)) { | 94 if (cmd_line->HasSwitch(kAuthConfigSwitchName)) { |
91 auth_config_path_ = cmd_line->GetSwitchValuePath(kAuthConfigSwitchName); | 95 auth_config_path_ = cmd_line->GetSwitchValuePath(kAuthConfigSwitchName); |
92 } else { | 96 } else { |
93 auth_config_path_ = default_config_dir.Append(kDefaultAuthConfigFile); | 97 auth_config_path_ = default_config_dir.Append(kDefaultAuthConfigFile); |
94 } | 98 } |
95 | 99 |
(...skipping 15 matching lines...) Expand all Loading... |
111 if (LoadConfig(&tokens_pending)) { | 115 if (LoadConfig(&tokens_pending)) { |
112 context_->network_message_loop()->PostTask( | 116 context_->network_message_loop()->PostTask( |
113 FROM_HERE, | 117 FROM_HERE, |
114 base::Bind(&HostProcess::CreateAuthenticatorFactory, | 118 base::Bind(&HostProcess::CreateAuthenticatorFactory, |
115 base::Unretained(this))); | 119 base::Unretained(this))); |
116 } else { | 120 } else { |
117 LOG(ERROR) << "Invalid configuration."; | 121 LOG(ERROR) << "Invalid configuration."; |
118 } | 122 } |
119 } | 123 } |
120 | 124 |
| 125 #if defined(OS_WIN) |
| 126 class ConfigChangedDelegate : public base::files::FilePathWatcher::Delegate { |
| 127 public: |
| 128 ConfigChangedDelegate(HostProcess* host_process) : |
| 129 host_process_(host_process) { |
| 130 } |
| 131 void OnFilePathChanged(const FilePath& path) OVERRIDE { |
| 132 // Call ConfigUpdated after a short delay, so that this object won't |
| 133 // try to read the updated configuration file before it has been |
| 134 // completely written. |
| 135 // If the writer moves the new configuration file into place atomically, |
| 136 // this delay may not be necessary. |
| 137 host_process_->config_updated_timer_->Reset(); |
| 138 } |
| 139 void OnFilePathError(const FilePath& path) OVERRIDE { |
| 140 } |
| 141 private: |
| 142 HostProcess* host_process_; |
| 143 |
| 144 DISALLOW_COPY_AND_ASSIGN(ConfigChangedDelegate); |
| 145 }; |
| 146 #endif // defined(OS_WIN) |
| 147 |
| 148 void ListenForConfigChanges() { |
121 #if defined(OS_MACOSX) | 149 #if defined(OS_MACOSX) |
122 void ListenForConfigChanges() { | |
123 remoting::RegisterHupSignalHandler( | 150 remoting::RegisterHupSignalHandler( |
124 base::Bind(&HostProcess::ConfigUpdated, base::Unretained(this))); | 151 base::Bind(&HostProcess::ConfigUpdated, base::Unretained(this))); |
| 152 #elif defined(OS_WIN) |
| 153 scoped_refptr<base::files::FilePathWatcher::Delegate> delegate( |
| 154 new ConfigChangedDelegate(this)); |
| 155 config_watcher_.reset(new base::files::FilePathWatcher()); |
| 156 if (!config_watcher_->Watch(host_config_path_, delegate)) { |
| 157 LOG(ERROR) << "Couldn't watch file " << host_config_path_.value(); |
| 158 } |
| 159 #endif |
125 } | 160 } |
126 #endif | |
127 | 161 |
128 void CreateAuthenticatorFactory() { | 162 void CreateAuthenticatorFactory() { |
129 scoped_ptr<protocol::AuthenticatorFactory> factory( | 163 scoped_ptr<protocol::AuthenticatorFactory> factory( |
130 new protocol::Me2MeHostAuthenticatorFactory( | 164 new protocol::Me2MeHostAuthenticatorFactory( |
131 xmpp_login_, key_pair_.GenerateCertificate(), | 165 xmpp_login_, key_pair_.GenerateCertificate(), |
132 *key_pair_.private_key(), host_secret_hash_)); | 166 *key_pair_.private_key(), host_secret_hash_)); |
133 host_->SetAuthenticatorFactory(factory.Pass()); | 167 host_->SetAuthenticatorFactory(factory.Pass()); |
134 } | 168 } |
135 | 169 |
136 int Run() { | 170 int Run() { |
137 bool tokens_pending = false; | 171 bool tokens_pending = false; |
138 if (!LoadConfig(&tokens_pending)) { | 172 if (!LoadConfig(&tokens_pending)) { |
139 return kInvalidHostConfigurationExitCode; | 173 return kInvalidHostConfigurationExitCode; |
140 } | 174 } |
141 if (tokens_pending) { | 175 if (tokens_pending) { |
142 // If we have an OAuth refresh token, then XmppSignalStrategy can't | 176 // If we have an OAuth refresh token, then XmppSignalStrategy can't |
143 // handle it directly, so refresh it asynchronously. A task will be | 177 // handle it directly, so refresh it asynchronously. A task will be |
144 // posted on the message loop to start watching the NAT policy when | 178 // posted on the message loop to start watching the NAT policy when |
145 // the access token is available. | 179 // the access token is available. |
146 // | 180 // |
147 // TODO(sergeyu): Move this code to SignalingConnector. | 181 // TODO(sergeyu): Move this code to SignalingConnector. |
148 oauth_client_.Start(oauth_refresh_token_, this, | 182 oauth_client_.Start(oauth_refresh_token_, this, |
149 message_loop_.message_loop_proxy()); | 183 message_loop_.message_loop_proxy()); |
150 } else { | 184 } else { |
151 StartWatchingNatPolicy(); | 185 StartWatchingNatPolicy(); |
152 } | 186 } |
153 | 187 |
154 #if defined(OS_MACOSX) | 188 #if defined(OS_MACOSX) || defined(OS_WIN) |
155 context_->file_message_loop()->PostTask( | 189 context_->file_message_loop()->PostTask( |
156 FROM_HERE, | 190 FROM_HERE, |
157 base::Bind(&HostProcess::ListenForConfigChanges, | 191 base::Bind(&HostProcess::ListenForConfigChanges, |
158 base::Unretained(this))); | 192 base::Unretained(this))); |
159 #endif | 193 #endif |
160 message_loop_.Run(); | 194 message_loop_.Run(); |
161 | 195 |
162 return kSuccessExitCode; | 196 return kSuccessExitCode; |
163 } | 197 } |
164 | 198 |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 protocol::SharedSecretHash host_secret_hash_; | 383 protocol::SharedSecretHash host_secret_hash_; |
350 std::string xmpp_login_; | 384 std::string xmpp_login_; |
351 std::string xmpp_auth_token_; | 385 std::string xmpp_auth_token_; |
352 std::string xmpp_auth_service_; | 386 std::string xmpp_auth_service_; |
353 | 387 |
354 std::string oauth_refresh_token_; | 388 std::string oauth_refresh_token_; |
355 OAuthClient oauth_client_; | 389 OAuthClient oauth_client_; |
356 | 390 |
357 scoped_ptr<policy_hack::NatPolicy> nat_policy_; | 391 scoped_ptr<policy_hack::NatPolicy> nat_policy_; |
358 bool allow_nat_traversal_; | 392 bool allow_nat_traversal_; |
| 393 scoped_ptr<base::files::FilePathWatcher> config_watcher_; |
| 394 scoped_ptr<base::DelayTimer<HostProcess> > config_updated_timer_; |
359 | 395 |
360 bool restarting_; | 396 bool restarting_; |
361 | 397 |
362 scoped_ptr<XmppSignalStrategy> signal_strategy_; | 398 scoped_ptr<XmppSignalStrategy> signal_strategy_; |
363 scoped_ptr<SignalingConnector> signaling_connector_; | 399 scoped_ptr<SignalingConnector> signaling_connector_; |
364 scoped_ptr<DesktopEnvironment> desktop_environment_; | 400 scoped_ptr<DesktopEnvironment> desktop_environment_; |
365 scoped_ptr<HeartbeatSender> heartbeat_sender_; | 401 scoped_ptr<HeartbeatSender> heartbeat_sender_; |
366 scoped_ptr<LogToServer> log_to_server_; | 402 scoped_ptr<LogToServer> log_to_server_; |
367 scoped_ptr<HostEventLogger> host_event_logger_; | 403 scoped_ptr<HostEventLogger> host_event_logger_; |
368 scoped_refptr<ChromotingHost> host_; | 404 scoped_refptr<ChromotingHost> host_; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 int CALLBACK WinMain(HINSTANCE instance, | 444 int CALLBACK WinMain(HINSTANCE instance, |
409 HINSTANCE previous_instance, | 445 HINSTANCE previous_instance, |
410 LPSTR command_line, | 446 LPSTR command_line, |
411 int show_command) { | 447 int show_command) { |
412 // CommandLine::Init() ignores the passed |argc| and |argv| on Windows getting | 448 // CommandLine::Init() ignores the passed |argc| and |argv| on Windows getting |
413 // the command line from GetCommandLineW(), so we can safely pass NULL here. | 449 // the command line from GetCommandLineW(), so we can safely pass NULL here. |
414 return main(0, NULL); | 450 return main(0, NULL); |
415 } | 451 } |
416 | 452 |
417 #endif | 453 #endif |
OLD | NEW |