| 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/command_line.h" |
| 9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/string16.h" |
| 10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #if defined(OS_WIN) |
| 15 #include "base/win_util.h" |
| 16 #endif // defined(OS_WIN) |
| 12 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
| 13 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/json_pref_store.h" | 20 #include "chrome/common/json_pref_store.h" |
| 15 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/common/service_process_type.h" | 22 #include "chrome/common/service_process_type.h" |
| 17 #include "chrome/common/service_process_util.h" | 23 #include "chrome/common/service_process_util.h" |
| 18 #include "chrome/service/cloud_print/cloud_print_proxy.h" | 24 #include "chrome/service/cloud_print/cloud_print_proxy.h" |
| 19 #include "chrome/service/service_ipc_server.h" | 25 #include "chrome/service/service_ipc_server.h" |
| 20 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
| 21 | 27 |
| 22 #if defined(ENABLE_REMOTING) | 28 #if defined(ENABLE_REMOTING) |
| 23 #include "remoting/base/constants.h" | 29 #include "remoting/base/constants.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 35 #elif defined(OS_MACOSX) | 41 #elif defined(OS_MACOSX) |
| 36 #include "remoting/host/capturer_mac.h" | 42 #include "remoting/host/capturer_mac.h" |
| 37 #include "remoting/host/event_executor_mac.h" | 43 #include "remoting/host/event_executor_mac.h" |
| 38 #endif | 44 #endif |
| 39 #endif // defined(ENABLED_REMOTING) | 45 #endif // defined(ENABLED_REMOTING) |
| 40 | 46 |
| 41 ServiceProcess* g_service_process = NULL; | 47 ServiceProcess* g_service_process = NULL; |
| 42 | 48 |
| 43 ServiceProcess::ServiceProcess() | 49 ServiceProcess::ServiceProcess() |
| 44 : shutdown_event_(true, false), | 50 : shutdown_event_(true, false), |
| 45 main_message_loop_(NULL) { | 51 main_message_loop_(NULL), |
| 52 enabled_services_(0) { |
| 46 DCHECK(!g_service_process); | 53 DCHECK(!g_service_process); |
| 47 g_service_process = this; | 54 g_service_process = this; |
| 48 } | 55 } |
| 49 | 56 |
| 50 bool ServiceProcess::Initialize(MessageLoop* message_loop) { | 57 bool ServiceProcess::Initialize(MessageLoop* message_loop) { |
| 51 main_message_loop_ = message_loop; | 58 main_message_loop_ = message_loop; |
| 52 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); | 59 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); |
| 53 base::Thread::Options options; | 60 base::Thread::Options options; |
| 54 options.message_loop_type = MessageLoop::TYPE_IO; | 61 options.message_loop_type = MessageLoop::TYPE_IO; |
| 55 io_thread_.reset(new base::Thread("ServiceProcess_IO")); | 62 io_thread_.reset(new base::Thread("ServiceProcess_IO")); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 70 DictionaryValue* values = service_prefs_->prefs(); | 77 DictionaryValue* values = service_prefs_->prefs(); |
| 71 bool remoting_host_enabled = false; | 78 bool remoting_host_enabled = false; |
| 72 | 79 |
| 73 // Check if remoting host is already enabled. | 80 // Check if remoting host is already enabled. |
| 74 if (values->GetBoolean(prefs::kRemotingHostEnabled, &remoting_host_enabled) && | 81 if (values->GetBoolean(prefs::kRemotingHostEnabled, &remoting_host_enabled) && |
| 75 remoting_host_enabled) { | 82 remoting_host_enabled) { |
| 76 // If true then we start the host. | 83 // If true then we start the host. |
| 77 StartChromotingHost(); | 84 StartChromotingHost(); |
| 78 } | 85 } |
| 79 | 86 |
| 80 // TODO(hclam): Each type of service process should has it own instance of | 87 bool cloud_print_proxy_enabled = false; |
| 81 // process and thus channel, but now we have only one process for all types | 88 |
| 82 // so the type parameter doesn't matter now. | 89 // Check if the cloud print proxy is already enabled. |
| 90 if (values->GetBoolean(prefs::kCloudPrintProxyEnabled, |
| 91 &cloud_print_proxy_enabled) && |
| 92 cloud_print_proxy_enabled) { |
| 93 GetCloudPrintProxy()->EnableForUser(std::string()); |
| 94 } |
| 95 |
| 83 LOG(INFO) << "Starting Service Process IPC Server"; | 96 LOG(INFO) << "Starting Service Process IPC Server"; |
| 84 ipc_server_.reset(new ServiceIPCServer( | 97 ipc_server_.reset(new ServiceIPCServer(GetServiceProcessChannelName())); |
| 85 GetServiceProcessChannelName(kServiceProcessCloudPrint))); | |
| 86 ipc_server_->Init(); | 98 ipc_server_->Init(); |
| 87 | 99 |
| 88 // After the IPC server has started we signal that the service process is | 100 // After the IPC server has started we signal that the service process is |
| 89 // running. | 101 // running. |
| 90 SignalServiceProcessRunning(kServiceProcessCloudPrint); | 102 SignalServiceProcessRunning(kServiceProcessCloudPrint); |
| 91 return true; | 103 return true; |
| 92 } | 104 } |
| 93 | 105 |
| 94 bool ServiceProcess::Teardown() { | 106 bool ServiceProcess::Teardown() { |
| 95 if (service_prefs_.get()) { | 107 if (service_prefs_.get()) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 118 } | 130 } |
| 119 | 131 |
| 120 void ServiceProcess::Shutdown() { | 132 void ServiceProcess::Shutdown() { |
| 121 // Quit the main message loop. | 133 // Quit the main message loop. |
| 122 main_message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 134 main_message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 123 } | 135 } |
| 124 | 136 |
| 125 CloudPrintProxy* ServiceProcess::GetCloudPrintProxy() { | 137 CloudPrintProxy* ServiceProcess::GetCloudPrintProxy() { |
| 126 if (!cloud_print_proxy_.get()) { | 138 if (!cloud_print_proxy_.get()) { |
| 127 cloud_print_proxy_.reset(new CloudPrintProxy()); | 139 cloud_print_proxy_.reset(new CloudPrintProxy()); |
| 128 cloud_print_proxy_->Initialize(service_prefs_.get()); | 140 cloud_print_proxy_->Initialize(service_prefs_.get(), this); |
| 129 } | 141 } |
| 130 return cloud_print_proxy_.get(); | 142 return cloud_print_proxy_.get(); |
| 131 } | 143 } |
| 132 | 144 |
| 145 void ServiceProcess::OnCloudPrintProxyEnabled() { |
| 146 // Save the preference that we have enabled the cloud print proxy. |
| 147 service_prefs_->prefs()->SetBoolean(prefs::kCloudPrintProxyEnabled, true); |
| 148 service_prefs_->WritePrefs(); |
| 149 OnServiceEnabled(); |
| 150 } |
| 151 |
| 152 void ServiceProcess::OnCloudPrintProxyDisabled() { |
| 153 // Save the preference that we have disabled the cloud print proxy. |
| 154 service_prefs_->prefs()->SetBoolean(prefs::kCloudPrintProxyEnabled, false); |
| 155 service_prefs_->WritePrefs(); |
| 156 OnServiceDisabled(); |
| 157 } |
| 158 |
| 159 void ServiceProcess::OnServiceEnabled() { |
| 160 enabled_services_++; |
| 161 if (1 == enabled_services_) { |
| 162 AddServiceProcessToAutoStart(); |
| 163 } |
| 164 } |
| 165 |
| 166 void ServiceProcess::OnServiceDisabled() { |
| 167 DCHECK(0 != enabled_services_); |
| 168 enabled_services_--; |
| 169 if (0 == enabled_services_) { |
| 170 RemoveServiceProcessFromAutoStart(); |
| 171 Shutdown(); |
| 172 } |
| 173 } |
| 174 |
| 175 bool ServiceProcess::AddServiceProcessToAutoStart() { |
| 176 // TODO(sanjeevr): This needs to move to some common place like base or |
| 177 // chrome/common and implementation for non-Windows platforms needs to be added. |
| 178 #if defined(OS_WIN) |
| 179 FilePath chrome_path; |
| 180 if (PathService::Get(base::FILE_EXE, &chrome_path)) { |
| 181 CommandLine cmd_line(chrome_path); |
| 182 cmd_line.AppendSwitchASCII(switches::kProcessType, |
| 183 switches::kServiceProcess); |
| 184 // We need a unique name for the command per user-date-dir. Just use the |
| 185 // channel name. |
| 186 return win_util::AddCommandToAutoRun( |
| 187 HKEY_CURRENT_USER, UTF8ToWide(GetServiceProcessChannelName()), |
| 188 cmd_line.command_line_string()); |
| 189 } |
| 190 #endif // defined(OS_WIN) |
| 191 return false; |
| 192 } |
| 193 |
| 194 bool ServiceProcess::RemoveServiceProcessFromAutoStart() { |
| 195 // TODO(sanjeevr): This needs to move to some common place like base or |
| 196 // chrome/common and implementation for non-Windows platforms needs to be added. |
| 197 #if defined(OS_WIN) |
| 198 return win_util::RemoveCommandFromAutoRun( |
| 199 HKEY_CURRENT_USER, UTF8ToWide(GetServiceProcessChannelName())); |
| 200 #endif // defined(OS_WIN) |
| 201 return false; |
| 202 } |
| 203 |
| 204 |
| 133 #if defined(ENABLE_REMOTING) | 205 #if defined(ENABLE_REMOTING) |
| 134 bool ServiceProcess::EnableChromotingHostWithTokens( | 206 bool ServiceProcess::EnableChromotingHostWithTokens( |
| 135 const std::string& login, | 207 const std::string& login, |
| 136 const std::string& remoting_token, | 208 const std::string& remoting_token, |
| 137 const std::string& talk_token) { | 209 const std::string& talk_token) { |
| 138 // Save the login info and tokens. | 210 // Save the login info and tokens. |
| 139 remoting_login_ = login; | 211 remoting_login_ = login; |
| 140 remoting_token_ = remoting_token; | 212 remoting_token_ = remoting_token; |
| 141 talk_token_ = talk_token; | 213 talk_token_ = talk_token; |
| 142 | 214 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 chromoting_config_, | 255 chromoting_config_, |
| 184 capturer.release(), | 256 capturer.release(), |
| 185 encoder.release(), | 257 encoder.release(), |
| 186 executor.release()); | 258 executor.release()); |
| 187 | 259 |
| 188 // Then start the chromoting host. | 260 // Then start the chromoting host. |
| 189 // When ChromotingHost is shutdown because of failure or a request that | 261 // When ChromotingHost is shutdown because of failure or a request that |
| 190 // we made OnChromotingShutdown() is calls. | 262 // we made OnChromotingShutdown() is calls. |
| 191 chromoting_host_->Start( | 263 chromoting_host_->Start( |
| 192 NewRunnableMethod(this, &ServiceProcess::OnChromotingHostShutdown)); | 264 NewRunnableMethod(this, &ServiceProcess::OnChromotingHostShutdown)); |
| 265 OnServiceEnabled(); |
| 193 return true; | 266 return true; |
| 194 } | 267 } |
| 195 | 268 |
| 196 bool ServiceProcess::ShutdownChromotingHost() { | 269 bool ServiceProcess::ShutdownChromotingHost() { |
| 197 // Chromoting host doesn't exist so return true. | 270 // Chromoting host doesn't exist so return true. |
| 198 if (!chromoting_host_) | 271 if (!chromoting_host_) |
| 199 return true; | 272 return true; |
| 200 | 273 |
| 201 // Shutdown the chromoting host asynchronously. This will signal the host to | 274 // Shutdown the chromoting host asynchronously. This will signal the host to |
| 202 // shutdown, we'll actually wait for all threads to stop when we destroy | 275 // shutdown, we'll actually wait for all threads to stop when we destroy |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 #endif | 366 #endif |
| 294 | 367 |
| 295 ServiceProcess::~ServiceProcess() { | 368 ServiceProcess::~ServiceProcess() { |
| 296 Teardown(); | 369 Teardown(); |
| 297 g_service_process = NULL; | 370 g_service_process = NULL; |
| 298 } | 371 } |
| 299 | 372 |
| 300 // Disable refcounting for runnable method because it is really not needed | 373 // Disable refcounting for runnable method because it is really not needed |
| 301 // when we post tasks on the main message loop. | 374 // when we post tasks on the main message loop. |
| 302 DISABLE_RUNNABLE_METHOD_REFCOUNT(ServiceProcess); | 375 DISABLE_RUNNABLE_METHOD_REFCOUNT(ServiceProcess); |
| OLD | NEW |