8c57e4f66ffce5efa82d66d0bc55d9623ec2ada1
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / android / xwalk_web_contents_delegate.cc
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.
4
5 #include "xwalk/runtime/browser/android/xwalk_web_contents_delegate.h"
6
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"
15
16 using base::android::AttachCurrentThread;
17 using base::android::ScopedJavaLocalRef;
18 using content::WebContents;
19
20 namespace xwalk {
21
22 XWalkWebContentsDelegate::XWalkWebContentsDelegate(
23     JNIEnv* env,
24     jobject obj)
25     : WebContentsDelegateAndroid(env, obj) {
26 }
27
28 XWalkWebContentsDelegate::~XWalkWebContentsDelegate() {
29 }
30
31 void XWalkWebContentsDelegate::AddNewContents(
32     content::WebContents* source,
33     content::WebContents* new_contents,
34     WindowOpenDisposition disposition,
35     const gfx::Rect& initial_pos,
36     bool user_gesture,
37     bool* was_blocked) {
38   JNIEnv* env = AttachCurrentThread();
39   bool is_dialog = disposition == NEW_POPUP;
40   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
41
42   if (java_delegate.obj()) {
43     Java_XWalkWebContentsDelegate_addNewContents(env,
44         java_delegate.obj(), is_dialog, user_gesture);
45   }
46 }
47
48 void XWalkWebContentsDelegate::CloseContents(content::WebContents* source) {
49   JNIEnv* env = AttachCurrentThread();
50
51   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
52   if (java_delegate.obj()) {
53     Java_XWalkWebContentsDelegate_closeContents(env, java_delegate.obj());
54   }
55 }
56
57 void XWalkWebContentsDelegate::ActivateContents(content::WebContents* source) {
58   JNIEnv* env = AttachCurrentThread();
59
60   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
61   if (java_delegate.obj()) {
62     Java_XWalkWebContentsDelegate_activateContents(env, java_delegate.obj());
63   }
64 }
65
66 void XWalkWebContentsDelegate::UpdatePreferredSize(
67     content::WebContents* contents,
68     const gfx::Size& pref_size) {
69   JNIEnv* env = AttachCurrentThread();
70
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());
75   }
76 }
77
78 void XWalkWebContentsDelegate::RunFileChooser(
79     content::WebContents* web_contents,
80     const content::FileChooserParams& params) {
81   RuntimeFileSelectHelper::RunFileChooser(web_contents, params);
82 }
83
84 content::JavaScriptDialogManager*
85 XWalkWebContentsDelegate::GetJavaScriptDialogManager() {
86   if (!javascript_dialog_manager_.get()) {
87     javascript_dialog_manager_.reset(new RuntimeJavaScriptDialogManager);
88   }
89   return javascript_dialog_manager_.get();
90 }
91
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);
98 }
99
100 void XWalkWebContentsDelegate::RendererUnresponsive(WebContents* source) {
101   JNIEnv* env = AttachCurrentThread();
102   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
103   if (obj.is_null())
104     return;
105   Java_XWalkWebContentsDelegate_rendererUnresponsive(env, obj.obj());
106 }
107
108 void XWalkWebContentsDelegate::RendererResponsive(WebContents* source) {
109   JNIEnv* env = AttachCurrentThread();
110   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
111   if (obj.is_null())
112     return;
113   Java_XWalkWebContentsDelegate_rendererResponsive(env, obj.obj());
114 }
115
116 void XWalkWebContentsDelegate::ToggleFullscreenModeForTab(
117     content::WebContents* web_contents,
118     bool enter_fullscreen) {
119   JNIEnv* env = AttachCurrentThread();
120   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
121   if (obj.is_null())
122     return;
123   Java_XWalkWebContentsDelegate_toggleFullscreen(
124       env, obj.obj(), enter_fullscreen);
125 }
126
127 bool XWalkWebContentsDelegate::IsFullscreenForTabOrPending(
128     const content::WebContents* web_contents) const {
129   JNIEnv* env = AttachCurrentThread();
130   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
131   if (obj.is_null())
132     return false;
133   return Java_XWalkWebContentsDelegate_isFullscreen(env, obj.obj());
134 }
135
136 bool RegisterXWalkWebContentsDelegate(JNIEnv* env) {
137   return RegisterNativesImpl(env);
138 }
139
140 }  // namespace xwalk