Fix crash on app list start page keyboard navigation with <4 apps.
[chromium-blink-merge.git] / components / web_contents_delegate_android / web_contents_delegate_android.cc
blob63b83a5082d6fd7f3ea3287b282d92d996172e09
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(
154 const WebContents* source) {
155 JNIEnv* env = AttachCurrentThread();
156 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
157 if (obj.is_null())
158 return;
159 Java_WebContentsDelegateAndroid_visibleSSLStateChanged(
160 env,
161 obj.obj());
164 void WebContentsDelegateAndroid::ActivateContents(WebContents* contents) {
165 JNIEnv* env = AttachCurrentThread();
166 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
167 if (obj.is_null())
168 return;
169 Java_WebContentsDelegateAndroid_activateContents(env, obj.obj());
172 void WebContentsDelegateAndroid::DeactivateContents(WebContents* contents) {
173 // On desktop the current window is deactivated here, bringing the next window
174 // to focus. Not implemented on Android.
177 void WebContentsDelegateAndroid::LoadingStateChanged(WebContents* source,
178 bool to_different_document) {
179 JNIEnv* env = AttachCurrentThread();
180 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
181 if (obj.is_null())
182 return;
183 bool has_stopped = source == NULL || !source->IsLoading();
185 if (has_stopped)
186 Java_WebContentsDelegateAndroid_onLoadStopped(env, obj.obj());
187 else
188 Java_WebContentsDelegateAndroid_onLoadStarted(env, obj.obj());
191 void WebContentsDelegateAndroid::LoadProgressChanged(WebContents* source,
192 double progress) {
193 JNIEnv* env = AttachCurrentThread();
194 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
195 if (obj.is_null())
196 return;
197 Java_WebContentsDelegateAndroid_notifyLoadProgressChanged(
198 env,
199 obj.obj(),
200 progress);
203 void WebContentsDelegateAndroid::RendererUnresponsive(WebContents* source) {
204 JNIEnv* env = AttachCurrentThread();
205 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
206 if (obj.is_null())
207 return;
208 Java_WebContentsDelegateAndroid_rendererUnresponsive(env, obj.obj());
211 void WebContentsDelegateAndroid::RendererResponsive(WebContents* source) {
212 JNIEnv* env = AttachCurrentThread();
213 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
214 if (obj.is_null())
215 return;
216 Java_WebContentsDelegateAndroid_rendererResponsive(env, obj.obj());
219 void WebContentsDelegateAndroid::DidNavigateToPendingEntry(
220 WebContents* source) {
221 JNIEnv* env = AttachCurrentThread();
222 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
223 if (obj.is_null())
224 return;
225 Java_WebContentsDelegateAndroid_didNavigateToPendingEntry(env, obj.obj());
228 bool WebContentsDelegateAndroid::ShouldCreateWebContents(
229 WebContents* web_contents,
230 int route_id,
231 int main_frame_route_id,
232 WindowContainerType window_container_type,
233 const base::string16& frame_name,
234 const GURL& target_url,
235 const std::string& partition_id,
236 content::SessionStorageNamespace* session_storage_namespace) {
237 JNIEnv* env = AttachCurrentThread();
238 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
239 if (obj.is_null())
240 return true;
241 ScopedJavaLocalRef<jstring> java_url =
242 ConvertUTF8ToJavaString(env, target_url.spec());
243 return Java_WebContentsDelegateAndroid_shouldCreateWebContents(env, obj.obj(),
244 java_url.obj());
247 bool WebContentsDelegateAndroid::OnGoToEntryOffset(int offset) {
248 JNIEnv* env = AttachCurrentThread();
249 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
250 if (obj.is_null())
251 return true;
252 return Java_WebContentsDelegateAndroid_onGoToEntryOffset(env, obj.obj(),
253 offset);
256 void WebContentsDelegateAndroid::WebContentsCreated(
257 WebContents* source_contents, int opener_render_frame_id,
258 const base::string16& frame_name, const GURL& target_url,
259 WebContents* new_contents) {
260 JNIEnv* env = AttachCurrentThread();
261 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
262 if (obj.is_null())
263 return;
265 ScopedJavaLocalRef<jobject> jsource_contents;
266 if (source_contents)
267 jsource_contents = source_contents->GetJavaWebContents();
268 ScopedJavaLocalRef<jobject> jnew_contents;
269 if (new_contents)
270 jnew_contents = new_contents->GetJavaWebContents();
272 Java_WebContentsDelegateAndroid_webContentsCreated(
273 env,
274 obj.obj(),
275 jsource_contents.obj(),
276 opener_render_frame_id,
277 base::android::ConvertUTF16ToJavaString(env, frame_name).Release(),
278 base::android::ConvertUTF8ToJavaString(env, target_url.spec()).Release(),
279 jnew_contents.obj());
282 void WebContentsDelegateAndroid::CloseContents(WebContents* source) {
283 JNIEnv* env = AttachCurrentThread();
284 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
285 if (obj.is_null())
286 return;
287 Java_WebContentsDelegateAndroid_closeContents(env, obj.obj());
290 void WebContentsDelegateAndroid::MoveContents(WebContents* source,
291 const gfx::Rect& pos) {
292 // Do nothing.
295 bool WebContentsDelegateAndroid::AddMessageToConsole(
296 WebContents* source,
297 int32 level,
298 const base::string16& message,
299 int32 line_no,
300 const base::string16& source_id) {
301 JNIEnv* env = AttachCurrentThread();
302 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
303 if (obj.is_null())
304 return WebContentsDelegate::AddMessageToConsole(source, level, message,
305 line_no, source_id);
306 ScopedJavaLocalRef<jstring> jmessage(ConvertUTF16ToJavaString(env, message));
307 ScopedJavaLocalRef<jstring> jsource_id(
308 ConvertUTF16ToJavaString(env, source_id));
309 int jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_DEBUG;
310 switch (level) {
311 case logging::LOG_VERBOSE:
312 jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_DEBUG;
313 break;
314 case logging::LOG_INFO:
315 jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_LOG;
316 break;
317 case logging::LOG_WARNING:
318 jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_WARNING;
319 break;
320 case logging::LOG_ERROR:
321 jlevel = WEB_CONTENTS_DELEGATE_LOG_LEVEL_ERROR;
322 break;
323 default:
324 NOTREACHED();
326 return Java_WebContentsDelegateAndroid_addMessageToConsole(
327 env,
328 GetJavaDelegate(env).obj(),
329 jlevel,
330 jmessage.obj(),
331 line_no,
332 jsource_id.obj());
335 // This is either called from TabContents::DidNavigateMainFramePostCommit() with
336 // an empty GURL or responding to RenderViewHost::OnMsgUpateTargetURL(). In
337 // Chrome, the latter is not always called, especially not during history
338 // navigation. So we only handle the first case and pass the source TabContents'
339 // url to Java to update the UI.
340 void WebContentsDelegateAndroid::UpdateTargetURL(WebContents* source,
341 const GURL& url) {
342 if (!url.is_empty())
343 return;
344 JNIEnv* env = AttachCurrentThread();
345 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
346 if (obj.is_null())
347 return;
348 ScopedJavaLocalRef<jstring> java_url =
349 ConvertUTF8ToJavaString(env, source->GetURL().spec());
350 Java_WebContentsDelegateAndroid_onUpdateUrl(env,
351 obj.obj(),
352 java_url.obj());
355 void WebContentsDelegateAndroid::HandleKeyboardEvent(
356 WebContents* source,
357 const content::NativeWebKeyboardEvent& event) {
358 jobject key_event = event.os_event;
359 if (key_event) {
360 JNIEnv* env = AttachCurrentThread();
361 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
362 if (obj.is_null())
363 return;
364 Java_WebContentsDelegateAndroid_handleKeyboardEvent(
365 env, obj.obj(), key_event);
369 bool WebContentsDelegateAndroid::TakeFocus(WebContents* source, bool reverse) {
370 JNIEnv* env = AttachCurrentThread();
371 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
372 if (obj.is_null())
373 return WebContentsDelegate::TakeFocus(source, reverse);
374 return Java_WebContentsDelegateAndroid_takeFocus(
375 env, obj.obj(), reverse);
378 void WebContentsDelegateAndroid::ShowRepostFormWarningDialog(
379 WebContents* source) {
380 JNIEnv* env = AttachCurrentThread();
381 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
382 if (obj.is_null())
383 return;
384 Java_WebContentsDelegateAndroid_showRepostFormWarningDialog(env, obj.obj());
387 void WebContentsDelegateAndroid::EnterFullscreenModeForTab(
388 WebContents* web_contents,
389 const GURL& origin) {
390 JNIEnv* env = AttachCurrentThread();
391 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
392 if (obj.is_null())
393 return;
394 Java_WebContentsDelegateAndroid_toggleFullscreenModeForTab(env, obj.obj(),
395 true);
398 void WebContentsDelegateAndroid::ExitFullscreenModeForTab(
399 WebContents* web_contents) {
400 JNIEnv* env = AttachCurrentThread();
401 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
402 if (obj.is_null())
403 return;
404 Java_WebContentsDelegateAndroid_toggleFullscreenModeForTab(env, obj.obj(),
405 false);
408 bool WebContentsDelegateAndroid::IsFullscreenForTabOrPending(
409 const WebContents* web_contents) const {
410 JNIEnv* env = AttachCurrentThread();
411 ScopedJavaLocalRef<jobject> obj = GetJavaDelegate(env);
412 if (obj.is_null())
413 return false;
414 return Java_WebContentsDelegateAndroid_isFullscreenForTabOrPending(
415 env, obj.obj());
418 void WebContentsDelegateAndroid::ShowValidationMessage(
419 WebContents* web_contents,
420 const gfx::Rect& anchor_in_root_view,
421 const base::string16& main_text,
422 const base::string16& sub_text) {
423 RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
424 if (rwhv) {
425 validation_message_bubble_.reset(
426 new ValidationMessageBubbleAndroid(rwhv->GetRenderWidgetHost(),
427 anchor_in_root_view,
428 main_text,
429 sub_text));
433 void WebContentsDelegateAndroid::HideValidationMessage(
434 WebContents* web_contents) {
435 validation_message_bubble_.reset();
438 void WebContentsDelegateAndroid::MoveValidationMessage(
439 WebContents* web_contents,
440 const gfx::Rect& anchor_in_root_view) {
441 if (!validation_message_bubble_)
442 return;
443 RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
444 if (rwhv) {
445 validation_message_bubble_->SetPositionRelativeToAnchor(
446 rwhv->GetRenderWidgetHost(), anchor_in_root_view);
449 // ----------------------------------------------------------------------------
450 // Native JNI methods
451 // ----------------------------------------------------------------------------
453 // Register native methods
455 bool RegisterWebContentsDelegateAndroid(JNIEnv* env) {
456 return RegisterNativesImpl(env);
459 } // namespace web_contents_delegate_android