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