Upstream version 8.36.169.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 <string>
8 #include <vector>
9
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/lazy_instance.h"
13 #include "base/message_loop/message_loop.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/common/file_chooser_params.h"
18 #include "jni/XWalkWebContentsDelegate_jni.h"
19 #include "xwalk/runtime/browser/media/media_capture_devices_dispatcher.h"
20 #include "xwalk/runtime/browser/runtime_file_select_helper.h"
21 #include "xwalk/runtime/browser/runtime_javascript_dialog_manager.h"
22 #include "ui/shell_dialogs/selected_file_info.h"
23
24 using base::android::AttachCurrentThread;
25 using base::android::ConvertUTF16ToJavaString;
26 using base::android::ScopedJavaLocalRef;
27 using content::FileChooserParams;
28 using content::WebContents;
29
30 namespace xwalk {
31
32 XWalkWebContentsDelegate::XWalkWebContentsDelegate(
33     JNIEnv* env,
34     jobject obj)
35     : WebContentsDelegateAndroid(env, obj) {
36 }
37
38 XWalkWebContentsDelegate::~XWalkWebContentsDelegate() {
39 }
40
41 void XWalkWebContentsDelegate::AddNewContents(
42     content::WebContents* source,
43     content::WebContents* new_contents,
44     WindowOpenDisposition disposition,
45     const gfx::Rect& initial_pos,
46     bool user_gesture,
47     bool* was_blocked) {
48   JNIEnv* env = AttachCurrentThread();
49   bool is_dialog = disposition == NEW_POPUP;
50   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
51
52   if (java_delegate.obj()) {
53     Java_XWalkWebContentsDelegate_addNewContents(env,
54         java_delegate.obj(), is_dialog, user_gesture);
55   }
56 }
57
58 void XWalkWebContentsDelegate::CloseContents(content::WebContents* source) {
59   JNIEnv* env = AttachCurrentThread();
60
61   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
62   if (java_delegate.obj()) {
63     Java_XWalkWebContentsDelegate_closeContents(env, java_delegate.obj());
64   }
65 }
66
67 void XWalkWebContentsDelegate::ActivateContents(content::WebContents* source) {
68   JNIEnv* env = AttachCurrentThread();
69
70   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
71   if (java_delegate.obj()) {
72     Java_XWalkWebContentsDelegate_activateContents(env, java_delegate.obj());
73   }
74 }
75
76 void XWalkWebContentsDelegate::UpdatePreferredSize(
77     content::WebContents* contents,
78     const gfx::Size& pref_size) {
79   JNIEnv* env = AttachCurrentThread();
80
81   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
82   if (java_delegate.obj()) {
83     Java_XWalkWebContentsDelegate_updatePreferredSize(env, java_delegate.obj(),
84         pref_size.width(), pref_size.height());
85   }
86 }
87
88 void XWalkWebContentsDelegate::RunFileChooser(
89     content::WebContents* web_contents,
90     const content::FileChooserParams& params) {
91   JNIEnv* env = AttachCurrentThread();
92
93   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
94   if (!java_delegate.obj())
95     return;
96
97   if (params.mode == FileChooserParams::Save) {
98     // Save not supported, so cancel it.
99     web_contents->GetRenderViewHost()->FilesSelectedInChooser(
100          std::vector<ui::SelectedFileInfo>(),
101          params.mode);
102     return;
103   }
104   int mode = static_cast<int>(params.mode);
105   jboolean overridden =
106       Java_XWalkWebContentsDelegate_shouldOverrideRunFileChooser(env,
107           java_delegate.obj(),
108           web_contents->GetRenderProcessHost()->GetID(),
109           web_contents->GetRenderViewHost()->GetRoutingID(),
110           mode,
111           ConvertUTF16ToJavaString(env,
112               JoinString(params.accept_types, ',')).obj(),
113           params.capture);
114   if (overridden == JNI_FALSE)
115     RuntimeFileSelectHelper::RunFileChooser(web_contents, params);
116 }
117
118 content::JavaScriptDialogManager*
119 XWalkWebContentsDelegate::GetJavaScriptDialogManager() {
120   if (!javascript_dialog_manager_.get()) {
121     javascript_dialog_manager_.reset(new RuntimeJavaScriptDialogManager);
122   }
123   return javascript_dialog_manager_.get();
124 }
125
126 void XWalkWebContentsDelegate::RequestMediaAccessPermission(
127     content::WebContents* web_contents,
128     const content::MediaStreamRequest& request,
129     const content::MediaResponseCallback& callback) {
130   XWalkMediaCaptureDevicesDispatcher::RunRequestMediaAccessPermission(
131       web_contents, request, callback);
132 }
133
134 void XWalkWebContentsDelegate::RendererUnresponsive(WebContents* source) {
135   JNIEnv* env = AttachCurrentThread();
136   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
137   if (obj.is_null())
138     return;
139   Java_XWalkWebContentsDelegate_rendererUnresponsive(env, obj.obj());
140 }
141
142 void XWalkWebContentsDelegate::RendererResponsive(WebContents* source) {
143   JNIEnv* env = AttachCurrentThread();
144   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
145   if (obj.is_null())
146     return;
147   Java_XWalkWebContentsDelegate_rendererResponsive(env, obj.obj());
148 }
149
150 void XWalkWebContentsDelegate::HandleKeyboardEvent(
151     content::WebContents* source,
152     const content::NativeWebKeyboardEvent& event) {
153   jobject key_event = event.os_event;
154   if (!key_event)
155     return;
156   JNIEnv* env = AttachCurrentThread();
157   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
158   if (obj.is_null())
159     return;
160   Java_XWalkWebContentsDelegate_handleKeyboardEvent(env, obj.obj(), key_event);
161 }
162
163
164 void XWalkWebContentsDelegate::ToggleFullscreenModeForTab(
165     content::WebContents* web_contents,
166     bool enter_fullscreen) {
167   JNIEnv* env = AttachCurrentThread();
168   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
169   if (obj.is_null())
170     return;
171   Java_XWalkWebContentsDelegate_toggleFullscreen(
172       env, obj.obj(), enter_fullscreen);
173 }
174
175 bool XWalkWebContentsDelegate::IsFullscreenForTabOrPending(
176     const content::WebContents* web_contents) const {
177   JNIEnv* env = AttachCurrentThread();
178   ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
179   if (obj.is_null())
180     return false;
181   return Java_XWalkWebContentsDelegate_isFullscreen(env, obj.obj());
182 }
183
184 bool XWalkWebContentsDelegate::ShouldCreateWebContents(
185     content::WebContents* web_contents,
186     int route_id,
187     WindowContainerType window_container_type,
188     const base::string16& frame_name,
189     const GURL& target_url,
190     const std::string& partition_id,
191     content::SessionStorageNamespace* session_storage_namespace) {
192   JNIEnv* env = AttachCurrentThread();
193   ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env);
194
195   if (java_delegate.obj()) {
196     ScopedJavaLocalRef<jstring> url_buffer =
197         base::android::ConvertUTF8ToJavaString(env, target_url.spec());
198     return Java_XWalkWebContentsDelegate_shouldOpenWithDefaultBrowser(env,
199         java_delegate.obj(), url_buffer.obj()) == JNI_FALSE;
200   }
201
202   // As multiple windows mode has not been implemented yet, return false
203   // to make sure new WebContents won't be created.
204   return false;
205 }
206
207 bool RegisterXWalkWebContentsDelegate(JNIEnv* env) {
208   return RegisterNativesImpl(env);
209 }
210
211 }  // namespace xwalk