Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 <windows.h> | |
| 6 #include <commctrl.h> | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/callback_helpers.h" | |
| 14 #include "base/i18n/message_formatter.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/memory/ptr_util.h" | |
| 18 #include "base/strings/string_util.h" | |
| 19 #include "base/strings/utf_string_conversions.h" | |
| 20 #include "remoting/host/it2me/it2me_confirmation_dialog.h" | |
| 21 #include "remoting/host/win/core_resource.h" | |
| 22 | |
| 23 namespace remoting { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Time to wait before closing the dialog and cancelling the connection. | |
| 28 const int kDialogTimeoutMS = 60 * 1000; | |
|
Sergey Ulanov
2016/09/13 00:13:56
TimeDelta constructor is declared as constexpr now
joedow
2016/09/13 15:06:42
Will switch to TimeDelta in my next CL. Changing
| |
| 29 | |
| 30 const HRESULT kTimeoutErrorCode = E_ABORT; | |
| 31 | |
| 32 // Loads an embedded string resource from the specified module. | |
| 33 bool LoadStringResource(HMODULE resource_module, | |
| 34 int resource_id, | |
| 35 base::string16* string) { | |
| 36 DCHECK(resource_module); | |
| 37 DCHECK(string); | |
| 38 | |
| 39 string->clear(); | |
| 40 | |
| 41 const wchar_t* string_resource = nullptr; | |
| 42 int string_length = LoadStringW(resource_module, resource_id, | |
| 43 reinterpret_cast<wchar_t*>(&string_resource), | |
| 44 /*nBufferMax=*/0); | |
| 45 if (string_length <= 0) { | |
| 46 PLOG(ERROR) << "LoadStringW() failed"; | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 string->append(string_resource, string_length); | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 class It2MeConfirmationDialogWin : public It2MeConfirmationDialog { | |
| 55 public: | |
| 56 It2MeConfirmationDialogWin(); | |
| 57 ~It2MeConfirmationDialogWin() override; | |
| 58 | |
| 59 static HRESULT CALLBACK TaskDialogCallbackProc(HWND hwnd, | |
| 60 UINT uNotification, | |
| 61 WPARAM wParam, | |
| 62 LPARAM lParam, | |
| 63 LONG_PTR dwRefData); | |
|
Sergey Ulanov
2016/09/13 00:13:56
Please update parameter names to avoid Hungarian n
joedow
2016/09/13 15:06:42
Darn MSDN :P
| |
| 64 | |
| 65 // It2MeConfirmationDialog implementation. | |
| 66 void Show(const std::string& remote_user_email, | |
| 67 const ResultCallback& callback) override; | |
| 68 | |
| 69 private: | |
| 70 // Tracks whether the dialog was in the foreground the last time we checked. | |
| 71 // Default to true so we will attempt to bring it back if it starts in the | |
| 72 // background for some reason. | |
| 73 bool is_foreground_window_ = true; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(It2MeConfirmationDialogWin); | |
| 76 }; | |
| 77 | |
| 78 It2MeConfirmationDialogWin::It2MeConfirmationDialogWin() {} | |
| 79 | |
| 80 It2MeConfirmationDialogWin::~It2MeConfirmationDialogWin() {} | |
| 81 | |
| 82 void It2MeConfirmationDialogWin::Show(const std::string& remote_user_email, | |
| 83 const ResultCallback& callback) { | |
| 84 DCHECK(!remote_user_email.empty()); | |
| 85 DCHECK(!callback.is_null()); | |
| 86 | |
| 87 // Default to a cancelled state. We only accept the connection if the user | |
| 88 // explicitly allows it. | |
| 89 Result result = Result::CANCEL; | |
| 90 | |
| 91 // |resource_module| does not need to be freed as GetModuleHandle() does not | |
| 92 // increment the refcount for the module. This DLL is not unloaded until the | |
| 93 // process exits so using a stored handle is safe. | |
| 94 HMODULE resource_module = GetModuleHandle(L"remoting_core.dll"); | |
| 95 if (resource_module == nullptr) { | |
| 96 PLOG(ERROR) << "GetModuleHandle() failed"; | |
| 97 callback.Run(result); | |
| 98 return; | |
| 99 } | |
| 100 | |
| 101 base::string16 title_text; | |
| 102 if (!LoadStringResource(resource_module, IDS_PRODUCT_NAME, &title_text)) { | |
| 103 LOG(ERROR) << "Failed to load title text for confirmation dialog."; | |
| 104 callback.Run(result); | |
| 105 return; | |
| 106 } | |
| 107 | |
| 108 base::string16 message_text; | |
| 109 if (!LoadStringResource(resource_module, | |
| 110 IDS_SHARE_CONFIRM_DIALOG_MESSAGE_WITH_USERNAME, | |
| 111 &message_text)) { | |
| 112 LOG(ERROR) << "Failed to load message text for confirmation dialog."; | |
| 113 callback.Run(result); | |
| 114 return; | |
| 115 } | |
| 116 message_text = base::i18n::MessageFormatter::FormatWithNumberedArgs( | |
| 117 message_text, base::UTF8ToUTF16(remote_user_email)); | |
| 118 | |
| 119 base::string16 share_button_text; | |
| 120 if (!LoadStringResource(resource_module, IDS_SHARE_CONFIRM_DIALOG_CONFIRM, | |
| 121 &share_button_text)) { | |
| 122 LOG(ERROR) << "Failed to load share button text for confirmation dialog."; | |
| 123 callback.Run(result); | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 base::string16 decline_button_text; | |
| 128 if (!LoadStringResource(resource_module, IDS_SHARE_CONFIRM_DIALOG_DECLINE, | |
| 129 &decline_button_text)) { | |
| 130 LOG(ERROR) << "Failed to load decline button text for confirmation dialog."; | |
| 131 callback.Run(result); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 TASKDIALOG_BUTTON dialog_buttons[] = { | |
| 136 {IDYES, share_button_text.c_str()}, {IDNO, decline_button_text.c_str()}, | |
| 137 }; | |
| 138 | |
| 139 TASKDIALOGCONFIG dialog_config = {0}; | |
| 140 dialog_config.cbSize = sizeof(dialog_config); | |
| 141 dialog_config.hInstance = resource_module; | |
| 142 dialog_config.pszWindowTitle = title_text.c_str(); | |
| 143 dialog_config.pszMainInstruction = message_text.c_str(); | |
| 144 dialog_config.pszMainIcon = MAKEINTRESOURCE(IDI_CHROME_REMOTE_DESKTOP); | |
| 145 dialog_config.dwFlags = TDF_CALLBACK_TIMER; | |
| 146 dialog_config.pfCallback = &TaskDialogCallbackProc; | |
| 147 dialog_config.lpCallbackData = reinterpret_cast<LONG_PTR>(this); | |
| 148 dialog_config.cButtons = ARRAYSIZE(dialog_buttons); | |
| 149 dialog_config.pButtons = dialog_buttons; | |
| 150 dialog_config.nDefaultButton = IDNO; | |
| 151 | |
| 152 int button_result = 0; | |
| 153 HRESULT hr = TaskDialogIndirect(&dialog_config, &button_result, | |
| 154 /*pnRadioButton=*/nullptr, | |
| 155 /*pfVerificationFlagChecked=*/nullptr); | |
| 156 if (FAILED(hr)) { | |
| 157 if (hr == kTimeoutErrorCode) { | |
| 158 LOG(INFO) << "TaskDialog timed out."; | |
| 159 } else { | |
| 160 LOG(ERROR) << "TaskDialogIndirect() Failed: 0x" << std::hex << hr; | |
| 161 } | |
| 162 | |
| 163 callback.Run(result); | |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 if (button_result == IDYES) { | |
| 168 // Only accept the connection if the user chose 'share'. | |
| 169 result = Result::OK; | |
| 170 } | |
| 171 | |
| 172 callback.Run(result); | |
| 173 } | |
| 174 | |
| 175 HRESULT CALLBACK | |
| 176 It2MeConfirmationDialogWin::TaskDialogCallbackProc(HWND hwnd, | |
| 177 UINT uNotification, | |
| 178 WPARAM wParam, | |
| 179 LPARAM lParam, | |
| 180 LONG_PTR dwRefData) { | |
| 181 if (uNotification == TDN_TIMER) { | |
| 182 if (wParam >= kDialogTimeoutMS) { | |
| 183 // Close the dialog window if we have reached the timeout. | |
| 184 return kTimeoutErrorCode; | |
| 185 } | |
| 186 | |
| 187 // Ensure the window is visible before checking if it is in the foreground. | |
| 188 if (!IsWindowVisible(hwnd)) { | |
| 189 ShowWindow(hwnd, SW_SHOWNORMAL); | |
| 190 } | |
| 191 | |
| 192 // Attempt to bring the dialog window to the foreground if needed. If the | |
| 193 // window is in the background and cannot be brought forward, this call will | |
| 194 // flash the placeholder on the taskbar. Do not call SetForegroundWindow() | |
| 195 // multiple times as it will cause annoying flashing for the user. | |
| 196 It2MeConfirmationDialogWin* dialog = | |
| 197 reinterpret_cast<It2MeConfirmationDialogWin*>(dwRefData); | |
| 198 if (hwnd == GetForegroundWindow()) { | |
| 199 dialog->is_foreground_window_ = true; | |
| 200 } else if (dialog->is_foreground_window_) { | |
| 201 SetForegroundWindow(hwnd); | |
| 202 dialog->is_foreground_window_ = false; | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 return S_OK; | |
| 207 } | |
| 208 | |
| 209 } // namespace | |
| 210 | |
| 211 std::unique_ptr<It2MeConfirmationDialog> | |
| 212 It2MeConfirmationDialogFactory::Create() { | |
| 213 return base::MakeUnique<It2MeConfirmationDialogWin>(); | |
| 214 } | |
| 215 | |
| 216 } // namespace remoting | |
| OLD | NEW |