| OLD | NEW |
| 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_creator.h" | 5 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_creator.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| 11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/extensions/extension_host.h" | 13 #include "chrome/browser/extensions/extension_host.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" | 15 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" |
| 16 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" | 16 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
| 17 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
| 20 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
| 21 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/common/content_client.h" | 22 #include "content/public/common/content_client.h" |
| 23 #include "content/public/common/javascript_message_type.h" | 23 #include "content/public/common/javascript_message_type.h" |
| 24 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 25 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 27 | 27 |
| 28 #if defined(OS_ANDROID) |
| 29 #include "base/android/jni_android.h" |
| 30 #include "base/android/scoped_java_ref.h" |
| 31 #include "chrome/browser/android/js_result_handler.h" |
| 32 #include "content/public/browser/android/content_view_core.h" |
| 33 |
| 34 using base::android::ScopedJavaLocalRef; |
| 35 using content::ContentViewCore; |
| 36 #endif |
| 37 |
| 28 using content::JavaScriptDialogCreator; | 38 using content::JavaScriptDialogCreator; |
| 29 using content::WebContents; | 39 using content::WebContents; |
| 30 | 40 |
| 31 namespace { | 41 namespace { |
| 32 | 42 |
| 33 class ChromeJavaScriptDialogCreator : public JavaScriptDialogCreator, | 43 class ChromeJavaScriptDialogCreator : public JavaScriptDialogCreator, |
| 34 public content::NotificationObserver { | 44 public content::NotificationObserver { |
| 35 public: | 45 public: |
| 36 static ChromeJavaScriptDialogCreator* GetInstance(); | 46 static ChromeJavaScriptDialogCreator* GetInstance(); |
| 37 | 47 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 66 virtual void Observe(int type, | 76 virtual void Observe(int type, |
| 67 const content::NotificationSource& source, | 77 const content::NotificationSource& source, |
| 68 const content::NotificationDetails& details) OVERRIDE; | 78 const content::NotificationDetails& details) OVERRIDE; |
| 69 | 79 |
| 70 string16 GetTitle(const GURL& origin_url, | 80 string16 GetTitle(const GURL& origin_url, |
| 71 const std::string& accept_lang, | 81 const std::string& accept_lang, |
| 72 bool is_alert); | 82 bool is_alert); |
| 73 | 83 |
| 74 void CancelPendingDialogs(WebContents* web_contents); | 84 void CancelPendingDialogs(WebContents* web_contents); |
| 75 | 85 |
| 86 void EnqueueDialog(JavaScriptAppModalDialog* dialog); |
| 87 |
| 76 // Wrapper around a DialogClosedCallback so that we can intercept it before | 88 // Wrapper around a DialogClosedCallback so that we can intercept it before |
| 77 // passing it onto the original callback. | 89 // passing it onto the original callback. |
| 78 void OnDialogClosed(DialogClosedCallback callback, | 90 void OnDialogClosed(DialogClosedCallback callback, |
| 79 bool success, | 91 bool success, |
| 80 const string16& user_input); | 92 const string16& user_input); |
| 81 | 93 |
| 82 // Mapping between the WebContents and their extra data. The key | 94 // Mapping between the WebContents and their extra data. The key |
| 83 // is a void* because the pointer is just a cookie and is never dereferenced. | 95 // is a void* because the pointer is just a cookie and is never dereferenced. |
| 84 typedef std::map<void*, ChromeJavaScriptDialogExtraData> | 96 typedef std::map<void*, ChromeJavaScriptDialogExtraData> |
| 85 JavaScriptDialogExtraDataMap; | 97 JavaScriptDialogExtraDataMap; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 // being displayed within kJavascriptMessageExpectedDelay of the last one. | 156 // being displayed within kJavascriptMessageExpectedDelay of the last one. |
| 145 if (time_since_last_message < | 157 if (time_since_last_message < |
| 146 base::TimeDelta::FromMilliseconds( | 158 base::TimeDelta::FromMilliseconds( |
| 147 chrome::kJavascriptMessageExpectedDelay)) { | 159 chrome::kJavascriptMessageExpectedDelay)) { |
| 148 display_suppress_checkbox = true; | 160 display_suppress_checkbox = true; |
| 149 } | 161 } |
| 150 | 162 |
| 151 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; | 163 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; |
| 152 string16 dialog_title = GetTitle(origin_url, accept_lang, is_alert); | 164 string16 dialog_title = GetTitle(origin_url, accept_lang, is_alert); |
| 153 | 165 |
| 154 if (extension_host_) | 166 JavaScriptAppModalDialog* dialog = new JavaScriptAppModalDialog( |
| 155 extension_host_->WillRunJavaScriptDialog(); | |
| 156 | |
| 157 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
| 158 web_contents, | 167 web_contents, |
| 159 extra_data, | 168 extra_data, |
| 160 dialog_title, | 169 dialog_title, |
| 161 message_type, | 170 message_type, |
| 162 message_text, | 171 message_text, |
| 163 default_prompt_text, | 172 default_prompt_text, |
| 164 display_suppress_checkbox, | 173 display_suppress_checkbox, |
| 165 false, // is_before_unload_dialog | 174 false, // is_before_unload_dialog |
| 166 false, // is_reload | 175 false, // is_reload |
| 167 base::Bind(&ChromeJavaScriptDialogCreator::OnDialogClosed, | 176 base::Bind(&ChromeJavaScriptDialogCreator::OnDialogClosed, |
| 168 base::Unretained(this), callback))); | 177 base::Unretained(this), callback)); |
| 178 |
| 179 EnqueueDialog(dialog); |
| 169 } | 180 } |
| 170 | 181 |
| 171 void ChromeJavaScriptDialogCreator::RunBeforeUnloadDialog( | 182 void ChromeJavaScriptDialogCreator::RunBeforeUnloadDialog( |
| 172 WebContents* web_contents, | 183 WebContents* web_contents, |
| 173 const string16& message_text, | 184 const string16& message_text, |
| 174 bool is_reload, | 185 bool is_reload, |
| 175 const DialogClosedCallback& callback) { | 186 const DialogClosedCallback& callback) { |
| 176 ChromeJavaScriptDialogExtraData* extra_data = | 187 ChromeJavaScriptDialogExtraData* extra_data = |
| 177 &javascript_dialog_extra_data_[web_contents]; | 188 &javascript_dialog_extra_data_[web_contents]; |
| 178 | 189 |
| 179 const string16 title = l10n_util::GetStringUTF16(is_reload ? | 190 const string16 title = l10n_util::GetStringUTF16(is_reload ? |
| 180 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); | 191 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); |
| 181 const string16 footer = l10n_util::GetStringUTF16(is_reload ? | 192 const string16 footer = l10n_util::GetStringUTF16(is_reload ? |
| 182 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); | 193 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); |
| 183 | 194 |
| 184 string16 full_message = message_text + ASCIIToUTF16("\n\n") + footer; | 195 string16 full_message = message_text + ASCIIToUTF16("\n\n") + footer; |
| 185 | 196 |
| 186 if (extension_host_) | 197 JavaScriptAppModalDialog* dialog = new JavaScriptAppModalDialog( |
| 187 extension_host_->WillRunJavaScriptDialog(); | |
| 188 | |
| 189 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
| 190 web_contents, | 198 web_contents, |
| 191 extra_data, | 199 extra_data, |
| 192 title, | 200 title, |
| 193 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, | 201 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, |
| 194 full_message, | 202 full_message, |
| 195 string16(), // default_prompt_text | 203 string16(), // default_prompt_text |
| 196 false, // display_suppress_checkbox | 204 false, // display_suppress_checkbox |
| 197 true, // is_before_unload_dialog | 205 true, // is_before_unload_dialog |
| 198 is_reload, | 206 is_reload, |
| 199 base::Bind(&ChromeJavaScriptDialogCreator::OnDialogClosed, | 207 base::Bind(&ChromeJavaScriptDialogCreator::OnDialogClosed, |
| 200 base::Unretained(this), callback))); | 208 base::Unretained(this), callback)); |
| 209 EnqueueDialog(dialog); |
| 210 } |
| 211 |
| 212 void ChromeJavaScriptDialogCreator::EnqueueDialog( |
| 213 JavaScriptAppModalDialog* dialog) { |
| 214 #if defined(OS_ANDROID) |
| 215 content::ContentViewCore* content_view_core = |
| 216 ContentViewCore::FromWebContents(dialog->web_contents()); |
| 217 JNIEnv* env = base::android::AttachCurrentThread(); |
| 218 ScopedJavaLocalRef<jobject> js_results_handler = createJsResultHandler( |
| 219 env, |
| 220 dialog); |
| 221 bool should_use_native = content_view_core-> |
| 222 ShouldUseNativeJsModalDialog(dialog->javascript_message_type(), |
| 223 dialog->is_before_unload_dialog(), |
| 224 dialog->message_text(), |
| 225 dialog->default_prompt_text(), |
| 226 js_results_handler.obj()); |
| 227 if (!should_use_native |
| 228 || clientHasCalledConfirmOrCancel(env, js_results_handler.obj())) |
| 229 return; |
| 230 setJsResultHandledByNative(env, js_results_handler.obj()); |
| 231 #else |
| 232 if (extension_host_) |
| 233 extension_host_->WillRunJavaScriptDialog(); |
| 234 #endif |
| 235 |
| 236 AppModalDialogQueue::GetInstance()->AddDialog(dialog); |
| 201 } | 237 } |
| 202 | 238 |
| 203 void ChromeJavaScriptDialogCreator::ResetJavaScriptState( | 239 void ChromeJavaScriptDialogCreator::ResetJavaScriptState( |
| 204 WebContents* web_contents) { | 240 WebContents* web_contents) { |
| 205 CancelPendingDialogs(web_contents); | 241 CancelPendingDialogs(web_contents); |
| 206 javascript_dialog_extra_data_.erase(web_contents); | 242 javascript_dialog_extra_data_.erase(web_contents); |
| 207 } | 243 } |
| 208 | 244 |
| 209 void ChromeJavaScriptDialogCreator::Observe( | 245 void ChromeJavaScriptDialogCreator::Observe( |
| 210 int type, | 246 int type, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 } // namespace | 304 } // namespace |
| 269 | 305 |
| 270 content::JavaScriptDialogCreator* GetJavaScriptDialogCreatorInstance() { | 306 content::JavaScriptDialogCreator* GetJavaScriptDialogCreatorInstance() { |
| 271 return ChromeJavaScriptDialogCreator::GetInstance(); | 307 return ChromeJavaScriptDialogCreator::GetInstance(); |
| 272 } | 308 } |
| 273 | 309 |
| 274 content::JavaScriptDialogCreator* CreateJavaScriptDialogCreatorInstance( | 310 content::JavaScriptDialogCreator* CreateJavaScriptDialogCreatorInstance( |
| 275 extensions::ExtensionHost* extension_host) { | 311 extensions::ExtensionHost* extension_host) { |
| 276 return new ChromeJavaScriptDialogCreator(extension_host); | 312 return new ChromeJavaScriptDialogCreator(extension_host); |
| 277 } | 313 } |
| OLD | NEW |