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.
5 #include "remoting/host/setup/win/start_host_window.h"
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"
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()) {
34 void StartHostWindow::OnCancel(UINT code, int id, HWND control) {
38 void StartHostWindow::OnClose() {
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);
50 void StartHostWindow::OnConsent(UINT code, int id, HWND control) {
51 bool checked = (IsDlgButtonChecked(IDC_CONSENT) == BST_CHECKED);
53 CheckDlgButton(IDC_CONSENT, checked ? BST_CHECKED : BST_UNCHECKED);
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);
67 if (!IsPinValid(pin_)) {
68 MessageBox(LoadStringFromResource(IDS_INVALID_PIN),
69 LoadStringFromResource(IDS_TITLE), MB_ICONEXCLAMATION | MB_OK);
72 MessageBox(LoadStringFromResource(IDS_USE_BROWSER),
73 LoadStringFromResource(IDS_TITLE), MB_OK);
74 auth_code_getter_.GetAuthCode(
75 base::Bind(&StartHostWindow::OnAuthCode, weak_ptr_));
78 void StartHostWindow::OnAuthCode(const std::string& auth_code) {
79 if (auth_code.empty())
82 host_starter_->StartHost(
83 host_name_, pin_, consent_to_collect_data_, auth_code,
84 GetDefaultOauthRedirectUrl(),
85 base::Bind(&StartHostWindow::OnHostStarted, weak_ptr_));
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);
94 MessageBox(LoadStringFromResource(IDS_HOST_START_FAILED),
95 LoadStringFromResource(IDS_TITLE), MB_ICONEXCLAMATION | MB_OK);
99 std::string StartHostWindow::GetDlgItemString(int id) {
100 CSimpleString str(L"", &string_mgr_);
101 GetDlgItemText(id, str);
102 return UTF16ToUTF8(str.GetString());
105 } // namespace remoting