Chromium Code Reviews| Index: chrome/browser/ui/android/javascript_app_modal_dialog_android.cc |
| diff --git a/chrome/browser/ui/android/javascript_app_modal_dialog_android.cc b/chrome/browser/ui/android/javascript_app_modal_dialog_android.cc |
| index 1bd472900a2fbf8d19316b6cacb8e7b0f2b50ed3..af0d10aab68df2975ae8b17479c2f155f7e82bd4 100644 |
| --- a/chrome/browser/ui/android/javascript_app_modal_dialog_android.cc |
| +++ b/chrome/browser/ui/android/javascript_app_modal_dialog_android.cc |
| @@ -2,14 +2,148 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" |
| +#include "chrome/browser/ui/android/javascript_app_modal_dialog_android.h" |
| -#include "base/logging.h" |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/utf_string_conversions.h" |
| + |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
| +#include "content/public/browser/android/content_view_core.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/common/javascript_message_type.h" |
| +#include "jni/JavascriptAppModalDialog_jni.h" |
| + |
| +using base::android::AttachCurrentThread; |
| +using base::android::ConvertUTF16ToJavaString; |
| +using base::android::ScopedJavaLocalRef; |
| +using content::BrowserThread; |
| +using content::ContentViewCore; |
| // static |
| NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( |
| JavaScriptAppModalDialog* dialog, |
| gfx::NativeWindow parent_window) { |
| - NOTIMPLEMENTED() << "TODO(benm): Upstream JsModalDialogAndroid"; |
| - return NULL; |
| + return new JavascriptAppModalDialogAndroid(AttachCurrentThread(), |
| + dialog, parent_window); |
|
joth
2012/08/24 23:09:56
wrong indent
Yaron
2012/08/28 20:26:40
Done.
Yaron
2012/08/28 20:26:40
Done.
|
| +} |
| + |
| +JavascriptAppModalDialogAndroid::JavascriptAppModalDialogAndroid( |
| + JNIEnv* env, |
| + JavaScriptAppModalDialog* dialog, |
| + gfx::NativeWindow parent) |
| + : dialog_(dialog), |
| + parent_jobject_weak_ref(env, parent->GetJavaObject().obj()) { |
|
joth
2012/08/24 23:09:56
parent_jobject_weak_ref_ (..needs trailing undersc
Yaron
2012/08/28 20:26:40
Done.
|
| +} |
| + |
| +JavascriptAppModalDialogAndroid::~JavascriptAppModalDialogAndroid() { |
| + JNIEnv* env = AttachCurrentThread(); |
| + // In case the dialog is still displaying, tell it to close itself. |
| + // This can happen if you trigger a dialog but close the Tab before it's |
| + // shown, and then accept the dialog. |
| + if (!dialog_jobject_.is_null()) |
| + Java_JavascriptAppModalDialog_dismiss(env, dialog_jobject_.obj()); |
| +} |
| + |
| +int JavascriptAppModalDialogAndroid::GetAppModalDialogButtons() const { |
| + NOTIMPLEMENTED(); |
| + return 0; |
| +} |
| + |
| +void JavascriptAppModalDialogAndroid::ShowAppModalDialog() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + JNIEnv* env = AttachCurrentThread(); |
| + // Keep a strong ref to the parent window while we make the call to java to |
| + // display the dialog. |
| + ScopedJavaLocalRef<jobject> parent_jobj = parent_jobject_weak_ref.get( |
| + env); |
| + if (parent_jobj.is_null()) { |
| + CancelAppModalDialog(); |
| + return; |
| + } |
| + |
| + ScopedJavaLocalRef<jobject> dialog_object; |
| + ScopedJavaLocalRef<jstring> title = |
| + ConvertUTF16ToJavaString(env, dialog_->title()); |
| + ScopedJavaLocalRef<jstring> message = |
| + ConvertUTF16ToJavaString(env, dialog_->message_text()); |
| + |
| + switch (dialog_->javascript_message_type()) { |
| + case content::JAVASCRIPT_MESSAGE_TYPE_ALERT: { |
| + dialog_object = Java_JavascriptAppModalDialog_createAlertDialog(env, |
| + title.obj(), message.obj(), |
| + dialog_->display_suppress_checkbox()); |
| + break; |
| + } |
| + case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM: { |
| + if (dialog_->is_before_unload_dialog()) { |
| + dialog_object = Java_JavascriptAppModalDialog_createBeforeUnloadDialog( |
| + env, title.obj(), message.obj(), dialog_->is_reload(), |
| + dialog_->display_suppress_checkbox()); |
| + } else { |
| + dialog_object = Java_JavascriptAppModalDialog_createConfirmDialog(env, |
| + title.obj(), message.obj(), |
| + dialog_->display_suppress_checkbox()); |
| + } |
| + break; |
| + } |
| + case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT: { |
| + ScopedJavaLocalRef<jstring> default_prompt_text = |
| + ConvertUTF16ToJavaString(env, dialog_->default_prompt_text()); |
| + dialog_object = Java_JavascriptAppModalDialog_createPromptDialog(env, |
| + title.obj(), message.obj(), |
| + dialog_->display_suppress_checkbox(), default_prompt_text.obj()); |
| + break; |
| + } |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| + // Keep a ref to the java side object until we get a confirm or cancel. |
| + dialog_jobject_.Reset(dialog_object); |
| + |
| + Java_JavascriptAppModalDialog_showJavascriptAppModalDialog(env, |
| + dialog_object.obj(), parent_jobj.obj(), |
| + reinterpret_cast<jint>(this)); |
| +} |
| + |
| +void JavascriptAppModalDialogAndroid::ActivateAppModalDialog() { |
| + ShowAppModalDialog(); |
| +} |
| + |
| +void JavascriptAppModalDialogAndroid::CloseAppModalDialog() { |
| + CancelAppModalDialog(); |
| +} |
| + |
| +void JavascriptAppModalDialogAndroid::AcceptAppModalDialog() { |
| + string16 prompt_text; |
| + dialog_->OnAccept(prompt_text, false); |
| + delete this; |
| +} |
| + |
| +void JavascriptAppModalDialogAndroid::DidAcceptAppModalDialog( |
| + JNIEnv* env, jobject, jstring prompt, bool should_suppress_js_dialogs) { |
| + string16 prompt_text = base::android::ConvertJavaStringToUTF16(env, prompt); |
| + dialog_->OnAccept(prompt_text, should_suppress_js_dialogs); |
| + delete this; |
| +} |
| + |
| +void JavascriptAppModalDialogAndroid::CancelAppModalDialog() { |
| + dialog_->OnCancel(false); |
| + delete this; |
| +} |
| + |
| +void JavascriptAppModalDialogAndroid::DidCancelAppModalDialog( |
| + JNIEnv* env, jobject, bool should_suppress_js_dialogs) { |
| + dialog_->OnCancel(should_suppress_js_dialogs); |
| + delete this; |
| +} |
| + |
| +// static |
| +bool JavascriptAppModalDialogAndroid::RegisterJavascriptAppModalDialog( |
| + JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| } |