| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "cloud_print/service/win/service_controller.h" | 5 #include "cloud_print/service/win/service_controller.h" |
| 6 | 6 |
| 7 #include <atlbase.h> | 7 #include <atlbase.h> |
| 8 #include <atlcom.h> | 8 #include <atlcom.h> |
| 9 #include <atlctl.h> | 9 #include <atlctl.h> |
| 10 | 10 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 HRESULT OpenService(const base::string16& name, DWORD access, | 64 HRESULT OpenService(const base::string16& name, DWORD access, |
| 65 ServiceHandle* service) { | 65 ServiceHandle* service) { |
| 66 if (!service) | 66 if (!service) |
| 67 return E_POINTER; | 67 return E_POINTER; |
| 68 | 68 |
| 69 ServiceHandle scm; | 69 ServiceHandle scm; |
| 70 HRESULT hr = OpenServiceManager(&scm); | 70 HRESULT hr = OpenServiceManager(&scm); |
| 71 if (FAILED(hr)) | 71 if (FAILED(hr)) |
| 72 return hr; | 72 return hr; |
| 73 | 73 |
| 74 service->Set(::OpenService(scm, name.c_str(), access)); | 74 service->Set(::OpenService(scm.Get(), name.c_str(), access)); |
| 75 | 75 |
| 76 if (!service->IsValid()) | 76 if (!service->IsValid()) |
| 77 return cloud_print::GetLastHResult(); | 77 return cloud_print::GetLastHResult(); |
| 78 | 78 |
| 79 return S_OK; | 79 return S_OK; |
| 80 } | 80 } |
| 81 | 81 |
| 82 } // namespace | 82 } // namespace |
| 83 | 83 |
| 84 ServiceController::ServiceController() | 84 ServiceController::ServiceController() |
| 85 : name_(cloud_print::LoadLocalString(IDS_SERVICE_NAME)), | 85 : name_(cloud_print::LoadLocalString(IDS_SERVICE_NAME)), |
| 86 command_line_(CommandLine::NO_PROGRAM) { | 86 command_line_(CommandLine::NO_PROGRAM) { |
| 87 } | 87 } |
| 88 | 88 |
| 89 ServiceController::~ServiceController() { | 89 ServiceController::~ServiceController() { |
| 90 } | 90 } |
| 91 | 91 |
| 92 HRESULT ServiceController::StartService() { | 92 HRESULT ServiceController::StartService() { |
| 93 ServiceHandle service; | 93 ServiceHandle service; |
| 94 HRESULT hr = OpenService(name_, SERVICE_START| SERVICE_QUERY_STATUS, | 94 HRESULT hr = OpenService(name_, SERVICE_START| SERVICE_QUERY_STATUS, |
| 95 &service); | 95 &service); |
| 96 if (FAILED(hr)) | 96 if (FAILED(hr)) |
| 97 return hr; | 97 return hr; |
| 98 if (!::StartService(service, 0, NULL)) | 98 if (!::StartService(service.Get(), 0, NULL)) |
| 99 return cloud_print::GetLastHResult(); | 99 return cloud_print::GetLastHResult(); |
| 100 SERVICE_STATUS status = {0}; | 100 SERVICE_STATUS status = {0}; |
| 101 while (::QueryServiceStatus(service, &status) && | 101 while (::QueryServiceStatus(service.Get(), &status) && |
| 102 status.dwCurrentState == SERVICE_START_PENDING) { | 102 status.dwCurrentState == SERVICE_START_PENDING) { |
| 103 Sleep(100); | 103 Sleep(100); |
| 104 } | 104 } |
| 105 return S_OK; | 105 return S_OK; |
| 106 } | 106 } |
| 107 | 107 |
| 108 HRESULT ServiceController::StopService() { | 108 HRESULT ServiceController::StopService() { |
| 109 ServiceHandle service; | 109 ServiceHandle service; |
| 110 HRESULT hr = OpenService(name_, SERVICE_STOP | SERVICE_QUERY_STATUS, | 110 HRESULT hr = OpenService(name_, SERVICE_STOP | SERVICE_QUERY_STATUS, |
| 111 &service); | 111 &service); |
| 112 if (FAILED(hr)) | 112 if (FAILED(hr)) |
| 113 return hr; | 113 return hr; |
| 114 SERVICE_STATUS status = {0}; | 114 SERVICE_STATUS status = {0}; |
| 115 if (!::ControlService(service, SERVICE_CONTROL_STOP, &status)) | 115 if (!::ControlService(service.Get(), SERVICE_CONTROL_STOP, &status)) |
| 116 return cloud_print::GetLastHResult(); | 116 return cloud_print::GetLastHResult(); |
| 117 while (::QueryServiceStatus(service, &status) && | 117 while (::QueryServiceStatus(service.Get(), &status) && |
| 118 status.dwCurrentState > SERVICE_STOPPED) { | 118 status.dwCurrentState > SERVICE_STOPPED) { |
| 119 Sleep(500); | 119 Sleep(500); |
| 120 ::ControlService(service, SERVICE_CONTROL_STOP, &status); | 120 ::ControlService(service.Get(), SERVICE_CONTROL_STOP, &status); |
| 121 } | 121 } |
| 122 return S_OK; | 122 return S_OK; |
| 123 } | 123 } |
| 124 | 124 |
| 125 base::FilePath ServiceController::GetBinary() const { | 125 base::FilePath ServiceController::GetBinary() const { |
| 126 base::FilePath service_path; | 126 base::FilePath service_path; |
| 127 CHECK(PathService::Get(base::FILE_EXE, &service_path)); | 127 CHECK(PathService::Get(base::FILE_EXE, &service_path)); |
| 128 return service_path.DirName().Append(base::FilePath(kServiceExeName)); | 128 return service_path.DirName().Append(base::FilePath(kServiceExeName)); |
| 129 } | 129 } |
| 130 | 130 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 | 190 |
| 191 ServiceHandle scm; | 191 ServiceHandle scm; |
| 192 hr = OpenServiceManager(&scm); | 192 hr = OpenServiceManager(&scm); |
| 193 if (FAILED(hr)) | 193 if (FAILED(hr)) |
| 194 return hr; | 194 return hr; |
| 195 | 195 |
| 196 base::string16 display_name = | 196 base::string16 display_name = |
| 197 cloud_print::LoadLocalString(IDS_SERVICE_DISPLAY_NAME); | 197 cloud_print::LoadLocalString(IDS_SERVICE_DISPLAY_NAME); |
| 198 ServiceHandle service( | 198 ServiceHandle service( |
| 199 ::CreateService( | 199 ::CreateService( |
| 200 scm, name_.c_str(), display_name.c_str(), SERVICE_ALL_ACCESS, | 200 scm.Get(), name_.c_str(), display_name.c_str(), SERVICE_ALL_ACCESS, |
| 201 SERVICE_WIN32_OWN_PROCESS, | 201 SERVICE_WIN32_OWN_PROCESS, |
| 202 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START, | 202 auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START, |
| 203 SERVICE_ERROR_NORMAL, command_line.GetCommandLineString().c_str(), | 203 SERVICE_ERROR_NORMAL, command_line.GetCommandLineString().c_str(), |
| 204 NULL, NULL, NULL, user.empty() ? NULL : user.c_str(), | 204 NULL, NULL, NULL, user.empty() ? NULL : user.c_str(), |
| 205 password.empty() ? NULL : password.c_str())); | 205 password.empty() ? NULL : password.c_str())); |
| 206 | 206 |
| 207 if (!service.IsValid()) { | 207 if (!service.IsValid()) { |
| 208 LOG(ERROR) << "Failed to install service as " << user << "."; | 208 LOG(ERROR) << "Failed to install service as " << user << "."; |
| 209 return cloud_print::GetLastHResult(); | 209 return cloud_print::GetLastHResult(); |
| 210 } | 210 } |
| 211 | 211 |
| 212 base::string16 description_string = | 212 base::string16 description_string = |
| 213 cloud_print::LoadLocalString(IDS_SERVICE_DESCRIPTION); | 213 cloud_print::LoadLocalString(IDS_SERVICE_DESCRIPTION); |
| 214 SERVICE_DESCRIPTION description = {0}; | 214 SERVICE_DESCRIPTION description = {0}; |
| 215 description.lpDescription = const_cast<wchar_t*>(description_string.c_str()); | 215 description.lpDescription = const_cast<wchar_t*>(description_string.c_str()); |
| 216 ::ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description); | 216 ::ChangeServiceConfig2(service.Get(), SERVICE_CONFIG_DESCRIPTION, |
| 217 &description); |
| 217 | 218 |
| 218 return S_OK; | 219 return S_OK; |
| 219 } | 220 } |
| 220 | 221 |
| 221 HRESULT ServiceController::UninstallService() { | 222 HRESULT ServiceController::UninstallService() { |
| 222 StopService(); | 223 StopService(); |
| 223 | 224 |
| 224 ServiceHandle service; | 225 ServiceHandle service; |
| 225 OpenService(name_, SERVICE_STOP | DELETE, &service); | 226 OpenService(name_, SERVICE_STOP | DELETE, &service); |
| 226 HRESULT hr = S_FALSE; | 227 HRESULT hr = S_FALSE; |
| 227 if (service) { | 228 if (service.IsValid()) { |
| 228 if (!::DeleteService(service)) { | 229 if (!::DeleteService(service.Get())) { |
| 229 LOG(ERROR) << "Failed to uninstall service"; | 230 LOG(ERROR) << "Failed to uninstall service"; |
| 230 hr = cloud_print::GetLastHResult(); | 231 hr = cloud_print::GetLastHResult(); |
| 231 } | 232 } |
| 232 } | 233 } |
| 233 UpdateRegistryAppId(false); | 234 UpdateRegistryAppId(false); |
| 234 return hr; | 235 return hr; |
| 235 } | 236 } |
| 236 | 237 |
| 237 HRESULT ServiceController::UpdateBinaryPath() { | 238 HRESULT ServiceController::UpdateBinaryPath() { |
| 238 UpdateState(); | 239 UpdateState(); |
| 239 ServiceController::State origina_state = state(); | 240 ServiceController::State origina_state = state(); |
| 240 if (origina_state < ServiceController::STATE_STOPPED) | 241 if (origina_state < ServiceController::STATE_STOPPED) |
| 241 return S_FALSE; | 242 return S_FALSE; |
| 242 | 243 |
| 243 ServiceHandle service; | 244 ServiceHandle service; |
| 244 HRESULT hr = OpenService(name_, SERVICE_CHANGE_CONFIG, &service); | 245 HRESULT hr = OpenService(name_, SERVICE_CHANGE_CONFIG, &service); |
| 245 if (FAILED(hr)) | 246 if (FAILED(hr)) |
| 246 return hr; | 247 return hr; |
| 247 | 248 |
| 248 base::FilePath service_path = GetBinary(); | 249 base::FilePath service_path = GetBinary(); |
| 249 if (!base::PathExists(service_path)) | 250 if (!base::PathExists(service_path)) |
| 250 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); | 251 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); |
| 251 | 252 |
| 252 command_line_.SetProgram(service_path); | 253 command_line_.SetProgram(service_path); |
| 253 if (!::ChangeServiceConfig(service, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, | 254 if (!::ChangeServiceConfig(service.Get(), SERVICE_NO_CHANGE, |
| 254 SERVICE_NO_CHANGE, | 255 SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, |
| 255 command_line_.GetCommandLineString().c_str(), NULL, | 256 command_line_.GetCommandLineString().c_str(), NULL, |
| 256 NULL, NULL, NULL, NULL, NULL)) { | 257 NULL, NULL, NULL, NULL, NULL)) { |
| 257 return cloud_print::GetLastHResult(); | 258 return cloud_print::GetLastHResult(); |
| 258 } | 259 } |
| 259 | 260 |
| 260 if (origina_state != ServiceController::STATE_RUNNING) | 261 if (origina_state != ServiceController::STATE_RUNNING) |
| 261 return S_OK; | 262 return S_OK; |
| 262 | 263 |
| 263 hr = StopService(); | 264 hr = StopService(); |
| 264 if (FAILED(hr)) | 265 if (FAILED(hr)) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 277 is_logging_enabled_ = false; | 278 is_logging_enabled_ = false; |
| 278 | 279 |
| 279 ServiceHandle service; | 280 ServiceHandle service; |
| 280 HRESULT hr = OpenService(name_, SERVICE_QUERY_STATUS | SERVICE_QUERY_CONFIG, | 281 HRESULT hr = OpenService(name_, SERVICE_QUERY_STATUS | SERVICE_QUERY_CONFIG, |
| 281 &service); | 282 &service); |
| 282 if (FAILED(hr)) | 283 if (FAILED(hr)) |
| 283 return; | 284 return; |
| 284 | 285 |
| 285 state_ = STATE_STOPPED; | 286 state_ = STATE_STOPPED; |
| 286 SERVICE_STATUS status = {0}; | 287 SERVICE_STATUS status = {0}; |
| 287 if (::QueryServiceStatus(service, &status) && | 288 if (::QueryServiceStatus(service.Get(), &status) && |
| 288 status.dwCurrentState == SERVICE_RUNNING) { | 289 status.dwCurrentState == SERVICE_RUNNING) { |
| 289 state_ = STATE_RUNNING; | 290 state_ = STATE_RUNNING; |
| 290 } | 291 } |
| 291 | 292 |
| 292 DWORD config_size = 0; | 293 DWORD config_size = 0; |
| 293 ::QueryServiceConfig(service, NULL, 0, &config_size); | 294 ::QueryServiceConfig(service.Get(), NULL, 0, &config_size); |
| 294 if (!config_size) | 295 if (!config_size) |
| 295 return; | 296 return; |
| 296 | 297 |
| 297 std::vector<uint8> buffer(config_size, 0); | 298 std::vector<uint8> buffer(config_size, 0); |
| 298 QUERY_SERVICE_CONFIG* config = | 299 QUERY_SERVICE_CONFIG* config = |
| 299 reinterpret_cast<QUERY_SERVICE_CONFIG*>(&buffer[0]); | 300 reinterpret_cast<QUERY_SERVICE_CONFIG*>(&buffer[0]); |
| 300 if (!::QueryServiceConfig(service, config, buffer.size(), &config_size) || | 301 if (!::QueryServiceConfig(service.Get(), config, buffer.size(), |
| 302 &config_size) || |
| 301 config_size != buffer.size()) { | 303 config_size != buffer.size()) { |
| 302 return; | 304 return; |
| 303 } | 305 } |
| 304 | 306 |
| 305 command_line_ = CommandLine::FromString(config->lpBinaryPathName); | 307 command_line_ = CommandLine::FromString(config->lpBinaryPathName); |
| 306 if (!command_line_.HasSwitch(kServiceSwitch)) { | 308 if (!command_line_.HasSwitch(kServiceSwitch)) { |
| 307 state_ = STATE_NOT_FOUND; | 309 state_ = STATE_NOT_FOUND; |
| 308 return; | 310 return; |
| 309 } | 311 } |
| 310 is_logging_enabled_ = command_line_.HasSwitch(switches::kEnableLogging); | 312 is_logging_enabled_ = command_line_.HasSwitch(switches::kEnableLogging); |
| 311 user_ = config->lpServiceStartName; | 313 user_ = config->lpServiceStartName; |
| 312 } | 314 } |
| 313 | 315 |
| 314 bool ServiceController::is_logging_enabled() const { | 316 bool ServiceController::is_logging_enabled() const { |
| 315 return command_line_.HasSwitch(switches::kEnableLogging); | 317 return command_line_.HasSwitch(switches::kEnableLogging); |
| 316 } | 318 } |
| OLD | NEW |