Index: chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.cc |
diff --git a/chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.cc b/chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.cc |
index 4abd35a0aff6e0b6d4d828970a122d7070a912f3..0734558fc1888600e985efb40253f52f27917cb6 100644 |
--- a/chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.cc |
+++ b/chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.cc |
@@ -4,11 +4,18 @@ |
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h" |
+#include "base/bind.h" |
#include "base/feature_list.h" |
#include "base/metrics/histogram_macros.h" |
#include "chrome/browser/engagement/site_engagement_service.h" |
#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_list.h" |
+#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_views.h" |
+#include "chrome/browser/ui/tab_modal_confirm_dialog.h" |
+#include "chrome/browser/ui/tabs/tab_strip_model.h" |
#include "components/app_modal/javascript_dialog_manager.h" |
+#include "content/public/browser/web_contents_delegate.h" |
DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); |
@@ -21,10 +28,20 @@ bool IsEnabled() { |
return base::FeatureList::IsEnabled(kAutoDismissingDialogsFeature); |
} |
-content::JavaScriptDialogManager* AppModalDialogManager() { |
+app_modal::JavaScriptDialogManager* AppModalDialogManager() { |
return app_modal::JavaScriptDialogManager::GetInstance(); |
} |
+bool IsWebContentsForemost(content::WebContents* web_contents) { |
+ Browser* browser = BrowserList::GetInstance()->GetLastActive(); |
+ if (!browser) { |
+ DCHECK(false); |
Peter Kasting
2016/10/17 20:16:04
Do not handle DCHECK failure (banned by style guid
Avi (use Gerrit)
2016/10/18 02:46:41
Done.
|
+ return false; |
+ } |
+ |
+ return browser->tab_strip_model()->GetActiveWebContents() == web_contents; |
+} |
+ |
} // namespace |
JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( |
@@ -35,11 +52,8 @@ JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( |
JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { |
} |
-void JavaScriptDialogTabHelper::WebContentsDestroyed() { |
-} |
- |
void JavaScriptDialogTabHelper::RunJavaScriptDialog( |
- content::WebContents* web_contents, |
+ content::WebContents* alerting_web_contents, |
Peter Kasting
2016/10/17 20:16:04
Nit: Param name in .cc and .h should match
Avi (use Gerrit)
2016/10/18 02:46:41
Hm.
The problem is that in this function, there a
Peter Kasting
2016/10/18 04:31:03
I'm willing to be overridden on this.
Another opt
|
const GURL& origin_url, |
content::JavaScriptMessageType message_type, |
const base::string16& message_text, |
@@ -47,7 +61,7 @@ void JavaScriptDialogTabHelper::RunJavaScriptDialog( |
const DialogClosedCallback& callback, |
bool* did_suppress_message) { |
SiteEngagementService* site_engagement_service = SiteEngagementService::Get( |
- Profile::FromBrowserContext(web_contents->GetBrowserContext())); |
+ Profile::FromBrowserContext(alerting_web_contents->GetBrowserContext())); |
double engagement_score = site_engagement_service->GetScore(origin_url); |
switch (message_type) { |
case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: |
@@ -79,10 +93,43 @@ void JavaScriptDialogTabHelper::RunJavaScriptDialog( |
} |
if (IsEnabled()) { |
- NOTREACHED() << "auto-dismissing dialog code does not yet exist"; |
+ content::WebContents* parent_web_contents = |
+ WebContentsObserver::web_contents(); |
+ |
+ if (dialog_) { |
+ // There's already a dialog up; clear it out. |
+ CloseDialog(false, false, base::string16()); |
+ } |
+ |
+ if (!IsWebContentsForemost(parent_web_contents) && |
+ message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) { |
+ // Don't allow "prompt" dialogs to steal the user's focus. TODO(avi): |
+ // Eventually, for subsequent phases of http://bit.ly/project-oldspice, |
+ // turn off focus stealing for other dialog types. |
+ *did_suppress_message = true; |
+ callback.Run(false, base::string16()); |
+ return; |
+ } else { |
Peter Kasting
2016/10/17 20:16:04
Nit: No else after return
Avi (use Gerrit)
2016/10/18 02:46:41
Done.
|
+ parent_web_contents->GetDelegate()->ActivateContents(parent_web_contents); |
+ } |
+ |
+ base::string16 title = |
+ AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); |
+ dialog_callback_ = callback; |
+ BrowserList::AddObserver(this); |
+ // TODO(avi): abstract out so that we have a Mac version too... |
+ dialog_ = JavaScriptDialogViews::Create( |
+ parent_web_contents, alerting_web_contents, title, message_type, |
+ message_text, default_prompt_text, callback); |
+ |
+ // Message suppression is something that we don't give the user a checkbox |
+ // for any more. It was useful back in the day when dialogs were app-modal |
+ // and clicking the checkbox was the only way to escape a loop that the page |
+ // was doing, but now the user can just close the page. |
Peter Kasting
2016/10/17 20:16:04
Nit: I sort of feel like this comment is really a
Avi (use Gerrit)
2016/10/18 02:46:41
Unfortunately, the old path without auto-dismissal
Peter Kasting
2016/10/18 04:31:03
Ah. Maybe just update this comment to clarify thi
|
+ *did_suppress_message = false; |
} else { |
AppModalDialogManager()->RunJavaScriptDialog( |
- web_contents, origin_url, message_type, message_text, |
+ alerting_web_contents, origin_url, message_type, message_text, |
default_prompt_text, callback, did_suppress_message); |
} |
@@ -136,24 +183,49 @@ bool JavaScriptDialogTabHelper::HandleJavaScriptDialog( |
content::WebContents* web_contents, |
bool accept, |
const base::string16* prompt_override) { |
- if (!IsEnabled()) { |
- return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, |
- prompt_override); |
+ if (dialog_) { |
+ CloseDialog(false /*suppress_callback*/, accept, |
+ prompt_override ? *prompt_override : base::string16()); |
+ return true; |
} |
- NOTREACHED() << "auto-dismissing dialog code does not yet exist"; |
- return false; |
+ // Handle any app-modal dialogs that may be going. |
Peter Kasting
2016/10/17 20:16:04
Nit: "going"? You mean "showing"? (Sorry, but my
Avi (use Gerrit)
2016/10/18 02:46:41
Done.
|
+ return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, |
+ prompt_override); |
} |
void JavaScriptDialogTabHelper::CancelDialogs( |
content::WebContents* web_contents, |
bool suppress_callbacks, |
bool reset_state) { |
+ if (dialog_) |
+ CloseDialog(suppress_callbacks, false, base::string16()); |
+ |
// Cancel any app-modal dialogs that may be going. |
- if (!IsEnabled()) { |
- return AppModalDialogManager()->CancelDialogs( |
- web_contents, suppress_callbacks, reset_state); |
- } |
+ return AppModalDialogManager()->CancelDialogs( |
+ web_contents, suppress_callbacks, reset_state); |
+} |
+ |
+void JavaScriptDialogTabHelper::WasHidden() { |
+ if (dialog_) |
+ CloseDialog(false, false, base::string16()); |
+} |
+ |
+void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) { |
+ if (dialog_ && !IsWebContentsForemost(web_contents())) |
+ CloseDialog(false, false, base::string16()); |
+} |
+ |
+void JavaScriptDialogTabHelper::CloseDialog(bool suppress_callback, |
+ bool success, |
+ const base::string16& user_input) { |
+ DCHECK(dialog_); |
+ |
+ dialog_->CloseJavaScriptDialog(); |
+ if (!suppress_callback) |
+ dialog_callback_.Run(success, user_input); |
- // More work here for the auto-dismissing dialogs. |
+ dialog_.reset(); |
+ dialog_callback_.Reset(); |
+ BrowserList::RemoveObserver(this); |
} |