Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: chrome/browser/jsmessage_box_handler_win.cc

Issue 63033: Refactor AppModalDialogQueue and move JS Alert boxes into a MVC. (Closed)
Patch Set: whitespace Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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.
4
5 #include "chrome/browser/jsmessage_box_handler_win.h"
6
7 #include "base/string_util.h"
8 #include "chrome/browser/app_modal_dialog_queue.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profile.h"
11 #include "chrome/browser/tab_contents/web_contents.h"
12 #include "chrome/common/gfx/text_elider.h"
13 #include "chrome/common/l10n_util.h"
14 #include "chrome/common/message_box_flags.h"
15 #include "chrome/common/notification_service.h"
16 #include "chrome/common/notification_type.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/pref_service.h"
19 #include "chrome/views/controls/message_box_view.h"
20 #include "chrome/views/window/window.h"
21 #include "grit/generated_resources.h"
22
23 void RunJavascriptMessageBox(WebContents* web_contents,
24 const GURL& frame_url,
25 int dialog_flags,
26 const std::wstring& message_text,
27 const std::wstring& default_prompt_text,
28 bool display_suppress_checkbox,
29 IPC::Message* reply_msg) {
30 JavascriptMessageBoxHandler* handler =
31 new JavascriptMessageBoxHandler(web_contents, frame_url, dialog_flags,
32 message_text, default_prompt_text,
33 display_suppress_checkbox, reply_msg);
34 AppModalDialogQueue::AddDialog(handler);
35 }
36
37 JavascriptMessageBoxHandler::JavascriptMessageBoxHandler(
38 WebContents* web_contents,
39 const GURL& frame_url,
40 int dialog_flags,
41 const std::wstring& message_text,
42 const std::wstring& default_prompt_text,
43 bool display_suppress_checkbox,
44 IPC::Message* reply_msg)
45 : web_contents_(web_contents),
46 frame_url_(frame_url),
47 reply_msg_(reply_msg),
48 dialog_flags_(dialog_flags),
49 dialog_(NULL),
50 message_box_view_(new MessageBoxView(
51 dialog_flags | MessageBox::kAutoDetectAlignment,
52 message_text, default_prompt_text)) {
53 DCHECK(message_box_view_);
54 DCHECK(reply_msg_);
55
56 if (display_suppress_checkbox) {
57 message_box_view_->SetCheckBoxLabel(
58 l10n_util::GetString(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION));
59 }
60
61 // Make sure we get navigation notifications so we know when our parent
62 // contents will disappear or navigate to a different page.
63 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
64 NotificationService::AllSources());
65 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED,
66 NotificationService::AllSources());
67 }
68
69 JavascriptMessageBoxHandler::~JavascriptMessageBoxHandler() {
70 }
71
72 //////////////////////////////////////////////////////////////////////////////
73 // JavascriptMessageBoxHandler, views::DialogDelegate implementation:
74
75 int JavascriptMessageBoxHandler::GetDialogButtons() const {
76 int dialog_buttons = 0;
77 if (dialog_flags_ & MessageBox::kFlagHasOKButton)
78 dialog_buttons = DIALOGBUTTON_OK;
79
80 if (dialog_flags_ & MessageBox::kFlagHasCancelButton)
81 dialog_buttons |= DIALOGBUTTON_CANCEL;
82
83 return dialog_buttons;
84 }
85
86 std::wstring JavascriptMessageBoxHandler::GetWindowTitle() const {
87 if (!frame_url_.has_host() || !web_contents_)
88 return l10n_util::GetString(IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
89
90 // We really only want the scheme, hostname, and port.
91 GURL::Replacements replacements;
92 replacements.ClearUsername();
93 replacements.ClearPassword();
94 replacements.ClearPath();
95 replacements.ClearQuery();
96 replacements.ClearRef();
97 GURL clean_url = frame_url_.ReplaceComponents(replacements);
98
99 // TODO(brettw) it should be easier than this to do the correct language
100 // handling without getting the accept language from the profile.
101 std::wstring base_address = gfx::ElideUrl(clean_url, ChromeFont(), 0,
102 web_contents_->profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
103 // Force URL to have LTR directionality.
104 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
105 l10n_util::WrapStringWithLTRFormatting(&base_address);
106 return l10n_util::GetStringF(IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base_address);
107 }
108
109 void JavascriptMessageBoxHandler::WindowClosing() {
110 dialog_ = NULL;
111
112 if (message_box_view_->IsCheckBoxSelected() && web_contents_)
113 web_contents_->set_suppress_javascript_messages(true);
114 }
115
116 void JavascriptMessageBoxHandler::DeleteDelegate() {
117 delete this;
118 }
119
120 bool JavascriptMessageBoxHandler::Cancel() {
121 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame
122 // will receive it's activation messages before this dialog receives
123 // WM_DESTROY. The parent frame would then try to activate any modal dialogs
124 // that were still open in the ModalDialogQueue, which would send activation
125 // back to this one. The framework should be improved to handle this, so this
126 // is a temporary workaround.
127 AppModalDialogQueue::ShowNextDialog();
128
129 if (web_contents_) {
130 web_contents_->OnJavaScriptMessageBoxClosed(reply_msg_, false,
131 EmptyWString());
132 }
133 return true;
134 }
135
136 bool JavascriptMessageBoxHandler::Accept() {
137 AppModalDialogQueue::ShowNextDialog();
138
139 if (web_contents_) {
140 web_contents_->OnJavaScriptMessageBoxClosed(
141 reply_msg_, true, message_box_view_->GetInputText());
142 }
143 return true;
144 }
145
146 //////////////////////////////////////////////////////////////////////////////
147 // JavascriptMessageBoxHandler, views::AppModalDialogDelegate
148 // implementation:
149
150 void JavascriptMessageBoxHandler::ShowModalDialog() {
151 // If the WebContents that created this dialog navigated away before this
152 // dialog became visible, simply show the next dialog if any.
153 if (!web_contents_) {
154 AppModalDialogQueue::ShowNextDialog();
155 delete this;
156 return;
157 }
158
159 web_contents_->Activate();
160 HWND root_hwnd = GetAncestor(web_contents_->GetNativeView(), GA_ROOT);
161 dialog_ = views::Window::CreateChromeWindow(root_hwnd, gfx::Rect(), this);
162 dialog_->Show();
163 }
164
165 void JavascriptMessageBoxHandler::ActivateModalDialog() {
166 // Ensure that the dialog is visible and at the top of the z-order. These
167 // conditions may not be true if the dialog was opened on a different virtual
168 // desktop to the one the browser window is on.
169 dialog_->Show();
170 dialog_->Activate();
171 }
172
173 ///////////////////////////////////////////////////////////////////////////////
174 // JavascriptMessageBoxHandler, views::WindowDelegate implementation:
175
176 views::View* JavascriptMessageBoxHandler::GetContentsView() {
177 return message_box_view_;
178 }
179
180 views::View* JavascriptMessageBoxHandler::GetInitiallyFocusedView() {
181 if (message_box_view_->text_box())
182 return message_box_view_->text_box();
183 return views::AppModalDialogDelegate::GetInitiallyFocusedView();
184 }
185
186 ///////////////////////////////////////////////////////////////////////////////
187 // JavascriptMessageBoxHandler, private:
188
189 void JavascriptMessageBoxHandler::Observe(NotificationType type,
190 const NotificationSource& source,
191 const NotificationDetails& details) {
192 if (!web_contents_)
193 return;
194
195 bool web_contents_gone = false;
196
197 if (type == NotificationType::NAV_ENTRY_COMMITTED &&
198 Source<NavigationController>(source).ptr() == web_contents_->controller())
199 web_contents_gone = true;
200
201 if (type == NotificationType::TAB_CONTENTS_DESTROYED &&
202 Source<TabContents>(source).ptr() ==
203 static_cast<TabContents*>(web_contents_))
204 web_contents_gone = true;
205
206 if (web_contents_gone) {
207 web_contents_ = NULL;
208
209 // If the dialog is visible close it.
210 if (dialog_)
211 dialog_->Close();
212 }
213 }
OLDNEW
« no previous file with comments | « chrome/browser/jsmessage_box_handler_win.h ('k') | chrome/browser/renderer_host/render_view_host.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698