Upstream version 6.35.131.0
[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 <vector>
8
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"
22
23 using base::android::AttachCurrentThread;
24 using base::android::ConvertUTF16ToJavaString;
25 using base::android::ScopedJavaLocalRef;
26 using content::FileChooserParams;
27 using content::WebContents;
28
29 namespace xwalk {
30
31 XWalkWebContentsDelegate::XWalkWebContentsDelegate(
32     JNIEnv* env,
33     jobject obj)
34     : WebContentsDelegateAndroid(env, obj) {
35 }
36
37 XWalkWebContentsDelegate::~XWalkWebContentsDelegate() {
38 }
39
40 void XWalkWebContentsDelegate::AddNewContents(
41     content::WebContents* source,
42     content::WebContents* new_contents,
43     WindowOpenDisposition disposition,
44     const gfx::Rect& initial_pos,
45     bool user_gesture,
46     bool* was_blocked) {
47   JNIEnv* env = AttachCurrentThread();
48   bool is_dialog = disposition == NEW_POPUP;
49   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
50
51   if (java_delegate.obj()) {
52     Java_XWalkWebContentsDelegate_addNewContents(env,
53         java_delegate.obj(), is_dialog, user_gesture);
54   }
55 }
56
57 void XWalkWebContentsDelegate::CloseContents(content::WebContents* source) {
58   JNIEnv* env = AttachCurrentThread();
59
60   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
61   if (java_delegate.obj()) {
62     Java_XWalkWebContentsDelegate_closeContents(env, java_delegate.obj());
63   }
64 }
65
66 void XWalkWebContentsDelegate::ActivateContents(content::WebContents* source) {
67   JNIEnv* env = AttachCurrentThread();
68
69   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
70   if (java_delegate.obj()) {
71     Java_XWalkWebContentsDelegate_activateContents(env, java_delegate.obj());
72   }
73 }
74
75 void XWalkWebContentsDelegate::UpdatePreferredSize(
76     content::WebContents* contents,
77     const gfx::Size& pref_size) {
78   JNIEnv* env = AttachCurrentThread();
79
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());
84   }
85 }
86
87 void XWalkWebContentsDelegate::RunFileChooser(
88     content::WebContents* web_contents,
89     const content::FileChooserParams& params) {
90   JNIEnv* env = AttachCurrentThread();
91
92   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
93   if (!java_delegate.obj())
94     return;
95
96   if (params.mode == FileChooserParams::Save) {
97     // Save not supported, so cancel it.
98     web_contents->GetRenderViewHost()->FilesSelectedInChooser(
99          std::vector<ui::SelectedFileInfo>(),
100          params.mode);
101     return;
102   }
103   int mode = static_cast<int>(params.mode);
104   jboolean overridden =
105       Java_XWalkWebContentsDelegate_shouldOverrideRunFileChooser(env,
106           java_delegate.obj(),
107           web_contents->GetRenderProcessHost()->GetID(),
108           web_contents->GetRenderViewHost()->GetRoutingID(),
109           mode,
110           ConvertUTF16ToJavaString(env,
111               JoinString(params.accept_types, ',')).obj(),
112           params.capture);
113   if (overridden == JNI_FALSE)
114     RuntimeFileSelectHelper::RunFileChooser(web_contents, params);
115 }
116
117 content::JavaScriptDialogManager*
118 XWalkWebContentsDelegate::GetJavaScriptDialogManager() {
119   if (!javascript_dialog_manager_.get()) {
120     javascript_dialog_manager_.reset(new RuntimeJavaScriptDialogManager);
121   }
122   return javascript_dialog_manager_.get();
123 }
124
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);
131 }
132
133 void XWalkWebContentsDelegate::RendererUnresponsive(WebContents* source) {
134   JNIEnv* env = AttachCurrentThread();
135   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
136   if (obj.is_null())
137     return;
138   Java_XWalkWebContentsDelegate_rendererUnresponsive(env, obj.obj());
139 }
140
141 void XWalkWebContentsDelegate::RendererResponsive(WebContents* source) {
142   JNIEnv* env = AttachCurrentThread();
143   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
144   if (obj.is_null())
145     return;
146   Java_XWalkWebContentsDelegate_rendererResponsive(env, obj.obj());
147 }
148
149 void XWalkWebContentsDelegate::ToggleFullscreenModeForTab(
150     content::WebContents* web_contents,
151     bool enter_fullscreen) {
152   JNIEnv* env = AttachCurrentThread();
153   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
154   if (obj.is_null())
155     return;
156   Java_XWalkWebContentsDelegate_toggleFullscreen(
157       env, obj.obj(), enter_fullscreen);
158 }
159
160 bool XWalkWebContentsDelegate::IsFullscreenForTabOrPending(
161     const content::WebContents* web_contents) const {
162   JNIEnv* env = AttachCurrentThread();
163   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
164   if (obj.is_null())
165     return false;
166   return Java_XWalkWebContentsDelegate_isFullscreen(env, obj.obj());
167 }
168
169 bool RegisterXWalkWebContentsDelegate(JNIEnv* env) {
170   return RegisterNativesImpl(env);
171 }
172
173 }  // namespace xwalk