OLD | NEW |
| (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 "chrome/browser/ui/app_modal_dialogs/message_box_handler.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/memory/singleton.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "base/i18n/rtl.h" | |
13 #include "chrome/browser/extensions/extension_host.h" | |
14 #include "chrome/browser/extensions/extension_service.h" | |
15 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" | |
16 #include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" | |
17 #include "chrome/common/chrome_constants.h" | |
18 #include "chrome/common/chrome_notification_types.h" | |
19 #include "content/public/browser/notification_observer.h" | |
20 #include "content/public/browser/notification_registrar.h" | |
21 #include "content/public/browser/notification_service.h" | |
22 #include "content/public/common/content_client.h" | |
23 #include "grit/generated_resources.h" | |
24 #include "net/base/net_util.h" | |
25 #include "ui/base/javascript_message_type.h" | |
26 #include "ui/base/l10n/l10n_util.h" | |
27 | |
28 using content::JavaScriptDialogCreator; | |
29 using content::WebContents; | |
30 | |
31 class ChromeJavaScriptDialogCreator | |
32 : public JavaScriptDialogCreator, | |
33 public content::NotificationObserver { | |
34 public: | |
35 static ChromeJavaScriptDialogCreator* GetInstance(); | |
36 | |
37 explicit ChromeJavaScriptDialogCreator(ExtensionHost* extension_host); | |
38 virtual ~ChromeJavaScriptDialogCreator(); | |
39 | |
40 virtual void RunJavaScriptDialog( | |
41 WebContents* web_contents, | |
42 const GURL& origin_url, | |
43 const std::string& accept_lang, | |
44 ui::JavascriptMessageType javascript_message_type, | |
45 const string16& message_text, | |
46 const string16& default_prompt_text, | |
47 const DialogClosedCallback& callback, | |
48 bool* did_suppress_message) OVERRIDE; | |
49 | |
50 virtual void RunBeforeUnloadDialog( | |
51 WebContents* web_contents, | |
52 const string16& message_text, | |
53 bool is_reload, | |
54 const DialogClosedCallback& callback) OVERRIDE; | |
55 | |
56 virtual void ResetJavaScriptState(WebContents* web_contents) OVERRIDE; | |
57 | |
58 private: | |
59 explicit ChromeJavaScriptDialogCreator(); | |
60 | |
61 friend struct DefaultSingletonTraits<ChromeJavaScriptDialogCreator>; | |
62 | |
63 // content::NotificationObserver | |
64 virtual void Observe(int type, | |
65 const content::NotificationSource& source, | |
66 const content::NotificationDetails& details) OVERRIDE; | |
67 | |
68 string16 GetTitle(const GURL& origin_url, | |
69 const std::string& accept_lang, | |
70 bool is_alert); | |
71 | |
72 void CancelPendingDialogs(WebContents* web_contents); | |
73 | |
74 // Wrapper around a DialogClosedCallback so that we can intercept it before | |
75 // passing it onto the original callback. | |
76 void OnDialogClosed(DialogClosedCallback callback, | |
77 bool success, | |
78 const string16& user_input); | |
79 | |
80 // Mapping between the WebContents and their extra data. The key | |
81 // is a void* because the pointer is just a cookie and is never dereferenced. | |
82 typedef std::map<void*, ChromeJavaScriptDialogExtraData> | |
83 JavaScriptDialogExtraDataMap; | |
84 JavaScriptDialogExtraDataMap javascript_dialog_extra_data_; | |
85 | |
86 // Extension Host which owns the ChromeJavaScriptDialogCreator instance. | |
87 // It's used to get a extension name from a URL. | |
88 // If it's not owned by any Extension, it should be NULL. | |
89 ExtensionHost* extension_host_; | |
90 | |
91 content::NotificationRegistrar registrar_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(ChromeJavaScriptDialogCreator); | |
94 }; | |
95 | |
96 //------------------------------------------------------------------------------ | |
97 | |
98 ChromeJavaScriptDialogCreator::ChromeJavaScriptDialogCreator() | |
99 : extension_host_(NULL) { | |
100 } | |
101 | |
102 ChromeJavaScriptDialogCreator::~ChromeJavaScriptDialogCreator() { | |
103 extension_host_ = NULL; | |
104 } | |
105 | |
106 ChromeJavaScriptDialogCreator::ChromeJavaScriptDialogCreator( | |
107 ExtensionHost* extension_host) | |
108 : extension_host_(extension_host) { | |
109 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
110 content::Source<Profile>(extension_host_->profile())); | |
111 } | |
112 | |
113 /* static */ | |
114 ChromeJavaScriptDialogCreator* ChromeJavaScriptDialogCreator::GetInstance() { | |
115 return Singleton<ChromeJavaScriptDialogCreator>::get(); | |
116 } | |
117 | |
118 void ChromeJavaScriptDialogCreator::RunJavaScriptDialog( | |
119 WebContents* web_contents, | |
120 const GURL& origin_url, | |
121 const std::string& accept_lang, | |
122 ui::JavascriptMessageType javascript_message_type, | |
123 const string16& message_text, | |
124 const string16& default_prompt_text, | |
125 const DialogClosedCallback& callback, | |
126 bool* did_suppress_message) { | |
127 *did_suppress_message = false; | |
128 | |
129 ChromeJavaScriptDialogExtraData* extra_data = | |
130 &javascript_dialog_extra_data_[web_contents]; | |
131 | |
132 if (extra_data->suppress_javascript_messages_) { | |
133 *did_suppress_message = true; | |
134 return; | |
135 } | |
136 | |
137 base::TimeDelta time_since_last_message = base::TimeTicks::Now() - | |
138 extra_data->last_javascript_message_dismissal_; | |
139 bool display_suppress_checkbox = false; | |
140 // Show a checkbox offering to suppress further messages if this message is | |
141 // being displayed within kJavascriptMessageExpectedDelay of the last one. | |
142 if (time_since_last_message < | |
143 base::TimeDelta::FromMilliseconds( | |
144 chrome::kJavascriptMessageExpectedDelay)) { | |
145 display_suppress_checkbox = true; | |
146 } | |
147 | |
148 bool is_alert = javascript_message_type == ui::JAVASCRIPT_MESSAGE_TYPE_ALERT; | |
149 string16 dialog_title = GetTitle(origin_url, accept_lang, is_alert); | |
150 | |
151 if (extension_host_) | |
152 extension_host_->WillRunJavaScriptDialog(); | |
153 | |
154 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
155 web_contents, | |
156 extra_data, | |
157 dialog_title, | |
158 javascript_message_type, | |
159 message_text, | |
160 default_prompt_text, | |
161 display_suppress_checkbox, | |
162 false, // is_before_unload_dialog | |
163 false, // is_reload | |
164 base::Bind(&ChromeJavaScriptDialogCreator::OnDialogClosed, | |
165 base::Unretained(this), callback))); | |
166 } | |
167 | |
168 void ChromeJavaScriptDialogCreator::RunBeforeUnloadDialog( | |
169 WebContents* web_contents, | |
170 const string16& message_text, | |
171 bool is_reload, | |
172 const DialogClosedCallback& callback) { | |
173 ChromeJavaScriptDialogExtraData* extra_data = | |
174 &javascript_dialog_extra_data_[web_contents]; | |
175 | |
176 string16 title = l10n_util::GetStringUTF16( | |
177 is_reload ? | |
178 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : | |
179 IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); | |
180 string16 footer = l10n_util::GetStringUTF16( | |
181 is_reload ? | |
182 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : | |
183 IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); | |
184 | |
185 string16 full_message = message_text + ASCIIToUTF16("\n\n") + footer; | |
186 | |
187 if (extension_host_) | |
188 extension_host_->WillRunJavaScriptDialog(); | |
189 | |
190 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
191 web_contents, | |
192 extra_data, | |
193 title, | |
194 ui::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, | |
195 full_message, | |
196 string16(), // default_prompt_text | |
197 false, // display_suppress_checkbox | |
198 true, // is_before_unload_dialog | |
199 is_reload, | |
200 base::Bind(&ChromeJavaScriptDialogCreator::OnDialogClosed, | |
201 base::Unretained(this), callback))); | |
202 } | |
203 | |
204 void ChromeJavaScriptDialogCreator::ResetJavaScriptState( | |
205 WebContents* web_contents) { | |
206 CancelPendingDialogs(web_contents); | |
207 javascript_dialog_extra_data_.erase(web_contents); | |
208 } | |
209 | |
210 void ChromeJavaScriptDialogCreator::Observe( | |
211 int type, | |
212 const content::NotificationSource& source, | |
213 const content::NotificationDetails& details) { | |
214 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED); | |
215 extension_host_ = NULL; | |
216 } | |
217 | |
218 string16 ChromeJavaScriptDialogCreator::GetTitle(const GURL& origin_url, | |
219 const std::string& accept_lang, | |
220 bool is_alert) { | |
221 // If the URL hasn't any host, return the default string. | |
222 if (!origin_url.has_host()) { | |
223 return l10n_util::GetStringUTF16( | |
224 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE | |
225 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); | |
226 } | |
227 | |
228 // If the URL is a chrome extension one, return the extension name. | |
229 if (extension_host_) { | |
230 const Extension* extension = extension_host_-> | |
231 profile()->GetExtensionService()->extensions()-> | |
232 GetExtensionOrAppByURL(ExtensionURLInfo(origin_url)); | |
233 if (extension) { | |
234 return UTF8ToUTF16(base::StringPiece(extension->name())); | |
235 } | |
236 } | |
237 | |
238 // Otherwise, return the formatted URL. | |
239 // In this case, force URL to have LTR directionality. | |
240 string16 url_string = net::FormatUrl(origin_url, accept_lang); | |
241 return l10n_util::GetStringFUTF16( | |
242 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE | |
243 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE, | |
244 base::i18n::GetDisplayStringInLTRDirectionality(url_string)); | |
245 } | |
246 | |
247 void ChromeJavaScriptDialogCreator::CancelPendingDialogs( | |
248 WebContents* web_contents) { | |
249 AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance(); | |
250 AppModalDialog* active_dialog = queue->active_dialog(); | |
251 if (active_dialog && active_dialog->web_contents() == web_contents) | |
252 active_dialog->Invalidate(); | |
253 for (AppModalDialogQueue::iterator i = queue->begin(); | |
254 i != queue->end(); ++i) { | |
255 if ((*i)->web_contents() == web_contents) | |
256 (*i)->Invalidate(); | |
257 } | |
258 } | |
259 | |
260 void ChromeJavaScriptDialogCreator::OnDialogClosed( | |
261 DialogClosedCallback callback, bool success, const string16& user_input) { | |
262 if (extension_host_) | |
263 extension_host_->DidCloseJavaScriptDialog(); | |
264 callback.Run(success, user_input); | |
265 } | |
266 | |
267 //------------------------------------------------------------------------------ | |
268 | |
269 content::JavaScriptDialogCreator* GetJavaScriptDialogCreatorInstance() { | |
270 return ChromeJavaScriptDialogCreator::GetInstance(); | |
271 } | |
272 | |
273 content::JavaScriptDialogCreator* CreateJavaScriptDialogCreatorInstance( | |
274 ExtensionHost* extension_host) { | |
275 return new ChromeJavaScriptDialogCreator(extension_host); | |
276 } | |
OLD | NEW |