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

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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/app_modal_dialogs/javascript_dialog_manager.h" 5 #include "components/app_modal_dialogs/javascript_dialog_manager_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/i18n/rtl.h" 9 #include "base/i18n/rtl.h"
10 #include "base/memory/singleton.h"
11 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
12 #include "components/app_modal_dialogs/app_modal_dialog.h" 11 #include "components/app_modal_dialogs/app_modal_dialog.h"
13 #include "components/app_modal_dialogs/app_modal_dialog_queue.h" 12 #include "components/app_modal_dialogs/app_modal_dialog_queue.h"
14 #include "components/app_modal_dialogs/javascript_app_modal_dialog.h" 13 #include "components/app_modal_dialogs/javascript_app_modal_dialog.h"
msw 2014/11/05 03:53:56 This is included in the header and not needed here
14 #include "components/app_modal_dialogs/javascript_dialog_extensions_client.h"
15 #include "components/app_modal_dialogs/javascript_native_dialog_factory.h"
15 #include "components/app_modal_dialogs/native_app_modal_dialog.h" 16 #include "components/app_modal_dialogs/native_app_modal_dialog.h"
msw 2014/11/05 03:53:56 I don't think this include is needed anymore.
oshima 2014/11/05 21:42:18 this is used to access native_dialog in HandleJava
16 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/content_client.h" 18 #include "content/public/common/content_client.h"
18 #include "content/public/common/javascript_message_type.h" 19 #include "content/public/common/javascript_message_type.h"
19 #include "grit/components_strings.h" 20 #include "grit/components_strings.h"
20 #include "net/base/net_util.h" 21 #include "net/base/net_util.h"
21 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
22 23
23 #if defined(ENABLE_EXTENSIONS)
24 #include "extensions/browser/process_manager.h"
25 #include "extensions/common/extension.h"
26 #endif // defined(ENABLE_EXTENSIONS)
27
28 using content::BrowserContext; 24 using content::BrowserContext;
msw 2014/11/05 03:53:55 Remove this; the type is no longer used here.
oshima 2014/11/05 21:42:18 Done.
29 using content::JavaScriptDialogManager; 25 using content::JavaScriptDialogManager;
msw 2014/11/05 03:53:56 Remove this; the type is no longer used here.
oshima 2014/11/05 21:42:19 Done.
30 using content::WebContents; 26 using content::WebContents;
msw 2014/11/05 03:53:56 nit: explicitly use "content::" and remove this, o
oshima 2014/11/05 21:42:18 Done.
31 27
32 #if defined(ENABLE_EXTENSIONS)
33 using extensions::Extension;
34 #endif // defined(ENABLE_EXTENSIONS)
35
36 namespace { 28 namespace {
37 29
38 #if defined(ENABLE_EXTENSIONS) 30 class DefaultExtensionsClient : public JavaScriptDialogExtensionsClient {
39 // Returns the ProcessManager for the browser context from |web_contents|. 31 public:
40 extensions::ProcessManager* GetExtensionsProcessManager( 32 DefaultExtensionsClient() {}
41 WebContents* web_contents) { 33 ~DefaultExtensionsClient() override {}
42 return extensions::ProcessManager::Get(web_contents->GetBrowserContext()); 34
35 private:
36 // JavaScriptDialogExtensionsClient:
37 void IncrementLazyKeepaliveCount(
38 content::WebContents* web_contents) override {}
39 void DecrementLazyKeepaliveCount(
40 content::WebContents* web_contents) override {}
41 bool GetExtensionName(content::WebContents* web_contents,
42 const GURL& origin_url,
43 std::string* name_out) override {
44 return false;
45 }
46
47 DISALLOW_COPY_AND_ASSIGN(DefaultExtensionsClient);
48 };
49
50 } // namespace
51
52 ////////////////////////////////////////////////////////////////////////////////
53 // JavaScriptDialogManagerImpl, public:
54
55 JavaScriptDialogManagerImpl::JavaScriptDialogManagerImpl()
msw 2014/11/05 03:53:56 nit: reorder to match decl.
oshima 2014/11/05 21:42:19 Done.
56 : extensions_client_(new DefaultExtensionsClient) {
43 } 57 }
44 58
45 // Returns the extension associated with |web_contents| or NULL if there is no 59 JavaScriptDialogManagerImpl::~JavaScriptDialogManagerImpl() {
msw 2014/11/05 03:53:56 nit: reorder to match decl.
oshima 2014/11/05 21:42:19 Done.
46 // associated extension (or extensions are not supported).
47 const Extension* GetExtensionForWebContents(WebContents* web_contents) {
48 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents);
49 return pm->GetExtensionForRenderViewHost(web_contents->GetRenderViewHost());
50 }
51 #endif // defined(ENABLE_EXTENSIONS)
52
53 // Keeps an |extension| from shutting down its lazy background page. If an
54 // extension opens a dialog its lazy background page must stay alive until the
55 // dialog closes.
56 void IncrementLazyKeepaliveCount(WebContents* web_contents) {
57 #if defined(ENABLE_EXTENSIONS)
58 const Extension* extension = GetExtensionForWebContents(web_contents);
59 if (extension == NULL)
60 return;
61
62 DCHECK(web_contents);
63 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents);
64 if (pm)
65 pm->IncrementLazyKeepaliveCount(extension);
66 #endif // defined(ENABLE_EXTENSIONS)
67 }
68
69 // Allows an |extension| to shut down its lazy background page after a dialog
70 // closes (if nothing else is keeping it open).
71 void DecrementLazyKeepaliveCount(WebContents* web_contents) {
72 #if defined(ENABLE_EXTENSIONS)
73 const Extension* extension = GetExtensionForWebContents(web_contents);
74 if (extension == NULL)
75 return;
76
77 DCHECK(web_contents);
78 extensions::ProcessManager* pm = GetExtensionsProcessManager(web_contents);
79 if (pm)
80 pm->DecrementLazyKeepaliveCount(extension);
81 #endif // defined(ENABLE_EXTENSIONS)
82 }
83
84 class ChromeJavaScriptDialogManager : public JavaScriptDialogManager {
85 public:
86 static ChromeJavaScriptDialogManager* GetInstance();
87
88 void RunJavaScriptDialog(WebContents* web_contents,
89 const GURL& origin_url,
90 const std::string& accept_lang,
91 content::JavaScriptMessageType message_type,
92 const base::string16& message_text,
93 const base::string16& default_prompt_text,
94 const DialogClosedCallback& callback,
95 bool* did_suppress_message) override;
96
97 void RunBeforeUnloadDialog(WebContents* web_contents,
98 const base::string16& message_text,
99 bool is_reload,
100 const DialogClosedCallback& callback) override;
101
102 bool HandleJavaScriptDialog(WebContents* web_contents,
103 bool accept,
104 const base::string16* prompt_override) override;
105
106 void CancelActiveAndPendingDialogs(WebContents* web_contents) override;
107
108 void WebContentsDestroyed(WebContents* web_contents) override;
109
110 private:
111 friend struct DefaultSingletonTraits<ChromeJavaScriptDialogManager>;
112
113 ChromeJavaScriptDialogManager();
114 ~ChromeJavaScriptDialogManager() override;
115
116 base::string16 GetTitle(WebContents* web_contents,
117 const GURL& origin_url,
118 const std::string& accept_lang,
119 bool is_alert);
120
121 // Wrapper around a DialogClosedCallback so that we can intercept it before
122 // passing it onto the original callback.
123 void OnDialogClosed(WebContents* web_contents,
124 DialogClosedCallback callback,
125 bool success,
126 const base::string16& user_input);
127
128 // Mapping between the WebContents and their extra data. The key
129 // is a void* because the pointer is just a cookie and is never dereferenced.
130 JavaScriptAppModalDialog::ExtraDataMap javascript_dialog_extra_data_;
131
132 DISALLOW_COPY_AND_ASSIGN(ChromeJavaScriptDialogManager);
133 };
134
135 ////////////////////////////////////////////////////////////////////////////////
136 // ChromeJavaScriptDialogManager, public:
137
138 ChromeJavaScriptDialogManager::ChromeJavaScriptDialogManager() {
139 }
140
141 ChromeJavaScriptDialogManager::~ChromeJavaScriptDialogManager() {
142 } 60 }
143 61
144 // static 62 // static
145 ChromeJavaScriptDialogManager* ChromeJavaScriptDialogManager::GetInstance() { 63 JavaScriptDialogManagerImpl* JavaScriptDialogManagerImpl::GetInstance() {
146 return Singleton<ChromeJavaScriptDialogManager>::get(); 64 return Singleton<JavaScriptDialogManagerImpl>::get();
147 } 65 }
148 66
149 void ChromeJavaScriptDialogManager::RunJavaScriptDialog( 67 void JavaScriptDialogManagerImpl::RunJavaScriptDialog(
150 WebContents* web_contents, 68 WebContents* web_contents,
151 const GURL& origin_url, 69 const GURL& origin_url,
152 const std::string& accept_lang, 70 const std::string& accept_lang,
153 content::JavaScriptMessageType message_type, 71 content::JavaScriptMessageType message_type,
154 const base::string16& message_text, 72 const base::string16& message_text,
155 const base::string16& default_prompt_text, 73 const base::string16& default_prompt_text,
156 const DialogClosedCallback& callback, 74 const DialogClosedCallback& callback,
157 bool* did_suppress_message) { 75 bool* did_suppress_message) {
158 *did_suppress_message = false; 76 *did_suppress_message = false;
159 77
(...skipping 18 matching lines...) Expand all
178 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay)) { 96 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay)) {
179 display_suppress_checkbox = true; 97 display_suppress_checkbox = true;
180 } else { 98 } else {
181 display_suppress_checkbox = false; 99 display_suppress_checkbox = false;
182 } 100 }
183 101
184 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; 102 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT;
185 base::string16 dialog_title = 103 base::string16 dialog_title =
186 GetTitle(web_contents, origin_url, accept_lang, is_alert); 104 GetTitle(web_contents, origin_url, accept_lang, is_alert);
187 105
188 IncrementLazyKeepaliveCount(web_contents); 106 extensions_client_->IncrementLazyKeepaliveCount(web_contents);
189 107
190 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( 108 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
191 web_contents, 109 web_contents,
192 &javascript_dialog_extra_data_, 110 &javascript_dialog_extra_data_,
193 dialog_title, 111 dialog_title,
194 message_type, 112 message_type,
195 message_text, 113 message_text,
196 default_prompt_text, 114 default_prompt_text,
197 display_suppress_checkbox, 115 display_suppress_checkbox,
198 false, // is_before_unload_dialog 116 false, // is_before_unload_dialog
199 false, // is_reload 117 false, // is_reload
200 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, 118 base::Bind(&JavaScriptDialogManagerImpl::OnDialogClosed,
201 base::Unretained(this), web_contents, callback))); 119 base::Unretained(this), web_contents, callback)));
202 } 120 }
203 121
204 void ChromeJavaScriptDialogManager::RunBeforeUnloadDialog( 122 void JavaScriptDialogManagerImpl::RunBeforeUnloadDialog(
205 WebContents* web_contents, 123 WebContents* web_contents,
206 const base::string16& message_text, 124 const base::string16& message_text,
207 bool is_reload, 125 bool is_reload,
208 const DialogClosedCallback& callback) { 126 const DialogClosedCallback& callback) {
209 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? 127 const base::string16 title = l10n_util::GetStringUTF16(is_reload ?
210 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); 128 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE);
211 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? 129 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ?
212 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); 130 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER);
213 131
214 base::string16 full_message = 132 base::string16 full_message =
215 message_text + base::ASCIIToUTF16("\n\n") + footer; 133 message_text + base::ASCIIToUTF16("\n\n") + footer;
216 134
217 IncrementLazyKeepaliveCount(web_contents); 135 extensions_client_->IncrementLazyKeepaliveCount(web_contents);
218 136
219 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( 137 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
220 web_contents, 138 web_contents,
221 &javascript_dialog_extra_data_, 139 &javascript_dialog_extra_data_,
222 title, 140 title,
223 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, 141 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
224 full_message, 142 full_message,
225 base::string16(), // default_prompt_text 143 base::string16(), // default_prompt_text
226 false, // display_suppress_checkbox 144 false, // display_suppress_checkbox
227 true, // is_before_unload_dialog 145 true, // is_before_unload_dialog
228 is_reload, 146 is_reload,
229 base::Bind(&ChromeJavaScriptDialogManager::OnDialogClosed, 147 base::Bind(&JavaScriptDialogManagerImpl::OnDialogClosed,
230 base::Unretained(this), web_contents, callback))); 148 base::Unretained(this), web_contents, callback)));
231 } 149 }
232 150
233 bool ChromeJavaScriptDialogManager::HandleJavaScriptDialog( 151 bool JavaScriptDialogManagerImpl::HandleJavaScriptDialog(
234 WebContents* web_contents, 152 WebContents* web_contents,
235 bool accept, 153 bool accept,
236 const base::string16* prompt_override) { 154 const base::string16* prompt_override) {
237 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance(); 155 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance();
238 if (!dialog_queue->HasActiveDialog() || 156 if (!dialog_queue->HasActiveDialog() ||
239 !dialog_queue->active_dialog()->IsJavaScriptModalDialog() || 157 !dialog_queue->active_dialog()->IsJavaScriptModalDialog() ||
240 dialog_queue->active_dialog()->web_contents() != web_contents) { 158 dialog_queue->active_dialog()->web_contents() != web_contents) {
241 return false; 159 return false;
242 } 160 }
243 JavaScriptAppModalDialog* dialog = static_cast<JavaScriptAppModalDialog*>( 161 JavaScriptAppModalDialog* dialog = static_cast<JavaScriptAppModalDialog*>(
244 dialog_queue->active_dialog()); 162 dialog_queue->active_dialog());
245 if (accept) { 163 if (accept) {
246 if (prompt_override) 164 if (prompt_override)
247 dialog->SetOverridePromptText(*prompt_override); 165 dialog->SetOverridePromptText(*prompt_override);
248 dialog->native_dialog()->AcceptAppModalDialog(); 166 dialog->native_dialog()->AcceptAppModalDialog();
249 } else { 167 } else {
250 dialog->native_dialog()->CancelAppModalDialog(); 168 dialog->native_dialog()->CancelAppModalDialog();
251 } 169 }
252 return true; 170 return true;
253 } 171 }
254 172
255 void ChromeJavaScriptDialogManager::WebContentsDestroyed( 173 void JavaScriptDialogManagerImpl::WebContentsDestroyed(
256 WebContents* web_contents) { 174 WebContents* web_contents) {
257 CancelActiveAndPendingDialogs(web_contents); 175 CancelActiveAndPendingDialogs(web_contents);
258 javascript_dialog_extra_data_.erase(web_contents); 176 javascript_dialog_extra_data_.erase(web_contents);
259 } 177 }
260 178
261 base::string16 ChromeJavaScriptDialogManager::GetTitle( 179 void JavaScriptDialogManagerImpl::SetNativeDialogFactory(
180 scoped_ptr<JavaScriptNativeDialogFactory> factory) {
181 native_dialog_factory_ = factory.Pass();
182 }
183
184 void JavaScriptDialogManagerImpl::SetExtensionsClient(
185 scoped_ptr<JavaScriptDialogExtensionsClient> extensions_client) {
186 extensions_client_ = extensions_client.Pass();
187 }
188
189 base::string16 JavaScriptDialogManagerImpl::GetTitle(
262 WebContents* web_contents, 190 WebContents* web_contents,
263 const GURL& origin_url, 191 const GURL& origin_url,
264 const std::string& accept_lang, 192 const std::string& accept_lang,
265 bool is_alert) { 193 bool is_alert) {
266 // If the URL hasn't any host, return the default string. 194 // If the URL hasn't any host, return the default string.
267 if (!origin_url.has_host()) { 195 if (!origin_url.has_host()) {
268 return l10n_util::GetStringUTF16( 196 return l10n_util::GetStringUTF16(
269 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE 197 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
270 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); 198 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
271 } 199 }
272 200
273 // For extensions, show the extension name, but only if the origin of 201 // For extensions, show the extension name, but only if the origin of
274 // the alert matches the top-level WebContents. 202 // the alert matches the top-level WebContents.
275 #if defined(ENABLE_EXTENSIONS) 203 std::string name;
276 const Extension* extension = GetExtensionForWebContents(web_contents); 204 if (extensions_client_->GetExtensionName(web_contents, origin_url, &name)) {
msw 2014/11/05 03:53:55 nit: remove curly braces.
oshima 2014/11/05 21:42:19 Done.
277 if (extension && 205 return base::UTF8ToUTF16(name);
278 web_contents->GetLastCommittedURL().GetOrigin() == origin_url) {
279 return base::UTF8ToUTF16(extension->name());
280 } 206 }
281 #endif // defined(ENABLE_EXTENSIONS)
282 207
283 // Otherwise, return the formatted URL. 208 // Otherwise, return the formatted URL.
284 // In this case, force URL to have LTR directionality. 209 // In this case, force URL to have LTR directionality.
285 base::string16 url_string = net::FormatUrl(origin_url, accept_lang); 210 base::string16 url_string = net::FormatUrl(origin_url, accept_lang);
286 return l10n_util::GetStringFUTF16( 211 return l10n_util::GetStringFUTF16(
287 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE 212 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE
288 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE, 213 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
289 base::i18n::GetDisplayStringInLTRDirectionality(url_string)); 214 base::i18n::GetDisplayStringInLTRDirectionality(url_string));
290 } 215 }
291 216
292 void ChromeJavaScriptDialogManager::CancelActiveAndPendingDialogs( 217 void JavaScriptDialogManagerImpl::CancelActiveAndPendingDialogs(
293 WebContents* web_contents) { 218 WebContents* web_contents) {
294 AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance(); 219 AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance();
295 AppModalDialog* active_dialog = queue->active_dialog(); 220 AppModalDialog* active_dialog = queue->active_dialog();
296 if (active_dialog && active_dialog->web_contents() == web_contents) 221 if (active_dialog && active_dialog->web_contents() == web_contents)
297 active_dialog->Invalidate(); 222 active_dialog->Invalidate();
298 for (AppModalDialogQueue::iterator i = queue->begin(); 223 for (AppModalDialogQueue::iterator i = queue->begin();
299 i != queue->end(); ++i) { 224 i != queue->end(); ++i) {
300 if ((*i)->web_contents() == web_contents) 225 if ((*i)->web_contents() == web_contents)
301 (*i)->Invalidate(); 226 (*i)->Invalidate();
302 } 227 }
303 } 228 }
304 229
305 void ChromeJavaScriptDialogManager::OnDialogClosed( 230 void JavaScriptDialogManagerImpl::OnDialogClosed(
306 WebContents* web_contents, 231 WebContents* web_contents,
307 DialogClosedCallback callback, 232 DialogClosedCallback callback,
308 bool success, 233 bool success,
309 const base::string16& user_input) { 234 const base::string16& user_input) {
310 // If an extension opened this dialog then the extension may shut down its 235 // If an extension opened this dialog then the extension may shut down its
311 // lazy background page after the dialog closes. (Dialogs are closed before 236 // lazy background page after the dialog closes. (Dialogs are closed before
312 // their WebContents is destroyed so |web_contents| is still valid here.) 237 // their WebContents is destroyed so |web_contents| is still valid here.)
313 DecrementLazyKeepaliveCount(web_contents); 238 extensions_client_->DecrementLazyKeepaliveCount(web_contents);
314 239
315 callback.Run(success, user_input); 240 callback.Run(success, user_input);
316 } 241 }
317
318 } // namespace
319
320 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() {
321 return ChromeJavaScriptDialogManager::GetInstance();
322 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698