Roll src/third_party/WebKit 3aea697:d9c6159 (svn 201973:201974)
[chromium-blink-merge.git] / components / web_contents_delegate_android / web_contents_delegate_android.cc
blob9524e876a521961a5237a1b42b983d693a95aa09
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/referrer.h"
24 #include "jni/WebContentsDelegateAndroid_jni.h"
25 #include "ui/base/window_open_disposition.h"
26 #include "ui/gfx/geometry/rect.h"
27 #include "url/gurl.h"
29 using base::android::AttachCurrentThread;
30 using base::android::ConvertUTF8ToJavaString;
31 using base::android::ConvertUTF16ToJavaString;
32 using base::android::ScopedJavaLocalRef;
33 using content::ColorChooser;
34 using content::RenderWidgetHostView;
35 using content::WebContents;
36 using content::WebContentsDelegate;
38 namespace web_contents_delegate_android {
40 WebContentsDelegateAndroid::WebContentsDelegateAndroid(JNIEnv* env, jobject obj)
41 : weak_java_delegate_(env, obj) {
44 WebContentsDelegateAndroid::~WebContentsDelegateAndroid() {
47 ScopedJavaLocalRef<jobject>
48 WebContentsDelegateAndroid::GetJavaDelegate(JNIEnv* env) const {
49 return weak_java_delegate_.get(env);
52 // ----------------------------------------------------------------------------
53 // WebContentsDelegate methods
54 // ----------------------------------------------------------------------------
56 ColorChooser* WebContentsDelegateAndroid::OpenColorChooser(
57 WebContents* source,
58 SkColor color,
59 const std::vector<content::ColorSuggestion>& suggestions) {
60 return new ColorChooserAndroid(source, color, suggestions);
63 // OpenURLFromTab() will be called when we're performing a browser-intiated
64 // navigation. The most common scenario for this is opening new tabs (see
65 // RenderViewImpl::decidePolicyForNavigation for more details).
66 WebContents* WebContentsDelegateAndroid::OpenURLFromTab(
67 WebContents* source,
68 const content::OpenURLParams& params) {
69 const GURL& url = params.url;
70 WindowOpenDisposition disposition = params.disposition;
72 if (!source || (disposition != CURRENT_TAB &&
73 disposition != NEW_FOREGROUND_TAB &&
74 disposition != NEW_BACKGROUND_TAB &&
75 disposition != OFF_THE_RECORD)) {
76 NOTIMPLEMENTED();
77 return NULL;
80 JNIEnv* env = AttachCurrentThread();
81 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
82 if (obj.is_null())
83 return WebContentsDelegate::OpenURLFromTab(source, params);
85 if (disposition == NEW_FOREGROUND_TAB ||
86 disposition == NEW_BACKGROUND_TAB ||
87 disposition == OFF_THE_RECORD) {
88 JNIEnv* env = AttachCurrentThread();
89 ScopedJavaLocalRef<jstring> java_url =
90 ConvertUTF8ToJavaString(env, url.spec());
91 ScopedJavaLocalRef<jstring> extra_headers =
92 ConvertUTF8ToJavaString(env, params.extra_headers);
93 ScopedJavaLocalRef<jbyteArray> post_data;
94 if (params.uses_post &&
95 params.browser_initiated_post_data.get() &&
96 params.browser_initiated_post_data.get()->size()) {
97 post_data = base::android::ToJavaByteArray(
98 env,
99 params.browser_initiated_post_data.get()->front_as<uint8>(),
100 params.browser_initiated_post_data.get()->size());
102 Java_WebContentsDelegateAndroid_openNewTab(env,
103 obj.obj(),
104 java_url.obj(),
105 extra_headers.obj(),
106 post_data.obj(),
107 disposition,
108 params.is_renderer_initiated);
109 return NULL;
112 // content::OpenURLParams -> content::NavigationController::LoadURLParams
113 content::NavigationController::LoadURLParams load_params(url);
114 load_params.referrer = params.referrer;
115 load_params.frame_tree_node_id = params.frame_tree_node_id;
116 load_params.redirect_chain = params.redirect_chain;
117 load_params.transition_type = params.transition;
118 load_params.extra_headers = params.extra_headers;
119 load_params.should_replace_current_entry =
120 params.should_replace_current_entry;
121 load_params.is_renderer_initiated = params.is_renderer_initiated;
123 if (params.transferred_global_request_id != content::GlobalRequestID()) {
124 load_params.transferred_global_request_id =
125 params.transferred_global_request_id;
128 // Only allows the browser-initiated navigation to use POST.
129 if (params.uses_post && !params.is_renderer_initiated) {
130 load_params.load_type =
131 content::NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
132 load_params.browser_initiated_post_data =
133 params.browser_initiated_post_data;
136 source->GetController().LoadURLWithParams(load_params);
138 return source;
141 void WebContentsDelegateAndroid::NavigationStateChanged(
142 WebContents* source, content::InvalidateTypes changed_flags) {
143 JNIEnv* env = AttachCurrentThread();
144 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
145 if (obj.is_null())
146 return;
147 Java_WebContentsDelegateAndroid_navigationStateChanged(
148 env,
149 obj.obj(),
150 changed_flags);
153 void WebContentsDelegateAndroid::VisibleSSLStateChanged(WebContents* source) {
154 JNIEnv* env = AttachCurrentThread();
155 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
156 if (obj.is_null())
157 return;
158 Java_WebContentsDelegateAndroid_visibleSSLStateChanged(
159 env,
160 obj.obj());
163 void WebContentsDelegateAndroid::ActivateContents(WebContents* contents) {
164 JNIEnv* env = AttachCurrentThread();
165 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
166 if (obj.is_null())
167 return;
168 Java_WebContentsDelegateAndroid_activateContents(env, obj.obj());
171 void WebContentsDelegateAndroid::DeactivateContents(WebContents* contents) {
172 // On desktop the current window is deactivated here, bringing the next window
173 // to focus. Not implemented on Android.
176 void WebContentsDelegateAndroid::LoadingStateChanged(WebContents* source,
177 bool to_different_document) {
178 JNIEnv* env = AttachCurrentThread();
179 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
180 if (obj.is_null())
181 return;
182 bool has_stopped = source == NULL || !source->IsLoading();
184 if (has_stopped)
185 Java_WebContentsDelegateAndroid_onLoadStopped(env, obj.obj());
186 else
187 Java_WebContentsDelegateAndroid_onLoadStarted(env, obj.obj());
190 void WebContentsDelegateAndroid::LoadProgressChanged(WebContents* source,
191 double progress) {
192 JNIEnv* env = AttachCurrentThread();
193 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
194 if (obj.is_null())
195 return;
196 Java_WebContentsDelegateAndroid_notifyLoadProgressChanged(
197 env,
198 obj.obj(),
199 progress);
202 void WebContentsDelegateAndroid::RendererUnresponsive(WebContents* source) {
203 JNIEnv* env = AttachCurrentThread();
204 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
205 if (obj.is_null())
206 return;
207 Java_WebContentsDelegateAndroid_rendererUnresponsive(env, obj.obj());
210 void WebContentsDelegateAndroid::RendererResponsive(WebContents* source) {
211 JNIEnv* env = AttachCurrentThread();
212 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
213 if (obj.is_null())
214 return;
215 Java_WebContentsDelegateAndroid_rendererResponsive(env, obj.obj());
218 bool WebContentsDelegateAndroid::ShouldCreateWebContents(
219 WebContents* web_contents,
220 int route_id,
221 int main_frame_route_id,
222 WindowContainerType window_container_type,
223 const std::string& frame_name,
224 const GURL& target_url,
225 const std::string& partition_id,
226 content::SessionStorageNamespace* session_storage_namespace) {
227 JNIEnv* env = AttachCurrentThread();
228 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
229 if (obj.is_null())
230 return true;
231 ScopedJavaLocalRef<jstring> java_url =
232 ConvertUTF8ToJavaString(env, target_url.spec());
233 return Java_WebContentsDelegateAndroid_shouldCreateWebContents(env, obj.obj(),
234 java_url.obj());
237 bool WebContentsDelegateAndroid::OnGoToEntryOffset(int offset) {
238 JNIEnv* env = AttachCurrentThread();
239 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
240 if (obj.is_null())
241 return true;
242 return Java_WebContentsDelegateAndroid_onGoToEntryOffset(env, obj.obj(),
243 offset);
246 void WebContentsDelegateAndroid::WebContentsCreated(
247 WebContents* source_contents, int opener_render_frame_id,
248 const std::string& frame_name, const GURL& target_url,
249 WebContents* new_contents) {
250 JNIEnv* env = AttachCurrentThread();
251 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
252 if (obj.is_null())
253 return;
255 ScopedJavaLocalRef<jobject> jsource_contents;
256 if (source_contents)
257 jsource_contents = source_contents->GetJavaWebContents();
258 ScopedJavaLocalRef<jobject> jnew_contents;
259 if (new_contents)
260 jnew_contents = new_contents->GetJavaWebContents();
262 Java_WebContentsDelegateAndroid_webContentsCreated(
263 env, obj.obj(), jsource_contents.obj(), opener_render_frame_id,
264 base::android::ConvertUTF8ToJavaString(env, frame_name).obj(),
265 base::android::ConvertUTF8ToJavaString(env, target_url.spec()).obj(),
266 jnew_contents.obj());
269 void WebContentsDelegateAndroid::CloseContents(WebContents* source) {
270 JNIEnv* env = AttachCurrentThread();
271 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
272 if (obj.is_null())
273 return;
274 Java_WebContentsDelegateAndroid_closeContents(env, obj.obj());
277 void WebContentsDelegateAndroid::MoveContents(WebContents* source,
278 const gfx::Rect& pos) {
279 // Do nothing.
282 bool WebContentsDelegateAndroid::AddMessageToConsole(
283 WebContents* source,
284 int32 level,
285 const base::string16& message,
286 int32 line_no,
287 const base::string16& source_id) {
288 JNIEnv* env = AttachCurrentThread();
289 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
290 if (obj.is_null())
291 return WebContentsDelegate::AddMessageToConsole(source, level, message,
292 line_no, source_id);
293 ScopedJavaLocalRef<jstring> jmessage(ConvertUTF16ToJavaString(env, message));
294 ScopedJavaLocalRef<jstring> jsource_id(
295 ConvertUTF16ToJavaString(env, source_id));
296 int jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_DEBUG;
297 switch (level) {
298 case logging::LOG_VERBOSE:
299 jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_DEBUG;
300 break;
301 case logging::LOG_INFO:
302 jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_LOG;
303 break;
304 case logging::LOG_WARNING:
305 jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_WARNING;
306 break;
307 case logging::LOG_ERROR:
308 jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_ERROR;
309 break;
310 default:
311 NOTREACHED();
313 return Java_WebContentsDelegateAndroid_addMessageToConsole(
314 env,
315 GetJavaDelegate(env).obj(),
316 jlevel,
317 jmessage.obj(),
318 line_no,
319 jsource_id.obj());
322 // This is either called from TabContents::DidNavigateMainFramePostCommit() with
323 // an empty GURL or responding to RenderViewHost::OnMsgUpateTargetURL(). In
324 // Chrome, the latter is not always called, especially not during history
325 // navigation. So we only handle the first case and pass the source TabContents'
326 // url to Java to update the UI.
327 void WebContentsDelegateAndroid::UpdateTargetURL(WebContents* source,
328 const GURL& url) {
329 if (!url.is_empty())
330 return;
331 JNIEnv* env = AttachCurrentThread();
332 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
333 if (obj.is_null())
334 return;
335 ScopedJavaLocalRef<jstring> java_url =
336 ConvertUTF8ToJavaString(env, source->GetURL().spec());
337 Java_WebContentsDelegateAndroid_onUpdateUrl(env,
338 obj.obj(),
339 java_url.obj());
342 void WebContentsDelegateAndroid::HandleKeyboardEvent(
343 WebContents* source,
344 const content::NativeWebKeyboardEvent& event) {
345 jobject key_event = event.os_event;
346 if (key_event) {
347 JNIEnv* env = AttachCurrentThread();
348 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
349 if (obj.is_null())
350 return;
351 Java_WebContentsDelegateAndroid_handleKeyboardEvent(
352 env, obj.obj(), key_event);
356 bool WebContentsDelegateAndroid::TakeFocus(WebContents* source, bool reverse) {
357 JNIEnv* env = AttachCurrentThread();
358 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
359 if (obj.is_null())
360 return WebContentsDelegate::TakeFocus(source, reverse);
361 return Java_WebContentsDelegateAndroid_takeFocus(
362 env, obj.obj(), reverse);
365 void WebContentsDelegateAndroid::ShowRepostFormWarningDialog(
366 WebContents* source) {
367 JNIEnv* env = AttachCurrentThread();
368 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
369 if (obj.is_null())
370 return;
371 Java_WebContentsDelegateAndroid_showRepostFormWarningDialog(env, obj.obj());
374 void WebContentsDelegateAndroid::EnterFullscreenModeForTab(
375 WebContents* web_contents,
376 const GURL& origin) {
377 JNIEnv* env = AttachCurrentThread();
378 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
379 if (obj.is_null())
380 return;
381 Java_WebContentsDelegateAndroid_toggleFullscreenModeForTab(env, obj.obj(),
382 true);
385 void WebContentsDelegateAndroid::ExitFullscreenModeForTab(
386 WebContents* web_contents) {
387 JNIEnv* env = AttachCurrentThread();
388 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
389 if (obj.is_null())
390 return;
391 Java_WebContentsDelegateAndroid_toggleFullscreenModeForTab(env, obj.obj(),
392 false);
395 bool WebContentsDelegateAndroid::IsFullscreenForTabOrPending(
396 const WebContents* web_contents) const {
397 JNIEnv* env = AttachCurrentThread();
398 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
399 if (obj.is_null())
400 return false;
401 return Java_WebContentsDelegateAndroid_isFullscreenForTabOrPending(
402 env, obj.obj());
405 void WebContentsDelegateAndroid::ShowValidationMessage(
406 WebContents* web_contents,
407 const gfx::Rect& anchor_in_root_view,
408 const base::string16& main_text,
409 const base::string16& sub_text) {
410 RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
411 if (rwhv) {
412 validation_message_bubble_.reset(
413 new ValidationMessageBubbleAndroid(rwhv->GetRenderWidgetHost(),
414 anchor_in_root_view,
415 main_text,
416 sub_text));
420 void WebContentsDelegateAndroid::HideValidationMessage(
421 WebContents* web_contents) {
422 validation_message_bubble_.reset();
425 void WebContentsDelegateAndroid::MoveValidationMessage(
426 WebContents* web_contents,
427 const gfx::Rect& anchor_in_root_view) {
428 if (!validation_message_bubble_)
429 return;
430 RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
431 if (rwhv) {
432 validation_message_bubble_->SetPositionRelativeToAnchor(
433 rwhv->GetRenderWidgetHost(), anchor_in_root_view);
436 // ----------------------------------------------------------------------------
437 // Native JNI methods
438 // ----------------------------------------------------------------------------
440 // Register native methods
442 bool RegisterWebContentsDelegateAndroid(JNIEnv* env) {
443 return RegisterNativesImpl(env);
446 } // namespace web_contents_delegate_android