OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/ui/app_modal_dialogs/javascript_dialog_manager.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/i18n/rtl.h" | |
10 #include "base/memory/singleton.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "components/app_modal_dialogs/app_modal_dialog.h" | |
13 #include "components/app_modal_dialogs/app_modal_dialog_queue.h" | |
14 #include "components/app_modal_dialogs/javascript_app_modal_dialog.h" | |
15 #include "components/app_modal_dialogs/native_app_modal_dialog.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 #include "content/public/common/content_client.h" | |
18 #include "content/public/common/javascript_message_type.h" | |
19 #include "grit/components_strings.h" | |
20 #include "net/base/net_util.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 | |
23 #if defined(ENABLE_EXTENSIONS) | |
24 #include "extensions/browser/process_manager.h" | |
25 #include "extensions/common/extension.h" | |
26 #endif // defined(ENABLE_EXTENSIONS) | |
27 | |
28 using content::BrowserContext; | |
29 using content::JavaScriptDialogManager; | |
30 using content::WebContents; | |
31 | |
32 #if defined(ENABLE_EXTENSIONS) | |
33 using extensions::Extension; | |
34 #endif // defined(ENABLE_EXTENSIONS) | |
35 | |
36 namespace { | |
37 | |
38 #if defined(ENABLE_EXTENSIONS) | |
39 // Returns the ProcessManager for the browser context from |web_contents|. | |
40 extensions::ProcessManager* GetExtensionsProcessManager( | |
41 WebContents* web_contents) { | |
42 return extensions::ProcessManager::Get(web_contents->GetBrowserContext()); | |
43 } | |
44 | |
45 // Returns the extension associated with |web_contents| or NULL if there is no | |
46 // associated extension (or extensions are not supported). | |
47 const Extension* GetExtensionForWebContents(WebContents* web_contents) { | |
48 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); | |
49 return pm->GetExtensionForRenderViewHost(web_contents->GetRenderViewHost()); | |
50 } | |
51 #endif // defined(ENABLE_EXTENSIONS) | |
52 | |
53 // Keeps an |extension| from shutting down its lazy background page. If an | |
54 // extension opens a dialog its lazy background page must stay alive until the | |
55 // dialog closes. | |
56 void IncrementLazyKeepaliveCount(WebContents* web_contents) { | |
57 #if defined(ENABLE_EXTENSIONS) | |
58 const Extension* extension = GetExtensionForWebContents(web_contents); | |
59 if (extension == NULL) | |
60 return; | |
61 | |
62 DCHECK(web_contents); | |
63 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); | |
64 if (pm) | |
65 pm->IncrementLazyKeepaliveCount(extension); | |
66 #endif // defined(ENABLE_EXTENSIONS) | |
67 } | |
68 | |
69 // Allows an |extension| to shut down its lazy background page after a dialog | |
70 // closes (if nothing else is keeping it open). | |
71 void DecrementLazyKeepaliveCount(WebContents* web_contents) { | |
72 #if defined(ENABLE_EXTENSIONS) | |
73 const Extension* extension = GetExtensionForWebContents(web_contents); | |
74 if (extension == NULL) | |
75 return; | |
76 | |
77 DCHECK(web_contents); | |
78 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); | |
79 if (pm) | |
80 pm->DecrementLazyKeepaliveCount(extension); | |
81 #endif // defined(ENABLE_EXTENSIONS) | |
82 } | |
83 | |
84 class ChromeJavaScriptDialogManager : public JavaScriptDialogManager { | |
85 public: | |
86 static ChromeJavaScriptDialogManager* GetInstance(); | |
87 | |
88 void RunJavaScriptDialog(WebContents* web_contents, | |
89 const GURL& origin_url, | |
90 const std::string& accept_lang, | |
91 content::JavaScriptMessageType message_type, | |
92 const base::string16& message_text, | |
93 const base::string16& default_prompt_text, | |
94 const DialogClosedCallback& callback, | |
95 bool* did_suppress_message) override; | |
96 | |
97 void RunBeforeUnloadDialog(WebContents* web_contents, | |
98 const base::string16& message_text, | |
99 bool is_reload, | |
100 const DialogClosedCallback& callback) override; | |
101 | |
102 bool HandleJavaScriptDialog(WebContents* web_contents, | |
103 bool accept, | |
104 const base::string16* prompt_override) override; | |
105 | |
106 void CancelActiveAndPendingDialogs(WebContents* web_contents) override; | |
107 | |
108 void WebContentsDestroyed(WebContents* web_contents) override; | |
109 | |
110 private: | |
111 friend struct DefaultSingletonTraits<ChromeJavaScriptDialogManager>; | |
112 | |
113 ChromeJavaScriptDialogManager(); | |
114 ~ChromeJavaScriptDialogManager() override; | |
115 | |
116 base::string16 GetTitle(WebContents* web_contents, | |
117 const GURL& origin_url, | |
118 const std::string& accept_lang, | |
119 bool is_alert); | |
120 | |
121 // Wrapper around a DialogClosedCallback so that we can intercept it before | |
122 // passing it onto the original callback. | |
123 void OnDialogClosed(WebContents* web_contents, | |
124 DialogClosedCallback callback, | |
125 bool success, | |
126 const base::string16& user_input); | |
127 | |
128 // Mapping between the WebContents and their extra data. The key | |
129 // is a void* because the pointer is just a cookie and is never dereferenced. | |
130 JavaScriptAppModalDialog::ExtraDataMap javascript_dialog_extra_data_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(ChromeJavaScriptDialogManager); | |
133 }; | |
134 | |
135 //////////////////////////////////////////////////////////////////////////////// | |
136 // ChromeJavaScriptDialogManager, public: | |
137 | |
138 ChromeJavaScriptDialogManager::ChromeJavaScriptDialogManager() { | |
139 } | |
140 | |
141 ChromeJavaScriptDialogManager::~ChromeJavaScriptDialogManager() { | |
142 } | |
143 | |
144 // static | |
145 ChromeJavaScriptDialogManager* ChromeJavaScriptDialogManager::GetInstance() { | |
146 return Singleton<ChromeJavaScriptDialogManager>::get(); | |
147 } | |
148 | |
149 void ChromeJavaScriptDialogManager::RunJavaScriptDialog( | |
150 WebContents* web_contents, | |
151 const GURL& origin_url, | |
152 const std::string& accept_lang, | |
153 content::JavaScriptMessageType message_type, | |
154 const base::string16& message_text, | |
155 const base::string16& default_prompt_text, | |
156 const DialogClosedCallback& callback, | |
157 bool* did_suppress_message) { | |
158 *did_suppress_message = false; | |
159 | |
160 ChromeJavaScriptDialogExtraData* extra_data = | |
161 &javascript_dialog_extra_data_[web_contents]; | |
162 | |
163 if (extra_data->suppress_javascript_messages_) { | |
164 *did_suppress_message = true; | |
165 return; | |
166 } | |
167 | |
168 base::TimeDelta time_since_last_message = base::TimeTicks::Now() - | |
169 extra_data->last_javascript_message_dismissal_; | |
170 bool display_suppress_checkbox = false; | |
171 // If a WebContents is impolite and displays a second JavaScript | |
172 // alert within kJavaScriptMessageExpectedDelay of a previous | |
173 // JavaScript alert being dismissed, show a checkbox offering to | |
174 // suppress future alerts from this WebContents. | |
175 const int kJavaScriptMessageExpectedDelay = 1000; | |
176 | |
177 if (time_since_last_message < | |
178 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay)) { | |
179 display_suppress_checkbox = true; | |
180 } else { | |
181 display_suppress_checkbox = false; | |
182 } | |
183 | |
184 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; | |
185 base::string16 dialog_title = | |
186 GetTitle(web_contents, origin_url, accept_lang, is_alert); | |
187 | |
188 IncrementLazyKeepaliveCount(web_contents); | |
189 | |
190 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
191 web_contents, | |
192 &javascript_dialog_extra_data_, | |
193 dialog_title, | |
194 message_type, | |
195 message_text, | |
196 default_prompt_text, | |
197 display_suppress_checkbox, | |
198 false, // is_before_unload_dialog | |
199 false, // is_reload | |
200 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, | |
201 base::Unretained(this), web_contents, callback))); | |
202 } | |
203 | |
204 void ChromeJavaScriptDialogManager::RunBeforeUnloadDialog( | |
205 WebContents* web_contents, | |
206 const base::string16& message_text, | |
207 bool is_reload, | |
208 const DialogClosedCallback& callback) { | |
209 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? | |
210 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); | |
211 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? | |
212 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); | |
213 | |
214 base::string16 full_message = | |
215 message_text + base::ASCIIToUTF16("\n\n") + footer; | |
216 | |
217 IncrementLazyKeepaliveCount(web_contents); | |
218 | |
219 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
220 web_contents, | |
221 &javascript_dialog_extra_data_, | |
222 title, | |
223 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, | |
224 full_message, | |
225 base::string16(), // default_prompt_text | |
226 false, // display_suppress_checkbox | |
227 true, // is_before_unload_dialog | |
228 is_reload, | |
229 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, | |
230 base::Unretained(this), web_contents, callback))); | |
231 } | |
232 | |
233 bool ChromeJavaScriptDialogManager::HandleJavaScriptDialog( | |
234 WebContents* web_contents, | |
235 bool accept, | |
236 const base::string16* prompt_override) { | |
237 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance(); | |
238 if (!dialog_queue->HasActiveDialog() || | |
239 !dialog_queue->active_dialog()->IsJavaScriptModalDialog() || | |
240 dialog_queue->active_dialog()->web_contents() != web_contents) { | |
241 return false; | |
242 } | |
243 JavaScriptAppModalDialog* dialog = static_cast<JavaScriptAppModalDialog*>( | |
244 dialog_queue->active_dialog()); | |
245 if (accept) { | |
246 if (prompt_override) | |
247 dialog->SetOverridePromptText(*prompt_override); | |
248 dialog->native_dialog()->AcceptAppModalDialog(); | |
249 } else { | |
250 dialog->native_dialog()->CancelAppModalDialog(); | |
251 } | |
252 return true; | |
253 } | |
254 | |
255 void ChromeJavaScriptDialogManager::WebContentsDestroyed( | |
256 WebContents* web_contents) { | |
257 CancelActiveAndPendingDialogs(web_contents); | |
258 javascript_dialog_extra_data_.erase(web_contents); | |
259 } | |
260 | |
261 base::string16 ChromeJavaScriptDialogManager::GetTitle( | |
262 WebContents* web_contents, | |
263 const GURL& origin_url, | |
264 const std::string& accept_lang, | |
265 bool is_alert) { | |
266 // If the URL hasn't any host, return the default string. | |
267 if (!origin_url.has_host()) { | |
268 return l10n_util::GetStringUTF16( | |
269 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE | |
270 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); | |
271 } | |
272 | |
273 // For extensions, show the extension name, but only if the origin of | |
274 // the alert matches the top-level WebContents. | |
275 #if defined(ENABLE_EXTENSIONS) | |
276 const Extension* extension = GetExtensionForWebContents(web_contents); | |
277 if (extension && | |
278 web_contents->GetLastCommittedURL().GetOrigin() == origin_url) { | |
279 return base::UTF8ToUTF16(extension->name()); | |
280 } | |
281 #endif // defined(ENABLE_EXTENSIONS) | |
282 | |
283 // Otherwise, return the formatted URL. | |
284 // In this case, force URL to have LTR directionality. | |
285 base::string16 url_string = net::FormatUrl(origin_url, accept_lang); | |
286 return l10n_util::GetStringFUTF16( | |
287 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE | |
288 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE, | |
289 base::i18n::GetDisplayStringInLTRDirectionality(url_string)); | |
290 } | |
291 | |
292 void ChromeJavaScriptDialogManager::CancelActiveAndPendingDialogs( | |
293 WebContents* web_contents) { | |
294 AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance(); | |
295 AppModalDialog* active_dialog = queue->active_dialog(); | |
296 if (active_dialog && active_dialog->web_contents() == web_contents) | |
297 active_dialog->Invalidate(); | |
298 for (AppModalDialogQueue::iterator i = queue->begin(); | |
299 i != queue->end(); ++i) { | |
300 if ((*i)->web_contents() == web_contents) | |
301 (*i)->Invalidate(); | |
302 } | |
303 } | |
304 | |
305 void ChromeJavaScriptDialogManager::OnDialogClosed( | |
306 WebContents* web_contents, | |
307 DialogClosedCallback callback, | |
308 bool success, | |
309 const base::string16& user_input) { | |
310 // If an extension opened this dialog then the extension may shut down its | |
311 // lazy background page after the dialog closes. (Dialogs are closed before | |
312 // their WebContents is destroyed so |web_contents| is still valid here.) | |
313 DecrementLazyKeepaliveCount(web_contents); | |
314 | |
315 callback.Run(success, user_input); | |
316 } | |
317 | |
318 } // namespace | |
319 | |
320 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() { | |
321 return ChromeJavaScriptDialogManager::GetInstance(); | |
322 } | |
OLD | NEW |