summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-25 00:35:23 +0000
committervitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-25 00:35:23 +0000
commit4b6ce4354e42fb4590e386d07e100493a41ce8d5 (patch)
treeb9402ed7a50770e4bf0795399964ebb9bc55d17a
parentaaf4eb5fc26bbfd776809c127aa3de8d71b46722 (diff)
downloadchromium_src-4b6ce4354e42fb4590e386d07e100493a41ce8d5.zip
chromium_src-4b6ce4354e42fb4590e386d07e100493a41ce8d5.tar.gz
chromium_src-4b6ce4354e42fb4590e386d07e100493a41ce8d5.tar.bz2
Improvements in service setup.
- Close chrome if service state is ready. - Better service display name and description. - Removed argument from ServiceController constructor. BUG=168692 Review URL: https://chromiumcodereview.appspot.com/14333022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@196274 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--cloud_print/service/win/chrome_launcher.cc78
-rw-r--r--cloud_print/service/win/cloud_print_service.cc40
-rw-r--r--cloud_print/service/win/cloud_print_service_config.cc3
-rw-r--r--cloud_print/service/win/installer.cc4
-rw-r--r--cloud_print/service/win/service_controller.cc15
-rw-r--r--cloud_print/service/win/service_controller.h2
-rw-r--r--cloud_print/service/win/service_resources.grd10
7 files changed, 106 insertions, 46 deletions
diff --git a/cloud_print/service/win/chrome_launcher.cc b/cloud_print/service/win/chrome_launcher.cc
index aa35817..2b93338 100644
--- a/cloud_print/service/win/chrome_launcher.cc
+++ b/cloud_print/service/win/chrome_launcher.cc
@@ -35,6 +35,7 @@ const int kUsageUpdateTimeoutMs = 6 * 3600 * 1000; // 6 hours.
static const char16 kAutoRunKeyPath[] =
L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
+// Terminates any process.
void ShutdownChrome(HANDLE process, DWORD thread_id) {
if (::PostThreadMessage(thread_id, WM_QUIT, 0, 0) &&
WAIT_OBJECT_0 == ::WaitForSingleObject(process, kShutdownTimeoutMs)) {
@@ -44,6 +45,27 @@ void ShutdownChrome(HANDLE process, DWORD thread_id) {
base::KillProcess(process, 0, true);
}
+BOOL CALLBACK CloseIfPidEqual(HWND wnd, LPARAM lparam) {
+ DWORD pid = 0;
+ ::GetWindowThreadProcessId(wnd, &pid);
+ if (pid == static_cast<DWORD>(lparam))
+ ::PostMessage(wnd, WM_CLOSE, 0, 0);
+ return TRUE;
+}
+
+void CloseAllProcessWindows(HANDLE process) {
+ ::EnumWindows(&CloseIfPidEqual, GetProcessId(process));
+}
+
+// Close Chrome browser window.
+void CloseChrome(HANDLE process, DWORD thread_id) {
+ CloseAllProcessWindows(process);
+ if (WAIT_OBJECT_0 == ::WaitForSingleObject(process, kShutdownTimeoutMs)) {
+ return;
+ }
+ ShutdownChrome(process, thread_id);
+}
+
bool LaunchProcess(const CommandLine& cmdline,
base::ProcessHandle* process_handle,
DWORD* thread_id) {
@@ -89,20 +111,31 @@ GURL GetCloudPrintServiceEnableURLWithSignin(const std::string& proxy_id) {
url, "continue", GetCloudPrintServiceEnableURL(proxy_id).spec());
}
-std::string UpdateServiceState(const std::string& json,
- const std::string& proxy_id) {
- std::string result;
+std::string ReadAndUpdateServiceState(const base::FilePath& directory,
+ const std::string& proxy_id) {
+ std::string json;
+ base::FilePath file_path = directory.Append(chrome::kServiceStateFileName);
+ if (!file_util::ReadFileToString(file_path, &json)) {
+ return std::string();
+ }
scoped_ptr<base::Value> service_state(base::JSONReader::Read(json));
base::DictionaryValue* dictionary = NULL;
if (!service_state->GetAsDictionary(&dictionary) || !dictionary) {
- return result;
+ return std::string();
}
bool enabled = false;
if (!dictionary->GetBoolean(prefs::kCloudPrintProxyEnabled, &enabled) ||
!enabled) {
- return result;
+ return std::string();
+ }
+
+ std::string refresh_token;
+ if (!dictionary->GetString(prefs::kCloudPrintRobotRefreshToken,
+ &refresh_token) ||
+ refresh_token.empty()) {
+ return std::string();
}
// Remove everything except kCloudPrintRoot.
@@ -112,7 +145,9 @@ std::string UpdateServiceState(const std::string& json,
dictionary->Set(prefs::kCloudPrintRoot, cloud_print_root);
dictionary->SetBoolean(prefs::kCloudPrintXmppPingEnabled, true);
- dictionary->SetString(prefs::kCloudPrintProxyId, proxy_id);
+ if (!proxy_id.empty()) // Reuse proxy id if we already had one.
+ dictionary->SetString(prefs::kCloudPrintProxyId, proxy_id);
+ std::string result;
base::JSONWriter::WriteWithOptions(dictionary,
base::JSONWriter::OPTIONS_PRETTY_PRINT,
&result);
@@ -287,18 +322,25 @@ std::string ChromeLauncher::CreateServiceStateFile(
return result;
}
- DWORD wait_result = ::WaitForSingleObject(chrome_handle, INFINITE);
- if (wait_result != WAIT_OBJECT_0) {
- LOG(ERROR) << "Chrome launch failed.";
- return result;
- }
-
- std::string json;
- if (!file_util::ReadFileToString(
- temp_user_data.path().Append(chrome::kServiceStateFileName), &json)) {
- return result;
+ for (;;) {
+ DWORD wait_result = ::WaitForSingleObject(chrome_handle, 500);
+ std::string json = ReadAndUpdateServiceState(temp_user_data.path(),
+ proxy_id);
+ if (wait_result == WAIT_OBJECT_0) {
+ // Return what we have because browser is closed.
+ return json;
+ } else if (wait_result == WAIT_TIMEOUT) {
+ if (!json.empty()) {
+ // Close chrome because Service State is ready.
+ CloseChrome(chrome_handle, thread_id);
+ return json;
+ }
+ } else {
+ LOG(ERROR) << "Chrome launch failed.";
+ return result;
+ }
}
-
- return UpdateServiceState(json, proxy_id);
+ NOTREACHED();
+ return std::string();
}
diff --git a/cloud_print/service/win/cloud_print_service.cc b/cloud_print/service/win/cloud_print_service.cc
index f29d4e7..bbdde7e 100644
--- a/cloud_print/service/win/cloud_print_service.cc
+++ b/cloud_print/service/win/cloud_print_service.cc
@@ -116,14 +116,15 @@ bool AskUser(const std::string& request) {
class CloudPrintServiceModule
- : public ATL::CAtlServiceModuleT<CloudPrintServiceModule, IDS_SERVICENAME> {
+ : public ATL::CAtlServiceModuleT<CloudPrintServiceModule,
+ IDS_SERVICE_NAME> {
public:
typedef ATL::CAtlServiceModuleT<CloudPrintServiceModule,
- IDS_SERVICENAME> Base;
+ IDS_SERVICE_NAME> Base;
CloudPrintServiceModule()
: check_requirements_(false),
- controller_(new ServiceController(m_szServiceName)) {
+ controller_(new ServiceController()) {
}
static wchar_t* GetAppIdT() {
@@ -244,6 +245,7 @@ class CloudPrintServiceModule
std::cout << "\nPlease provide Windows account to run service.\n";
*run_as_user = ASCIIToWide(GetOption("Account as DOMAIN\\USERNAME",
WideToASCII(*run_as_user), false));
+ *run_as_user = ReplaceLocalHostInName(*run_as_user);
*run_as_password = ASCIIToWide(GetOption("Password", "", true));
SetupListener setup(*run_as_user);
@@ -268,7 +270,7 @@ class CloudPrintServiceModule
continue;
}
if (setup.user_data_dir().empty()) {
- LOG(ERROR) << "Service can't access user data dir.";
+ LOG(ERROR) << "Service can't write data directory.";
continue;
}
if (setup.chrome_path().empty()) {
@@ -324,20 +326,22 @@ class CloudPrintServiceModule
}
}
- while (!is_valid) {
- std::cout << "\nUse Chrome to setup Cloud Print Settings.\n";
- std::string new_contents =
- ChromeLauncher::CreateServiceStateFile(proxy_id, printers);
- is_valid = !new_contents.empty();
- if (is_valid) {
- if (new_contents != contents) {
- size_t written = file_util::WriteFile(file, new_contents.c_str(),
- new_contents.size());
- if (written != new_contents.size()) {
- LOG(ERROR) << "Failed to write file " << file.value() << ".";
- return cloud_print::GetLastHResult();
- }
- }
+ string16 message =
+ cloud_print::LoadLocalString(IDS_ADD_PRINTERS_USING_CHROME);
+ std::cout << "\n" << message.c_str() << "\n" ;
+ std::string new_contents =
+ ChromeLauncher::CreateServiceStateFile(proxy_id, printers);
+
+ if (new_contents.empty()) {
+ LOG(ERROR) << "Failed create Service State file.";
+ return E_FAIL;
+ }
+ if (new_contents != contents) {
+ size_t written = file_util::WriteFile(file, new_contents.c_str(),
+ new_contents.size());
+ if (written != new_contents.size()) {
+ LOG(ERROR) << "Failed to write file " << file.value() << ".";
+ return cloud_print::GetLastHResult();
}
}
}
diff --git a/cloud_print/service/win/cloud_print_service_config.cc b/cloud_print/service/win/cloud_print_service_config.cc
index 059d89d..fcfe636 100644
--- a/cloud_print/service/win/cloud_print_service_config.cc
+++ b/cloud_print/service/win/cloud_print_service_config.cc
@@ -118,8 +118,7 @@ class SetupDialog : public base::RefCounted<SetupDialog>,
SetupDialog::SetupDialog()
: state_(ServiceController::STATE_NOT_FOUND),
- worker_("worker"),
- controller_(cloud_print::LoadLocalString(IDS_SERVICENAME)) {
+ worker_("worker") {
ui_loop_ = MessageLoop::current();
DCHECK(ui_loop_->IsType(MessageLoop::TYPE_UI));
diff --git a/cloud_print/service/win/installer.cc b/cloud_print/service/win/installer.cc
index 662a44b..57a1819 100644
--- a/cloud_print/service/win/installer.cc
+++ b/cloud_print/service/win/installer.cc
@@ -84,7 +84,7 @@ HRESULT ProcessInstallerSwitches() {
kGoogleUpdateId, cloud_print::LoadLocalString(IDS_FULL_PRODUCT_NAME),
kUninstallSwitch);
- ServiceController controller(cloud_print::LoadLocalString(IDS_SERVICENAME));
+ ServiceController controller;
HRESULT hr = controller.UpdateBinaryPath();
if (FAILED(hr))
return hr;
@@ -102,7 +102,7 @@ HRESULT ProcessInstallerSwitches() {
return S_OK;
} else if (command_line.HasSwitch(kUninstallSwitch)) {
- ServiceController controller(cloud_print::LoadLocalString(IDS_SERVICENAME));
+ ServiceController controller;
HRESULT hr = controller.UninstallService();
if (FAILED(hr))
return hr;
diff --git a/cloud_print/service/win/service_controller.cc b/cloud_print/service/win/service_controller.cc
index 19ffc6e..e67d5a2 100644
--- a/cloud_print/service/win/service_controller.cc
+++ b/cloud_print/service/win/service_controller.cc
@@ -81,8 +81,9 @@ HRESULT OpenService(const string16& name, DWORD access,
} // namespace
-ServiceController::ServiceController(const string16& name)
- : name_(name), command_line_(CommandLine::NO_PROGRAM) {
+ServiceController::ServiceController()
+ : name_(cloud_print::LoadLocalString(IDS_SERVICE_NAME)),
+ command_line_(CommandLine::NO_PROGRAM) {
}
ServiceController::~ServiceController() {
@@ -192,9 +193,11 @@ HRESULT ServiceController::InstallService(const string16& user,
if (FAILED(hr))
return hr;
+ string16 display_name =
+ cloud_print::LoadLocalString(IDS_SERVICE_DISPLAY_NAME);
ServiceHandle service(
::CreateService(
- scm, name_.c_str(), name_.c_str(), SERVICE_ALL_ACCESS,
+ scm, name_.c_str(), display_name.c_str(), SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL, command_line.GetCommandLineString().c_str(),
@@ -206,6 +209,12 @@ HRESULT ServiceController::InstallService(const string16& user,
return cloud_print::GetLastHResult();
}
+ string16 description_string =
+ cloud_print::LoadLocalString(IDS_SERVICE_DESCRIPTION);
+ SERVICE_DESCRIPTION description = {0};
+ description.lpDescription = const_cast<wchar_t*>(description_string.c_str());
+ ::ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
+
return S_OK;
}
diff --git a/cloud_print/service/win/service_controller.h b/cloud_print/service/win/service_controller.h
index ab95396..449864c 100644
--- a/cloud_print/service/win/service_controller.h
+++ b/cloud_print/service/win/service_controller.h
@@ -28,7 +28,7 @@ class ServiceController {
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_CLOUDPRINTSERVICE,
"{8013FB7C-2E3E-4992-B8BD-05C0C4AB0627}")
- explicit ServiceController(const string16& name);
+ ServiceController();
~ServiceController();
// Installs temporarily service to check pre-requirements.
diff --git a/cloud_print/service/win/service_resources.grd b/cloud_print/service/win/service_resources.grd
index a804e42..bc05ce7 100644
--- a/cloud_print/service/win/service_resources.grd
+++ b/cloud_print/service/win/service_resources.grd
@@ -18,9 +18,15 @@ file.
</translations>
<release seq="1">
<messages>
- <message name="IDS_SERVICENAME" translateable="false">
+ <message name="IDS_SERVICE_NAME" translateable="false">
CloudPrintService
</message>
+ <message name="IDS_SERVICE_DISPLAY_NAME">
+ Google Cloud Print Service
+ </message>
+ <message name="IDS_SERVICE_DESCRIPTION">
+ Connects classic printers with Google Cloud Print.
+ </message>
<message name="IDS_FULL_PRODUCT_NAME" translateable="false">
Google Cloud Print Service
</message>
@@ -76,7 +82,7 @@ file.
Continue in Chrome.
</message>
<message name="IDS_ADD_PRINTERS_USING_CHROME">
- Login and add printers using opened Chrome window. Close Chrome after completion.
+ Login and add printers using opened Chrome window. Close Chrome to cancel process.
</message>
</messages>