| 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/native_app_modal_dialog.h" | 5 #include "chrome/browser/ui/android/javascript_app_modal_dialog_android.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 |
| 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
| 13 #include "content/public/browser/android/content_view_core.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/common/javascript_message_type.h" |
| 17 #include "jni/JavascriptAppModalDialog_jni.h" |
| 18 |
| 19 using base::android::AttachCurrentThread; |
| 20 using base::android::ConvertUTF16ToJavaString; |
| 21 using base::android::ScopedJavaLocalRef; |
| 22 using content::BrowserThread; |
| 23 using content::ContentViewCore; |
| 8 | 24 |
| 9 // static | 25 // static |
| 10 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( | 26 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( |
| 11 JavaScriptAppModalDialog* dialog, | 27 JavaScriptAppModalDialog* dialog, |
| 12 gfx::NativeWindow parent_window) { | 28 gfx::NativeWindow parent_window) { |
| 13 NOTIMPLEMENTED() << "TODO(benm): Upstream JsModalDialogAndroid"; | 29 return new JavascriptAppModalDialogAndroid(AttachCurrentThread(), |
| 14 return NULL; | 30 dialog, parent_window); |
| 15 } | 31 } |
| 32 |
| 33 JavascriptAppModalDialogAndroid::JavascriptAppModalDialogAndroid( |
| 34 JNIEnv* env, |
| 35 JavaScriptAppModalDialog* dialog, |
| 36 gfx::NativeWindow parent) |
| 37 : dialog_(dialog), |
| 38 parent_jobject_weak_ref_(env, parent->GetJavaObject().obj()) { |
| 39 } |
| 40 |
| 41 JavascriptAppModalDialogAndroid::~JavascriptAppModalDialogAndroid() { |
| 42 JNIEnv* env = AttachCurrentThread(); |
| 43 // In case the dialog is still displaying, tell it to close itself. |
| 44 // This can happen if you trigger a dialog but close the Tab before it's |
| 45 // shown, and then accept the dialog. |
| 46 if (!dialog_jobject_.is_null()) |
| 47 Java_JavascriptAppModalDialog_dismiss(env, dialog_jobject_.obj()); |
| 48 } |
| 49 |
| 50 int JavascriptAppModalDialogAndroid::GetAppModalDialogButtons() const { |
| 51 NOTIMPLEMENTED(); |
| 52 return 0; |
| 53 } |
| 54 |
| 55 void JavascriptAppModalDialogAndroid::ShowAppModalDialog() { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 57 |
| 58 JNIEnv* env = AttachCurrentThread(); |
| 59 // Keep a strong ref to the parent window while we make the call to java to |
| 60 // display the dialog. |
| 61 ScopedJavaLocalRef<jobject> parent_jobj = parent_jobject_weak_ref_.get( |
| 62 env); |
| 63 if (parent_jobj.is_null()) { |
| 64 CancelAppModalDialog(); |
| 65 return; |
| 66 } |
| 67 |
| 68 ScopedJavaLocalRef<jobject> dialog_object; |
| 69 ScopedJavaLocalRef<jstring> title = |
| 70 ConvertUTF16ToJavaString(env, dialog_->title()); |
| 71 ScopedJavaLocalRef<jstring> message = |
| 72 ConvertUTF16ToJavaString(env, dialog_->message_text()); |
| 73 |
| 74 switch (dialog_->javascript_message_type()) { |
| 75 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: { |
| 76 dialog_object = Java_JavascriptAppModalDialog_createAlertDialog(env, |
| 77 title.obj(), message.obj(), |
| 78 dialog_->display_suppress_checkbox()); |
| 79 break; |
| 80 } |
| 81 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: { |
| 82 if (dialog_->is_before_unload_dialog()) { |
| 83 dialog_object = Java_JavascriptAppModalDialog_createBeforeUnloadDialog( |
| 84 env, title.obj(), message.obj(), dialog_->is_reload(), |
| 85 dialog_->display_suppress_checkbox()); |
| 86 } else { |
| 87 dialog_object = Java_JavascriptAppModalDialog_createConfirmDialog(env, |
| 88 title.obj(), message.obj(), |
| 89 dialog_->display_suppress_checkbox()); |
| 90 } |
| 91 break; |
| 92 } |
| 93 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT: { |
| 94 ScopedJavaLocalRef<jstring> default_prompt_text = |
| 95 ConvertUTF16ToJavaString(env, dialog_->default_prompt_text()); |
| 96 dialog_object = Java_JavascriptAppModalDialog_createPromptDialog(env, |
| 97 title.obj(), message.obj(), |
| 98 dialog_->display_suppress_checkbox(), default_prompt_text.obj()); |
| 99 break; |
| 100 } |
| 101 default: |
| 102 NOTREACHED(); |
| 103 } |
| 104 |
| 105 // Keep a ref to the java side object until we get a confirm or cancel. |
| 106 dialog_jobject_.Reset(dialog_object); |
| 107 |
| 108 Java_JavascriptAppModalDialog_showJavascriptAppModalDialog(env, |
| 109 dialog_object.obj(), parent_jobj.obj(), |
| 110 reinterpret_cast<jint>(this)); |
| 111 } |
| 112 |
| 113 void JavascriptAppModalDialogAndroid::ActivateAppModalDialog() { |
| 114 ShowAppModalDialog(); |
| 115 } |
| 116 |
| 117 void JavascriptAppModalDialogAndroid::CloseAppModalDialog() { |
| 118 CancelAppModalDialog(); |
| 119 } |
| 120 |
| 121 void JavascriptAppModalDialogAndroid::AcceptAppModalDialog() { |
| 122 string16 prompt_text; |
| 123 dialog_->OnAccept(prompt_text, false); |
| 124 delete this; |
| 125 } |
| 126 |
| 127 void JavascriptAppModalDialogAndroid::DidAcceptAppModalDialog( |
| 128 JNIEnv* env, jobject, jstring prompt, bool should_suppress_js_dialogs) { |
| 129 string16 prompt_text = base::android::ConvertJavaStringToUTF16(env, prompt); |
| 130 dialog_->OnAccept(prompt_text, should_suppress_js_dialogs); |
| 131 delete this; |
| 132 } |
| 133 |
| 134 void JavascriptAppModalDialogAndroid::CancelAppModalDialog() { |
| 135 dialog_->OnCancel(false); |
| 136 delete this; |
| 137 } |
| 138 |
| 139 void JavascriptAppModalDialogAndroid::DidCancelAppModalDialog( |
| 140 JNIEnv* env, jobject, bool should_suppress_js_dialogs) { |
| 141 dialog_->OnCancel(should_suppress_js_dialogs); |
| 142 delete this; |
| 143 } |
| 144 |
| 145 // static |
| 146 bool JavascriptAppModalDialogAndroid::RegisterJavascriptAppModalDialog( |
| 147 JNIEnv* env) { |
| 148 return RegisterNativesImpl(env); |
| 149 } |
| OLD | NEW |