| 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 chromoting_host_manager_ = new remoting::ChromotingHostManager(this); |
| 116 StartChromotingHost(); | 103 chromoting_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 chromoting_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::OnChromotingHostEnabled() { |
| 194 OnServiceEnabled(); |
| 195 } |
| 196 |
| 197 void ServiceProcess::OnChromotingHostDisabled() { |
| 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 return true; | |
| 299 } | |
| 300 | |
| 301 void ServiceProcess::OnRemotingHostAdded() { | |
| 302 // Save configuration for chromoting. | |
| 303 SaveChromotingConfig(remoting_login_, | |
| 304 talk_token_, | |
| 305 remoting_directory_->host_id(), | |
| 306 remoting_directory_->host_name(), | |
| 307 remoting_directory_->host_key_pair()); | |
| 308 remoting_directory_.reset(); | |
| 309 remoting_login_ = ""; | |
| 310 remoting_token_ = ""; | |
| 311 talk_token_ = ""; | |
| 312 | |
| 313 // Save the preference that we have enabled the remoting host. | |
| 314 service_prefs_->SetBoolean(prefs::kRemotingHostEnabled, true); | |
| 315 | |
| 316 // Force writing prefs to the disk. | |
| 317 service_prefs_->WritePrefs(); | |
| 318 | |
| 319 // TODO(hclam): If we have a problem we need to send an IPC message back | |
| 320 // to the client that started this. | |
| 321 bool ret = StartChromotingHost(); | |
| 322 DCHECK(ret); | |
| 323 } | |
| 324 | |
| 325 void ServiceProcess::OnRemotingDirectoryError() { | |
| 326 remoting_directory_.reset(); | |
| 327 remoting_login_ = ""; | |
| 328 remoting_token_ = ""; | |
| 329 talk_token_ = ""; | |
| 330 | |
| 331 // TODO(hclam): If we have a problem we need to send an IPC message back | |
| 332 // to the client that started this. | |
| 333 } | |
| 334 | |
| 335 void ServiceProcess::SaveChromotingConfig( | |
| 336 const std::string& login, | |
| 337 const std::string& token, | |
| 338 const std::string& host_id, | |
| 339 const std::string& host_name, | |
| 340 remoting::HostKeyPair* host_key_pair) { | |
| 341 // First we need to load the config first. | |
| 342 LoadChromotingConfig(); | |
| 343 | |
| 344 // And then do the update. | |
| 345 chromoting_config_->SetString(remoting::kXmppLoginConfigPath, login); | |
| 346 chromoting_config_->SetString(remoting::kXmppAuthTokenConfigPath, token); | |
| 347 chromoting_config_->SetString(remoting::kHostIdConfigPath, host_id); | |
| 348 chromoting_config_->SetString(remoting::kHostNameConfigPath, host_name); | |
| 349 chromoting_config_->Save(); | |
| 350 | |
| 351 // And then save the key pair. | |
| 352 host_key_pair->Save(chromoting_config_); | |
| 353 } | |
| 354 | |
| 355 void ServiceProcess::LoadChromotingConfig() { | |
| 356 // TODO(hclam): We really should be doing this on IO thread so we are not | |
| 357 // blocked on file IOs. | |
| 358 if (chromoting_config_) | |
| 359 return; | |
| 360 | |
| 361 FilePath user_data_dir; | |
| 362 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
| 363 FilePath chromoting_config_path = | |
| 364 user_data_dir.Append(FILE_PATH_LITERAL(".ChromotingConfig.json")); | |
| 365 chromoting_config_ = new remoting::JsonHostConfig( | |
| 366 chromoting_config_path, file_thread_->message_loop_proxy()); | |
| 367 if (!chromoting_config_->Read()) | |
| 368 VLOG(1) << "Failed to read chromoting config file."; | |
| 369 } | |
| 370 | |
| 371 void ServiceProcess::OnChromotingHostShutdown() { | |
| 372 // TODO(hclam): Implement. | |
| 373 } | |
| 374 #endif | |
| 375 | |
| 376 ServiceProcess::~ServiceProcess() { | 239 ServiceProcess::~ServiceProcess() { |
| 377 Teardown(); | 240 Teardown(); |
| 378 g_service_process = NULL; | 241 g_service_process = NULL; |
| 379 } | 242 } |
| 380 | 243 |
| 381 // Disable refcounting for runnable method because it is really not needed | 244 // Disable refcounting for runnable method because it is really not needed |
| 382 // when we post tasks on the main message loop. | 245 // when we post tasks on the main message loop. |
| 383 DISABLE_RUNNABLE_METHOD_REFCOUNT(ServiceProcess); | 246 DISABLE_RUNNABLE_METHOD_REFCOUNT(ServiceProcess); |
| OLD | NEW |