summaryrefslogtreecommitdiffstats
path: root/cloud_print
diff options
context:
space:
mode:
authorvitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-09 21:26:02 +0000
committervitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-09 21:26:02 +0000
commit8e6e93688b5f67d3fe508b4c538ff13ae2694226 (patch)
tree5ea51c6e668b196c158c34ee103cdad130731a6e /cloud_print
parent15852e44383b5def72173d7a00930027cca583bd (diff)
downloadchromium_src-8e6e93688b5f67d3fe508b4c538ff13ae2694226.zip
chromium_src-8e6e93688b5f67d3fe508b4c538ff13ae2694226.tar.gz
chromium_src-8e6e93688b5f67d3fe508b4c538ff13ae2694226.tar.bz2
Re-factored service controlled for using in new UI.
Two Install methods to distinguish between different install cases. Methods to query service status. BUG=229183 Review URL: https://chromiumcodereview.appspot.com/13845002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@193216 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'cloud_print')
-rw-r--r--cloud_print/service/win/cloud_print_service.cc12
-rw-r--r--cloud_print/service/win/service_controller.cc78
-rw-r--r--cloud_print/service/win/service_controller.h38
3 files changed, 115 insertions, 13 deletions
diff --git a/cloud_print/service/win/cloud_print_service.cc b/cloud_print/service/win/cloud_print_service.cc
index 079c18d..3327d60 100644
--- a/cloud_print/service/win/cloud_print_service.cc
+++ b/cloud_print/service/win/cloud_print_service.cc
@@ -205,9 +205,9 @@ class CloudPrintServiceModule
if (FAILED(hr))
return hr;
- hr = controller_->InstallService(run_as_user, run_as_password,
- kServiceSwitch, user_data_dir_switch_,
- true);
+ hr = controller_->InstallConnectorService(
+ run_as_user, run_as_password, user_data_dir_switch_,
+ command_line.HasSwitch(switches::kEnableLogging));
if (SUCCEEDED(hr) && command_line.HasSwitch(kStartSwitch))
return controller_->StartService();
@@ -248,9 +248,9 @@ class CloudPrintServiceModule
*run_as_password = ASCIIToWide(GetOption("Password", "", true));
SetupListener setup(*run_as_user);
- if (FAILED(controller_->InstallService(*run_as_user, *run_as_password,
- kRequirementsSwitch,
- user_data_dir_switch_, false))) {
+ if (FAILED(controller_->InstallCheckService(*run_as_user,
+ *run_as_password,
+ user_data_dir_switch_))) {
LOG(ERROR) << "Failed to install service as " << *run_as_user << ".";
continue;
}
diff --git a/cloud_print/service/win/service_controller.cc b/cloud_print/service/win/service_controller.cc
index c311424..dd643d1 100644
--- a/cloud_print/service/win/service_controller.cc
+++ b/cloud_print/service/win/service_controller.cc
@@ -9,16 +9,20 @@
#include <atlctl.h>
#include "base/command_line.h"
+#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/win/scoped_handle.h"
#include "chrome/common/chrome_switches.h"
#include "cloud_print/common/win/cloud_print_utils.h"
+#include "cloud_print/service/service_switches.h"
#include "cloud_print/service/win/chrome_launcher.h"
#include "cloud_print/service/win/local_security_policy.h"
namespace {
+const wchar_t kServiceExeName[] = L"cloud_print_service.exe";
+
// The traits class for Windows Service.
class ServiceHandleTraits {
public:
@@ -84,11 +88,17 @@ ServiceController::~ServiceController() {
HRESULT ServiceController::StartService() {
ServiceHandle service;
- HRESULT hr = OpenService(name_, SERVICE_START, &service);
+ HRESULT hr = OpenService(name_, SERVICE_START| SERVICE_QUERY_STATUS,
+ &service);
if (FAILED(hr))
return hr;
if (!::StartService(service, 0, NULL))
return cloud_print::GetLastHResult();
+ SERVICE_STATUS status = {0};
+ while (::QueryServiceStatus(service, &status) &&
+ status.dwCurrentState == SERVICE_START_PENDING) {
+ Sleep(100);
+ }
return S_OK;
}
@@ -109,11 +119,29 @@ HRESULT ServiceController::StopService() {
return S_OK;
}
+HRESULT ServiceController::InstallConnectorService(
+ const string16& user,
+ const string16& password,
+ const base::FilePath& user_data_dir,
+ bool enable_logging) {
+ return InstallService(user, password, true, kServiceSwitch, user_data_dir,
+ enable_logging);
+}
+
+HRESULT ServiceController::InstallCheckService(
+ const string16& user,
+ const string16& password,
+ const base::FilePath& user_data_dir) {
+ return InstallService(user, password, false, kRequirementsSwitch,
+ user_data_dir, true);
+}
+
HRESULT ServiceController::InstallService(const string16& user,
const string16& password,
+ bool auto_start,
const std::string& run_switch,
const base::FilePath& user_data_dir,
- bool auto_start) {
+ bool enable_logging) {
// TODO(vitalybuka): consider "lite" version if we don't want unregister
// printers here.
HRESULT hr = UninstallService();
@@ -126,10 +154,17 @@ HRESULT ServiceController::InstallService(const string16& user,
base::FilePath service_path;
CHECK(PathService::Get(base::FILE_EXE, &service_path));
+ service_path = service_path.DirName().Append(base::FilePath(kServiceExeName));
+ if (!file_util::PathExists(service_path))
+ return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
CommandLine command_line(service_path);
command_line.AppendSwitch(run_switch);
if (!user_data_dir.empty())
command_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir);
+ if (enable_logging) {
+ command_line.AppendSwitch(switches::kEnableLogging);
+ command_line.AppendSwitchASCII(switches::kV, "1");
+ }
ChromeLauncher::CopySwitchesFromCurrent(&command_line);
LocalSecurityPolicy local_security_policy;
@@ -183,3 +218,42 @@ HRESULT ServiceController::UninstallService() {
return hr;
}
+void ServiceController::UpdateState() {
+ ServiceHandle service;
+ state_ = STATE_NOT_FOUND;
+ user_.clear();
+ is_logging_enabled_ = false;
+
+ HRESULT hr = OpenService(name_, SERVICE_QUERY_STATUS | SERVICE_QUERY_CONFIG,
+ &service);
+ if (FAILED(hr))
+ return;
+
+ state_ = STATE_STOPPED;
+ SERVICE_STATUS status = {0};
+ if (::QueryServiceStatus(service, &status) &&
+ status.dwCurrentState == SERVICE_RUNNING) {
+ state_ = STATE_RUNNING;
+ }
+
+ DWORD config_size = 0;
+ ::QueryServiceConfig(service, NULL, 0, &config_size);
+ if (!config_size)
+ return;
+
+ std::vector<uint8> buffer(config_size, 0);
+ QUERY_SERVICE_CONFIG* config =
+ reinterpret_cast<QUERY_SERVICE_CONFIG*>(&buffer[0]);
+ if (!::QueryServiceConfig(service, config, buffer.size(), &config_size) ||
+ config_size != buffer.size()) {
+ return;
+ }
+
+ CommandLine command_line(CommandLine::FromString(config->lpBinaryPathName));
+ if (!command_line.HasSwitch(kServiceSwitch)) {
+ state_ = STATE_NOT_FOUND;
+ return;
+ }
+ is_logging_enabled_ = command_line.HasSwitch(switches::kEnableLogging);
+ user_ = config->lpServiceStartName;
+}
diff --git a/cloud_print/service/win/service_controller.h b/cloud_print/service/win/service_controller.h
index 77d9a3c..d703c0a 100644
--- a/cloud_print/service/win/service_controller.h
+++ b/cloud_print/service/win/service_controller.h
@@ -17,25 +17,53 @@ class FilePath;
class ServiceController {
public:
+ enum State {
+ STATE_UNKNOWN = 0,
+ STATE_NOT_FOUND,
+ STATE_STOPPED,
+ STATE_RUNNING,
+ };
+
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_CLOUDPRINTSERVICE,
"{8013FB7C-2E3E-4992-B8BD-05C0C4AB0627}")
explicit ServiceController(const string16& name);
~ServiceController();
- HRESULT InstallService(const string16& user,
- const string16& password,
- const std::string& run_switch,
- const base::FilePath& user_data_dir,
- bool auto_start);
+ // Installs temporarily service to check pre-requirements.
+ HRESULT InstallCheckService(const string16& user,
+ const string16& password,
+ const base::FilePath& user_data_dir);
+
+ // Installs real service that will run connector.
+ HRESULT InstallConnectorService(const string16& user,
+ const string16& password,
+ const base::FilePath& user_data_dir,
+ bool enable_logging);
HRESULT UninstallService();
HRESULT StartService();
HRESULT StopService();
+ // Query service status and options. Results accessible with getters below.
+ void UpdateState();
+ State state() const { return state_; }
+ const string16& user() const { return user_; }
+ bool is_logging_enabled() const { return is_logging_enabled_; }
+
private:
+ HRESULT InstallService(const string16& user,
+ const string16& password,
+ bool auto_start,
+ const std::string& run_switch,
+ const base::FilePath& user_data_dir,
+ bool enable_logging);
+
const string16 name_;
+ State state_;
+ string16 user_;
+ bool is_logging_enabled_;
};
#endif // CLOUD_PRINT_SERVICE_SERVICE_CONTROLLER_H_