Home | History | Annotate | Download | only in host
      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/verify_config_window_win.h"
      6 
      7 #include <atlbase.h>
      8 #include <atlwin.h>
      9 #include <windows.h>
     10 
     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"
     18 
     19 namespace remoting {
     20 
     21 VerifyConfigWindowWin::VerifyConfigWindowWin(const std::string& email,
     22     const std::string& host_id, const std::string& host_secret_hash)
     23   : email_(email),
     24     host_id_(host_id),
     25     host_secret_hash_(host_secret_hash) {
     26 }
     27 
     28 void VerifyConfigWindowWin::OnCancel(UINT code, int id, HWND control) {
     29   EndDialog(ERROR_CANCELLED);
     30 }
     31 
     32 void VerifyConfigWindowWin::OnClose() {
     33   EndDialog(ERROR_CANCELLED);
     34 }
     35 
     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);
     41   }
     42 
     43   CenterWindow();
     44 
     45   CWindow email_text(GetDlgItem(IDC_EMAIL));
     46   email_text.SetWindowText(base::UTF8ToUTF16(email_).c_str());
     47   return TRUE;
     48 }
     49 
     50 void VerifyConfigWindowWin::OnOk(UINT code, int id, HWND control) {
     51   if (VerifyHostSecretHash()) {
     52     EndDialog(ERROR_SUCCESS);
     53   } else {
     54     EndDialog(ERROR_LOGON_FAILURE);
     55   }
     56 }
     57 
     58 void VerifyConfigWindowWin::CenterWindow() {
     59   // Get the window dimensions.
     60   RECT rect;
     61   if (!GetWindowRect(&rect)) {
     62     return;
     63   }
     64 
     65   // Center against the owner window unless it is minimized or invisible.
     66   HWND owner = ::GetWindow(m_hWnd, GW_OWNER);
     67   if (owner != NULL) {
     68     DWORD style = ::GetWindowLong(owner, GWL_STYLE);
     69     if ((style & WS_MINIMIZE) != 0 || (style & WS_VISIBLE) == 0) {
     70       owner = NULL;
     71     }
     72   }
     73 
     74   // Make sure that the window will not end up split by a monitor's boundary.
     75   RECT area_rect;
     76   if (!::SystemParametersInfo(SPI_GETWORKAREA, NULL, &area_rect, NULL)) {
     77     return;
     78   }
     79 
     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;
     89       }
     90     }
     91   }
     92 
     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;
     97 
     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);
    102 
    103   SetWindowPos(NULL, x, y, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
    104 }
    105 
    106 bool VerifyConfigWindowWin::VerifyHostSecretHash() {
    107   CWindow pin_edit(GetDlgItem(IDC_PIN));
    108 
    109   // Get the PIN length.
    110   int pin_length = pin_edit.GetWindowTextLength();
    111   scoped_ptr<base::char16[]> pin(new base::char16[pin_length + 1]);
    112 
    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;
    116 
    117   return VerifyHostPinHash(host_secret_hash_,
    118                            host_id_, base::UTF16ToUTF8(pin.get()));
    119 }
    120 
    121 }  // namespace remoting
    122