Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / javascript_app_modal_dialog_android.cc
blob297a6d06b08801c04adc6ac0dc89fd242882f7bc
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(
27 JNIEnv* env,
28 app_modal::JavaScriptAppModalDialog* dialog,
29 gfx::NativeWindow parent)
30 : dialog_(dialog),
31 parent_jobject_weak_ref_(env, parent->GetJavaObject().obj()) {
34 int JavascriptAppModalDialogAndroid::GetAppModalDialogButtons() const {
35 NOTIMPLEMENTED();
36 return 0;
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();
48 return;
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());
62 break;
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());
69 } else {
70 dialog_object = Java_JavascriptAppModalDialog_createConfirmDialog(env,
71 title.obj(), message.obj(),
72 dialog_->display_suppress_checkbox());
74 break;
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());
82 break;
84 default:
85 NOTREACHED();
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() {
97 ShowAppModalDialog();
100 void JavascriptAppModalDialogAndroid::CloseAppModalDialog() {
101 CancelAppModalDialog();
104 void JavascriptAppModalDialogAndroid::AcceptAppModalDialog() {
105 base::string16 prompt_text;
106 dialog_->OnAccept(prompt_text, false);
107 delete this;
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);
115 delete this;
118 void JavascriptAppModalDialogAndroid::CancelAppModalDialog() {
119 dialog_->OnCancel(false);
120 delete this;
123 bool JavascriptAppModalDialogAndroid::IsShowing() const {
124 return true;
127 void JavascriptAppModalDialogAndroid::DidCancelAppModalDialog(
128 JNIEnv* env, jobject, bool should_suppress_js_dialogs) {
129 dialog_->OnCancel(should_suppress_js_dialogs);
130 delete this;
133 const ScopedJavaGlobalRef<jobject>&
134 JavascriptAppModalDialogAndroid::GetDialogObject() const {
135 return dialog_jobject_;
138 // static
139 ScopedJavaLocalRef<jobject> GetCurrentModalDialog(
140 JNIEnv* env,
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());
152 // static
153 bool JavascriptAppModalDialogAndroid::RegisterJavascriptAppModalDialog(
154 JNIEnv* env) {
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());
168 namespace {
170 class ChromeJavaScriptNativeDialogAndroidFactory
171 : public app_modal::JavaScriptNativeDialogFactory {
172 public:
173 ChromeJavaScriptNativeDialogAndroidFactory() {}
174 ~ChromeJavaScriptNativeDialogAndroidFactory() override {}
176 private:
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);
189 } // namespace
191 void InstallChromeJavaScriptNativeDialogFactory() {
192 app_modal::JavaScriptDialogManager::GetInstance()->
193 SetNativeDialogFactory(
194 make_scoped_ptr(new ChromeJavaScriptNativeDialogAndroidFactory));