OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h" | 5 #include "components/app_modal_dialogs/javascript_dialog_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "components/app_modal_dialogs/app_modal_dialog.h" | 12 #include "components/app_modal_dialogs/app_modal_dialog.h" |
13 #include "components/app_modal_dialogs/app_modal_dialog_queue.h" | 13 #include "components/app_modal_dialogs/app_modal_dialog_queue.h" |
14 #include "components/app_modal_dialogs/javascript_app_modal_dialog.h" | 14 #include "components/app_modal_dialogs/javascript_app_modal_dialog.h" |
| 15 #include "components/app_modal_dialogs/javascript_dialog_manager_client.h" |
15 #include "components/app_modal_dialogs/native_app_modal_dialog.h" | 16 #include "components/app_modal_dialogs/native_app_modal_dialog.h" |
16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
17 #include "content/public/common/content_client.h" | 18 #include "content/public/common/content_client.h" |
18 #include "content/public/common/javascript_message_type.h" | 19 #include "content/public/common/javascript_message_type.h" |
19 #include "grit/components_strings.h" | 20 #include "grit/components_strings.h" |
20 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
21 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
22 | 23 |
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; | 24 using content::BrowserContext; |
29 using content::JavaScriptDialogManager; | 25 using content::JavaScriptDialogManager; |
30 using content::WebContents; | 26 using content::WebContents; |
31 | 27 |
32 #if defined(ENABLE_EXTENSIONS) | |
33 using extensions::Extension; | |
34 #endif // defined(ENABLE_EXTENSIONS) | |
35 | |
36 namespace { | 28 namespace { |
37 | 29 |
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 { | 30 class ChromeJavaScriptDialogManager : public JavaScriptDialogManager { |
85 public: | 31 public: |
86 static ChromeJavaScriptDialogManager* GetInstance(); | 32 static ChromeJavaScriptDialogManager* GetInstance(); |
87 | 33 |
88 void RunJavaScriptDialog(WebContents* web_contents, | 34 void RunJavaScriptDialog(WebContents* web_contents, |
89 const GURL& origin_url, | 35 const GURL& origin_url, |
90 const std::string& accept_lang, | 36 const std::string& accept_lang, |
91 content::JavaScriptMessageType message_type, | 37 content::JavaScriptMessageType message_type, |
92 const base::string16& message_text, | 38 const base::string16& message_text, |
93 const base::string16& default_prompt_text, | 39 const base::string16& default_prompt_text, |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay)) { | 124 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay)) { |
179 display_suppress_checkbox = true; | 125 display_suppress_checkbox = true; |
180 } else { | 126 } else { |
181 display_suppress_checkbox = false; | 127 display_suppress_checkbox = false; |
182 } | 128 } |
183 | 129 |
184 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; | 130 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; |
185 base::string16 dialog_title = | 131 base::string16 dialog_title = |
186 GetTitle(web_contents, origin_url, accept_lang, is_alert); | 132 GetTitle(web_contents, origin_url, accept_lang, is_alert); |
187 | 133 |
188 IncrementLazyKeepaliveCount(web_contents); | 134 GetJavaScriptDialogManagerClient()->IncrementLazyKeepaliveCount(web_contents); |
189 | 135 |
190 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | 136 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
191 web_contents, | 137 web_contents, |
192 &javascript_dialog_extra_data_, | 138 &javascript_dialog_extra_data_, |
193 dialog_title, | 139 dialog_title, |
194 message_type, | 140 message_type, |
195 message_text, | 141 message_text, |
196 default_prompt_text, | 142 default_prompt_text, |
197 display_suppress_checkbox, | 143 display_suppress_checkbox, |
198 false, // is_before_unload_dialog | 144 false, // is_before_unload_dialog |
199 false, // is_reload | 145 false, // is_reload |
200 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, | 146 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, |
201 base::Unretained(this), web_contents, callback))); | 147 base::Unretained(this), web_contents, callback))); |
202 } | 148 } |
203 | 149 |
204 void ChromeJavaScriptDialogManager::RunBeforeUnloadDialog( | 150 void ChromeJavaScriptDialogManager::RunBeforeUnloadDialog( |
205 WebContents* web_contents, | 151 WebContents* web_contents, |
206 const base::string16& message_text, | 152 const base::string16& message_text, |
207 bool is_reload, | 153 bool is_reload, |
208 const DialogClosedCallback& callback) { | 154 const DialogClosedCallback& callback) { |
209 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? | 155 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? |
210 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); | 156 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); |
211 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? | 157 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? |
212 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); | 158 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); |
213 | 159 |
214 base::string16 full_message = | 160 base::string16 full_message = |
215 message_text + base::ASCIIToUTF16("\n\n") + footer; | 161 message_text + base::ASCIIToUTF16("\n\n") + footer; |
216 | 162 |
217 IncrementLazyKeepaliveCount(web_contents); | 163 GetJavaScriptDialogManagerClient()->IncrementLazyKeepaliveCount(web_contents); |
218 | 164 |
219 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | 165 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
220 web_contents, | 166 web_contents, |
221 &javascript_dialog_extra_data_, | 167 &javascript_dialog_extra_data_, |
222 title, | 168 title, |
223 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, | 169 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, |
224 full_message, | 170 full_message, |
225 base::string16(), // default_prompt_text | 171 base::string16(), // default_prompt_text |
226 false, // display_suppress_checkbox | 172 false, // display_suppress_checkbox |
227 true, // is_before_unload_dialog | 173 true, // is_before_unload_dialog |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 bool is_alert) { | 211 bool is_alert) { |
266 // If the URL hasn't any host, return the default string. | 212 // If the URL hasn't any host, return the default string. |
267 if (!origin_url.has_host()) { | 213 if (!origin_url.has_host()) { |
268 return l10n_util::GetStringUTF16( | 214 return l10n_util::GetStringUTF16( |
269 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE | 215 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE |
270 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); | 216 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); |
271 } | 217 } |
272 | 218 |
273 // For extensions, show the extension name, but only if the origin of | 219 // For extensions, show the extension name, but only if the origin of |
274 // the alert matches the top-level WebContents. | 220 // the alert matches the top-level WebContents. |
275 #if defined(ENABLE_EXTENSIONS) | 221 std::string name; |
276 const Extension* extension = GetExtensionForWebContents(web_contents); | 222 if (GetJavaScriptDialogManagerClient() |
277 if (extension && | 223 ->GetExtensionName(web_contents, origin_url, &name)) { |
278 web_contents->GetLastCommittedURL().GetOrigin() == origin_url) { | 224 return base::UTF8ToUTF16(name); |
279 return base::UTF8ToUTF16(extension->name()); | |
280 } | 225 } |
281 #endif // defined(ENABLE_EXTENSIONS) | |
282 | 226 |
283 // Otherwise, return the formatted URL. | 227 // Otherwise, return the formatted URL. |
284 // In this case, force URL to have LTR directionality. | 228 // In this case, force URL to have LTR directionality. |
285 base::string16 url_string = net::FormatUrl(origin_url, accept_lang); | 229 base::string16 url_string = net::FormatUrl(origin_url, accept_lang); |
286 return l10n_util::GetStringFUTF16( | 230 return l10n_util::GetStringFUTF16( |
287 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE | 231 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE |
288 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE, | 232 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE, |
289 base::i18n::GetDisplayStringInLTRDirectionality(url_string)); | 233 base::i18n::GetDisplayStringInLTRDirectionality(url_string)); |
290 } | 234 } |
291 | 235 |
(...skipping 11 matching lines...) Expand all Loading... |
303 } | 247 } |
304 | 248 |
305 void ChromeJavaScriptDialogManager::OnDialogClosed( | 249 void ChromeJavaScriptDialogManager::OnDialogClosed( |
306 WebContents* web_contents, | 250 WebContents* web_contents, |
307 DialogClosedCallback callback, | 251 DialogClosedCallback callback, |
308 bool success, | 252 bool success, |
309 const base::string16& user_input) { | 253 const base::string16& user_input) { |
310 // If an extension opened this dialog then the extension may shut down its | 254 // 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 | 255 // lazy background page after the dialog closes. (Dialogs are closed before |
312 // their WebContents is destroyed so |web_contents| is still valid here.) | 256 // their WebContents is destroyed so |web_contents| is still valid here.) |
313 DecrementLazyKeepaliveCount(web_contents); | 257 GetJavaScriptDialogManagerClient()->DecrementLazyKeepaliveCount(web_contents); |
314 | 258 |
315 callback.Run(success, user_input); | 259 callback.Run(success, user_input); |
316 } | 260 } |
317 | 261 |
318 } // namespace | 262 } // namespace |
319 | 263 |
320 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() { | 264 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() { |
321 return ChromeJavaScriptDialogManager::GetInstance(); | 265 return ChromeJavaScriptDialogManager::GetInstance(); |
322 } | 266 } |
OLD | NEW |