1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/browser/shell_javascript_dialog.h"
7 #include "base/strings/string_util.h"
8 #include "content/shell/app/resource.h"
9 #include "content/shell/browser/shell.h"
10 #include "content/shell/browser/shell_javascript_dialog_manager.h"
14 class ShellJavaScriptDialog
;
16 INT_PTR CALLBACK
ShellJavaScriptDialog::DialogProc(HWND dialog
,
22 SetWindowLongPtr(dialog
, DWLP_USER
, static_cast<LONG_PTR
>(lparam
));
23 ShellJavaScriptDialog
* owner
=
24 reinterpret_cast<ShellJavaScriptDialog
*>(lparam
);
25 owner
->dialog_win_
= dialog
;
26 SetDlgItemText(dialog
, IDC_DIALOGTEXT
, owner
->message_text_
.c_str());
27 if (owner
->message_type_
== JAVASCRIPT_MESSAGE_TYPE_PROMPT
)
28 SetDlgItemText(dialog
, IDC_PROMPTEDIT
,
29 owner
->default_prompt_text_
.c_str());
33 ShellJavaScriptDialog
* owner
= reinterpret_cast<ShellJavaScriptDialog
*>(
34 GetWindowLongPtr(dialog
, DWLP_USER
));
35 if (owner
->dialog_win_
) {
36 owner
->dialog_win_
= 0;
37 owner
->callback_
.Run(false, base::string16());
38 owner
->manager_
->DialogClosed(owner
);
43 ShellJavaScriptDialog
* owner
= reinterpret_cast<ShellJavaScriptDialog
*>(
44 GetWindowLongPtr(dialog
, DWLP_USER
));
45 base::string16 user_input
;
48 switch (LOWORD(wparam
)) {
52 if (owner
->message_type_
== JAVASCRIPT_MESSAGE_TYPE_PROMPT
) {
54 GetWindowTextLength(GetDlgItem(dialog
, IDC_PROMPTEDIT
)) + 1;
55 GetDlgItemText(dialog
, IDC_PROMPTEDIT
,
56 base::WriteInto(&user_input
, length
), length
);
65 owner
->dialog_win_
= 0;
66 owner
->callback_
.Run(result
, user_input
);
67 DestroyWindow(dialog
);
68 owner
->manager_
->DialogClosed(owner
);
73 return DefWindowProc(dialog
, message
, wparam
, lparam
);
78 ShellJavaScriptDialog::ShellJavaScriptDialog(
79 ShellJavaScriptDialogManager
* manager
,
80 gfx::NativeWindow parent_window
,
81 JavaScriptMessageType message_type
,
82 const base::string16
& message_text
,
83 const base::string16
& default_prompt_text
,
84 const JavaScriptDialogManager::DialogClosedCallback
& callback
)
87 message_type_(message_type
),
88 message_text_(message_text
),
89 default_prompt_text_(default_prompt_text
) {
91 if (message_type
== JAVASCRIPT_MESSAGE_TYPE_ALERT
)
92 dialog_type
= IDD_ALERT
;
93 else if (message_type
== JAVASCRIPT_MESSAGE_TYPE_CONFIRM
)
94 dialog_type
= IDD_CONFIRM
;
95 else // JAVASCRIPT_MESSAGE_TYPE_PROMPT
96 dialog_type
= IDD_PROMPT
;
98 dialog_win_
= CreateDialogParam(GetModuleHandle(0),
99 MAKEINTRESOURCE(dialog_type
), 0, DialogProc
,
100 reinterpret_cast<LPARAM
>(this));
101 ShowWindow(dialog_win_
, SW_SHOWNORMAL
);
104 ShellJavaScriptDialog::~ShellJavaScriptDialog() {
108 void ShellJavaScriptDialog::Cancel() {
110 DestroyWindow(dialog_win_
);
113 } // namespace content