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"
7 #include "base/android/scoped_java_ref.h"
8 #include "base/lazy_instance.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/public/browser/web_contents.h"
11 #include "jni/XWalkWebContentsDelegate_jni.h"
12 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
13 #include "xwalk/runtime/browser/runtime_file_select_helper.h"
14 #include "xwalk/runtime/browser/runtime_javascript_dialog_manager.h"
16 using base::android::AttachCurrentThread;
17 using base::android::ScopedJavaLocalRef;
18 using content::WebContents;
22 XWalkWebContentsDelegate::XWalkWebContentsDelegate(
25 : WebContentsDelegateAndroid(env, obj) {
28 XWalkWebContentsDelegate::~XWalkWebContentsDelegate() {
31 void XWalkWebContentsDelegate::AddNewContents(
32 content::WebContents* source,
33 content::WebContents* new_contents,
34 WindowOpenDisposition disposition,
35 const gfx::Rect& initial_pos,
38 JNIEnv* env = AttachCurrentThread();
39 bool is_dialog = disposition == NEW_POPUP;
40 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
42 if (java_delegate.obj()) {
43 Java_XWalkWebContentsDelegate_addNewContents(env,
44 java_delegate.obj(), is_dialog, user_gesture);
48 void XWalkWebContentsDelegate::CloseContents(content::WebContents* source) {
49 JNIEnv* env = AttachCurrentThread();
51 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
52 if (java_delegate.obj()) {
53 Java_XWalkWebContentsDelegate_closeContents(env, java_delegate.obj());
57 void XWalkWebContentsDelegate::ActivateContents(content::WebContents* source) {
58 JNIEnv* env = AttachCurrentThread();
60 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
61 if (java_delegate.obj()) {
62 Java_XWalkWebContentsDelegate_activateContents(env, java_delegate.obj());
66 void XWalkWebContentsDelegate::UpdatePreferredSize(
67 content::WebContents* contents,
68 const gfx::Size& pref_size) {
69 JNIEnv* env = AttachCurrentThread();
71 ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
72 if (java_delegate.obj()) {
73 Java_XWalkWebContentsDelegate_updatePreferredSize(env, java_delegate.obj(),
74 pref_size.width(), pref_size.height());
78 void XWalkWebContentsDelegate::RunFileChooser(
79 content::WebContents* web_contents,
80 const content::FileChooserParams& params) {
81 RuntimeFileSelectHelper::RunFileChooser(web_contents, params);
84 content::JavaScriptDialogManager*
85 XWalkWebContentsDelegate::GetJavaScriptDialogManager() {
86 if (!javascript_dialog_manager_.get()) {
87 javascript_dialog_manager_.reset(new RuntimeJavaScriptDialogManager);
89 return javascript_dialog_manager_.get();
92 void XWalkWebContentsDelegate::RequestMediaAccessPermission(
93 content::WebContents* web_contents,
94 const content::MediaStreamRequest& request,
95 const content::MediaResponseCallback& callback) {
96 XWalkMediaCaptureDevicesDispatcher::RunRequestMediaAccessPermission(
97 web_contents, request, callback);
100 void XWalkWebContentsDelegate::RendererUnresponsive(WebContents* source) {
101 JNIEnv* env = AttachCurrentThread();
102 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
105 Java_XWalkWebContentsDelegate_rendererUnresponsive(env, obj.obj());
108 void XWalkWebContentsDelegate::RendererResponsive(WebContents* source) {
109 JNIEnv* env = AttachCurrentThread();
110 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
113 Java_XWalkWebContentsDelegate_rendererResponsive(env, obj.obj());
116 void XWalkWebContentsDelegate::ToggleFullscreenModeForTab(
117 content::WebContents* web_contents,
118 bool enter_fullscreen) {
119 JNIEnv* env = AttachCurrentThread();
120 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
123 Java_XWalkWebContentsDelegate_toggleFullscreen(
124 env, obj.obj(), enter_fullscreen);
127 bool XWalkWebContentsDelegate::IsFullscreenForTabOrPending(
128 const content::WebContents* web_contents) const {
129 JNIEnv* env = AttachCurrentThread();
130 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
133 return Java_XWalkWebContentsDelegate_isFullscreen(env, obj.obj());
136 bool RegisterXWalkWebContentsDelegate(JNIEnv* env) {
137 return RegisterNativesImpl(env);