blob: 5ded67cf2853a924e968c0ac4e3fb51a62fcd4ae [file] [log] [blame]
[email protected]de7d61ff2013-08-20 11:30:411// Copyright 2013 The Chromium Authors. All rights reserved.
[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
15namespace content {
16
17class ShellJavaScriptDialog;
18
[email protected]be2510c02012-05-28 14:52:1419INT_PTR CALLBACK ShellJavaScriptDialog::DialogProc(HWND dialog,
20 UINT message,
[email protected]7fc83822012-03-30 19:53:2921 WPARAM wparam,
22 LPARAM lparam) {
23 switch (message) {
24 case WM_INITDIALOG: {
[email protected]3bed5302013-02-15 19:31:4125 SetWindowLongPtr(dialog, DWLP_USER, static_cast<LONG_PTR>(lparam));
[email protected]7fc83822012-03-30 19:53:2926 ShellJavaScriptDialog* owner =
27 reinterpret_cast<ShellJavaScriptDialog*>(lparam);
28 owner->dialog_win_ = dialog;
Jan Wilken Dörrie86069892021-02-23 17:09:1329 SetDlgItemText(dialog, IDC_DIALOGTEXT,
30 base::as_wcstr(owner->message_text_));
avi777ff452017-02-09 19:04:4831 if (owner->dialog_type_ == JAVASCRIPT_DIALOG_TYPE_PROMPT)
[email protected]7fc83822012-03-30 19:53:2932 SetDlgItemText(dialog, IDC_PROMPTEDIT,
Jan Wilken Dörrie86069892021-02-23 17:09:1333 base::as_wcstr(owner->default_prompt_text_));
[email protected]7fc83822012-03-30 19:53:2934 break;
35 }
36 case WM_DESTROY: {
37 ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>(
[email protected]3bed5302013-02-15 19:31:4138 GetWindowLongPtr(dialog, DWLP_USER));
[email protected]7fc83822012-03-30 19:53:2939 if (owner->dialog_win_) {
40 owner->dialog_win_ = 0;
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5841 std::move(owner->callback_).Run(false, std::u16string());
[email protected]71a88bb2013-02-01 22:05:1542 owner->manager_->DialogClosed(owner);
[email protected]7fc83822012-03-30 19:53:2943 }
44 break;
45 }
46 case WM_COMMAND: {
47 ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>(
[email protected]3bed5302013-02-15 19:31:4148 GetWindowLongPtr(dialog, DWLP_USER));
Jan Wilken Dörrie86069892021-02-23 17:09:1349 std::wstring user_input;
[email protected]7fc83822012-03-30 19:53:2950 bool finish = false;
tzikb7d06d52016-02-20 08:21:2851 bool result = false;
[email protected]7fc83822012-03-30 19:53:2952 switch (LOWORD(wparam)) {
53 case IDOK:
54 finish = true;
55 result = true;
avi777ff452017-02-09 19:04:4856 if (owner->dialog_type_ == JAVASCRIPT_DIALOG_TYPE_PROMPT) {
[email protected]3bed5302013-02-15 19:31:4157 int length =
[email protected]7fc83822012-03-30 19:53:2958 GetWindowTextLength(GetDlgItem(dialog, IDC_PROMPTEDIT)) + 1;
59 GetDlgItemText(dialog, IDC_PROMPTEDIT,
Brett Wilsone3c4d1a2015-07-07 23:38:0960 base::WriteInto(&user_input, length), length);
[email protected]7fc83822012-03-30 19:53:2961 }
62 break;
63 case IDCANCEL:
64 finish = true;
65 result = false;
66 break;
67 }
68 if (finish) {
69 owner->dialog_win_ = 0;
Jan Wilken Dörrie86069892021-02-23 17:09:1370 std::move(owner->callback_).Run(result, base::WideToUTF16(user_input));
[email protected]7fc83822012-03-30 19:53:2971 DestroyWindow(dialog);
[email protected]71a88bb2013-02-01 22:05:1572 owner->manager_->DialogClosed(owner);
[email protected]7fc83822012-03-30 19:53:2973 }
74 break;
75 }
76 default:
77 return DefWindowProc(dialog, message, wparam, lparam);
78 }
79 return 0;
80}
81
82ShellJavaScriptDialog::ShellJavaScriptDialog(
[email protected]71a88bb2013-02-01 22:05:1583 ShellJavaScriptDialogManager* manager,
[email protected]fc4f4dd42012-07-30 20:52:4884 gfx::NativeWindow parent_window,
avi777ff452017-02-09 19:04:4885 JavaScriptDialogType dialog_type,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5886 const std::u16string& message_text,
87 const std::u16string& default_prompt_text,
Avi Drissmane04d3992017-10-05 15:11:3688 JavaScriptDialogManager::DialogClosedCallback callback)
89 : callback_(std::move(callback)),
pkasting10cf76e2016-05-19 18:10:3790 manager_(manager),
avi777ff452017-02-09 19:04:4891 dialog_type_(dialog_type),
[email protected]7fc83822012-03-30 19:53:2992 message_text_(message_text),
sammc5648c852015-07-02 01:25:0093 default_prompt_text_(default_prompt_text) {
avi777ff452017-02-09 19:04:4894 int dialog_resource;
95 if (dialog_type == JAVASCRIPT_DIALOG_TYPE_ALERT)
96 dialog_resource = IDD_ALERT;
97 else if (dialog_type == JAVASCRIPT_DIALOG_TYPE_CONFIRM)
98 dialog_resource = IDD_CONFIRM;
99 else // JAVASCRIPT_DIALOG_TYPE_PROMPT
100 dialog_resource = IDD_PROMPT;
[email protected]7fc83822012-03-30 19:53:29101
avi777ff452017-02-09 19:04:48102 dialog_win_ =
103 CreateDialogParam(GetModuleHandle(0), MAKEINTRESOURCE(dialog_resource), 0,
104 DialogProc, reinterpret_cast<LPARAM>(this));
[email protected]7fc83822012-03-30 19:53:29105 ShowWindow(dialog_win_, SW_SHOWNORMAL);
106}
107
108ShellJavaScriptDialog::~ShellJavaScriptDialog() {
109 Cancel();
110}
111
112void ShellJavaScriptDialog::Cancel() {
113 if (dialog_win_)
114 DestroyWindow(dialog_win_);
Fergal Daly2b053c82019-12-19 02:05:23115 dialog_win_ = 0;
116 if (callback_)
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58117 std::move(callback_).Run(false, std::u16string());
[email protected]7fc83822012-03-30 19:53:29118}
119
120} // namespace content