Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / android / javascript_app_modal_dialog_android.cc
blobbf08bc53ee02d419c984a9e8e1f18d6fb65f999b
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 jobject GetCurrentModalDialog(JNIEnv* env, jclass clazz) {
140 app_modal::AppModalDialog* dialog =
141 app_modal::AppModalDialogQueue::GetInstance()->active_dialog();
142 if (!dialog || !dialog->native_dialog())
143 return NULL;
145 JavascriptAppModalDialogAndroid* js_dialog =
146 static_cast<JavascriptAppModalDialogAndroid*>(dialog->native_dialog());
147 return js_dialog->GetDialogObject().obj();
150 // static
151 bool JavascriptAppModalDialogAndroid::RegisterJavascriptAppModalDialog(
152 JNIEnv* env) {
153 return RegisterNativesImpl(env);
156 JavascriptAppModalDialogAndroid::~JavascriptAppModalDialogAndroid() {
157 // In case the dialog is still displaying, tell it to close itself.
158 // This can happen if you trigger a dialog but close the Tab before it's
159 // shown, and then accept the dialog.
160 if (!dialog_jobject_.is_null()) {
161 JNIEnv* env = AttachCurrentThread();
162 Java_JavascriptAppModalDialog_dismiss(env, dialog_jobject_.obj());
166 namespace {
168 class ChromeJavaScriptNativeDialogAndroidFactory
169 : public app_modal::JavaScriptNativeDialogFactory {
170 public:
171 ChromeJavaScriptNativeDialogAndroidFactory() {}
172 ~ChromeJavaScriptNativeDialogAndroidFactory() override {}
174 private:
175 app_modal::NativeAppModalDialog* CreateNativeJavaScriptDialog(
176 app_modal::JavaScriptAppModalDialog* dialog) override {
177 dialog->web_contents()->GetDelegate()->ActivateContents(
178 dialog->web_contents());
179 return new JavascriptAppModalDialogAndroid(
180 base::android::AttachCurrentThread(),
181 dialog, dialog->web_contents()->GetTopLevelNativeWindow());
184 DISALLOW_COPY_AND_ASSIGN(ChromeJavaScriptNativeDialogAndroidFactory);
187 } // namespace
189 void InstallChromeJavaScriptNativeDialogFactory() {
190 app_modal::JavaScriptDialogManager::GetInstance()->
191 SetNativeDialogFactory(
192 make_scoped_ptr(new ChromeJavaScriptNativeDialogAndroidFactory));