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_content.h"
6 #include "xwalk/runtime/browser/android/xwalk_web_contents_delegate.h"
11 #include "base/android/jni_string.h"
12 #include "base/android/scoped_java_ref.h"
13 #include "base/lazy_instance.h"
14 #include "base/message_loop/message_loop.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/render_view_host.h"
18 #include "content/public/common/file_chooser_file_info.h"
19 #include "content/public/common/file_chooser_params.h"
20 #include "jni/XWalkWebContentsDelegate_jni.h"
21 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
22 #include "xwalk/runtime/browser/runtime_file_select_helper.h"
23 #include "xwalk/runtime/browser/runtime_javascript_dialog_manager.h"
25 using base::android::AttachCurrentThread;
26 using base::android::ConvertUTF16ToJavaString;
27 using base::android::ScopedJavaLocalRef;
28 using content::FileChooserParams;
29 using content::WebContents;
33 XWalkWebContentsDelegate::XWalkWebContentsDelegate(
36 : WebContentsDelegateAndroid(env, obj) {
39 XWalkWebContentsDelegate::~XWalkWebContentsDelegate() {
42 void XWalkWebContentsDelegate::AddNewContents(
43 content::WebContents* source,
44 content::WebContents* new_contents,
45 WindowOpenDisposition disposition,
46 const gfx::Rect& initial_pos,
49 JNIEnv* env = AttachCurrentThread();
51 bool is_dialog = disposition == NEW_POPUP;
52 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
53 bool create_popup = false;
55 if (java_delegate.obj()) {
56 create_popup = Java_XWalkWebContentsDelegate_addNewContents(env,
57 java_delegate.obj(), is_dialog, user_gesture);
61 XWalkContent::FromWebContents(source)->SetPendingWebContentsForPopup(
62 make_scoped_ptr(new_contents));
63 new_contents->WasHidden();
65 base::MessageLoop::current()->DeleteSoon(FROM_HERE, new_contents);
69 *was_blocked = !create_popup;
73 void XWalkWebContentsDelegate::CloseContents(content::WebContents* source) {
74 JNIEnv* env = AttachCurrentThread();
76 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
77 if (java_delegate.obj()) {
78 Java_XWalkWebContentsDelegate_closeContents(env, java_delegate.obj());
82 void XWalkWebContentsDelegate::ActivateContents(content::WebContents* source) {
83 JNIEnv* env = AttachCurrentThread();
85 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
86 if (java_delegate.obj()) {
87 Java_XWalkWebContentsDelegate_activateContents(env, java_delegate.obj());
91 void XWalkWebContentsDelegate::UpdatePreferredSize(
92 content::WebContents* contents,
93 const gfx::Size& pref_size) {
94 JNIEnv* env = AttachCurrentThread();
96 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
97 if (java_delegate.obj()) {
98 Java_XWalkWebContentsDelegate_updatePreferredSize(env, java_delegate.obj(),
99 pref_size.width(), pref_size.height());
103 void XWalkWebContentsDelegate::RunFileChooser(
104 content::WebContents* web_contents,
105 const content::FileChooserParams& params) {
106 JNIEnv* env = AttachCurrentThread();
108 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
109 if (!java_delegate.obj())
112 if (params.mode == FileChooserParams::Save) {
113 // Save not supported, so cancel it.
114 web_contents->GetRenderViewHost()->FilesSelectedInChooser(
115 std::vector<content::FileChooserFileInfo>(),
119 int mode = static_cast<int>(params.mode);
120 jboolean overridden =
121 Java_XWalkWebContentsDelegate_shouldOverrideRunFileChooser(env,
123 web_contents->GetRenderProcessHost()->GetID(),
124 web_contents->GetRenderViewHost()->GetRoutingID(),
126 ConvertUTF16ToJavaString(env,
127 JoinString(params.accept_types, ',')).obj(),
129 if (overridden == JNI_FALSE)
130 RuntimeFileSelectHelper::RunFileChooser(web_contents, params);
133 content::JavaScriptDialogManager*
134 XWalkWebContentsDelegate::GetJavaScriptDialogManager() {
135 if (!javascript_dialog_manager_.get()) {
136 javascript_dialog_manager_.reset(new RuntimeJavaScriptDialogManager);
138 return javascript_dialog_manager_.get();
141 void XWalkWebContentsDelegate::RequestMediaAccessPermission(
142 content::WebContents* web_contents,
143 const content::MediaStreamRequest& request,
144 const content::MediaResponseCallback& callback) {
145 XWalkMediaCaptureDevicesDispatcher::RunRequestMediaAccessPermission(
146 web_contents, request, callback);
149 void XWalkWebContentsDelegate::RendererUnresponsive(WebContents* source) {
150 JNIEnv* env = AttachCurrentThread();
151 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
154 Java_XWalkWebContentsDelegate_rendererUnresponsive(env, obj.obj());
157 void XWalkWebContentsDelegate::RendererResponsive(WebContents* source) {
158 JNIEnv* env = AttachCurrentThread();
159 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
162 Java_XWalkWebContentsDelegate_rendererResponsive(env, obj.obj());
165 void XWalkWebContentsDelegate::HandleKeyboardEvent(
166 content::WebContents* source,
167 const content::NativeWebKeyboardEvent& event) {
168 jobject key_event = event.os_event;
171 JNIEnv* env = AttachCurrentThread();
172 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
175 Java_XWalkWebContentsDelegate_handleKeyboardEvent(env, obj.obj(), key_event);
179 void XWalkWebContentsDelegate::ToggleFullscreenModeForTab(
180 content::WebContents* web_contents,
181 bool enter_fullscreen) {
182 JNIEnv* env = AttachCurrentThread();
183 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
186 Java_XWalkWebContentsDelegate_toggleFullscreen(
187 env, obj.obj(), enter_fullscreen);
190 bool XWalkWebContentsDelegate::IsFullscreenForTabOrPending(
191 const content::WebContents* web_contents) const {
192 JNIEnv* env = AttachCurrentThread();
193 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
196 return Java_XWalkWebContentsDelegate_isFullscreen(env, obj.obj());
199 bool XWalkWebContentsDelegate::ShouldCreateWebContents(
200 content::WebContents* web_contents,
202 WindowContainerType window_container_type,
203 const base::string16& frame_name,
204 const GURL& target_url,
205 const std::string& partition_id,
206 content::SessionStorageNamespace* session_storage_namespace) {
207 JNIEnv* env = AttachCurrentThread();
208 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
210 if (java_delegate.obj()) {
211 ScopedJavaLocalRef<jstring> url_buffer =
212 base::android::ConvertUTF8ToJavaString(env, target_url.spec());
213 return Java_XWalkWebContentsDelegate_shouldOpenWithDefaultBrowser(env,
214 java_delegate.obj(), url_buffer.obj()) == JNI_FALSE;
217 // As multiple windows mode has not been implemented yet, return false
218 // to make sure new WebContents won't be created.
222 bool RegisterXWalkWebContentsDelegate(JNIEnv* env) {
223 return RegisterNativesImpl(env);