1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/runtime/browser/android/xwalk_web_contents_delegate.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/lazy_instance.h"
13 #include "base/message_loop/message_loop.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/common/file_chooser_params.h"
18 #include "jni/XWalkWebContentsDelegate_jni.h"
19 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
20 #include "xwalk/runtime/browser/runtime_file_select_helper.h"
21 #include "xwalk/runtime/browser/runtime_javascript_dialog_manager.h"
22 #include "ui/shell_dialogs/selected_file_info.h"
24 using base::android::AttachCurrentThread;
25 using base::android::ConvertUTF16ToJavaString;
26 using base::android::ScopedJavaLocalRef;
27 using content::FileChooserParams;
28 using content::WebContents;
32 XWalkWebContentsDelegate::XWalkWebContentsDelegate(
35 : WebContentsDelegateAndroid(env, obj) {
38 XWalkWebContentsDelegate::~XWalkWebContentsDelegate() {
41 void XWalkWebContentsDelegate::AddNewContents(
42 content::WebContents* source,
43 content::WebContents* new_contents,
44 WindowOpenDisposition disposition,
45 const gfx::Rect& initial_pos,
48 JNIEnv* env = AttachCurrentThread();
49 bool is_dialog = disposition == NEW_POPUP;
50 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
52 if (java_delegate.obj()) {
53 Java_XWalkWebContentsDelegate_addNewContents(env,
54 java_delegate.obj(), is_dialog, user_gesture);
58 void XWalkWebContentsDelegate::CloseContents(content::WebContents* source) {
59 JNIEnv* env = AttachCurrentThread();
61 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
62 if (java_delegate.obj()) {
63 Java_XWalkWebContentsDelegate_closeContents(env, java_delegate.obj());
67 void XWalkWebContentsDelegate::ActivateContents(content::WebContents* source) {
68 JNIEnv* env = AttachCurrentThread();
70 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
71 if (java_delegate.obj()) {
72 Java_XWalkWebContentsDelegate_activateContents(env, java_delegate.obj());
76 void XWalkWebContentsDelegate::UpdatePreferredSize(
77 content::WebContents* contents,
78 const gfx::Size& pref_size) {
79 JNIEnv* env = AttachCurrentThread();
81 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
82 if (java_delegate.obj()) {
83 Java_XWalkWebContentsDelegate_updatePreferredSize(env, java_delegate.obj(),
84 pref_size.width(), pref_size.height());
88 void XWalkWebContentsDelegate::RunFileChooser(
89 content::WebContents* web_contents,
90 const content::FileChooserParams& params) {
91 JNIEnv* env = AttachCurrentThread();
93 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
94 if (!java_delegate.obj())
97 if (params.mode == FileChooserParams::Save) {
98 // Save not supported, so cancel it.
99 web_contents->GetRenderViewHost()->FilesSelectedInChooser(
100 std::vector<ui::SelectedFileInfo>(),
104 int mode = static_cast<int>(params.mode);
105 jboolean overridden =
106 Java_XWalkWebContentsDelegate_shouldOverrideRunFileChooser(env,
108 web_contents->GetRenderProcessHost()->GetID(),
109 web_contents->GetRenderViewHost()->GetRoutingID(),
111 ConvertUTF16ToJavaString(env,
112 JoinString(params.accept_types, ',')).obj(),
114 if (overridden == JNI_FALSE)
115 RuntimeFileSelectHelper::RunFileChooser(web_contents, params);
118 content::JavaScriptDialogManager*
119 XWalkWebContentsDelegate::GetJavaScriptDialogManager() {
120 if (!javascript_dialog_manager_.get()) {
121 javascript_dialog_manager_.reset(new RuntimeJavaScriptDialogManager);
123 return javascript_dialog_manager_.get();
126 void XWalkWebContentsDelegate::RequestMediaAccessPermission(
127 content::WebContents* web_contents,
128 const content::MediaStreamRequest& request,
129 const content::MediaResponseCallback& callback) {
130 XWalkMediaCaptureDevicesDispatcher::RunRequestMediaAccessPermission(
131 web_contents, request, callback);
134 void XWalkWebContentsDelegate::RendererUnresponsive(WebContents* source) {
135 JNIEnv* env = AttachCurrentThread();
136 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
139 Java_XWalkWebContentsDelegate_rendererUnresponsive(env, obj.obj());
142 void XWalkWebContentsDelegate::RendererResponsive(WebContents* source) {
143 JNIEnv* env = AttachCurrentThread();
144 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
147 Java_XWalkWebContentsDelegate_rendererResponsive(env, obj.obj());
150 void XWalkWebContentsDelegate::HandleKeyboardEvent(
151 content::WebContents* source,
152 const content::NativeWebKeyboardEvent& event) {
153 jobject key_event = event.os_event;
156 JNIEnv* env = AttachCurrentThread();
157 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
160 Java_XWalkWebContentsDelegate_handleKeyboardEvent(env, obj.obj(), key_event);
164 void XWalkWebContentsDelegate::ToggleFullscreenModeForTab(
165 content::WebContents* web_contents,
166 bool enter_fullscreen) {
167 JNIEnv* env = AttachCurrentThread();
168 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
171 Java_XWalkWebContentsDelegate_toggleFullscreen(
172 env, obj.obj(), enter_fullscreen);
175 bool XWalkWebContentsDelegate::IsFullscreenForTabOrPending(
176 const content::WebContents* web_contents) const {
177 JNIEnv* env = AttachCurrentThread();
178 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
181 return Java_XWalkWebContentsDelegate_isFullscreen(env, obj.obj());
184 bool XWalkWebContentsDelegate::ShouldCreateWebContents(
185 content::WebContents* web_contents,
187 WindowContainerType window_container_type,
188 const base::string16& frame_name,
189 const GURL& target_url,
190 const std::string& partition_id,
191 content::SessionStorageNamespace* session_storage_namespace) {
192 JNIEnv* env = AttachCurrentThread();
193 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
195 if (java_delegate.obj()) {
196 ScopedJavaLocalRef<jstring> url_buffer =
197 base::android::ConvertUTF8ToJavaString(env, target_url.spec());
198 return Java_XWalkWebContentsDelegate_shouldOpenWithDefaultBrowser(env,
199 java_delegate.obj(), url_buffer.obj()) == JNI_FALSE;
202 // As multiple windows mode has not been implemented yet, return false
203 // to make sure new WebContents won't be created.
207 bool RegisterXWalkWebContentsDelegate(JNIEnv* env) {
208 return RegisterNativesImpl(env);