Chromium Code Reviews| 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 | |
| 52 return NULL; | |
| 53 #endif // defined(ENABLE_EXTENSIONS) | |
| 54 } | 53 } |
| 55 | 54 |
| 56 // Keeps an |extension| from shutting down its lazy background page. If an | 55 // 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 | 56 // extension opens a dialog its lazy background page must stay alive until the |
| 58 // dialog closes. | 57 // dialog closes. |
| 59 void IncrementLazyKeepaliveCount(const Extension* extension, | 58 void IncrementLazyKeepaliveCount(WebContents* web_contents) { |
|
Lei Zhang
2014/08/22 23:19:01
This can be a no-op function when extensions are d
| |
| 60 WebContents* web_contents) { | 59 const Extension* extension = GetExtensionForWebContents(web_contents); |
| 61 DCHECK(extension); | 60 if (extension == NULL) |
| 61 return; | |
| 62 | |
| 62 DCHECK(web_contents); | 63 DCHECK(web_contents); |
| 63 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); | 64 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); |
| 64 if (pm) | 65 if (pm) |
| 65 pm->IncrementLazyKeepaliveCount(extension); | 66 pm->IncrementLazyKeepaliveCount(extension); |
| 66 } | 67 } |
| 67 | 68 |
| 68 // Allows an |extension| to shut down its lazy background page after a dialog | 69 // Allows an |extension| to shut down its lazy background page after a dialog |
| 69 // closes (if nothing else is keeping it open). | 70 // closes (if nothing else is keeping it open). |
| 70 void DecrementLazyKeepaliveCount(const Extension* extension, | 71 void DecrementLazyKeepaliveCount(WebContents* web_contents) { |
| 71 WebContents* web_contents) { | 72 const Extension* extension = GetExtensionForWebContents(web_contents); |
| 72 DCHECK(extension); | 73 if (extension == NULL) |
| 74 return; | |
| 75 | |
| 73 DCHECK(web_contents); | 76 DCHECK(web_contents); |
| 74 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); | 77 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents); |
| 75 if (pm) | 78 if (pm) |
| 76 pm->DecrementLazyKeepaliveCount(extension); | 79 pm->DecrementLazyKeepaliveCount(extension); |
| 77 } | 80 } |
| 81 #endif // defined(ENABLE_EXTENSIONS) | |
| 78 | 82 |
| 79 class ChromeJavaScriptDialogManager : public JavaScriptDialogManager { | 83 class ChromeJavaScriptDialogManager : public JavaScriptDialogManager { |
| 80 public: | 84 public: |
| 81 static ChromeJavaScriptDialogManager* GetInstance(); | 85 static ChromeJavaScriptDialogManager* GetInstance(); |
| 82 | 86 |
| 83 virtual void RunJavaScriptDialog( | 87 virtual void RunJavaScriptDialog( |
| 84 WebContents* web_contents, | 88 WebContents* web_contents, |
| 85 const GURL& origin_url, | 89 const GURL& origin_url, |
| 86 const std::string& accept_lang, | 90 const std::string& accept_lang, |
| 87 content::JavaScriptMessageType message_type, | 91 content::JavaScriptMessageType message_type, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 chrome::kJavaScriptMessageExpectedDelay)) { | 178 chrome::kJavaScriptMessageExpectedDelay)) { |
| 175 display_suppress_checkbox = true; | 179 display_suppress_checkbox = true; |
| 176 } else { | 180 } else { |
| 177 display_suppress_checkbox = false; | 181 display_suppress_checkbox = false; |
| 178 } | 182 } |
| 179 | 183 |
| 180 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; | 184 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; |
| 181 base::string16 dialog_title = | 185 base::string16 dialog_title = |
| 182 GetTitle(web_contents, origin_url, accept_lang, is_alert); | 186 GetTitle(web_contents, origin_url, accept_lang, is_alert); |
| 183 | 187 |
| 184 const Extension* extension = GetExtensionForWebContents(web_contents); | 188 #if defined(ENABLE_EXTENSIONS) |
| 185 if (extension) | 189 IncrementLazyKeepaliveCount(web_contents); |
| 186 IncrementLazyKeepaliveCount(extension, web_contents); | 190 #endif // defined(ENABLE_EXTENSIONS) |
| 187 | 191 |
| 188 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | 192 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
| 189 web_contents, | 193 web_contents, |
| 190 &javascript_dialog_extra_data_, | 194 &javascript_dialog_extra_data_, |
| 191 dialog_title, | 195 dialog_title, |
| 192 message_type, | 196 message_type, |
| 193 message_text, | 197 message_text, |
| 194 default_prompt_text, | 198 default_prompt_text, |
| 195 display_suppress_checkbox, | 199 display_suppress_checkbox, |
| 196 false, // is_before_unload_dialog | 200 false, // is_before_unload_dialog |
| 197 false, // is_reload | 201 false, // is_reload |
| 198 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, | 202 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, |
| 199 base::Unretained(this), web_contents, callback))); | 203 base::Unretained(this), web_contents, callback))); |
| 200 } | 204 } |
| 201 | 205 |
| 202 void ChromeJavaScriptDialogManager::RunBeforeUnloadDialog( | 206 void ChromeJavaScriptDialogManager::RunBeforeUnloadDialog( |
| 203 WebContents* web_contents, | 207 WebContents* web_contents, |
| 204 const base::string16& message_text, | 208 const base::string16& message_text, |
| 205 bool is_reload, | 209 bool is_reload, |
| 206 const DialogClosedCallback& callback) { | 210 const DialogClosedCallback& callback) { |
| 207 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? | 211 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? |
| 208 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); | 212 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); |
| 209 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? | 213 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? |
| 210 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); | 214 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); |
| 211 | 215 |
| 212 base::string16 full_message = | 216 base::string16 full_message = |
| 213 message_text + base::ASCIIToUTF16("\n\n") + footer; | 217 message_text + base::ASCIIToUTF16("\n\n") + footer; |
| 214 | 218 |
| 215 const Extension* extension = GetExtensionForWebContents(web_contents); | 219 #if defined(ENABLE_EXTENSIONS) |
| 216 if (extension) | 220 IncrementLazyKeepaliveCount(web_contents); |
| 217 IncrementLazyKeepaliveCount(extension, web_contents); | 221 #endif // defined(ENABLE_EXTENSIONS) |
| 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 #if defined(ENABLE_EXTENSIONS) |
| 312 if (extension) | 318 DecrementLazyKeepaliveCount(web_contents); |
| 313 DecrementLazyKeepaliveCount(extension, web_contents); | 319 #endif // defined(ENABLE_EXTENSIONS) |
| 314 | 320 |
| 315 callback.Run(success, user_input); | 321 callback.Run(success, user_input); |
| 316 } | 322 } |
| 317 | 323 |
| 318 } // namespace | 324 } // namespace |
| 319 | 325 |
| 320 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() { | 326 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() { |
| 321 return ChromeJavaScriptDialogManager::GetInstance(); | 327 return ChromeJavaScriptDialogManager::GetInstance(); |
| 322 } | 328 } |
| OLD | NEW |