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

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

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

Powered by Google App Engine
This is Rietveld 408576698