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

Side by Side Diff: components/app_modal/javascript_dialog_manager.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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/app_modal/javascript_dialog_manager.h" 5 #include "components/app_modal/javascript_dialog_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 63
64 int32_t newline_count = 64 int32_t newline_count =
65 std::count_if(message.begin(), message.end(), 65 std::count_if(message.begin(), message.end(),
66 [](const base::char16& c) { return c == '\n'; }); 66 [](const base::char16& c) { return c == '\n'; });
67 UMA_HISTOGRAM_COUNTS("JSDialogs.CountOfJSDialogMessageNewlines", 67 UMA_HISTOGRAM_COUNTS("JSDialogs.CountOfJSDialogMessageNewlines",
68 newline_count); 68 newline_count);
69 } 69 }
70 70
71 } // namespace 71 } // namespace
72 72
73 ////////////////////////////////////////////////////////////////////////////////
74 // JavaScriptDialogManager, public:
75
76 // static 73 // static
77 JavaScriptDialogManager* JavaScriptDialogManager::GetInstance() { 74 JavaScriptDialogManager* JavaScriptDialogManager::GetInstance() {
78 return base::Singleton<JavaScriptDialogManager>::get(); 75 return base::Singleton<JavaScriptDialogManager>::get();
79 } 76 }
80 77
81 void JavaScriptDialogManager::SetNativeDialogFactory( 78 void JavaScriptDialogManager::SetNativeDialogFactory(
82 std::unique_ptr<JavaScriptNativeDialogFactory> factory) { 79 std::unique_ptr<JavaScriptNativeDialogFactory> factory) {
83 native_dialog_factory_ = std::move(factory); 80 native_dialog_factory_ = std::move(factory);
84 } 81 }
85 82
86 void JavaScriptDialogManager::SetExtensionsClient( 83 void JavaScriptDialogManager::SetExtensionsClient(
87 std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client) { 84 std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client) {
88 extensions_client_ = std::move(extensions_client); 85 extensions_client_ = std::move(extensions_client);
89 } 86 }
90 87
91 ////////////////////////////////////////////////////////////////////////////////
92 // JavaScriptDialogManager, private:
93
94 JavaScriptDialogManager::JavaScriptDialogManager() 88 JavaScriptDialogManager::JavaScriptDialogManager()
95 : extensions_client_(new DefaultExtensionsClient) { 89 : extensions_client_(new DefaultExtensionsClient) {
96 } 90 }
97 91
98 JavaScriptDialogManager::~JavaScriptDialogManager() { 92 JavaScriptDialogManager::~JavaScriptDialogManager() {
99 } 93 }
100 94
95 base::string16 JavaScriptDialogManager::GetTitle(
96 content::WebContents* web_contents,
97 const GURL& origin_url) {
98 // For extensions, show the extension name, but only if the origin of
99 // the alert matches the top-level WebContents.
100 std::string name;
101 if (extensions_client_->GetExtensionName(web_contents, origin_url, &name))
102 return base::UTF8ToUTF16(name);
103
104 // Otherwise, return the formatted URL. For non-standard URLs such as |data:|,
105 // just say "This page".
106 bool is_same_origin_as_main_frame =
107 (web_contents->GetURL().GetOrigin() == origin_url.GetOrigin());
108 if (origin_url.IsStandard() && !origin_url.SchemeIsFile() &&
109 !origin_url.SchemeIsFileSystem()) {
110 #if !defined(OS_ANDROID)
Peter Kasting 2016/10/18 04:31:04 Nit: I know you're just moving this code... but co
Avi (use Gerrit) 2016/10/18 16:31:49 Done.
111 base::string16 url_string =
112 url_formatter::ElideHost(origin_url, gfx::FontList(), kUrlElideWidth);
113 #else
114 base::string16 url_string = url_formatter::FormatUrlForSecurityDisplay(
115 origin_url, url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
116 #endif
117 return l10n_util::GetStringFUTF16(
118 is_same_origin_as_main_frame ? IDS_JAVASCRIPT_MESSAGEBOX_TITLE
119 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE_IFRAME,
120 base::i18n::GetDisplayStringInLTRDirectionality(url_string));
121 }
122 return l10n_util::GetStringUTF16(
123 is_same_origin_as_main_frame
124 ? IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL
125 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL_IFRAME);
126 }
127
101 void JavaScriptDialogManager::RunJavaScriptDialog( 128 void JavaScriptDialogManager::RunJavaScriptDialog(
102 content::WebContents* web_contents, 129 content::WebContents* web_contents,
103 const GURL& origin_url, 130 const GURL& origin_url,
104 content::JavaScriptMessageType message_type, 131 content::JavaScriptMessageType message_type,
105 const base::string16& message_text, 132 const base::string16& message_text,
106 const base::string16& default_prompt_text, 133 const base::string16& default_prompt_text,
107 const DialogClosedCallback& callback, 134 const DialogClosedCallback& callback,
108 bool* did_suppress_message) { 135 bool* did_suppress_message) {
109 *did_suppress_message = false; 136 *did_suppress_message = false;
110 137
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 170
144 // Also log the time since a dialog was closed, but only if this is the first 171 // Also log the time since a dialog was closed, but only if this is the first
145 // dialog that was opened since the closing. 172 // dialog that was opened since the closing.
146 if (!last_close_time_.is_null()) { 173 if (!last_close_time_.is_null()) {
147 UMA_HISTOGRAM_MEDIUM_TIMES( 174 UMA_HISTOGRAM_MEDIUM_TIMES(
148 "JSDialogs.FineTiming.TimeBetweenDialogClosedAndNextDialogCreated", 175 "JSDialogs.FineTiming.TimeBetweenDialogClosedAndNextDialogCreated",
149 now - last_close_time_); 176 now - last_close_time_);
150 last_close_time_ = base::TimeTicks(); 177 last_close_time_ = base::TimeTicks();
151 } 178 }
152 179
153 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; 180 base::string16 dialog_title = GetTitle(web_contents, origin_url);
154 base::string16 dialog_title = GetTitle(web_contents, origin_url, is_alert);
155 181
156 extensions_client_->OnDialogOpened(web_contents); 182 extensions_client_->OnDialogOpened(web_contents);
157 183
158 LogUMAMessageLengthStats(message_text); 184 LogUMAMessageLengthStats(message_text);
159 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( 185 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
160 web_contents, 186 web_contents,
161 &javascript_dialog_extra_data_, 187 &javascript_dialog_extra_data_,
162 dialog_title, 188 dialog_title,
163 message_type, 189 message_type,
164 message_text, 190 message_text,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 if (accept) { 260 if (accept) {
235 if (prompt_override) 261 if (prompt_override)
236 dialog->SetOverridePromptText(*prompt_override); 262 dialog->SetOverridePromptText(*prompt_override);
237 dialog->native_dialog()->AcceptAppModalDialog(); 263 dialog->native_dialog()->AcceptAppModalDialog();
238 } else { 264 } else {
239 dialog->native_dialog()->CancelAppModalDialog(); 265 dialog->native_dialog()->CancelAppModalDialog();
240 } 266 }
241 return true; 267 return true;
242 } 268 }
243 269
244 base::string16 JavaScriptDialogManager::GetTitle(
245 content::WebContents* web_contents,
246 const GURL& origin_url,
247 bool is_alert) {
248 // For extensions, show the extension name, but only if the origin of
249 // the alert matches the top-level WebContents.
250 std::string name;
251 if (extensions_client_->GetExtensionName(web_contents, origin_url, &name))
252 return base::UTF8ToUTF16(name);
253
254 // Otherwise, return the formatted URL. For non-standard URLs such as |data:|,
255 // just say "This page".
256 bool is_same_origin_as_main_frame =
257 (web_contents->GetURL().GetOrigin() == origin_url.GetOrigin());
258 if (origin_url.IsStandard() && !origin_url.SchemeIsFile() &&
259 !origin_url.SchemeIsFileSystem()) {
260 #if !defined(OS_ANDROID)
261 base::string16 url_string =
262 url_formatter::ElideHost(origin_url, gfx::FontList(), kUrlElideWidth);
263 #else
264 base::string16 url_string = url_formatter::FormatUrlForSecurityDisplay(
265 origin_url, url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
266 #endif
267 return l10n_util::GetStringFUTF16(
268 is_same_origin_as_main_frame ? IDS_JAVASCRIPT_MESSAGEBOX_TITLE
269 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE_IFRAME,
270 base::i18n::GetDisplayStringInLTRDirectionality(url_string));
271 }
272 return l10n_util::GetStringUTF16(
273 is_same_origin_as_main_frame
274 ? IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL
275 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL_IFRAME);
276 }
277
278 void JavaScriptDialogManager::CancelDialogs(content::WebContents* web_contents, 270 void JavaScriptDialogManager::CancelDialogs(content::WebContents* web_contents,
279 bool suppress_callbacks, 271 bool suppress_callbacks,
280 bool reset_state) { 272 bool reset_state) {
281 AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance(); 273 AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance();
282 AppModalDialog* active_dialog = queue->active_dialog(); 274 AppModalDialog* active_dialog = queue->active_dialog();
283 for (AppModalDialogQueue::iterator i = queue->begin(); 275 for (AppModalDialogQueue::iterator i = queue->begin();
284 i != queue->end(); ++i) { 276 i != queue->end(); ++i) {
285 // Invalidating the active dialog might trigger showing a not-yet 277 // Invalidating the active dialog might trigger showing a not-yet
286 // invalidated dialog, so invalidate the active dialog last. 278 // invalidated dialog, so invalidate the active dialog last.
287 if ((*i) == active_dialog) 279 if ((*i) == active_dialog)
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 // lazy background page after the dialog closes. (Dialogs are closed before 315 // lazy background page after the dialog closes. (Dialogs are closed before
324 // their WebContents is destroyed so |web_contents| is still valid here.) 316 // their WebContents is destroyed so |web_contents| is still valid here.)
325 extensions_client_->OnDialogClosed(web_contents); 317 extensions_client_->OnDialogClosed(web_contents);
326 318
327 last_close_time_ = base::TimeTicks::Now(); 319 last_close_time_ = base::TimeTicks::Now();
328 320
329 callback.Run(success, user_input); 321 callback.Run(success, user_input);
330 } 322 }
331 323
332 } // namespace app_modal 324 } // namespace app_modal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698