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 "chrome/browser/ui/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 "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" | 13 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" |
14 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" | 14 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" |
15 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" | 15 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
16 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" | 16 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" |
17 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
19 #include "content/public/common/content_client.h" | 19 #include "content/public/common/content_client.h" |
20 #include "content/public/common/javascript_message_type.h" | 20 #include "content/public/common/javascript_message_type.h" |
21 #include "extensions/browser/extension_system.h" | |
22 #include "extensions/browser/process_manager.h" | |
23 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
24 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
25 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
26 | 24 |
| 25 #if defined(ENABLE_EXTENSIONS) |
| 26 #include "extensions/browser/extension_system.h" |
| 27 #include "extensions/browser/process_manager.h" |
| 28 #endif // defined(ENABLE_EXTENSIONS) |
| 29 |
27 using content::BrowserContext; | 30 using content::BrowserContext; |
28 using content::JavaScriptDialogManager; | 31 using content::JavaScriptDialogManager; |
29 using content::WebContents; | 32 using content::WebContents; |
| 33 |
| 34 #if defined(ENABLE_EXTENSIONS) |
30 using extensions::Extension; | 35 using extensions::Extension; |
| 36 #endif // defined(ENABLE_EXTENSIONS) |
31 | 37 |
32 namespace { | 38 namespace { |
33 | 39 |
| 40 #if defined(ENABLE_EXTENSIONS) |
34 // Returns the ProcessManager for the browser context from |web_contents|. | 41 // Returns the ProcessManager for the browser context from |web_contents|. |
35 extensions::ProcessManager* GetExtensionsProcessManager( | 42 extensions::ProcessManager* GetExtensionsProcessManager( |
36 WebContents* web_contents) { | 43 WebContents* web_contents) { |
37 #if defined(ENABLE_EXTENSIONS) | |
38 return extensions::ExtensionSystem::Get( | 44 return extensions::ExtensionSystem::Get( |
39 web_contents->GetBrowserContext())->process_manager(); | 45 web_contents->GetBrowserContext())->process_manager(); |
40 #else | |
41 return NULL; | |
42 #endif // defined(ENABLE_EXTENSIONS) | |
43 } | 46 } |
44 | 47 |
45 // Returns the extension associated with |web_contents| or NULL if there is no | 48 // Returns the extension associated with |web_contents| or NULL if there is no |
46 // associated extension (or extensions are not supported). | 49 // associated extension (or extensions are not supported). |
47 const Extension* GetExtensionForWebContents(WebContents* web_contents) { | 50 const Extension* GetExtensionForWebContents(WebContents* web_contents) { |
48 #if defined(ENABLE_EXTENSIONS) | |
49 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); | 51 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); |
50 return pm->GetExtensionForRenderViewHost(web_contents->GetRenderViewHost()); | 52 return pm->GetExtensionForRenderViewHost(web_contents->GetRenderViewHost()); |
51 #else | 53 } |
52 return NULL; | |
53 #endif // defined(ENABLE_EXTENSIONS) | 54 #endif // defined(ENABLE_EXTENSIONS) |
54 } | |
55 | 55 |
56 // Keeps an |extension| from shutting down its lazy background page. If an | 56 // Keeps an |extension| from shutting down its lazy background page. If an |
57 // extension opens a dialog its lazy background page must stay alive until the | 57 // extension opens a dialog its lazy background page must stay alive until the |
58 // dialog closes. | 58 // dialog closes. |
59 void IncrementLazyKeepaliveCount(const Extension* extension, | 59 void IncrementLazyKeepaliveCount(WebContents* web_contents) { |
60 WebContents* web_contents) { | 60 #if defined(ENABLE_EXTENSIONS) |
61 DCHECK(extension); | 61 const Extension* extension = GetExtensionForWebContents(web_contents); |
| 62 if (extension == NULL) |
| 63 return; |
| 64 |
62 DCHECK(web_contents); | 65 DCHECK(web_contents); |
63 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); | 66 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); |
64 if (pm) | 67 if (pm) |
65 pm->IncrementLazyKeepaliveCount(extension); | 68 pm->IncrementLazyKeepaliveCount(extension); |
| 69 #endif // defined(ENABLE_EXTENSIONS) |
66 } | 70 } |
67 | 71 |
68 // Allows an |extension| to shut down its lazy background page after a dialog | 72 // Allows an |extension| to shut down its lazy background page after a dialog |
69 // closes (if nothing else is keeping it open). | 73 // closes (if nothing else is keeping it open). |
70 void DecrementLazyKeepaliveCount(const Extension* extension, | 74 void DecrementLazyKeepaliveCount(WebContents* web_contents) { |
71 WebContents* web_contents) { | 75 #if defined(ENABLE_EXTENSIONS) |
72 DCHECK(extension); | 76 const Extension* extension = GetExtensionForWebContents(web_contents); |
| 77 if (extension == NULL) |
| 78 return; |
| 79 |
73 DCHECK(web_contents); | 80 DCHECK(web_contents); |
74 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); | 81 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); |
75 if (pm) | 82 if (pm) |
76 pm->DecrementLazyKeepaliveCount(extension); | 83 pm->DecrementLazyKeepaliveCount(extension); |
| 84 #endif // defined(ENABLE_EXTENSIONS) |
77 } | 85 } |
78 | 86 |
79 class ChromeJavaScriptDialogManager : public JavaScriptDialogManager { | 87 class ChromeJavaScriptDialogManager : public JavaScriptDialogManager { |
80 public: | 88 public: |
81 static ChromeJavaScriptDialogManager* GetInstance(); | 89 static ChromeJavaScriptDialogManager* GetInstance(); |
82 | 90 |
83 virtual void RunJavaScriptDialog( | 91 virtual void RunJavaScriptDialog( |
84 WebContents* web_contents, | 92 WebContents* web_contents, |
85 const GURL& origin_url, | 93 const GURL& origin_url, |
86 const std::string& accept_lang, | 94 const std::string& accept_lang, |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 chrome::kJavaScriptMessageExpectedDelay)) { | 182 chrome::kJavaScriptMessageExpectedDelay)) { |
175 display_suppress_checkbox = true; | 183 display_suppress_checkbox = true; |
176 } else { | 184 } else { |
177 display_suppress_checkbox = false; | 185 display_suppress_checkbox = false; |
178 } | 186 } |
179 | 187 |
180 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; | 188 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; |
181 base::string16 dialog_title = | 189 base::string16 dialog_title = |
182 GetTitle(web_contents, origin_url, accept_lang, is_alert); | 190 GetTitle(web_contents, origin_url, accept_lang, is_alert); |
183 | 191 |
184 const Extension* extension = GetExtensionForWebContents(web_contents); | 192 IncrementLazyKeepaliveCount(web_contents); |
185 if (extension) | |
186 IncrementLazyKeepaliveCount(extension, web_contents); | |
187 | 193 |
188 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | 194 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
189 web_contents, | 195 web_contents, |
190 &javascript_dialog_extra_data_, | 196 &javascript_dialog_extra_data_, |
191 dialog_title, | 197 dialog_title, |
192 message_type, | 198 message_type, |
193 message_text, | 199 message_text, |
194 default_prompt_text, | 200 default_prompt_text, |
195 display_suppress_checkbox, | 201 display_suppress_checkbox, |
196 false, // is_before_unload_dialog | 202 false, // is_before_unload_dialog |
197 false, // is_reload | 203 false, // is_reload |
198 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, | 204 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, |
199 base::Unretained(this), web_contents, callback))); | 205 base::Unretained(this), web_contents, callback))); |
200 } | 206 } |
201 | 207 |
202 void ChromeJavaScriptDialogManager::RunBeforeUnloadDialog( | 208 void ChromeJavaScriptDialogManager::RunBeforeUnloadDialog( |
203 WebContents* web_contents, | 209 WebContents* web_contents, |
204 const base::string16& message_text, | 210 const base::string16& message_text, |
205 bool is_reload, | 211 bool is_reload, |
206 const DialogClosedCallback& callback) { | 212 const DialogClosedCallback& callback) { |
207 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? | 213 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? |
208 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); | 214 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); |
209 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? | 215 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? |
210 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); | 216 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); |
211 | 217 |
212 base::string16 full_message = | 218 base::string16 full_message = |
213 message_text + base::ASCIIToUTF16("\n\n") + footer; | 219 message_text + base::ASCIIToUTF16("\n\n") + footer; |
214 | 220 |
215 const Extension* extension = GetExtensionForWebContents(web_contents); | 221 IncrementLazyKeepaliveCount(web_contents); |
216 if (extension) | |
217 IncrementLazyKeepaliveCount(extension, web_contents); | |
218 | 222 |
219 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | 223 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
220 web_contents, | 224 web_contents, |
221 &javascript_dialog_extra_data_, | 225 &javascript_dialog_extra_data_, |
222 title, | 226 title, |
223 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, | 227 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, |
224 full_message, | 228 full_message, |
225 base::string16(), // default_prompt_text | 229 base::string16(), // default_prompt_text |
226 false, // display_suppress_checkbox | 230 false, // display_suppress_checkbox |
227 true, // is_before_unload_dialog | 231 true, // is_before_unload_dialog |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 bool is_alert) { | 269 bool is_alert) { |
266 // If the URL hasn't any host, return the default string. | 270 // If the URL hasn't any host, return the default string. |
267 if (!origin_url.has_host()) { | 271 if (!origin_url.has_host()) { |
268 return l10n_util::GetStringUTF16( | 272 return l10n_util::GetStringUTF16( |
269 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE | 273 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE |
270 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); | 274 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); |
271 } | 275 } |
272 | 276 |
273 // For extensions, show the extension name, but only if the origin of | 277 // For extensions, show the extension name, but only if the origin of |
274 // the alert matches the top-level WebContents. | 278 // the alert matches the top-level WebContents. |
| 279 #if defined(ENABLE_EXTENSIONS) |
275 const Extension* extension = GetExtensionForWebContents(web_contents); | 280 const Extension* extension = GetExtensionForWebContents(web_contents); |
276 if (extension && | 281 if (extension && |
277 web_contents->GetLastCommittedURL().GetOrigin() == origin_url) { | 282 web_contents->GetLastCommittedURL().GetOrigin() == origin_url) { |
278 return base::UTF8ToUTF16(extension->name()); | 283 return base::UTF8ToUTF16(extension->name()); |
279 } | 284 } |
| 285 #endif // defined(ENABLE_EXTENSIONS) |
280 | 286 |
281 // Otherwise, return the formatted URL. | 287 // Otherwise, return the formatted URL. |
282 // In this case, force URL to have LTR directionality. | 288 // In this case, force URL to have LTR directionality. |
283 base::string16 url_string = net::FormatUrl(origin_url, accept_lang); | 289 base::string16 url_string = net::FormatUrl(origin_url, accept_lang); |
284 return l10n_util::GetStringFUTF16( | 290 return l10n_util::GetStringFUTF16( |
285 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE | 291 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE |
286 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE, | 292 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE, |
287 base::i18n::GetDisplayStringInLTRDirectionality(url_string)); | 293 base::i18n::GetDisplayStringInLTRDirectionality(url_string)); |
288 } | 294 } |
289 | 295 |
(...skipping 11 matching lines...) Expand all Loading... |
301 } | 307 } |
302 | 308 |
303 void ChromeJavaScriptDialogManager::OnDialogClosed( | 309 void ChromeJavaScriptDialogManager::OnDialogClosed( |
304 WebContents* web_contents, | 310 WebContents* web_contents, |
305 DialogClosedCallback callback, | 311 DialogClosedCallback callback, |
306 bool success, | 312 bool success, |
307 const base::string16& user_input) { | 313 const base::string16& user_input) { |
308 // If an extension opened this dialog then the extension may shut down its | 314 // If an extension opened this dialog then the extension may shut down its |
309 // lazy background page after the dialog closes. (Dialogs are closed before | 315 // lazy background page after the dialog closes. (Dialogs are closed before |
310 // their WebContents is destroyed so |web_contents| is still valid here.) | 316 // their WebContents is destroyed so |web_contents| is still valid here.) |
311 const Extension* extension = GetExtensionForWebContents(web_contents); | 317 DecrementLazyKeepaliveCount(web_contents); |
312 if (extension) | |
313 DecrementLazyKeepaliveCount(extension, web_contents); | |
314 | 318 |
315 callback.Run(success, user_input); | 319 callback.Run(success, user_input); |
316 } | 320 } |
317 | 321 |
318 } // namespace | 322 } // namespace |
319 | 323 |
320 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() { | 324 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() { |
321 return ChromeJavaScriptDialogManager::GetInstance(); | 325 return ChromeJavaScriptDialogManager::GetInstance(); |
322 } | 326 } |
OLD | NEW |