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/verify_config_window_win.h"
11 #include "base/base64.h"
12 #include "base/compiler_specific.h"
13 #include "base/logging.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "remoting/host/pin_hash.h"
16 #include "remoting/host/win/core_resource.h"
17 #include "remoting/protocol/authentication_method.h"
21 VerifyConfigWindowWin::VerifyConfigWindowWin(const std::string
& email
,
22 const std::string
& host_id
, const std::string
& host_secret_hash
)
25 host_secret_hash_(host_secret_hash
) {
28 void VerifyConfigWindowWin::OnCancel(UINT code
, int id
, HWND control
) {
29 EndDialog(ERROR_CANCELLED
);
32 void VerifyConfigWindowWin::OnClose() {
33 EndDialog(ERROR_CANCELLED
);
36 LRESULT
VerifyConfigWindowWin::OnInitDialog(HWND wparam
, LPARAM lparam
) {
37 // Set the small window icon.
38 if (icon_
.LoadIcon(IDD
, ::GetSystemMetrics(SM_CXSMICON
),
39 ::GetSystemMetrics(SM_CYSMICON
)) != NULL
) {
40 SetIcon(icon_
, FALSE
);
45 CWindow
email_text(GetDlgItem(IDC_EMAIL
));
46 email_text
.SetWindowText(base::UTF8ToUTF16(email_
).c_str());
50 void VerifyConfigWindowWin::OnOk(UINT code
, int id
, HWND control
) {
51 if (VerifyHostSecretHash()) {
52 EndDialog(ERROR_SUCCESS
);
54 EndDialog(ERROR_LOGON_FAILURE
);
58 void VerifyConfigWindowWin::CenterWindow() {
59 // Get the window dimensions.
61 if (!GetWindowRect(&rect
)) {
65 // Center against the owner window unless it is minimized or invisible.
66 HWND owner
= ::GetWindow(m_hWnd
, GW_OWNER
);
68 DWORD style
= ::GetWindowLong(owner
, GWL_STYLE
);
69 if ((style
& WS_MINIMIZE
) != 0 || (style
& WS_VISIBLE
) == 0) {
74 // Make sure that the window will not end up split by a monitor's boundary.
76 if (!::SystemParametersInfo(SPI_GETWORKAREA
, NULL
, &area_rect
, NULL
)) {
80 // On a multi-monitor system use the monitor where the owner window is shown.
81 RECT owner_rect
= area_rect
;
82 if (owner
!= NULL
&& ::GetWindowRect(owner
, &owner_rect
)) {
83 HMONITOR monitor
= ::MonitorFromRect(&owner_rect
, MONITOR_DEFAULTTONEAREST
);
84 if (monitor
!= NULL
) {
85 MONITORINFO monitor_info
= {0};
86 monitor_info
.cbSize
= sizeof(monitor_info
);
87 if (::GetMonitorInfo(monitor
, &monitor_info
)) {
88 area_rect
= monitor_info
.rcWork
;
93 LONG width
= rect
.right
- rect
.left
;
94 LONG height
= rect
.bottom
- rect
.top
;
95 LONG x
= (owner_rect
.left
+ owner_rect
.right
- width
) / 2;
96 LONG y
= (owner_rect
.top
+ owner_rect
.bottom
- height
) / 2;
98 x
= std::max(x
, area_rect
.left
);
99 x
= std::min(x
, area_rect
.right
- width
);
100 y
= std::max(y
, area_rect
.top
);
101 y
= std::min(y
, area_rect
.bottom
- width
);
103 SetWindowPos(NULL
, x
, y
, -1, -1, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
106 bool VerifyConfigWindowWin::VerifyHostSecretHash() {
107 CWindow
pin_edit(GetDlgItem(IDC_PIN
));
109 // Get the PIN length.
110 int pin_length
= pin_edit
.GetWindowTextLength();
111 scoped_ptr
<base::char16
[]> pin(new base::char16
[pin_length
+ 1]);
113 // Get the PIN making sure it is NULL terminated even if an error occurs.
114 int result
= pin_edit
.GetWindowText(pin
.get(), pin_length
+ 1);
115 pin
[std::min(result
, pin_length
)] = 0;
117 return VerifyHostPinHash(host_secret_hash_
,
118 host_id_
, base::UTF16ToUTF8(pin
.get()));
121 } // namespace remoting