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.
5 #include "chrome/browser/ui/android/javascript_app_modal_dialog_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "chrome/browser/ui/app_modal/chrome_javascript_native_dialog_factory.h"
10 #include "components/app_modal/app_modal_dialog_queue.h"
11 #include "components/app_modal/javascript_app_modal_dialog.h"
12 #include "components/app_modal/javascript_dialog_manager.h"
13 #include "components/app_modal/javascript_native_dialog_factory.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "content/public/common/javascript_message_type.h"
18 #include "jni/JavascriptAppModalDialog_jni.h"
19 #include "ui/android/window_android.h"
21 using base::android::AttachCurrentThread
;
22 using base::android::ConvertUTF16ToJavaString
;
23 using base::android::ScopedJavaGlobalRef
;
24 using base::android::ScopedJavaLocalRef
;
26 JavascriptAppModalDialogAndroid::JavascriptAppModalDialogAndroid(
28 app_modal::JavaScriptAppModalDialog
* dialog
,
29 gfx::NativeWindow parent
)
31 parent_jobject_weak_ref_(env
, parent
->GetJavaObject().obj()) {
34 int JavascriptAppModalDialogAndroid::GetAppModalDialogButtons() const {
39 void JavascriptAppModalDialogAndroid::ShowAppModalDialog() {
40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
42 JNIEnv
* env
= AttachCurrentThread();
43 // Keep a strong ref to the parent window while we make the call to java to
44 // display the dialog.
45 ScopedJavaLocalRef
<jobject
> parent_jobj
= parent_jobject_weak_ref_
.get(env
);
46 if (parent_jobj
.is_null()) {
47 CancelAppModalDialog();
51 ScopedJavaLocalRef
<jobject
> dialog_object
;
52 ScopedJavaLocalRef
<jstring
> title
=
53 ConvertUTF16ToJavaString(env
, dialog_
->title());
54 ScopedJavaLocalRef
<jstring
> message
=
55 ConvertUTF16ToJavaString(env
, dialog_
->message_text());
57 switch (dialog_
->javascript_message_type()) {
58 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT
: {
59 dialog_object
= Java_JavascriptAppModalDialog_createAlertDialog(env
,
60 title
.obj(), message
.obj(),
61 dialog_
->display_suppress_checkbox());
64 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM
: {
65 if (dialog_
->is_before_unload_dialog()) {
66 dialog_object
= Java_JavascriptAppModalDialog_createBeforeUnloadDialog(
67 env
, title
.obj(), message
.obj(), dialog_
->is_reload(),
68 dialog_
->display_suppress_checkbox());
70 dialog_object
= Java_JavascriptAppModalDialog_createConfirmDialog(env
,
71 title
.obj(), message
.obj(),
72 dialog_
->display_suppress_checkbox());
76 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT
: {
77 ScopedJavaLocalRef
<jstring
> default_prompt_text
=
78 ConvertUTF16ToJavaString(env
, dialog_
->default_prompt_text());
79 dialog_object
= Java_JavascriptAppModalDialog_createPromptDialog(env
,
80 title
.obj(), message
.obj(),
81 dialog_
->display_suppress_checkbox(), default_prompt_text
.obj());
88 // Keep a ref to the java side object until we get a confirm or cancel.
89 dialog_jobject_
.Reset(dialog_object
);
91 Java_JavascriptAppModalDialog_showJavascriptAppModalDialog(env
,
92 dialog_object
.obj(), parent_jobj
.obj(),
93 reinterpret_cast<intptr_t>(this));
96 void JavascriptAppModalDialogAndroid::ActivateAppModalDialog() {
100 void JavascriptAppModalDialogAndroid::CloseAppModalDialog() {
101 CancelAppModalDialog();
104 void JavascriptAppModalDialogAndroid::AcceptAppModalDialog() {
105 base::string16 prompt_text
;
106 dialog_
->OnAccept(prompt_text
, false);
110 void JavascriptAppModalDialogAndroid::DidAcceptAppModalDialog(
111 JNIEnv
* env
, jobject
, jstring prompt
, bool should_suppress_js_dialogs
) {
112 base::string16 prompt_text
=
113 base::android::ConvertJavaStringToUTF16(env
, prompt
);
114 dialog_
->OnAccept(prompt_text
, should_suppress_js_dialogs
);
118 void JavascriptAppModalDialogAndroid::CancelAppModalDialog() {
119 dialog_
->OnCancel(false);
123 bool JavascriptAppModalDialogAndroid::IsShowing() const {
127 void JavascriptAppModalDialogAndroid::DidCancelAppModalDialog(
128 JNIEnv
* env
, jobject
, bool should_suppress_js_dialogs
) {
129 dialog_
->OnCancel(should_suppress_js_dialogs
);
133 const ScopedJavaGlobalRef
<jobject
>&
134 JavascriptAppModalDialogAndroid::GetDialogObject() const {
135 return dialog_jobject_
;
139 ScopedJavaLocalRef
<jobject
> GetCurrentModalDialog(
141 const JavaParamRef
<jclass
>& clazz
) {
142 app_modal::AppModalDialog
* dialog
=
143 app_modal::AppModalDialogQueue::GetInstance()->active_dialog();
144 if (!dialog
|| !dialog
->native_dialog())
145 return ScopedJavaLocalRef
<jobject
>();
147 JavascriptAppModalDialogAndroid
* js_dialog
=
148 static_cast<JavascriptAppModalDialogAndroid
*>(dialog
->native_dialog());
149 return ScopedJavaLocalRef
<jobject
>(js_dialog
->GetDialogObject());
153 bool JavascriptAppModalDialogAndroid::RegisterJavascriptAppModalDialog(
155 return RegisterNativesImpl(env
);
158 JavascriptAppModalDialogAndroid::~JavascriptAppModalDialogAndroid() {
159 // In case the dialog is still displaying, tell it to close itself.
160 // This can happen if you trigger a dialog but close the Tab before it's
161 // shown, and then accept the dialog.
162 if (!dialog_jobject_
.is_null()) {
163 JNIEnv
* env
= AttachCurrentThread();
164 Java_JavascriptAppModalDialog_dismiss(env
, dialog_jobject_
.obj());
170 class ChromeJavaScriptNativeDialogAndroidFactory
171 : public app_modal::JavaScriptNativeDialogFactory
{
173 ChromeJavaScriptNativeDialogAndroidFactory() {}
174 ~ChromeJavaScriptNativeDialogAndroidFactory() override
{}
177 app_modal::NativeAppModalDialog
* CreateNativeJavaScriptDialog(
178 app_modal::JavaScriptAppModalDialog
* dialog
) override
{
179 dialog
->web_contents()->GetDelegate()->ActivateContents(
180 dialog
->web_contents());
181 return new JavascriptAppModalDialogAndroid(
182 base::android::AttachCurrentThread(),
183 dialog
, dialog
->web_contents()->GetTopLevelNativeWindow());
186 DISALLOW_COPY_AND_ASSIGN(ChromeJavaScriptNativeDialogAndroidFactory
);
191 void InstallChromeJavaScriptNativeDialogFactory() {
192 app_modal::JavaScriptDialogManager::GetInstance()->
193 SetNativeDialogFactory(
194 make_scoped_ptr(new ChromeJavaScriptNativeDialogAndroidFactory
));