Revert of Fix missing GN dependencies. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / cloud_print / service / win / cloud_print_service_config.cc
blob83860be026d9d92fcc5fade8fb470d4f213b0614
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
3 // found in the LICENSE file.
5 #include <atlbase.h>
6 #include <atlapp.h> // NOLINT
8 #include "base/at_exit.h"
9 #include "base/bind.h"
10 #include "base/callback_helpers.h"
11 #include "base/command_line.h"
12 #include "base/files/file_util.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/message_loop/message_pump_dispatcher.h"
15 #include "base/run_loop.h"
16 #include "base/strings/string16.h"
17 #include "base/threading/thread.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "cloud_print/common/win/cloud_print_utils.h"
20 #include "cloud_print/resources.h"
21 #include "cloud_print/service/service_state.h"
22 #include "cloud_print/service/win/chrome_launcher.h"
23 #include "cloud_print/service/win/service_controller.h"
24 #include "cloud_print/service/win/service_utils.h"
25 #include "cloud_print/service/win/setup_listener.h"
27 using cloud_print::LoadLocalString;
28 using cloud_print::GetErrorMessage;
30 class SetupDialog : public base::RefCounted<SetupDialog>,
31 public ATL::CDialogImpl<SetupDialog> {
32 public:
33 // Enables accelerators.
34 class Dispatcher : public base::MessagePumpDispatcher {
35 public:
36 explicit Dispatcher(SetupDialog* dialog) : dialog_(dialog) {}
37 ~Dispatcher() override {}
39 // MessagePumpDispatcher:
40 uint32_t Dispatch(const MSG& msg) override {
41 MSG msg2 = msg;
42 uint32_t action = POST_DISPATCH_NONE;
43 if (!dialog_->IsDialogMessage(&msg2))
44 action = POST_DISPATCH_PERFORM_DEFAULT;
45 return action;
48 private:
49 scoped_refptr<SetupDialog> dialog_;
52 typedef ATL::CDialogImpl<SetupDialog> Base;
53 enum { IDD = IDD_SETUP_DIALOG };
55 BEGIN_MSG_MAP(SetupDialog)
56 MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
57 MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtrColor)
58 MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
59 COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
60 COMMAND_ID_HANDLER(IDC_START, OnStart)
61 COMMAND_ID_HANDLER(IDC_INSTALL, OnInstall)
62 COMMAND_ID_HANDLER(IDC_LOGGING, OnLogging)
63 END_MSG_MAP()
65 SetupDialog();
67 private:
68 friend class base::RefCounted<SetupDialog>;
69 ~SetupDialog() override {}
71 // Window Message Handlers
72 LRESULT OnInitDialog(UINT message, WPARAM wparam, LPARAM lparam,
73 BOOL& handled);
74 LRESULT OnCtrColor(UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled);
75 LRESULT OnCancel(UINT, INT nIdentifier, HWND, BOOL& handled);
76 LRESULT OnStart(UINT, INT nIdentifier, HWND, BOOL& handled);
77 LRESULT OnInstall(UINT, INT nIdentifier, HWND, BOOL& handled);
78 LRESULT OnLogging(UINT, INT nIdentifier, HWND, BOOL& handled);
79 LRESULT OnDestroy(UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled);
81 void PostUITask(const base::Closure& task);
82 void PostIOTask(const base::Closure& task);
84 // UI Calls.
86 // Disables all controls after users actions.
87 void DisableControls();
88 // Updates state of controls after when we received service status.
89 void SetState(ServiceController::State state, const base::string16& user,
90 bool is_logging_enabled);
91 // Show message box with error.
92 void ShowErrorMessageBox(const base::string16& error_message);
93 // Show use message box instructions how to deal with opened Chrome window.
94 void AskToCloseChrome();
95 base::string16 GetDlgItemText(int id) const;
96 base::string16 GetUser() const;
97 base::string16 GetPassword() const;
98 bool IsLoggingEnabled() const;
99 bool IsInstalled() const {
100 return state_ > ServiceController::STATE_NOT_FOUND;
103 // IO Calls.
104 // Installs service.
105 void Install(const base::string16& user, const base::string16& password,
106 bool enable_logging);
107 // Starts service.
108 void Start();
109 // Stops service.
110 void Stop();
111 // Uninstall service.
112 void Uninstall();
113 // Update service state.
114 void UpdateState();
115 // Posts task to UI thread to show error using string id.
116 void ShowError(int string_id);
117 // Posts task to UI thread to show error using string.
118 void ShowError(const base::string16& error_message);
119 // Posts task to UI thread to show error using error code.
120 void ShowError(HRESULT hr);
122 ServiceController::State state_;
123 base::Thread worker_;
125 base::MessageLoop* ui_loop_;
126 base::MessageLoop* io_loop_;
128 ServiceController controller_;
131 SetupDialog::SetupDialog()
132 : state_(ServiceController::STATE_NOT_FOUND),
133 worker_("worker") {
134 ui_loop_ = base::MessageLoop::current();
135 DCHECK(base::MessageLoopForUI::IsCurrent());
137 worker_.StartWithOptions(
138 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
139 io_loop_ = worker_.message_loop();
140 DCHECK(io_loop_->IsType(base::MessageLoop::TYPE_IO));
143 void SetupDialog::PostUITask(const base::Closure& task) {
144 ui_loop_->PostTask(FROM_HERE, task);
147 void SetupDialog::PostIOTask(const base::Closure& task) {
148 io_loop_->PostTask(FROM_HERE, task);
151 void SetupDialog::ShowErrorMessageBox(const base::string16& error_message) {
152 DCHECK(base::MessageLoopForUI::IsCurrent());
153 MessageBox(error_message.c_str(),
154 LoadLocalString(IDS_OPERATION_FAILED_TITLE).c_str(),
155 MB_ICONERROR | MB_OK);
158 void SetupDialog::AskToCloseChrome() {
159 DCHECK(base::MessageLoopForUI::IsCurrent());
160 MessageBox(LoadLocalString(IDS_ADD_PRINTERS_USING_CHROME).c_str(),
161 LoadLocalString(IDS_CONTINUE_IN_CHROME_TITLE).c_str(),
162 MB_OK);
165 void SetupDialog::SetState(ServiceController::State status,
166 const base::string16& user,
167 bool is_logging_enabled) {
168 DCHECK(base::MessageLoopForUI::IsCurrent());
169 state_ = status;
171 DWORD status_string = 0;
172 switch(status) {
173 case ServiceController::STATE_NOT_FOUND:
174 status_string = IDS_SERVICE_NOT_FOUND;
175 break;
176 case ServiceController::STATE_STOPPED:
177 status_string = IDS_SERVICE_STOPPED;
178 break;
179 case ServiceController::STATE_RUNNING:
180 status_string = IDS_SERVICE_RUNNING;
181 break;
183 SetDlgItemText(IDC_STATUS,
184 status_string ? LoadLocalString(status_string).c_str() : L"");
185 if (IsInstalled()) {
186 SetDlgItemText(IDC_USER, user.c_str());
187 CheckDlgButton(IDC_LOGGING,
188 is_logging_enabled ? BST_CHECKED : BST_UNCHECKED);
191 ATL::CWindow start_button = GetDlgItem(IDC_START);
192 DWORD start_string = (status == ServiceController::STATE_STOPPED) ?
193 IDS_SERVICE_START : IDS_SERVICE_STOP;
194 start_button.SetWindowText(LoadLocalString(start_string).c_str());
195 start_button.ShowWindow(IsInstalled() ? SW_SHOW : SW_HIDE);
196 start_button.EnableWindow(TRUE);
198 ATL::CWindow install_button = GetDlgItem(IDC_INSTALL);
199 DWORD install_string = IsInstalled() ? IDS_SERVICE_UNINSTALL :
200 IDS_SERVICE_INSTALL;
201 install_button.SetWindowText(LoadLocalString(install_string).c_str());
202 install_button.ShowWindow(SW_SHOW);
203 install_button.EnableWindow(TRUE);
205 if (!IsInstalled()) {
206 GetDlgItem(IDC_USER).EnableWindow(TRUE);
207 GetDlgItem(IDC_PASSWORD).EnableWindow(TRUE);
208 GetDlgItem(IDC_LOGGING).EnableWindow(TRUE);
212 LRESULT SetupDialog::OnInitDialog(UINT message, WPARAM wparam, LPARAM lparam,
213 BOOL& handled) {
214 ATLVERIFY(CenterWindow());
216 WTL::CIcon icon;
217 if (icon.LoadIcon(MAKEINTRESOURCE(IDI_ICON))) {
218 SetIcon(icon);
221 SetWindowText(LoadLocalString(IDS_SETUP_PROGRAM_NAME).c_str());
222 SetDlgItemText(IDC_STATE_LABEL, LoadLocalString(IDS_STATE_LABEL).c_str());
223 SetDlgItemText(IDC_USER_LABEL, LoadLocalString(IDS_USER_LABEL).c_str());
224 SetDlgItemText(IDC_PASSWORD_LABEL,
225 LoadLocalString(IDS_PASSWORD_LABEL).c_str());
226 SetDlgItemText(IDC_LOGGING, LoadLocalString(IDS_LOGGING_LABEL).c_str());
227 SetDlgItemText(IDCANCEL, LoadLocalString(IDS_CLOSE).c_str());
229 SetState(ServiceController::STATE_UNKNOWN, L"", false);
230 DisableControls();
232 SetDlgItemText(IDC_USER, GetCurrentUserName().c_str());
234 PostIOTask(base::Bind(&SetupDialog::UpdateState, this));
236 return 0;
239 LRESULT SetupDialog::OnCtrColor(UINT message, WPARAM wparam, LPARAM lparam,
240 BOOL& handled) {
241 HWND window = reinterpret_cast<HWND>(lparam);
242 if (GetDlgItem(IDC_LOGO).m_hWnd == window) {
243 return reinterpret_cast<LRESULT>(::GetStockObject(WHITE_BRUSH));
245 return 0;
248 LRESULT SetupDialog::OnStart(UINT, INT nIdentifier, HWND, BOOL& handled) {
249 DisableControls();
250 DCHECK(IsInstalled());
251 if (state_ == ServiceController::STATE_RUNNING)
252 PostIOTask(base::Bind(&SetupDialog::Stop, this));
253 else
254 PostIOTask(base::Bind(&SetupDialog::Start, this));
255 return 0;
258 LRESULT SetupDialog::OnInstall(UINT, INT nIdentifier, HWND, BOOL& handled) {
259 DisableControls();
260 if (IsInstalled()) {
261 PostIOTask(base::Bind(&SetupDialog::Uninstall, this));
262 } else {
263 PostIOTask(base::Bind(&SetupDialog::Install, this, GetUser(),
264 GetPassword(), IsLoggingEnabled()));
266 return 0;
269 LRESULT SetupDialog::OnLogging(UINT, INT nIdentifier, HWND, BOOL& handled) {
270 CheckDlgButton(IDC_LOGGING, IsLoggingEnabled()? BST_UNCHECKED : BST_CHECKED);
271 return 0;
274 LRESULT SetupDialog::OnCancel(UINT, INT nIdentifier, HWND, BOOL& handled) {
275 DestroyWindow();
276 return 0;
279 LRESULT SetupDialog::OnDestroy(UINT message, WPARAM wparam, LPARAM lparam,
280 BOOL& handled) {
281 base::MessageLoop::current()->PostTask(FROM_HERE,
282 base::MessageLoop::QuitClosure());
283 return 1;
286 void SetupDialog::DisableControls() {
287 GetDlgItem(IDC_START).EnableWindow(FALSE);
288 GetDlgItem(IDC_INSTALL).EnableWindow(FALSE);
289 GetDlgItem(IDC_USER).EnableWindow(FALSE);
290 GetDlgItem(IDC_PASSWORD).EnableWindow(FALSE);
291 GetDlgItem(IDC_LOGGING).EnableWindow(FALSE);
294 base::string16 SetupDialog::GetDlgItemText(int id) const {
295 const ATL::CWindow& item = GetDlgItem(id);
296 size_t length = item.GetWindowTextLength();
297 base::string16 result(length + 1, L'\0');
298 result.resize(item.GetWindowText(&result[0], result.size()));
299 return result;
302 base::string16 SetupDialog::GetUser() const {
303 return GetDlgItemText(IDC_USER);
306 base::string16 SetupDialog::GetPassword() const {
307 return GetDlgItemText(IDC_PASSWORD);
310 bool SetupDialog::IsLoggingEnabled() const{
311 return IsDlgButtonChecked(IDC_LOGGING) == BST_CHECKED;
314 void SetupDialog::UpdateState() {
315 DCHECK(base::MessageLoopForIO::IsCurrent());
316 controller_.UpdateState();
317 PostUITask(base::Bind(&SetupDialog::SetState, this, controller_.state(),
318 controller_.user(), controller_.is_logging_enabled()));
321 void SetupDialog::ShowError(const base::string16& error_message) {
322 DCHECK(base::MessageLoopForIO::IsCurrent());
323 PostUITask(base::Bind(&SetupDialog::SetState,
324 this,
325 ServiceController::STATE_UNKNOWN,
326 L"",
327 false));
328 PostUITask(base::Bind(&SetupDialog::ShowErrorMessageBox, this,
329 error_message));
330 LOG(ERROR) << error_message;
333 void SetupDialog::ShowError(int string_id) {
334 ShowError(cloud_print::LoadLocalString(string_id));
337 void SetupDialog::ShowError(HRESULT hr) {
338 ShowError(GetErrorMessage(hr));
341 void SetupDialog::Install(const base::string16& user,
342 const base::string16& password,
343 bool enable_logging) {
344 // Don't forget to update state on exit.
345 base::ScopedClosureRunner scoped_update_status(
346 base::Bind(&SetupDialog::UpdateState, this));
348 DCHECK(base::MessageLoopForIO::IsCurrent());
350 SetupListener setup(GetUser());
351 HRESULT hr = controller_.InstallCheckService(user, password,
352 base::FilePath());
353 if (FAILED(hr))
354 return ShowError(hr);
357 // Always uninstall service after requirements check.
358 base::ScopedClosureRunner scoped_uninstall(
359 base::Bind(base::IgnoreResult(&ServiceController::UninstallService),
360 base::Unretained(&controller_)));
362 hr = controller_.StartService();
363 if (FAILED(hr))
364 return ShowError(hr);
366 if (!setup.WaitResponce(base::TimeDelta::FromSeconds(30)))
367 return ShowError(IDS_ERROR_FAILED_START_SERVICE);
370 if (setup.user_data_dir().empty())
371 return ShowError(IDS_ERROR_NO_DATA_DIR);
373 if (setup.chrome_path().empty())
374 return ShowError(IDS_ERROR_NO_CHROME);
376 if (!setup.is_xps_available())
377 return ShowError(IDS_ERROR_NO_XPS);
379 base::FilePath file = setup.user_data_dir();
380 file = file.Append(chrome::kServiceStateFileName);
382 std::string proxy_id;
383 std::string contents;
385 if (base::ReadFileToString(file, &contents)) {
386 ServiceState service_state;
387 if (service_state.FromString(contents))
388 proxy_id = service_state.proxy_id();
390 PostUITask(base::Bind(&SetupDialog::AskToCloseChrome, this));
391 contents = ChromeLauncher::CreateServiceStateFile(proxy_id, setup.printers());
393 if (contents.empty())
394 return ShowError(IDS_ERROR_FAILED_CREATE_CONFIG);
396 size_t written = base::WriteFile(file, contents.c_str(),
397 contents.size());
398 if (written != contents.size()) {
399 DWORD last_error = GetLastError();
400 if (!last_error)
401 return ShowError(IDS_ERROR_FAILED_CREATE_CONFIG);
402 return ShowError(HRESULT_FROM_WIN32(last_error));
405 hr = controller_.InstallConnectorService(user, password, base::FilePath(),
406 enable_logging);
407 if (FAILED(hr))
408 return ShowError(hr);
410 hr = controller_.StartService();
411 if (FAILED(hr))
412 return ShowError(hr);
415 void SetupDialog::Start() {
416 DCHECK(base::MessageLoopForIO::IsCurrent());
417 HRESULT hr = controller_.StartService();
418 if (FAILED(hr))
419 ShowError(hr);
420 UpdateState();
423 void SetupDialog::Stop() {
424 DCHECK(base::MessageLoopForIO::IsCurrent());
425 HRESULT hr = controller_.StopService();
426 if (FAILED(hr))
427 ShowError(hr);
428 UpdateState();
431 void SetupDialog::Uninstall() {
432 DCHECK(base::MessageLoopForIO::IsCurrent());
433 HRESULT hr = controller_.UninstallService();
434 if (FAILED(hr))
435 ShowError(hr);
436 UpdateState();
439 class CloudPrintServiceConfigModule
440 : public ATL::CAtlExeModuleT<CloudPrintServiceConfigModule> {
443 CloudPrintServiceConfigModule _AtlModule;
445 int WINAPI WinMain(__in HINSTANCE hInstance,
446 __in HINSTANCE hPrevInstance,
447 __in LPSTR lpCmdLine,
448 __in int nCmdShow) {
449 base::AtExitManager at_exit;
450 base::CommandLine::Init(0, NULL);
452 base::MessageLoopForUI loop;
453 scoped_refptr<SetupDialog> dialog(new SetupDialog());
454 dialog->Create(NULL);
455 dialog->ShowWindow(SW_SHOW);
456 SetupDialog::Dispatcher dispatcher(dialog.get());
457 base::RunLoop run_loop(&dispatcher);
458 run_loop.Run();
459 return 0;