| 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 if (parent_jobject_weak_ref.get(env).is_null()) { |
| 60 CancelAppModalDialog(); |
| 61 return; |
| 62 } |
| 63 |
| 64 // Keep a strong ref to the ContentViewCore while we make the call |
| 65 // to java to display the dialog. |
| 66 ScopedJavaLocalRef<jobject> content_view_core = |
| 67 ContentViewCore::FromWebContents(dialog_->web_contents()) |
| 68 ->GetJavaObject(); |
| 69 if (!content_view_core.obj()) |
| 70 return; |
| 71 |
| 72 ScopedJavaLocalRef<jobject> dialog_object; |
| 73 ScopedJavaLocalRef<jstring> title = |
| 74 ConvertUTF16ToJavaString(env, dialog_->title()); |
| 75 ScopedJavaLocalRef<jstring> message = |
| 76 ConvertUTF16ToJavaString(env, dialog_->message_text()); |
| 77 |
| 78 switch (dialog_->javascript_message_type()) { |
| 79 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: { |
| 80 dialog_object = Java_JavascriptAppModalDialog_createAlertDialog(env, |
| 81 title.obj(), message.obj(), |
| 82 dialog_->display_suppress_checkbox()); |
| 83 break; |
| 84 } |
| 85 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: { |
| 86 if (dialog_->is_before_unload_dialog()) { |
| 87 dialog_object = Java_JavascriptAppModalDialog_createBeforeUnloadDialog( |
| 88 env, title.obj(), message.obj(), dialog_->is_reload(), |
| 89 dialog_->display_suppress_checkbox()); |
| 90 } else { |
| 91 dialog_object = Java_JavascriptAppModalDialog_createConfirmDialog(env, |
| 92 title.obj(), message.obj(), |
| 93 dialog_->display_suppress_checkbox()); |
| 94 } |
| 95 break; |
| 96 } |
| 97 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT: { |
| 98 ScopedJavaLocalRef<jstring> default_prompt_text = |
| 99 ConvertUTF16ToJavaString(env, dialog_->default_prompt_text()); |
| 100 dialog_object = Java_JavascriptAppModalDialog_createPromptDialog(env, |
| 101 title.obj(), message.obj(), |
| 102 dialog_->display_suppress_checkbox(), default_prompt_text.obj()); |
| 103 break; |
| 104 } |
| 105 default: |
| 106 NOTREACHED(); |
| 107 } |
| 108 |
| 109 // Keep a ref to the java side object until we get a confirm or cancel. |
| 110 dialog_jobject_.Reset(dialog_object); |
| 111 |
| 112 Java_JavascriptAppModalDialog_showJavascriptAppModalDialog(env, |
| 113 dialog_object.obj(), content_view_core.obj(), |
| 114 reinterpret_cast<jint>(this)); |
| 115 } |
| 116 |
| 117 void JavascriptAppModalDialogAndroid::ActivateAppModalDialog() { |
| 118 ShowAppModalDialog(); |
| 119 } |
| 120 |
| 121 void JavascriptAppModalDialogAndroid::CloseAppModalDialog() { |
| 122 CancelAppModalDialog(); |
| 123 } |
| 124 |
| 125 void JavascriptAppModalDialogAndroid::AcceptAppModalDialog() { |
| 126 string16 prompt_text; |
| 127 dialog_->OnAccept(prompt_text, false); |
| 128 delete this; |
| 129 } |
| 130 |
| 131 void JavascriptAppModalDialogAndroid::DidAcceptAppModalDialog( |
| 132 JNIEnv* env, jobject, jstring prompt, bool should_suppress_js_dialogs) { |
| 133 string16 prompt_text = base::android::ConvertJavaStringToUTF16(env, prompt); |
| 134 dialog_->OnAccept(prompt_text, should_suppress_js_dialogs); |
| 135 delete this; |
| 136 } |
| 137 |
| 138 void JavascriptAppModalDialogAndroid::CancelAppModalDialog() { |
| 139 dialog_->OnCancel(false); |
| 140 delete this; |
| 141 } |
| 142 |
| 143 void JavascriptAppModalDialogAndroid::DidCancelAppModalDialog( |
| 144 JNIEnv* env, jobject, bool should_suppress_js_dialogs) { |
| 145 dialog_->OnCancel(should_suppress_js_dialogs); |
| 146 delete this; |
| 147 } |
| 148 |
| 149 // static |
| 150 bool JavascriptAppModalDialogAndroid::RegisterJavascriptAppModalDialog( |
| 151 JNIEnv* env) { |
| 152 return RegisterNativesImpl(env); |
| 153 } |
| OLD | NEW |