[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[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" |
[email protected] | 993951d | 2013-05-08 21:37:02 | [diff] [blame] | 10 | #include "content/shell/app/resource.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 11 | #include "content/shell/browser/shell.h" |
| 12 | #include "content/shell/browser/shell_javascript_dialog_manager.h" |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 13 | |
| 14 | namespace content { |
| 15 | |
| 16 | class ShellJavaScriptDialog; |
| 17 | |
[email protected] | be2510c0 | 2012-05-28 14:52:14 | [diff] [blame] | 18 | INT_PTR CALLBACK ShellJavaScriptDialog::DialogProc(HWND dialog, |
| 19 | UINT message, |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 20 | WPARAM wparam, |
| 21 | LPARAM lparam) { |
| 22 | switch (message) { |
| 23 | case WM_INITDIALOG: { |
[email protected] | 3bed530 | 2013-02-15 19:31:41 | [diff] [blame] | 24 | SetWindowLongPtr(dialog, DWLP_USER, static_cast<LONG_PTR>(lparam)); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 25 | ShellJavaScriptDialog* owner = |
| 26 | reinterpret_cast<ShellJavaScriptDialog*>(lparam); |
| 27 | owner->dialog_win_ = dialog; |
| 28 | SetDlgItemText(dialog, IDC_DIALOGTEXT, owner->message_text_.c_str()); |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 29 | if (owner->dialog_type_ == JAVASCRIPT_DIALOG_TYPE_PROMPT) |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 30 | SetDlgItemText(dialog, IDC_PROMPTEDIT, |
| 31 | owner->default_prompt_text_.c_str()); |
| 32 | break; |
| 33 | } |
| 34 | case WM_DESTROY: { |
| 35 | ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>( |
[email protected] | 3bed530 | 2013-02-15 19:31:41 | [diff] [blame] | 36 | GetWindowLongPtr(dialog, DWLP_USER)); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 37 | if (owner->dialog_win_) { |
| 38 | owner->dialog_win_ = 0; |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame^] | 39 | std::move(owner->callback_).Run(false, base::string16()); |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 40 | owner->manager_->DialogClosed(owner); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 41 | } |
| 42 | break; |
| 43 | } |
| 44 | case WM_COMMAND: { |
| 45 | ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>( |
[email protected] | 3bed530 | 2013-02-15 19:31:41 | [diff] [blame] | 46 | GetWindowLongPtr(dialog, DWLP_USER)); |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 47 | base::string16 user_input; |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 48 | bool finish = false; |
tzik | b7d06d5 | 2016-02-20 08:21:28 | [diff] [blame] | 49 | bool result = false; |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 50 | switch (LOWORD(wparam)) { |
| 51 | case IDOK: |
| 52 | finish = true; |
| 53 | result = true; |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 54 | if (owner->dialog_type_ == JAVASCRIPT_DIALOG_TYPE_PROMPT) { |
[email protected] | 3bed530 | 2013-02-15 19:31:41 | [diff] [blame] | 55 | int length = |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 56 | GetWindowTextLength(GetDlgItem(dialog, IDC_PROMPTEDIT)) + 1; |
| 57 | GetDlgItemText(dialog, IDC_PROMPTEDIT, |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 58 | base::WriteInto(&user_input, length), length); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 59 | } |
| 60 | break; |
| 61 | case IDCANCEL: |
| 62 | finish = true; |
| 63 | result = false; |
| 64 | break; |
| 65 | } |
| 66 | if (finish) { |
| 67 | owner->dialog_win_ = 0; |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame^] | 68 | std::move(owner->callback_).Run(result, user_input); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 69 | DestroyWindow(dialog); |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 70 | owner->manager_->DialogClosed(owner); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 71 | } |
| 72 | break; |
| 73 | } |
| 74 | default: |
| 75 | return DefWindowProc(dialog, message, wparam, lparam); |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | ShellJavaScriptDialog::ShellJavaScriptDialog( |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 81 | ShellJavaScriptDialogManager* manager, |
[email protected] | fc4f4dd4 | 2012-07-30 20:52:48 | [diff] [blame] | 82 | gfx::NativeWindow parent_window, |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 83 | JavaScriptDialogType dialog_type, |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 84 | const base::string16& message_text, |
| 85 | const base::string16& default_prompt_text, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame^] | 86 | JavaScriptDialogManager::DialogClosedCallback callback) |
| 87 | : callback_(std::move(callback)), |
pkasting | 10cf76e | 2016-05-19 18:10:37 | [diff] [blame] | 88 | manager_(manager), |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 89 | dialog_type_(dialog_type), |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 90 | message_text_(message_text), |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame] | 91 | default_prompt_text_(default_prompt_text) { |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 92 | int dialog_resource; |
| 93 | if (dialog_type == JAVASCRIPT_DIALOG_TYPE_ALERT) |
| 94 | dialog_resource = IDD_ALERT; |
| 95 | else if (dialog_type == JAVASCRIPT_DIALOG_TYPE_CONFIRM) |
| 96 | dialog_resource = IDD_CONFIRM; |
| 97 | else // JAVASCRIPT_DIALOG_TYPE_PROMPT |
| 98 | dialog_resource = IDD_PROMPT; |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 99 | |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 100 | dialog_win_ = |
| 101 | CreateDialogParam(GetModuleHandle(0), MAKEINTRESOURCE(dialog_resource), 0, |
| 102 | DialogProc, reinterpret_cast<LPARAM>(this)); |
[email protected] | 7fc8382 | 2012-03-30 19:53:29 | [diff] [blame] | 103 | ShowWindow(dialog_win_, SW_SHOWNORMAL); |
| 104 | } |
| 105 | |
| 106 | ShellJavaScriptDialog::~ShellJavaScriptDialog() { |
| 107 | Cancel(); |
| 108 | } |
| 109 | |
| 110 | void ShellJavaScriptDialog::Cancel() { |
| 111 | if (dialog_win_) |
| 112 | DestroyWindow(dialog_win_); |
| 113 | } |
| 114 | |
| 115 | } // namespace content |