summaryrefslogtreecommitdiffstats
path: root/remoting/host/setup/win/start_host_window.cc
blob: 2ae6a2fcf6945e23107d341adf28f282a5fb3cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/setup/win/start_host_window.h"

#include <atlbase.h>
#include <atlwin.h>
#include <windows.h>

#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "google_apis/gaia/gaia_urls.h"
#include "remoting/host/service_urls.h"
#include "remoting/host/setup/oauth_helper.h"
#include "remoting/host/setup/pin_validator.h"
#include "remoting/host/setup/win/load_string_from_resource.h"

namespace remoting {

StartHostWindow::StartHostWindow(
    scoped_refptr<net::URLRequestContextGetter> url_request_context_getter)
    : host_starter_(remoting::HostStarter::Create(
          GaiaUrls::GetInstance()->oauth2_token_url(),
          remoting::ServiceUrls::GetInstance()->directory_hosts_url(),
          url_request_context_getter)),
      consent_to_collect_data_(true),
      mem_mgr_(GetProcessHeap()),
      string_mgr_(&mem_mgr_),
      ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)),
      weak_ptr_(weak_ptr_factory_.GetWeakPtr()) {
}

void StartHostWindow::OnCancel(UINT code, int id, HWND control) {
  EndDialog(IDCANCEL);
}

void StartHostWindow::OnClose() {
  EndDialog(IDCANCEL);
}

LRESULT StartHostWindow::OnInitDialog(HWND wparam, LPARAM lparam) {
  // Failure of any of these calls is acceptable.
  SetWindowText(LoadStringFromResource(IDS_TITLE));
  GetDlgItem(IDC_CONSENT).SetWindowText(LoadStringFromResource(IDS_CONSENT));
  CheckDlgButton(IDC_CONSENT, BST_CHECKED);
  return TRUE;
}

void StartHostWindow::OnConsent(UINT code, int id, HWND control) {
  bool checked = (IsDlgButtonChecked(IDC_CONSENT) == BST_CHECKED);
  checked = !checked;
  CheckDlgButton(IDC_CONSENT, checked ? BST_CHECKED : BST_UNCHECKED);
}

void StartHostWindow::OnOk(UINT code, int id, HWND control) {
  host_name_ = GetDlgItemString(IDC_HOST_NAME);
  pin_ = GetDlgItemString(IDC_PIN);
  std::string confirm_pin = GetDlgItemString(IDC_CONFIRM_PIN);
  consent_to_collect_data_ = (IsDlgButtonChecked(IDC_CONSENT) == BST_CHECKED);
  if (pin_ != confirm_pin) {
    MessageBox(LoadStringFromResource(IDS_SAME_PIN),
               LoadStringFromResource(IDS_TITLE),
               MB_ICONEXCLAMATION | MB_OK);
    return;
  }
  if (!IsPinValid(pin_)) {
    MessageBox(LoadStringFromResource(IDS_INVALID_PIN),
               LoadStringFromResource(IDS_TITLE), MB_ICONEXCLAMATION | MB_OK);
    return;
  }
  MessageBox(LoadStringFromResource(IDS_USE_BROWSER),
             LoadStringFromResource(IDS_TITLE), MB_OK);
  auth_code_getter_.GetAuthCode(
      base::Bind(&StartHostWindow::OnAuthCode, weak_ptr_));
}

void StartHostWindow::OnAuthCode(const std::string& auth_code) {
  if (auth_code.empty())
    return;

  host_starter_->StartHost(
      host_name_, pin_, consent_to_collect_data_, auth_code,
      GetDefaultOauthRedirectUrl(),
      base::Bind(&StartHostWindow::OnHostStarted, weak_ptr_));
}

void StartHostWindow::OnHostStarted(remoting::HostStarter::Result result) {
  if (result == remoting::HostStarter::START_COMPLETE) {
    MessageBox(LoadStringFromResource(IDS_HOST_START_SUCCEEDED),
               LoadStringFromResource(IDS_TITLE), MB_OK);
    EndDialog(IDOK);
  } else {
    MessageBox(LoadStringFromResource(IDS_HOST_START_FAILED),
               LoadStringFromResource(IDS_TITLE), MB_ICONEXCLAMATION | MB_OK);
  }
}

std::string StartHostWindow::GetDlgItemString(int id) {
  CSimpleString str(L"", &string_mgr_);
  GetDlgItemText(id, str);
  return UTF16ToUTF8(str.GetString());
}

}  // namespace remoting