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

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: pkasting 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 DCHECK(browser);
38
Peter Kasting 2016/10/18 04:31:03 Nit: Maybe remove blank line?
Avi (use Gerrit) 2016/10/18 16:31:49 Done.
39 return browser->tab_strip_model()->GetActiveWebContents() == web_contents;
40 }
41
28 } // namespace 42 } // namespace
29 43
30 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper( 44 JavaScriptDialogTabHelper::JavaScriptDialogTabHelper(
31 content::WebContents* web_contents) 45 content::WebContents* web_contents)
32 : content::WebContentsObserver(web_contents) { 46 : content::WebContentsObserver(web_contents) {
33 } 47 }
34 48
35 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() { 49 JavaScriptDialogTabHelper::~JavaScriptDialogTabHelper() {
36 } 50 }
37 51
38 void JavaScriptDialogTabHelper::WebContentsDestroyed() {
39 }
40
41 void JavaScriptDialogTabHelper::RunJavaScriptDialog( 52 void JavaScriptDialogTabHelper::RunJavaScriptDialog(
42 content::WebContents* web_contents, 53 content::WebContents* alerting_web_contents,
43 const GURL& origin_url, 54 const GURL& origin_url,
44 content::JavaScriptMessageType message_type, 55 content::JavaScriptMessageType message_type,
45 const base::string16& message_text, 56 const base::string16& message_text,
46 const base::string16& default_prompt_text, 57 const base::string16& default_prompt_text,
47 const DialogClosedCallback& callback, 58 const DialogClosedCallback& callback,
48 bool* did_suppress_message) { 59 bool* did_suppress_message) {
49 SiteEngagementService* site_engagement_service = SiteEngagementService::Get( 60 SiteEngagementService* site_engagement_service = SiteEngagementService::Get(
50 Profile::FromBrowserContext(web_contents->GetBrowserContext())); 61 Profile::FromBrowserContext(alerting_web_contents->GetBrowserContext()));
51 double engagement_score = site_engagement_service->GetScore(origin_url); 62 double engagement_score = site_engagement_service->GetScore(origin_url);
52 switch (message_type) { 63 switch (message_type) {
53 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: 64 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT:
54 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Alert", 65 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Alert",
55 engagement_score); 66 engagement_score);
56 break; 67 break;
57 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: 68 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM:
58 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Confirm", 69 UMA_HISTOGRAM_PERCENTAGE("JSDialogs.SiteEngagementOfDialogs.Confirm",
59 engagement_score); 70 engagement_score);
60 break; 71 break;
(...skipping 11 matching lines...) Expand all
72 message_length); 83 message_length);
73 } else if (engagement_score < 5) { 84 } else if (engagement_score < 5) {
74 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementOneToFive", 85 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementOneToFive",
75 message_length); 86 message_length);
76 } else { 87 } else {
77 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementHigher", 88 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCount.EngagementHigher",
78 message_length); 89 message_length);
79 } 90 }
80 91
81 if (IsEnabled()) { 92 if (IsEnabled()) {
82 NOTREACHED() << "auto-dismissing dialog code does not yet exist"; 93 content::WebContents* parent_web_contents =
94 WebContentsObserver::web_contents();
95
96 if (!IsWebContentsForemost(parent_web_contents) &&
97 message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) {
98 // Don't allow "prompt" dialogs to steal the user's focus. TODO(avi):
99 // Eventually, for subsequent phases of http://bit.ly/project-oldspice,
100 // turn off focus stealing for other dialog types.
101 *did_suppress_message = true;
102 callback.Run(false, base::string16());
103 return;
104 }
105
106 if (dialog_) {
107 // There's already a dialog up; clear it out.
108 CloseDialog(false, false, base::string16());
109 }
110
111 parent_web_contents->GetDelegate()->ActivateContents(parent_web_contents);
112
113 base::string16 title =
114 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url);
115 dialog_callback_ = callback;
116 BrowserList::AddObserver(this);
117 // TODO(avi): abstract out so that we have a Mac version too...
118 dialog_ = JavaScriptDialogViews::Create(
119 parent_web_contents, alerting_web_contents, title, message_type,
120 message_text, default_prompt_text, callback);
121
122 // Message suppression is something that we don't give the user a checkbox
123 // for any more. It was useful back in the day when dialogs were app-modal
124 // and clicking the checkbox was the only way to escape a loop that the page
125 // was doing, but now the user can just close the page.
126 *did_suppress_message = false;
83 } else { 127 } else {
84 AppModalDialogManager()->RunJavaScriptDialog( 128 AppModalDialogManager()->RunJavaScriptDialog(
85 web_contents, origin_url, message_type, message_text, 129 alerting_web_contents, origin_url, message_type, message_text,
86 default_prompt_text, callback, did_suppress_message); 130 default_prompt_text, callback, did_suppress_message);
87 } 131 }
88 132
89 if (did_suppress_message) { 133 if (did_suppress_message) {
90 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed", 134 UMA_HISTOGRAM_COUNTS("JSDialogs.CharacterCountUserSuppressed",
91 message_length); 135 message_length);
92 } 136 }
93 } 137 }
94 138
95 namespace { 139 namespace {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 173
130 return AppModalDialogManager()->RunBeforeUnloadDialog( 174 return AppModalDialogManager()->RunBeforeUnloadDialog(
131 web_contents, is_reload, 175 web_contents, is_reload,
132 base::Bind(&SaveUnloadUmaStats, engagement_score, callback)); 176 base::Bind(&SaveUnloadUmaStats, engagement_score, callback));
133 } 177 }
134 178
135 bool JavaScriptDialogTabHelper::HandleJavaScriptDialog( 179 bool JavaScriptDialogTabHelper::HandleJavaScriptDialog(
136 content::WebContents* web_contents, 180 content::WebContents* web_contents,
137 bool accept, 181 bool accept,
138 const base::string16* prompt_override) { 182 const base::string16* prompt_override) {
139 if (!IsEnabled()) { 183 if (dialog_) {
140 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept, 184 CloseDialog(false /*suppress_callback*/, accept,
141 prompt_override); 185 prompt_override ? *prompt_override : base::string16());
186 return true;
142 } 187 }
143 188
144 NOTREACHED() << "auto-dismissing dialog code does not yet exist"; 189 // Handle any app-modal dialogs being run by the app-modal dialog system.
145 return false; 190 return AppModalDialogManager()->HandleJavaScriptDialog(web_contents, accept,
191 prompt_override);
146 } 192 }
147 193
148 void JavaScriptDialogTabHelper::CancelDialogs( 194 void JavaScriptDialogTabHelper::CancelDialogs(
149 content::WebContents* web_contents, 195 content::WebContents* web_contents,
150 bool suppress_callbacks, 196 bool suppress_callbacks,
151 bool reset_state) { 197 bool reset_state) {
152 // Cancel any app-modal dialogs that may be going. 198 if (dialog_)
153 if (!IsEnabled()) { 199 CloseDialog(suppress_callbacks, false, base::string16());
154 return AppModalDialogManager()->CancelDialogs(
155 web_contents, suppress_callbacks, reset_state);
156 }
157 200
158 // More work here for the auto-dismissing dialogs. 201 // Cancel any app-modal dialogs being run by the app-modal dialog system.
202 return AppModalDialogManager()->CancelDialogs(
203 web_contents, suppress_callbacks, reset_state);
159 } 204 }
205
206 void JavaScriptDialogTabHelper::WasHidden() {
207 if (dialog_)
208 CloseDialog(false, false, base::string16());
209 }
210
211 void JavaScriptDialogTabHelper::OnBrowserSetLastActive(Browser* browser) {
212 if (dialog_ && !IsWebContentsForemost(web_contents()))
213 CloseDialog(false, false, base::string16());
214 }
215
216 void JavaScriptDialogTabHelper::CloseDialog(bool suppress_callback,
217 bool success,
218 const base::string16& user_input) {
219 DCHECK(dialog_);
220
221 dialog_->CloseJavaScriptDialog();
222 if (!suppress_callback)
223 dialog_callback_.Run(success, user_input);
224
225 dialog_.reset();
226 dialog_callback_.Reset();
227 BrowserList::RemoveObserver(this);
228 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698