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

Side by Side Diff: components/app_modal_dialogs/javascript_dialog_manager_impl.cc

Issue 735473002: Rename app_modal_dialogs dir to app_modal (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: rebase Created 6 years, 1 month 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/app_modal_dialogs/javascript_dialog_manager_impl.h"
6
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "base/i18n/rtl.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/app_modal_dialogs/app_modal_dialog.h"
12 #include "components/app_modal_dialogs/app_modal_dialog_queue.h"
13 #include "components/app_modal_dialogs/javascript_dialog_extensions_client.h"
14 #include "components/app_modal_dialogs/javascript_native_dialog_factory.h"
15 #include "components/app_modal_dialogs/native_app_modal_dialog.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/content_client.h"
18 #include "content/public/common/javascript_message_type.h"
19 #include "grit/components_strings.h"
20 #include "net/base/net_util.h"
21 #include "ui/base/l10n/l10n_util.h"
22
23 namespace {
24
25 class DefaultExtensionsClient : public JavaScriptDialogExtensionsClient {
26 public:
27 DefaultExtensionsClient() {}
28 ~DefaultExtensionsClient() override {}
29
30 private:
31 // JavaScriptDialogExtensionsClient:
32 void OnDialogOpened(content::WebContents* web_contents) override {}
33 void OnDialogClosed(content::WebContents* web_contents) override {}
34 bool GetExtensionName(content::WebContents* web_contents,
35 const GURL& origin_url,
36 std::string* name_out) override {
37 return false;
38 }
39
40 DISALLOW_COPY_AND_ASSIGN(DefaultExtensionsClient);
41 };
42
43 } // namespace
44
45 ////////////////////////////////////////////////////////////////////////////////
46 // JavaScriptDialogManagerImpl, public:
47
48 // static
49 JavaScriptDialogManagerImpl* JavaScriptDialogManagerImpl::GetInstance() {
50 return Singleton<JavaScriptDialogManagerImpl>::get();
51 }
52
53 void JavaScriptDialogManagerImpl::RunJavaScriptDialog(
54 content::WebContents* web_contents,
55 const GURL& origin_url,
56 const std::string& accept_lang,
57 content::JavaScriptMessageType message_type,
58 const base::string16& message_text,
59 const base::string16& default_prompt_text,
60 const DialogClosedCallback& callback,
61 bool* did_suppress_message) {
62 *did_suppress_message = false;
63
64 ChromeJavaScriptDialogExtraData* extra_data =
65 &javascript_dialog_extra_data_[web_contents];
66
67 if (extra_data->suppress_javascript_messages_) {
68 *did_suppress_message = true;
69 return;
70 }
71
72 base::TimeDelta time_since_last_message = base::TimeTicks::Now() -
73 extra_data->last_javascript_message_dismissal_;
74 bool display_suppress_checkbox = false;
75 // If a WebContents is impolite and displays a second JavaScript
76 // alert within kJavaScriptMessageExpectedDelay of a previous
77 // JavaScript alert being dismissed, show a checkbox offering to
78 // suppress future alerts from this WebContents.
79 const int kJavaScriptMessageExpectedDelay = 1000;
80
81 if (time_since_last_message <
82 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay)) {
83 display_suppress_checkbox = true;
84 } else {
85 display_suppress_checkbox = false;
86 }
87
88 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT;
89 base::string16 dialog_title =
90 GetTitle(web_contents, origin_url, accept_lang, is_alert);
91
92 extensions_client_->OnDialogOpened(web_contents);
93
94 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
95 web_contents,
96 &javascript_dialog_extra_data_,
97 dialog_title,
98 message_type,
99 message_text,
100 default_prompt_text,
101 display_suppress_checkbox,
102 false, // is_before_unload_dialog
103 false, // is_reload
104 base::Bind(&JavaScriptDialogManagerImpl::OnDialogClosed,
105 base::Unretained(this), web_contents, callback)));
106 }
107
108 void JavaScriptDialogManagerImpl::RunBeforeUnloadDialog(
109 content::WebContents* web_contents,
110 const base::string16& message_text,
111 bool is_reload,
112 const DialogClosedCallback& callback) {
113 const base::string16 title = l10n_util::GetStringUTF16(is_reload ?
114 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE);
115 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ?
116 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER);
117
118 base::string16 full_message =
119 message_text + base::ASCIIToUTF16("\n\n") + footer;
120
121 extensions_client_->OnDialogOpened(web_contents);
122
123 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
124 web_contents,
125 &javascript_dialog_extra_data_,
126 title,
127 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
128 full_message,
129 base::string16(), // default_prompt_text
130 false, // display_suppress_checkbox
131 true, // is_before_unload_dialog
132 is_reload,
133 base::Bind(&JavaScriptDialogManagerImpl::OnDialogClosed,
134 base::Unretained(this), web_contents, callback)));
135 }
136
137 bool JavaScriptDialogManagerImpl::HandleJavaScriptDialog(
138 content::WebContents* web_contents,
139 bool accept,
140 const base::string16* prompt_override) {
141 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance();
142 if (!dialog_queue->HasActiveDialog() ||
143 !dialog_queue->active_dialog()->IsJavaScriptModalDialog() ||
144 dialog_queue->active_dialog()->web_contents() != web_contents) {
145 return false;
146 }
147 JavaScriptAppModalDialog* dialog = static_cast<JavaScriptAppModalDialog*>(
148 dialog_queue->active_dialog());
149 if (accept) {
150 if (prompt_override)
151 dialog->SetOverridePromptText(*prompt_override);
152 dialog->native_dialog()->AcceptAppModalDialog();
153 } else {
154 dialog->native_dialog()->CancelAppModalDialog();
155 }
156 return true;
157 }
158
159 void JavaScriptDialogManagerImpl::WebContentsDestroyed(
160 content::WebContents* web_contents) {
161 CancelActiveAndPendingDialogs(web_contents);
162 javascript_dialog_extra_data_.erase(web_contents);
163 }
164
165 void JavaScriptDialogManagerImpl::SetNativeDialogFactory(
166 scoped_ptr<JavaScriptNativeDialogFactory> factory) {
167 native_dialog_factory_ = factory.Pass();
168 }
169
170 void JavaScriptDialogManagerImpl::SetExtensionsClient(
171 scoped_ptr<JavaScriptDialogExtensionsClient> extensions_client) {
172 extensions_client_ = extensions_client.Pass();
173 }
174
175 JavaScriptDialogManagerImpl::JavaScriptDialogManagerImpl()
176 : extensions_client_(new DefaultExtensionsClient) {
177 }
178
179 JavaScriptDialogManagerImpl::~JavaScriptDialogManagerImpl() {
180 }
181
182 base::string16 JavaScriptDialogManagerImpl::GetTitle(
183 content::WebContents* web_contents,
184 const GURL& origin_url,
185 const std::string& accept_lang,
186 bool is_alert) {
187 // If the URL hasn't any host, return the default string.
188 if (!origin_url.has_host()) {
189 return l10n_util::GetStringUTF16(
190 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
191 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
192 }
193
194 // For extensions, show the extension name, but only if the origin of
195 // the alert matches the top-level WebContents.
196 std::string name;
197 if (extensions_client_->GetExtensionName(web_contents, origin_url, &name))
198 return base::UTF8ToUTF16(name);
199
200 // Otherwise, return the formatted URL.
201 // In this case, force URL to have LTR directionality.
202 base::string16 url_string = net::FormatUrl(origin_url, accept_lang);
203 return l10n_util::GetStringFUTF16(
204 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE
205 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
206 base::i18n::GetDisplayStringInLTRDirectionality(url_string));
207 }
208
209 void JavaScriptDialogManagerImpl::CancelActiveAndPendingDialogs(
210 content::WebContents* web_contents) {
211 AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance();
212 AppModalDialog* active_dialog = queue->active_dialog();
213 if (active_dialog && active_dialog->web_contents() == web_contents)
214 active_dialog->Invalidate();
215 for (AppModalDialogQueue::iterator i = queue->begin();
216 i != queue->end(); ++i) {
217 if ((*i)->web_contents() == web_contents)
218 (*i)->Invalidate();
219 }
220 }
221
222 void JavaScriptDialogManagerImpl::OnDialogClosed(
223 content::WebContents* web_contents,
224 DialogClosedCallback callback,
225 bool success,
226 const base::string16& user_input) {
227 // If an extension opened this dialog then the extension may shut down its
228 // lazy background page after the dialog closes. (Dialogs are closed before
229 // their WebContents is destroyed so |web_contents| is still valid here.)
230 extensions_client_->OnDialogClosed(web_contents);
231
232 callback.Run(success, user_input);
233 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698