1 // Copyright (c) 2012 The Chromium Authors. 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 "components/web_contents_delegate_android/web_contents_delegate_android.h"
7 #include <android/keycodes.h>
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h"
12 #include "components/web_contents_delegate_android/color_chooser_android.h"
13 #include "components/web_contents_delegate_android/validation_message_bubble_android.h"
14 #include "content/public/browser/android/content_view_core.h"
15 #include "content/public/browser/color_chooser.h"
16 #include "content/public/browser/global_request_id.h"
17 #include "content/public/browser/invalidate_type.h"
18 #include "content/public/browser/native_web_keyboard_event.h"
19 #include "content/public/browser/navigation_controller.h"
20 #include "content/public/browser/page_navigator.h"
21 #include "content/public/browser/render_widget_host_view.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/page_transition_types.h"
24 #include "content/public/common/referrer.h"
25 #include "jni/WebContentsDelegateAndroid_jni.h"
26 #include "ui/base/window_open_disposition.h"
27 #include "ui/gfx/rect.h"
30 using base::android::AttachCurrentThread
;
31 using base::android::ConvertUTF8ToJavaString
;
32 using base::android::ConvertUTF16ToJavaString
;
33 using base::android::ScopedJavaLocalRef
;
34 using content::ColorChooser
;
35 using content::RenderWidgetHostView
;
36 using content::WebContents
;
37 using content::WebContentsDelegate
;
39 namespace web_contents_delegate_android
{
41 WebContentsDelegateAndroid::WebContentsDelegateAndroid(JNIEnv
* env
, jobject obj
)
42 : weak_java_delegate_(env
, obj
) {
45 WebContentsDelegateAndroid::~WebContentsDelegateAndroid() {
48 ScopedJavaLocalRef
<jobject
>
49 WebContentsDelegateAndroid::GetJavaDelegate(JNIEnv
* env
) const {
50 return weak_java_delegate_
.get(env
);
53 // ----------------------------------------------------------------------------
54 // WebContentsDelegate methods
55 // ----------------------------------------------------------------------------
57 ColorChooser
* WebContentsDelegateAndroid::OpenColorChooser(
60 const std::vector
<content::ColorSuggestion
>& suggestions
) {
61 return new ColorChooserAndroid(source
, color
, suggestions
);
64 // OpenURLFromTab() will be called when we're performing a browser-intiated
65 // navigation. The most common scenario for this is opening new tabs (see
66 // RenderViewImpl::decidePolicyForNavigation for more details).
67 WebContents
* WebContentsDelegateAndroid::OpenURLFromTab(
69 const content::OpenURLParams
& params
) {
70 const GURL
& url
= params
.url
;
71 WindowOpenDisposition disposition
= params
.disposition
;
73 if (!source
|| (disposition
!= CURRENT_TAB
&&
74 disposition
!= NEW_FOREGROUND_TAB
&&
75 disposition
!= NEW_BACKGROUND_TAB
&&
76 disposition
!= OFF_THE_RECORD
)) {
81 JNIEnv
* env
= AttachCurrentThread();
82 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
84 return WebContentsDelegate::OpenURLFromTab(source
, params
);
86 if (disposition
== NEW_FOREGROUND_TAB
||
87 disposition
== NEW_BACKGROUND_TAB
||
88 disposition
== OFF_THE_RECORD
) {
89 JNIEnv
* env
= AttachCurrentThread();
90 ScopedJavaLocalRef
<jstring
> java_url
=
91 ConvertUTF8ToJavaString(env
, url
.spec());
92 ScopedJavaLocalRef
<jstring
> extra_headers
=
93 ConvertUTF8ToJavaString(env
, params
.extra_headers
);
94 ScopedJavaLocalRef
<jbyteArray
> post_data
;
95 if (params
.uses_post
&&
96 params
.browser_initiated_post_data
.get() &&
97 params
.browser_initiated_post_data
.get()->size()) {
98 post_data
= base::android::ToJavaByteArray(
100 params
.browser_initiated_post_data
.get()->front_as
<uint8
>(),
101 params
.browser_initiated_post_data
.get()->size());
103 Java_WebContentsDelegateAndroid_openNewTab(env
,
109 params
.is_renderer_initiated
);
113 // content::OpenURLParams -> content::NavigationController::LoadURLParams
114 content::NavigationController::LoadURLParams
load_params(url
);
115 load_params
.referrer
= params
.referrer
;
116 load_params
.frame_tree_node_id
= params
.frame_tree_node_id
;
117 load_params
.redirect_chain
= params
.redirect_chain
;
118 load_params
.transition_type
= params
.transition
;
119 load_params
.extra_headers
= params
.extra_headers
;
120 load_params
.should_replace_current_entry
=
121 params
.should_replace_current_entry
;
122 load_params
.is_renderer_initiated
= params
.is_renderer_initiated
;
124 if (params
.transferred_global_request_id
!= content::GlobalRequestID()) {
125 load_params
.transferred_global_request_id
=
126 params
.transferred_global_request_id
;
129 // Only allows the browser-initiated navigation to use POST.
130 if (params
.uses_post
&& !params
.is_renderer_initiated
) {
131 load_params
.load_type
=
132 content::NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST
;
133 load_params
.browser_initiated_post_data
=
134 params
.browser_initiated_post_data
;
137 source
->GetController().LoadURLWithParams(load_params
);
142 void WebContentsDelegateAndroid::NavigationStateChanged(
143 const WebContents
* source
, content::InvalidateTypes changed_flags
) {
144 JNIEnv
* env
= AttachCurrentThread();
145 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
148 Java_WebContentsDelegateAndroid_navigationStateChanged(
154 void WebContentsDelegateAndroid::VisibleSSLStateChanged(
155 const WebContents
* source
) {
156 JNIEnv
* env
= AttachCurrentThread();
157 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
160 Java_WebContentsDelegateAndroid_visibleSSLStateChanged(
165 void WebContentsDelegateAndroid::ActivateContents(WebContents
* contents
) {
166 JNIEnv
* env
= AttachCurrentThread();
167 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
170 Java_WebContentsDelegateAndroid_activateContents(env
, obj
.obj());
173 void WebContentsDelegateAndroid::DeactivateContents(WebContents
* contents
) {
174 // On desktop the current window is deactivated here, bringing the next window
175 // to focus. Not implemented on Android.
178 void WebContentsDelegateAndroid::LoadingStateChanged(WebContents
* source
,
179 bool to_different_document
) {
180 JNIEnv
* env
= AttachCurrentThread();
181 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
184 bool has_stopped
= source
== NULL
|| !source
->IsLoading();
187 Java_WebContentsDelegateAndroid_onLoadStopped(env
, obj
.obj());
189 Java_WebContentsDelegateAndroid_onLoadStarted(env
, obj
.obj());
192 void WebContentsDelegateAndroid::LoadProgressChanged(WebContents
* source
,
194 JNIEnv
* env
= AttachCurrentThread();
195 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
198 Java_WebContentsDelegateAndroid_notifyLoadProgressChanged(
204 void WebContentsDelegateAndroid::RendererUnresponsive(WebContents
* source
) {
205 JNIEnv
* env
= AttachCurrentThread();
206 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
209 Java_WebContentsDelegateAndroid_rendererUnresponsive(env
, obj
.obj());
212 void WebContentsDelegateAndroid::RendererResponsive(WebContents
* source
) {
213 JNIEnv
* env
= AttachCurrentThread();
214 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
217 Java_WebContentsDelegateAndroid_rendererResponsive(env
, obj
.obj());
220 void WebContentsDelegateAndroid::CloseContents(WebContents
* source
) {
221 JNIEnv
* env
= AttachCurrentThread();
222 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
225 Java_WebContentsDelegateAndroid_closeContents(env
, obj
.obj());
228 void WebContentsDelegateAndroid::MoveContents(WebContents
* source
,
229 const gfx::Rect
& pos
) {
233 bool WebContentsDelegateAndroid::AddMessageToConsole(
236 const base::string16
& message
,
238 const base::string16
& source_id
) {
239 JNIEnv
* env
= AttachCurrentThread();
240 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
242 return WebContentsDelegate::AddMessageToConsole(source
, level
, message
,
244 ScopedJavaLocalRef
<jstring
> jmessage(ConvertUTF16ToJavaString(env
, message
));
245 ScopedJavaLocalRef
<jstring
> jsource_id(
246 ConvertUTF16ToJavaString(env
, source_id
));
247 int jlevel
= WEB_CONTENTS_DELEGATE_LOG_LEVEL_DEBUG
;
249 case logging::LOG_VERBOSE
:
250 jlevel
= WEB_CONTENTS_DELEGATE_LOG_LEVEL_DEBUG
;
252 case logging::LOG_INFO
:
253 jlevel
= WEB_CONTENTS_DELEGATE_LOG_LEVEL_LOG
;
255 case logging::LOG_WARNING
:
256 jlevel
= WEB_CONTENTS_DELEGATE_LOG_LEVEL_WARNING
;
258 case logging::LOG_ERROR
:
259 jlevel
= WEB_CONTENTS_DELEGATE_LOG_LEVEL_ERROR
;
264 return Java_WebContentsDelegateAndroid_addMessageToConsole(
266 GetJavaDelegate(env
).obj(),
273 // This is either called from TabContents::DidNavigateMainFramePostCommit() with
274 // an empty GURL or responding to RenderViewHost::OnMsgUpateTargetURL(). In
275 // Chrome, the latter is not always called, especially not during history
276 // navigation. So we only handle the first case and pass the source TabContents'
277 // url to Java to update the UI.
278 void WebContentsDelegateAndroid::UpdateTargetURL(WebContents
* source
,
283 JNIEnv
* env
= AttachCurrentThread();
284 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
287 ScopedJavaLocalRef
<jstring
> java_url
=
288 ConvertUTF8ToJavaString(env
, source
->GetURL().spec());
289 Java_WebContentsDelegateAndroid_onUpdateUrl(env
,
294 void WebContentsDelegateAndroid::HandleKeyboardEvent(
296 const content::NativeWebKeyboardEvent
& event
) {
297 jobject key_event
= event
.os_event
;
299 JNIEnv
* env
= AttachCurrentThread();
300 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
303 Java_WebContentsDelegateAndroid_handleKeyboardEvent(
304 env
, obj
.obj(), key_event
);
308 bool WebContentsDelegateAndroid::TakeFocus(WebContents
* source
, bool reverse
) {
309 JNIEnv
* env
= AttachCurrentThread();
310 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
312 return WebContentsDelegate::TakeFocus(source
, reverse
);
313 return Java_WebContentsDelegateAndroid_takeFocus(
314 env
, obj
.obj(), reverse
);
317 void WebContentsDelegateAndroid::ShowRepostFormWarningDialog(
318 WebContents
* source
) {
319 JNIEnv
* env
= AttachCurrentThread();
320 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
323 ScopedJavaLocalRef
<jobject
> content_view_core
=
324 content::ContentViewCore::FromWebContents(source
)->GetJavaObject();
325 if (content_view_core
.is_null())
327 Java_WebContentsDelegateAndroid_showRepostFormWarningDialog(env
, obj
.obj(),
328 content_view_core
.obj());
331 void WebContentsDelegateAndroid::ToggleFullscreenModeForTab(
332 WebContents
* web_contents
,
333 bool enter_fullscreen
) {
334 JNIEnv
* env
= AttachCurrentThread();
335 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
338 Java_WebContentsDelegateAndroid_toggleFullscreenModeForTab(
339 env
, obj
.obj(), enter_fullscreen
);
342 bool WebContentsDelegateAndroid::IsFullscreenForTabOrPending(
343 const WebContents
* web_contents
) const {
344 JNIEnv
* env
= AttachCurrentThread();
345 ScopedJavaLocalRef
<jobject
> obj
= GetJavaDelegate(env
);
348 return Java_WebContentsDelegateAndroid_isFullscreenForTabOrPending(
352 void WebContentsDelegateAndroid::ShowValidationMessage(
353 WebContents
* web_contents
,
354 const gfx::Rect
& anchor_in_root_view
,
355 const base::string16
& main_text
,
356 const base::string16
& sub_text
) {
357 RenderWidgetHostView
* rwhv
= web_contents
->GetRenderWidgetHostView();
359 validation_message_bubble_
.reset(
360 new ValidationMessageBubbleAndroid(rwhv
->GetRenderWidgetHost(),
367 void WebContentsDelegateAndroid::HideValidationMessage(
368 WebContents
* web_contents
) {
369 validation_message_bubble_
.reset();
372 void WebContentsDelegateAndroid::MoveValidationMessage(
373 WebContents
* web_contents
,
374 const gfx::Rect
& anchor_in_root_view
) {
375 if (!validation_message_bubble_
)
377 RenderWidgetHostView
* rwhv
= web_contents
->GetRenderWidgetHostView();
379 validation_message_bubble_
->SetPositionRelativeToAnchor(
380 rwhv
->GetRenderWidgetHost(), anchor_in_root_view
);
383 // ----------------------------------------------------------------------------
384 // Native JNI methods
385 // ----------------------------------------------------------------------------
387 // Register native methods
389 bool RegisterWebContentsDelegateAndroid(JNIEnv
* env
) {
390 return RegisterNativesImpl(env
);
393 } // namespace web_contents_delegate_android