blob: 95798e2add3ca8d9fe4ebdad14680b01954daf59 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]7fc83822012-03-30 19:53:292// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de7d61ff2013-08-20 11:30:415#include "content/shell/browser/shell_javascript_dialog.h"
[email protected]7fc83822012-03-30 19:53:296
Avi Drissmane04d3992017-10-05 15:11:367#include <utility>
8
[email protected]21aa99682013-06-11 07:17:019#include "base/strings/string_util.h"
Jan Wilken Dörrie86069892021-02-23 17:09:1310#include "base/strings/utf_string_conversions.h"
[email protected]993951d2013-05-08 21:37:0211#include "content/shell/app/resource.h"
[email protected]de7d61ff2013-08-20 11:30:4112#include "content/shell/browser/shell.h"
13#include "content/shell/browser/shell_javascript_dialog_manager.h"
[email protected]7fc83822012-03-30 19:53:2914
Bruce Dawsonbdea9d662021-06-23 17:34:1515#include <windows.h>
16
[email protected]7fc83822012-03-30 19:53:2917namespace content {
18
19class ShellJavaScriptDialog;
20
[email protected]be2510c02012-05-28 14:52:1421INT_PTR CALLBACK ShellJavaScriptDialog::DialogProc(HWND dialog,
22 UINT message,
[email protected]7fc83822012-03-30 19:53:2923 WPARAM wparam,
24 LPARAM lparam) {
25 switch (message) {
26 case WM_INITDIALOG: {
[email protected]3bed5302013-02-15 19:31:4127 SetWindowLongPtr(dialog, DWLP_USER, static_cast<LONG_PTR>(lparam));
[email protected]7fc83822012-03-30 19:53:2928 ShellJavaScriptDialog* owner =
29 reinterpret_cast<ShellJavaScriptDialog*>(lparam);
30 owner->dialog_win_ = dialog;
Jan Wilken Dörrie86069892021-02-23 17:09:1331 SetDlgItemText(dialog, IDC_DIALOGTEXT,
32 base::as_wcstr(owner->message_text_));
avi777ff452017-02-09 19:04:4833 if (owner->dialog_type_ == JAVASCRIPT_DIALOG_TYPE_PROMPT)
[email protected]7fc83822012-03-30 19:53:2934 SetDlgItemText(dialog, IDC_PROMPTEDIT,
Jan Wilken Dörrie86069892021-02-23 17:09:1335 base::as_wcstr(owner->default_prompt_text_));
[email protected]7fc83822012-03-30 19:53:2936 break;
37 }
38 case WM_DESTROY: {
39 ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>(
[email protected]3bed5302013-02-15 19:31:4140 GetWindowLongPtr(dialog, DWLP_USER));
[email protected]7fc83822012-03-30 19:53:2941 if (owner->dialog_win_) {
42 owner->dialog_win_ = 0;
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5843 std::move(owner->callback_).Run(false, std::u16string());
[email protected]71a88bb2013-02-01 22:05:1544 owner->manager_->DialogClosed(owner);
[email protected]7fc83822012-03-30 19:53:2945 }
46 break;
47 }
48 case WM_COMMAND: {
49 ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>(
[email protected]3bed5302013-02-15 19:31:4150 GetWindowLongPtr(dialog, DWLP_USER));
Jan Wilken Dörrie86069892021-02-23 17:09:1351 std::wstring user_input;
[email protected]7fc83822012-03-30 19:53:2952 bool finish = false;
tzikb7d06d52016-02-20 08:21:2853 bool result = false;
[email protected]7fc83822012-03-30 19:53:2954 switch (LOWORD(wparam)) {
55 case IDOK:
56 finish = true;
57 result = true;
avi777ff452017-02-09 19:04:4858 if (owner->dialog_type_ == JAVASCRIPT_DIALOG_TYPE_PROMPT) {
[email protected]3bed5302013-02-15 19:31:4159 int length =
[email protected]7fc83822012-03-30 19:53:2960 GetWindowTextLength(GetDlgItem(dialog, IDC_PROMPTEDIT)) + 1;
61 GetDlgItemText(dialog, IDC_PROMPTEDIT,
Brett Wilsone3c4d1a2015-07-07 23:38:0962 base::WriteInto(&user_input, length), length);
[email protected]7fc83822012-03-30 19:53:2963 }
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örrie86069892021-02-23 17:09:1372 std::move(owner->callback_).Run(result, base::WideToUTF16(user_input));
[email protected]7fc83822012-03-30 19:53:2973 DestroyWindow(dialog);
[email protected]71a88bb2013-02-01 22:05:1574 owner->manager_->DialogClosed(owner);
[email protected]7fc83822012-03-30 19:53:2975 }
76 break;
77 }
78 default:
79 return DefWindowProc(dialog, message, wparam, lparam);
80 }
81 return 0;
82}
83
84ShellJavaScriptDialog::ShellJavaScriptDialog(
[email protected]71a88bb2013-02-01 22:05:1585 ShellJavaScriptDialogManager* manager,
[email protected]fc4f4dd42012-07-30 20:52:4886 gfx::NativeWindow parent_window,
avi777ff452017-02-09 19:04:4887 JavaScriptDialogType dialog_type,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5888 const std::u16string& message_text,
89 const std::u16string& default_prompt_text,
Avi Drissmane04d3992017-10-05 15:11:3690 JavaScriptDialogManager::DialogClosedCallback callback)
91 : callback_(std::move(callback)),
pkasting10cf76e2016-05-19 18:10:3792 manager_(manager),
avi777ff452017-02-09 19:04:4893 dialog_type_(dialog_type),
[email protected]7fc83822012-03-30 19:53:2994 message_text_(message_text),
sammc5648c852015-07-02 01:25:0095 default_prompt_text_(default_prompt_text) {
avi777ff452017-02-09 19:04:4896 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]7fc83822012-03-30 19:53:29103
avi777ff452017-02-09 19:04:48104 dialog_win_ =
105 CreateDialogParam(GetModuleHandle(0), MAKEINTRESOURCE(dialog_resource), 0,
106 DialogProc, reinterpret_cast<LPARAM>(this));
[email protected]7fc83822012-03-30 19:53:29107 ShowWindow(dialog_win_, SW_SHOWNORMAL);
108}
109
danakjca2b5db2021-12-15 16:13:49110ShellJavaScriptDialog::~ShellJavaScriptDialog() = default;
[email protected]7fc83822012-03-30 19:53:29111
112void ShellJavaScriptDialog::Cancel() {
danakjca2b5db2021-12-15 16:13:49113 if (dialog_win_) {
114 // DestroyWindow() will delete `this` as the WM_DESTROY event handler
115 // deletes `this` through the `manager_`.
[email protected]7fc83822012-03-30 19:53:29116 DestroyWindow(dialog_win_);
danakjca2b5db2021-12-15 16:13:49117 } 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örrieaace0cfef2021-03-11 22:01:58121 std::move(callback_).Run(false, std::u16string());
danakjca2b5db2021-12-15 16:13:49122 // DialogClosed() will delete `this`.
123 manager_->DialogClosed(this);
124 }
[email protected]7fc83822012-03-30 19:53:29125}
126
127} // namespace content