Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame^] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 5 | #include "content/shell/browser/shell_javascript_dialog.h" |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 6 | |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | 21aa9968 | 2013-06-11 07:17:01 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
Jan Wilken Dörrie | 8606989 | 2021-02-23 17:09:13 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 993951d | 2013-05-08 21:37:02 | [diff] [blame] | 11 | #include "content/shell/app/resource.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 12 | #include "content/shell/browser/shell.h" |
| 13 | #include "content/shell/browser/shell_javascript_dialog_manager.h" |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 14 | |
Bruce Dawson | bdea9d66 | 2021-06-23 17:34:15 | [diff] [blame] | 15 | #include <windows.h> |
| 16 | |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 17 | namespace content { |
| 18 | |
| 19 | class ShellJavaScriptDialog; |
| 20 | |
[email protected] | be2510c0 | 2012-05-28 14:52:14 | [diff] [blame] | 21 | INT_PTR CALLBACK ShellJavaScriptDialog::DialogProc(HWND dialog, |
| 22 | UINT message, |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 23 | WPARAM wparam, |
| 24 | LPARAM lparam) { |
| 25 | switch (message) { |
| 26 | case WM_INITDIALOG: { |
[email protected] | 3bed530 | 2013-02-15 19:31:41 | [diff] [blame] | 27 | SetWindowLongPtr(dialog, DWLP_USER, static_cast<LONG_PTR>(lparam)); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 28 | ShellJavaScriptDialog* owner = |
| 29 | reinterpret_cast<ShellJavaScriptDialog*>(lparam); |
| 30 | owner->dialog_win_ = dialog; |
Jan Wilken Dörrie | 8606989 | 2021-02-23 17:09:13 | [diff] [blame] | 31 | SetDlgItemText(dialog, IDC_DIALOGTEXT, |
| 32 | base::as_wcstr(owner->message_text_)); |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 33 | if (owner->dialog_type_ == JAVASCRIPT_DIALOG_TYPE_PROMPT) |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 34 | SetDlgItemText(dialog, IDC_PROMPTEDIT, |
Jan Wilken Dörrie | 8606989 | 2021-02-23 17:09:13 | [diff] [blame] | 35 | base::as_wcstr(owner->default_prompt_text_)); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 36 | break; |
| 37 | } |
| 38 | case WM_DESTROY: { |
| 39 | ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>( |
[email protected] | 3bed530 | 2013-02-15 19:31:41 | [diff] [blame] | 40 | GetWindowLongPtr(dialog, DWLP_USER)); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 41 | if (owner->dialog_win_) { |
| 42 | owner->dialog_win_ = 0; |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 43 | std::move(owner->callback_).Run(false, std::u16string()); |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 44 | owner->manager_->DialogClosed(owner); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 45 | } |
| 46 | break; |
| 47 | } |
| 48 | case WM_COMMAND: { |
| 49 | ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>( |
[email protected] | 3bed530 | 2013-02-15 19:31:41 | [diff] [blame] | 50 | GetWindowLongPtr(dialog, DWLP_USER)); |
Jan Wilken Dörrie | 8606989 | 2021-02-23 17:09:13 | [diff] [blame] | 51 | std::wstring user_input; |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 52 | bool finish = false; |
tzik | b7d06d5 | 2016-02-20 08:21:28 | [diff] [blame] | 53 | bool result = false; |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 54 | switch (LOWORD(wparam)) { |
| 55 | case IDOK: |
| 56 | finish = true; |
| 57 | result = true; |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 58 | if (owner->dialog_type_ == JAVASCRIPT_DIALOG_TYPE_PROMPT) { |
[email protected] | 3bed530 | 2013-02-15 19:31:41 | [diff] [blame] | 59 | int length = |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 60 | GetWindowTextLength(GetDlgItem(dialog, IDC_PROMPTEDIT)) + 1; |
| 61 | GetDlgItemText(dialog, IDC_PROMPTEDIT, |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 62 | base::WriteInto(&user_input, length), length); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 63 | } |
| 64 | break; |
| 65 | case IDCANCEL: |
| 66 | finish = true; |
| 67 | result = false; |
| 68 | break; |
| 69 | } |
| 70 | if (finish) { |
| 71 | owner->dialog_win_ = 0; |
Jan Wilken Dörrie | 8606989 | 2021-02-23 17:09:13 | [diff] [blame] | 72 | std::move(owner->callback_).Run(result, base::WideToUTF16(user_input)); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 73 | DestroyWindow(dialog); |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 74 | owner->manager_->DialogClosed(owner); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 75 | } |
| 76 | break; |
| 77 | } |
| 78 | default: |
| 79 | return DefWindowProc(dialog, message, wparam, lparam); |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | ShellJavaScriptDialog::ShellJavaScriptDialog( |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 85 | ShellJavaScriptDialogManager* manager, |
[email protected] | fc4f4dd4 | 2012-07-30 20:52:48 | [diff] [blame] | 86 | gfx::NativeWindow parent_window, |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 87 | JavaScriptDialogType dialog_type, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 88 | const std::u16string& message_text, |
| 89 | const std::u16string& default_prompt_text, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 90 | JavaScriptDialogManager::DialogClosedCallback callback) |
| 91 | : callback_(std::move(callback)), |
pkasting | 10cf76e | 2016-05-19 18:10:37 | [diff] [blame] | 92 | manager_(manager), |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 93 | dialog_type_(dialog_type), |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 94 | message_text_(message_text), |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame] | 95 | default_prompt_text_(default_prompt_text) { |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 96 | int dialog_resource; |
| 97 | if (dialog_type == JAVASCRIPT_DIALOG_TYPE_ALERT) |
| 98 | dialog_resource = IDD_ALERT; |
| 99 | else if (dialog_type == JAVASCRIPT_DIALOG_TYPE_CONFIRM) |
| 100 | dialog_resource = IDD_CONFIRM; |
| 101 | else // JAVASCRIPT_DIALOG_TYPE_PROMPT |
| 102 | dialog_resource = IDD_PROMPT; |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 103 | |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 104 | dialog_win_ = |
| 105 | CreateDialogParam(GetModuleHandle(0), MAKEINTRESOURCE(dialog_resource), 0, |
| 106 | DialogProc, reinterpret_cast<LPARAM>(this)); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 107 | ShowWindow(dialog_win_, SW_SHOWNORMAL); |
| 108 | } |
| 109 | |
danakj | ca2b5db | 2021-12-15 16:13:49 | [diff] [blame] | 110 | ShellJavaScriptDialog::~ShellJavaScriptDialog() = default; |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 111 | |
| 112 | void ShellJavaScriptDialog::Cancel() { |
danakj | ca2b5db | 2021-12-15 16:13:49 | [diff] [blame] | 113 | if (dialog_win_) { |
| 114 | // DestroyWindow() will delete `this` as the WM_DESTROY event handler |
| 115 | // deletes `this` through the `manager_`. |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 116 | DestroyWindow(dialog_win_); |
danakj | ca2b5db | 2021-12-15 16:13:49 | [diff] [blame] | 117 | } else { |
| 118 | // If the window failed to be created then we emulate WM_DESTROY, since |
| 119 | // tests don't succeed in making dialogs always (e.g. |
| 120 | // BackForwardCacheBrowserTest.CanUseCacheWhenPageAlertsInTimeoutLoop). |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 121 | std::move(callback_).Run(false, std::u16string()); |
danakj | ca2b5db | 2021-12-15 16:13:49 | [diff] [blame] | 122 | // DialogClosed() will delete `this`. |
| 123 | manager_->DialogClosed(this); |
| 124 | } |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // namespace content |