- add sources.
[platform/framework/web/crosswalk.git] / src / remoting / host / setup / win / start_host_window.cc
1 // Copyright (c) 2012 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.
4
5 #include "remoting/host/setup/win/start_host_window.h"
6
7 #include <atlbase.h>
8 #include <atlwin.h>
9 #include <windows.h>
10
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "google_apis/gaia/gaia_urls.h"
15 #include "remoting/host/service_urls.h"
16 #include "remoting/host/setup/oauth_helper.h"
17 #include "remoting/host/setup/pin_validator.h"
18 #include "remoting/host/setup/win/load_string_from_resource.h"
19
20 namespace remoting {
21
22 StartHostWindow::StartHostWindow(
23     scoped_refptr<net::URLRequestContextGetter> url_request_context_getter)
24     : host_starter_(remoting::HostStarter::Create(
25           remoting::ServiceUrls::GetInstance()->directory_hosts_url(),
26           url_request_context_getter)),
27       consent_to_collect_data_(true),
28       mem_mgr_(GetProcessHeap()),
29       string_mgr_(&mem_mgr_),
30       weak_ptr_factory_(this),
31       weak_ptr_(weak_ptr_factory_.GetWeakPtr()) {
32 }
33
34 void StartHostWindow::OnCancel(UINT code, int id, HWND control) {
35   EndDialog(IDCANCEL);
36 }
37
38 void StartHostWindow::OnClose() {
39   EndDialog(IDCANCEL);
40 }
41
42 LRESULT StartHostWindow::OnInitDialog(HWND wparam, LPARAM lparam) {
43   // Failure of any of these calls is acceptable.
44   SetWindowText(LoadStringFromResource(IDS_TITLE));
45   GetDlgItem(IDC_CONSENT).SetWindowText(LoadStringFromResource(IDS_CONSENT));
46   CheckDlgButton(IDC_CONSENT, BST_CHECKED);
47   return TRUE;
48 }
49
50 void StartHostWindow::OnConsent(UINT code, int id, HWND control) {
51   bool checked = (IsDlgButtonChecked(IDC_CONSENT) == BST_CHECKED);
52   checked = !checked;
53   CheckDlgButton(IDC_CONSENT, checked ? BST_CHECKED : BST_UNCHECKED);
54 }
55
56 void StartHostWindow::OnOk(UINT code, int id, HWND control) {
57   host_name_ = GetDlgItemString(IDC_HOST_NAME);
58   pin_ = GetDlgItemString(IDC_PIN);
59   std::string confirm_pin = GetDlgItemString(IDC_CONFIRM_PIN);
60   consent_to_collect_data_ = (IsDlgButtonChecked(IDC_CONSENT) == BST_CHECKED);
61   if (pin_ != confirm_pin) {
62     MessageBox(LoadStringFromResource(IDS_SAME_PIN),
63                LoadStringFromResource(IDS_TITLE),
64                MB_ICONEXCLAMATION | MB_OK);
65     return;
66   }
67   if (!IsPinValid(pin_)) {
68     MessageBox(LoadStringFromResource(IDS_INVALID_PIN),
69                LoadStringFromResource(IDS_TITLE), MB_ICONEXCLAMATION | MB_OK);
70     return;
71   }
72   MessageBox(LoadStringFromResource(IDS_USE_BROWSER),
73              LoadStringFromResource(IDS_TITLE), MB_OK);
74   auth_code_getter_.GetAuthCode(
75       base::Bind(&StartHostWindow::OnAuthCode, weak_ptr_));
76 }
77
78 void StartHostWindow::OnAuthCode(const std::string& auth_code) {
79   if (auth_code.empty())
80     return;
81
82   host_starter_->StartHost(
83       host_name_, pin_, consent_to_collect_data_, auth_code,
84       GetDefaultOauthRedirectUrl(),
85       base::Bind(&StartHostWindow::OnHostStarted, weak_ptr_));
86 }
87
88 void StartHostWindow::OnHostStarted(remoting::HostStarter::Result result) {
89   if (result == remoting::HostStarter::START_COMPLETE) {
90     MessageBox(LoadStringFromResource(IDS_HOST_START_SUCCEEDED),
91                LoadStringFromResource(IDS_TITLE), MB_OK);
92     EndDialog(IDOK);
93   } else {
94     MessageBox(LoadStringFromResource(IDS_HOST_START_FAILED),
95                LoadStringFromResource(IDS_TITLE), MB_ICONEXCLAMATION | MB_OK);
96   }
97 }
98
99 std::string StartHostWindow::GetDlgItemString(int id) {
100   CSimpleString str(L"", &string_mgr_);
101   GetDlgItemText(id, str);
102   return UTF16ToUTF8(str.GetString());
103 }
104
105 }  // namespace remoting