OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/service/service_process.h" | 5 #include "chrome/service/service_process.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/singleton.h" | 12 #include "base/singleton.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/common/chrome_constants.h" | 16 #include "chrome/common/chrome_constants.h" |
17 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/net/url_fetcher.h" |
19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
20 #include "chrome/common/service_process_util.h" | 21 #include "chrome/common/service_process_util.h" |
21 #include "chrome/service/cloud_print/cloud_print_proxy.h" | 22 #include "chrome/service/cloud_print/cloud_print_proxy.h" |
22 #include "chrome/service/service_ipc_server.h" | 23 #include "chrome/service/service_ipc_server.h" |
23 #include "chrome/service/service_process_prefs.h" | 24 #include "chrome/service/service_process_prefs.h" |
24 #include "net/base/network_change_notifier.h" | 25 #include "net/base/network_change_notifier.h" |
25 | 26 |
26 #if defined(ENABLE_REMOTING) | 27 #if defined(ENABLE_REMOTING) |
27 #include "remoting/base/constants.h" | 28 #include "chrome/service/remoting/chromoting_host_manager.h" |
28 #include "remoting/host/chromoting_host.h" | |
29 #include "remoting/host/chromoting_host_context.h" | |
30 #include "remoting/host/json_host_config.h" | |
31 #endif // defined(ENABLED_REMOTING) | 29 #endif // defined(ENABLED_REMOTING) |
32 | 30 |
33 ServiceProcess* g_service_process = NULL; | 31 ServiceProcess* g_service_process = NULL; |
34 | 32 |
35 namespace { | 33 namespace { |
36 | 34 |
37 // Delay in millseconds after the last service is disabled before we attempt | 35 // Delay in millseconds after the last service is disabled before we attempt |
38 // a shutdown. | 36 // a shutdown. |
39 const int64 kShutdownDelay = 60000; | 37 const int64 kShutdownDelay = 60000; |
40 | 38 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 std::string lsid = command_line.GetSwitchValueASCII( | 90 std::string lsid = command_line.GetSwitchValueASCII( |
93 switches::kServiceAccountLsid); | 91 switches::kServiceAccountLsid); |
94 | 92 |
95 FilePath user_data_dir; | 93 FilePath user_data_dir; |
96 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | 94 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
97 FilePath pref_path = user_data_dir.Append(chrome::kServiceStateFileName); | 95 FilePath pref_path = user_data_dir.Append(chrome::kServiceStateFileName); |
98 service_prefs_.reset( | 96 service_prefs_.reset( |
99 new ServiceProcessPrefs(pref_path, file_thread_->message_loop_proxy())); | 97 new ServiceProcessPrefs(pref_path, file_thread_->message_loop_proxy())); |
100 service_prefs_->ReadPrefs(); | 98 service_prefs_->ReadPrefs(); |
101 | 99 |
102 bool remoting_host_enabled = false; | |
103 | |
104 // For development, we allow forcing the enabling of the host daemon via a | |
105 // commandline flag, regardless of the preference setting. | |
106 // | |
107 // TODO(ajwong): When we've gotten the preference setting workflow more | |
108 // stable, we should remove the command-line flag force-enable. | |
109 service_prefs_->GetBoolean(prefs::kRemotingHostEnabled, | |
110 &remoting_host_enabled); | |
111 remoting_host_enabled |= command_line.HasSwitch(switches::kEnableRemoting); | |
112 | |
113 #if defined(ENABLE_REMOTING) | 100 #if defined(ENABLE_REMOTING) |
114 // Check if remoting host is already enabled. | 101 // Initialize chromoting host manager. |
115 if (remoting_host_enabled) { | 102 remoting_host_manager_ = new remoting::ChromotingHostManager(this); |
116 StartChromotingHost(); | 103 remoting_host_manager_->Initialize(file_thread_->message_loop_proxy()); |
117 } | |
118 #endif | 104 #endif |
119 | 105 |
120 // Enable Cloud Print if needed. First check the command-line. | 106 // Enable Cloud Print if needed. First check the command-line. |
121 bool cloud_print_proxy_enabled = | 107 bool cloud_print_proxy_enabled = |
122 command_line.HasSwitch(switches::kEnableCloudPrintProxy); | 108 command_line.HasSwitch(switches::kEnableCloudPrintProxy); |
123 if (!cloud_print_proxy_enabled) { | 109 if (!cloud_print_proxy_enabled) { |
124 // Then check if the cloud print proxy was previously enabled. | 110 // Then check if the cloud print proxy was previously enabled. |
125 service_prefs_->GetBoolean(prefs::kCloudPrintProxyEnabled, | 111 service_prefs_->GetBoolean(prefs::kCloudPrintProxyEnabled, |
126 &cloud_print_proxy_enabled); | 112 &cloud_print_proxy_enabled); |
127 } | 113 } |
(...skipping 14 matching lines...) Expand all Loading... |
142 // See if we need to stay running. | 128 // See if we need to stay running. |
143 ScheduleShutdownCheck(); | 129 ScheduleShutdownCheck(); |
144 return true; | 130 return true; |
145 } | 131 } |
146 | 132 |
147 bool ServiceProcess::Teardown() { | 133 bool ServiceProcess::Teardown() { |
148 service_prefs_.reset(); | 134 service_prefs_.reset(); |
149 cloud_print_proxy_.reset(); | 135 cloud_print_proxy_.reset(); |
150 | 136 |
151 #if defined(ENABLE_REMOTING) | 137 #if defined(ENABLE_REMOTING) |
152 ShutdownChromotingHost(); | 138 remoting_host_manager_->Teardown(); |
153 #endif | 139 #endif |
154 | 140 |
155 ipc_server_.reset(); | 141 ipc_server_.reset(); |
156 // Signal this event before shutting down the service process. That way all | 142 // Signal this event before shutting down the service process. That way all |
157 // background threads can cleanup. | 143 // background threads can cleanup. |
158 shutdown_event_.Signal(); | 144 shutdown_event_.Signal(); |
159 io_thread_.reset(); | 145 io_thread_.reset(); |
160 file_thread_.reset(); | 146 file_thread_.reset(); |
161 // The NetworkChangeNotifier must be destroyed after all other threads that | 147 // The NetworkChangeNotifier must be destroyed after all other threads that |
162 // might use it have been shut down. | 148 // might use it have been shut down. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 OnServiceEnabled(); | 183 OnServiceEnabled(); |
198 } | 184 } |
199 | 185 |
200 void ServiceProcess::OnCloudPrintProxyDisabled() { | 186 void ServiceProcess::OnCloudPrintProxyDisabled() { |
201 // Save the preference that we have disabled the cloud print proxy. | 187 // Save the preference that we have disabled the cloud print proxy. |
202 service_prefs_->SetBoolean(prefs::kCloudPrintProxyEnabled, false); | 188 service_prefs_->SetBoolean(prefs::kCloudPrintProxyEnabled, false); |
203 service_prefs_->WritePrefs(); | 189 service_prefs_->WritePrefs(); |
204 OnServiceDisabled(); | 190 OnServiceDisabled(); |
205 } | 191 } |
206 | 192 |
| 193 void ServiceProcess::OnRemotingHostEnabled() { |
| 194 OnServiceEnabled(); |
| 195 } |
| 196 |
| 197 void ServiceProcess::OnRemotingHostDisabled() { |
| 198 OnServiceDisabled(); |
| 199 } |
| 200 |
207 void ServiceProcess::OnServiceEnabled() { | 201 void ServiceProcess::OnServiceEnabled() { |
208 enabled_services_++; | 202 enabled_services_++; |
209 if (1 == enabled_services_) { | 203 if (1 == enabled_services_) { |
210 ServiceProcessState::GetInstance()->AddToAutoRun(); | 204 ServiceProcessState::GetInstance()->AddToAutoRun(); |
211 } | 205 } |
212 } | 206 } |
213 | 207 |
214 void ServiceProcess::OnServiceDisabled() { | 208 void ServiceProcess::OnServiceDisabled() { |
215 DCHECK_NE(enabled_services_, 0); | 209 DCHECK_NE(enabled_services_, 0); |
216 enabled_services_--; | 210 enabled_services_--; |
(...skipping 18 matching lines...) Expand all Loading... |
235 // Note that there is still a timing window here because a client may | 229 // Note that there is still a timing window here because a client may |
236 // decide to connect at this point. | 230 // decide to connect at this point. |
237 // TODO(sanjeevr): Fix this timing window. | 231 // TODO(sanjeevr): Fix this timing window. |
238 ScheduleShutdownCheck(); | 232 ScheduleShutdownCheck(); |
239 } else { | 233 } else { |
240 Shutdown(); | 234 Shutdown(); |
241 } | 235 } |
242 } | 236 } |
243 } | 237 } |
244 | 238 |
245 #if defined(ENABLE_REMOTING) | |
246 bool ServiceProcess::EnableChromotingHostWithTokens( | |
247 const std::string& login, | |
248 const std::string& remoting_token, | |
249 const std::string& talk_token) { | |
250 // Save the login info and tokens. | |
251 remoting_login_ = login; | |
252 remoting_token_ = remoting_token; | |
253 talk_token_ = talk_token; | |
254 | |
255 // Use the remoting directory to register the host. | |
256 if (remoting_directory_.get()) | |
257 remoting_directory_->CancelRequest(); | |
258 remoting_directory_.reset(new RemotingDirectoryService(this)); | |
259 remoting_directory_->AddHost(remoting_token); | |
260 return true; | |
261 } | |
262 | |
263 bool ServiceProcess::StartChromotingHost() { | |
264 // We have already started. | |
265 if (chromoting_context_.get()) | |
266 return true; | |
267 | |
268 // Load chromoting config from the disk. | |
269 LoadChromotingConfig(); | |
270 | |
271 // Start the chromoting context first. | |
272 chromoting_context_.reset(new remoting::ChromotingHostContext()); | |
273 chromoting_context_->Start(); | |
274 | |
275 // Create a chromoting host object. | |
276 chromoting_host_ = remoting::ChromotingHost::Create(chromoting_context_.get(), | |
277 chromoting_config_); | |
278 | |
279 // Then start the chromoting host. | |
280 // When ChromotingHost is shutdown because of failure or a request that | |
281 // we made OnChromotingShutdown() is calls. | |
282 chromoting_host_->Start( | |
283 NewRunnableMethod(this, &ServiceProcess::OnChromotingHostShutdown)); | |
284 OnServiceEnabled(); | |
285 return true; | |
286 } | |
287 | |
288 bool ServiceProcess::ShutdownChromotingHost() { | |
289 // Chromoting host doesn't exist so return true. | |
290 if (!chromoting_host_) | |
291 return true; | |
292 | |
293 // Shutdown the chromoting host asynchronously. This will signal the host to | |
294 // shutdown, we'll actually wait for all threads to stop when we destroy | |
295 // the chromoting context. | |
296 chromoting_host_->Shutdown(); | |
297 chromoting_host_ = NULL; | |
298 | |
299 chromoting_context_->Stop(); | |
300 chromoting_context_ .reset(); | |
301 | |
302 return true; | |
303 } | |
304 | |
305 void ServiceProcess::OnRemotingHostAdded() { | |
306 // Save configuration for chromoting. | |
307 SaveChromotingConfig(remoting_login_, | |
308 talk_token_, | |
309 remoting_directory_->host_id(), | |
310 remoting_directory_->host_name(), | |
311 remoting_directory_->host_key_pair()); | |
312 remoting_directory_.reset(); | |
313 remoting_login_ = ""; | |
314 remoting_token_ = ""; | |
315 talk_token_ = ""; | |
316 | |
317 // Save the preference that we have enabled the remoting host. | |
318 service_prefs_->SetBoolean(prefs::kRemotingHostEnabled, true); | |
319 | |
320 // Force writing prefs to the disk. | |
321 service_prefs_->WritePrefs(); | |
322 | |
323 // TODO(hclam): If we have a problem we need to send an IPC message back | |
324 // to the client that started this. | |
325 bool ret = StartChromotingHost(); | |
326 DCHECK(ret); | |
327 } | |
328 | |
329 void ServiceProcess::OnRemotingDirectoryError() { | |
330 remoting_directory_.reset(); | |
331 remoting_login_ = ""; | |
332 remoting_token_ = ""; | |
333 talk_token_ = ""; | |
334 | |
335 // TODO(hclam): If we have a problem we need to send an IPC message back | |
336 // to the client that started this. | |
337 } | |
338 | |
339 void ServiceProcess::SaveChromotingConfig( | |
340 const std::string& login, | |
341 const std::string& token, | |
342 const std::string& host_id, | |
343 const std::string& host_name, | |
344 remoting::HostKeyPair* host_key_pair) { | |
345 // First we need to load the config first. | |
346 LoadChromotingConfig(); | |
347 | |
348 // And then do the update. | |
349 chromoting_config_->SetString(remoting::kXmppLoginConfigPath, login); | |
350 chromoting_config_->SetString(remoting::kXmppAuthTokenConfigPath, token); | |
351 chromoting_config_->SetString(remoting::kHostIdConfigPath, host_id); | |
352 chromoting_config_->SetString(remoting::kHostNameConfigPath, host_name); | |
353 chromoting_config_->Save(); | |
354 | |
355 // And then save the key pair. | |
356 host_key_pair->Save(chromoting_config_); | |
357 } | |
358 | |
359 void ServiceProcess::LoadChromotingConfig() { | |
360 // TODO(hclam): We really should be doing this on IO thread so we are not | |
361 // blocked on file IOs. | |
362 if (chromoting_config_) | |
363 return; | |
364 | |
365 FilePath user_data_dir; | |
366 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
367 FilePath chromoting_config_path = | |
368 user_data_dir.Append(FILE_PATH_LITERAL(".ChromotingConfig.json")); | |
369 chromoting_config_ = new remoting::JsonHostConfig( | |
370 chromoting_config_path, file_thread_->message_loop_proxy()); | |
371 if (!chromoting_config_->Read()) | |
372 VLOG(1) << "Failed to read chromoting config file."; | |
373 } | |
374 | |
375 void ServiceProcess::OnChromotingHostShutdown() { | |
376 // TODO(hclam): Implement. | |
377 } | |
378 #endif | |
379 | |
380 ServiceProcess::~ServiceProcess() { | 239 ServiceProcess::~ServiceProcess() { |
381 Teardown(); | 240 Teardown(); |
382 g_service_process = NULL; | 241 g_service_process = NULL; |
383 } | 242 } |
384 | 243 |
385 // Disable refcounting for runnable method because it is really not needed | 244 // Disable refcounting for runnable method because it is really not needed |
386 // when we post tasks on the main message loop. | 245 // when we post tasks on the main message loop. |
387 DISABLE_RUNNABLE_METHOD_REFCOUNT(ServiceProcess); | 246 DISABLE_RUNNABLE_METHOD_REFCOUNT(ServiceProcess); |
OLD | NEW |