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