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"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/lazy_instance.h"
12 #include "base/message_loop/message_loop.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/common/file_chooser_params.h"
17 #include "jni/XWalkWebContentsDelegate_jni.h"
18 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
19 #include "xwalk/runtime/browser/runtime_file_select_helper.h"
20 #include "xwalk/runtime/browser/runtime_javascript_dialog_manager.h"
21 #include "ui/shell_dialogs/selected_file_info.h"
23 using base::android::AttachCurrentThread;
24 using base::android::ConvertUTF16ToJavaString;
25 using base::android::ScopedJavaLocalRef;
26 using content::FileChooserParams;
27 using content::WebContents;
31 XWalkWebContentsDelegate::XWalkWebContentsDelegate(
34 : WebContentsDelegateAndroid(env, obj) {
37 XWalkWebContentsDelegate::~XWalkWebContentsDelegate() {
40 void XWalkWebContentsDelegate::AddNewContents(
41 content::WebContents* source,
42 content::WebContents* new_contents,
43 WindowOpenDisposition disposition,
44 const gfx::Rect& initial_pos,
47 JNIEnv* env = AttachCurrentThread();
48 bool is_dialog = disposition == NEW_POPUP;
49 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
51 if (java_delegate.obj()) {
52 Java_XWalkWebContentsDelegate_addNewContents(env,
53 java_delegate.obj(), is_dialog, user_gesture);
57 void XWalkWebContentsDelegate::CloseContents(content::WebContents* source) {
58 JNIEnv* env = AttachCurrentThread();
60 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
61 if (java_delegate.obj()) {
62 Java_XWalkWebContentsDelegate_closeContents(env, java_delegate.obj());
66 void XWalkWebContentsDelegate::ActivateContents(content::WebContents* source) {
67 JNIEnv* env = AttachCurrentThread();
69 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
70 if (java_delegate.obj()) {
71 Java_XWalkWebContentsDelegate_activateContents(env, java_delegate.obj());
75 void XWalkWebContentsDelegate::UpdatePreferredSize(
76 content::WebContents* contents,
77 const gfx::Size& pref_size) {
78 JNIEnv* env = AttachCurrentThread();
80 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
81 if (java_delegate.obj()) {
82 Java_XWalkWebContentsDelegate_updatePreferredSize(env, java_delegate.obj(),
83 pref_size.width(), pref_size.height());
87 void XWalkWebContentsDelegate::RunFileChooser(
88 content::WebContents* web_contents,
89 const content::FileChooserParams& params) {
90 JNIEnv* env = AttachCurrentThread();
92 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
93 if (!java_delegate.obj())
96 if (params.mode == FileChooserParams::Save) {
97 // Save not supported, so cancel it.
98 web_contents->GetRenderViewHost()->FilesSelectedInChooser(
99 std::vector<ui::SelectedFileInfo>(),
103 int mode = static_cast<int>(params.mode);
104 jboolean overridden =
105 Java_XWalkWebContentsDelegate_shouldOverrideRunFileChooser(env,
107 web_contents->GetRenderProcessHost()->GetID(),
108 web_contents->GetRenderViewHost()->GetRoutingID(),
110 ConvertUTF16ToJavaString(env,
111 JoinString(params.accept_types, ',')).obj(),
113 if (overridden == JNI_FALSE)
114 RuntimeFileSelectHelper::RunFileChooser(web_contents, params);
117 content::JavaScriptDialogManager*
118 XWalkWebContentsDelegate::GetJavaScriptDialogManager() {
119 if (!javascript_dialog_manager_.get()) {
120 javascript_dialog_manager_.reset(new RuntimeJavaScriptDialogManager);
122 return javascript_dialog_manager_.get();
125 void XWalkWebContentsDelegate::RequestMediaAccessPermission(
126 content::WebContents* web_contents,
127 const content::MediaStreamRequest& request,
128 const content::MediaResponseCallback& callback) {
129 XWalkMediaCaptureDevicesDispatcher::RunRequestMediaAccessPermission(
130 web_contents, request, callback);
133 void XWalkWebContentsDelegate::RendererUnresponsive(WebContents* source) {
134 JNIEnv* env = AttachCurrentThread();
135 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
138 Java_XWalkWebContentsDelegate_rendererUnresponsive(env, obj.obj());
141 void XWalkWebContentsDelegate::RendererResponsive(WebContents* source) {
142 JNIEnv* env = AttachCurrentThread();
143 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
146 Java_XWalkWebContentsDelegate_rendererResponsive(env, obj.obj());
149 void XWalkWebContentsDelegate::ToggleFullscreenModeForTab(
150 content::WebContents* web_contents,
151 bool enter_fullscreen) {
152 JNIEnv* env = AttachCurrentThread();
153 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
156 Java_XWalkWebContentsDelegate_toggleFullscreen(
157 env, obj.obj(), enter_fullscreen);
160 bool XWalkWebContentsDelegate::IsFullscreenForTabOrPending(
161 const content::WebContents* web_contents) const {
162 JNIEnv* env = AttachCurrentThread();
163 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
166 return Java_XWalkWebContentsDelegate_isFullscreen(env, obj.obj());
169 bool RegisterXWalkWebContentsDelegate(JNIEnv* env) {
170 return RegisterNativesImpl(env);