Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Side by Side Diff: chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.cc

Issue 2421943002: Make JavaScript dialogs auto-dismiss on tab switch. (Closed)
Patch Set: fix Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/javascript_dialogs/javascript_dialog_tab_helper.h" 5 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h"
6 6
7 #include "base/bind.h"
7 #include "base/feature_list.h" 8 #include "base/feature_list.h"
8 #include "base/metrics/histogram_macros.h" 9 #include "base/metrics/histogram_macros.h"
9 #include "chrome/browser/engagement/site_engagement_service.h" 10 #include "chrome/browser/engagement/site_engagement_service.h"
10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_list.h"
14 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_views.h"
15 #include "chrome/browser/ui/tab_modal_confirm_dialog.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "components/app_modal/javascript_dialog_manager.h" 17 #include "components/app_modal/javascript_dialog_manager.h"
18 #include "content/public/browser/web_contents_delegate.h"
12 19
13 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); 20 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper);
14 21
15 namespace { 22 namespace {
16 23
17 const base::Feature kAutoDismissingDialogsFeature{ 24 const base::Feature kAutoDismissingDialogsFeature{
18 "AutoDismissingDialogs", base::FEATURE_DISABLED_BY_DEFAULT}; 25 "AutoDismissingDialogs", base::FEATURE_DISABLED_BY_DEFAULT};
19 26
20 bool IsEnabled() { 27 bool IsEnabled() {
21 return base::FeatureList::IsEnabled(kAutoDismissingDialogsFeature); 28 return base::FeatureList::IsEnabled(kAutoDismissingDialogsFeature);
22 } 29 }
23 30
24 content::JavaScriptDialogManager* AppModalDialogManager() { 31 app_modal::JavaScriptDialogManager* AppModalDialogManager() {
25 return app_modal::JavaScriptDialogManager::GetInstance(); 32 return app_modal::JavaScriptDialogManager::GetInstance();
26 } 33 }
27 34
35 bool IsWebContentsForemost(content::WebContents* web_contents) {
36 Browser* browser = BrowserList::GetInstance()->GetLastActive();
37 if (!browser) {
38 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.
39 return false;
40 }
41
42 return browser->tab_strip_model()->GetActiveWebContents() == web_contents;
43 }
44
28 } // namespace 45 } // namespace
29 46
30 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( 47 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper(
31 content::WebContents* web_contents) 48 content::WebContents* web_contents)
32 : content::WebContentsObserver(web_contents) { 49 : content::WebContentsObserver(web_contents) {
33 } 50 }
34 51
35 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { 52 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() {
36 } 53 }
37 54
38 void JavaScriptDialogTabHelper::WebContentsDestroyed() {
39 }
40
41 void JavaScriptDialogTabHelper::RunJavaScriptDialog( 55 void JavaScriptDialogTabHelper::RunJavaScriptDialog(
42 content::WebContents* web_contents, 56 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
43 const GURL& origin_url, 57 const GURL& origin_url,
44 content::JavaScriptMessageType message_type, 58 content::JavaScriptMessageType message_type,
45 const base::string16& message_text, 59 const base::string16& message_text,
46 const base::string16& default_prompt_text, 60 const base::string16& default_prompt_text,
47 const DialogClosedCallback& callback, 61 const DialogClosedCallback& callback,
48 bool* did_suppress_message) { 62 bool* did_suppress_message) {
49 SiteEngagementService* site_engagement_service = SiteEngagementService::Get( 63 SiteEngagementService* site_engagement_service = SiteEngagementService::Get(
50 Profile::FromBrowserContext(web_contents->GetBrowserContext())); 64 Profile::FromBrowserContext(alerting_web_contents->GetBrowserContext()));
51 double engagement_score = site_engagement_service->GetScore(origin_url); 65 double engagement_score = site_engagement_service->GetScore(origin_url);
52 switch (message_type) { 66 switch (message_type) {
53 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: 67 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT:
54 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Alert", 68 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Alert",
55 engagement_score); 69 engagement_score);
56 break; 70 break;
57 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: 71 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM:
58 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Confirm", 72 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Confirm",
59 engagement_score); 73 engagement_score);
60 break; 74 break;
(...skipping 11 matching lines...) Expand all
72 message_length); 86 message_length);
73 } else if (engagement_score < 5) { 87 } else if (engagement_score < 5) {
74 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementOneToFive", 88 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementOneToFive",
75 message_length); 89 message_length);
76 } else { 90 } else {
77 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementHigher", 91 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementHigher",
78 message_length); 92 message_length);
79 } 93 }
80 94
81 if (IsEnabled()) { 95 if (IsEnabled()) {
82 NOTREACHED() << "auto-dismissing dialog code does not yet exist"; 96 content::WebContents* parent_web_contents =
97 WebContentsObserver::web_contents();
98
99 if (dialog_) {
100 // There's already a dialog up; clear it out.
101 CloseDialog(false, false, base::string16());
102 }
103
104 if (!IsWebContentsForemost(parent_web_contents) &&
105 message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) {
106 // Don't allow "prompt" dialogs to steal the user's focus. TODO(avi):
107 // Eventually, for subsequent phases of http://bit.ly/project-oldspice,
108 // turn off focus stealing for other dialog types.
109 *did_suppress_message = true;
110 callback.Run(false, base::string16());
111 return;
112 } else {
Peter Kasting 2016/10/17 20:16:04 Nit: No else after return
Avi (use Gerrit) 2016/10/18 02:46:41 Done.
113 parent_web_contents->GetDelegate()->ActivateContents(parent_web_contents);
114 }
115
116 base::string16 title =
117 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url);
118 dialog_callback_ = callback;
119 BrowserList::AddObserver(this);
120 // TODO(avi): abstract out so that we have a Mac version too...
121 dialog_ = JavaScriptDialogViews::Create(
122 parent_web_contents, alerting_web_contents, title, message_type,
123 message_text, default_prompt_text, callback);
124
125 // Message suppression is something that we don't give the user a checkbox
126 // for any more. It was useful back in the day when dialogs were app-modal
127 // and clicking the checkbox was the only way to escape a loop that the page
128 // 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
129 *did_suppress_message = false;
83 } else { 130 } else {
84 AppModalDialogManager()->RunJavaScriptDialog( 131 AppModalDialogManager()->RunJavaScriptDialog(
85 web_contents, origin_url, message_type, message_text, 132 alerting_web_contents, origin_url, message_type, message_text,
86 default_prompt_text, callback, did_suppress_message); 133 default_prompt_text, callback, did_suppress_message);
87 } 134 }
88 135
89 if (did_suppress_message) { 136 if (did_suppress_message) {
90 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed", 137 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed",
91 message_length); 138 message_length);
92 } 139 }
93 } 140 }
94 141
95 namespace { 142 namespace {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 176
130 return AppModalDialogManager()->RunBeforeUnloadDialog( 177 return AppModalDialogManager()->RunBeforeUnloadDialog(
131 web_contents, is_reload, 178 web_contents, is_reload,
132 base::Bind(&SaveUnloadUmaStats, engagement_score, callback)); 179 base::Bind(&SaveUnloadUmaStats, engagement_score, callback));
133 } 180 }
134 181
135 bool JavaScriptDialogTabHelper::HandleJavaScriptDialog( 182 bool JavaScriptDialogTabHelper::HandleJavaScriptDialog(
136 content::WebContents* web_contents, 183 content::WebContents* web_contents,
137 bool accept, 184 bool accept,
138 const base::string16* prompt_override) { 185 const base::string16* prompt_override) {
139 if (!IsEnabled()) { 186 if (dialog_) {
140 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, 187 CloseDialog(false /*suppress_callback*/, accept,
141 prompt_override); 188 prompt_override ? *prompt_override : base::string16());
189 return true;
142 } 190 }
143 191
144 NOTREACHED() << "auto-dismissing dialog code does not yet exist"; 192 // 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.
145 return false; 193 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept,
194 prompt_override);
146 } 195 }
147 196
148 void JavaScriptDialogTabHelper::CancelDialogs( 197 void JavaScriptDialogTabHelper::CancelDialogs(
149 content::WebContents* web_contents, 198 content::WebContents* web_contents,
150 bool suppress_callbacks, 199 bool suppress_callbacks,
151 bool reset_state) { 200 bool reset_state) {
201 if (dialog_)
202 CloseDialog(suppress_callbacks, false, base::string16());
203
152 // Cancel any app-modal dialogs that may be going. 204 // Cancel any app-modal dialogs that may be going.
153 if (!IsEnabled()) { 205 return AppModalDialogManager()->CancelDialogs(
154 return AppModalDialogManager()->CancelDialogs( 206 web_contents, suppress_callbacks, reset_state);
155 web_contents, suppress_callbacks, reset_state); 207 }
156 }
157 208
158 // More work here for the auto-dismissing dialogs. 209 void JavaScriptDialogTabHelper::WasHidden() {
210 if (dialog_)
211 CloseDialog(false, false, base::string16());
159 } 212 }
213
214 void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) {
215 if (dialog_ && !IsWebContentsForemost(web_contents()))
216 CloseDialog(false, false, base::string16());
217 }
218
219 void JavaScriptDialogTabHelper::CloseDialog(bool suppress_callback,
220 bool success,
221 const base::string16& user_input) {
222 DCHECK(dialog_);
223
224 dialog_->CloseJavaScriptDialog();
225 if (!suppress_callback)
226 dialog_callback_.Run(success, user_input);
227
228 dialog_.reset();
229 dialog_callback_.Reset();
230 BrowserList::RemoveObserver(this);
231 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698