[Mac] Implement Ambient Light API
[chromium-blink-merge.git] / content / browser / android / content_view_core_impl.cc
blobf868b4f0147d1e7952e6af39a2ce40439e63fc99
1 // Copyright 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 "content/browser/android/content_view_core_impl.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "cc/layers/layer.h"
17 #include "cc/layers/solid_color_layer.h"
18 #include "cc/output/begin_frame_args.h"
19 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
20 #include "content/browser/android/gesture_event_type.h"
21 #include "content/browser/android/interstitial_page_delegate_android.h"
22 #include "content/browser/android/java/gin_java_bridge_dispatcher_host.h"
23 #include "content/browser/android/load_url_params.h"
24 #include "content/browser/android/popup_touch_handle_drawable.h"
25 #include "content/browser/frame_host/interstitial_page_impl.h"
26 #include "content/browser/geolocation/geolocation_service_context.h"
27 #include "content/browser/media/media_web_contents_observer.h"
28 #include "content/browser/renderer_host/compositor_impl_android.h"
29 #include "content/browser/renderer_host/input/motion_event_android.h"
30 #include "content/browser/renderer_host/input/web_input_event_builders_android.h"
31 #include "content/browser/renderer_host/input/web_input_event_util.h"
32 #include "content/browser/renderer_host/render_view_host_impl.h"
33 #include "content/browser/renderer_host/render_widget_host_impl.h"
34 #include "content/browser/renderer_host/render_widget_host_view_android.h"
35 #include "content/browser/transition_request_manager.h"
36 #include "content/browser/web_contents/web_contents_view_android.h"
37 #include "content/common/frame_messages.h"
38 #include "content/common/input/web_input_event_traits.h"
39 #include "content/common/input_messages.h"
40 #include "content/common/view_messages.h"
41 #include "content/public/browser/browser_context.h"
42 #include "content/public/browser/browser_thread.h"
43 #include "content/public/browser/favicon_status.h"
44 #include "content/public/browser/render_frame_host.h"
45 #include "content/public/browser/screen_orientation_dispatcher_host.h"
46 #include "content/public/browser/ssl_host_state_delegate.h"
47 #include "content/public/browser/web_contents.h"
48 #include "content/public/common/content_client.h"
49 #include "content/public/common/content_switches.h"
50 #include "content/public/common/menu_item.h"
51 #include "content/public/common/user_agent.h"
52 #include "jni/ContentViewCore_jni.h"
53 #include "third_party/WebKit/public/web/WebInputEvent.h"
54 #include "ui/base/android/view_android.h"
55 #include "ui/base/android/window_android.h"
56 #include "ui/gfx/android/java_bitmap.h"
57 #include "ui/gfx/geometry/size_conversions.h"
58 #include "ui/gfx/geometry/size_f.h"
59 #include "ui/gfx/screen.h"
61 using base::android::AttachCurrentThread;
62 using base::android::ConvertJavaStringToUTF16;
63 using base::android::ConvertJavaStringToUTF8;
64 using base::android::ConvertUTF16ToJavaString;
65 using base::android::ConvertUTF8ToJavaString;
66 using base::android::ScopedJavaLocalRef;
67 using blink::WebGestureEvent;
68 using blink::WebInputEvent;
70 // Describes the type and enabled state of a select popup item.
71 namespace {
73 // A Java counterpart will be generated for this enum.
74 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.content.browser.input
75 enum PopupItemType {
76 // Popup item is of type group
77 POPUP_ITEM_TYPE_GROUP,
79 // Popup item is disabled
80 POPUP_ITEM_TYPE_DISABLED,
82 // Popup item is enabled
83 POPUP_ITEM_TYPE_ENABLED,
86 } //namespace
88 namespace content {
90 namespace {
92 const void* kContentViewUserDataKey = &kContentViewUserDataKey;
94 int GetRenderProcessIdFromRenderViewHost(RenderViewHost* host) {
95 DCHECK(host);
96 RenderProcessHost* render_process = host->GetProcess();
97 DCHECK(render_process);
98 if (render_process->HasConnection())
99 return render_process->GetHandle();
100 else
101 return 0;
104 ScopedJavaLocalRef<jobject> CreateJavaRect(
105 JNIEnv* env,
106 const gfx::Rect& rect) {
107 return ScopedJavaLocalRef<jobject>(
108 Java_ContentViewCore_createRect(env,
109 static_cast<int>(rect.x()),
110 static_cast<int>(rect.y()),
111 static_cast<int>(rect.right()),
112 static_cast<int>(rect.bottom())));
115 int ToGestureEventType(WebInputEvent::Type type) {
116 switch (type) {
117 case WebInputEvent::GestureScrollBegin:
118 return GESTURE_EVENT_TYPE_SCROLL_START;
119 case WebInputEvent::GestureScrollEnd:
120 return GESTURE_EVENT_TYPE_SCROLL_END;
121 case WebInputEvent::GestureScrollUpdate:
122 return GESTURE_EVENT_TYPE_SCROLL_BY;
123 case WebInputEvent::GestureFlingStart:
124 return GESTURE_EVENT_TYPE_FLING_START;
125 case WebInputEvent::GestureFlingCancel:
126 return GESTURE_EVENT_TYPE_FLING_CANCEL;
127 case WebInputEvent::GestureShowPress:
128 return GESTURE_EVENT_TYPE_SHOW_PRESS;
129 case WebInputEvent::GestureTap:
130 return GESTURE_EVENT_TYPE_SINGLE_TAP_CONFIRMED;
131 case WebInputEvent::GestureTapUnconfirmed:
132 return GESTURE_EVENT_TYPE_SINGLE_TAP_UNCONFIRMED;
133 case WebInputEvent::GestureTapDown:
134 return GESTURE_EVENT_TYPE_TAP_DOWN;
135 case WebInputEvent::GestureTapCancel:
136 return GESTURE_EVENT_TYPE_TAP_CANCEL;
137 case WebInputEvent::GestureDoubleTap:
138 return GESTURE_EVENT_TYPE_DOUBLE_TAP;
139 case WebInputEvent::GestureLongPress:
140 return GESTURE_EVENT_TYPE_LONG_PRESS;
141 case WebInputEvent::GestureLongTap:
142 return GESTURE_EVENT_TYPE_LONG_TAP;
143 case WebInputEvent::GesturePinchBegin:
144 return GESTURE_EVENT_TYPE_PINCH_BEGIN;
145 case WebInputEvent::GesturePinchEnd:
146 return GESTURE_EVENT_TYPE_PINCH_END;
147 case WebInputEvent::GesturePinchUpdate:
148 return GESTURE_EVENT_TYPE_PINCH_BY;
149 case WebInputEvent::GestureTwoFingerTap:
150 default:
151 NOTREACHED() << "Invalid source gesture type: "
152 << WebInputEventTraits::GetName(type);
153 return -1;
157 float GetPrimaryDisplayDeviceScaleFactor() {
158 const gfx::Display& display =
159 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
160 return display.device_scale_factor();
163 } // namespace
165 // Enables a callback when the underlying WebContents is destroyed, to enable
166 // nulling the back-pointer.
167 class ContentViewCoreImpl::ContentViewUserData
168 : public base::SupportsUserData::Data {
169 public:
170 explicit ContentViewUserData(ContentViewCoreImpl* content_view_core)
171 : content_view_core_(content_view_core) {
174 virtual ~ContentViewUserData() {
175 // TODO(joth): When chrome has finished removing the TabContents class (see
176 // crbug.com/107201) consider inverting relationship, so ContentViewCore
177 // would own WebContents. That effectively implies making the WebContents
178 // destructor private on Android.
179 delete content_view_core_;
182 ContentViewCoreImpl* get() const { return content_view_core_; }
184 private:
185 // Not using scoped_ptr as ContentViewCoreImpl destructor is private.
186 ContentViewCoreImpl* content_view_core_;
188 DISALLOW_IMPLICIT_CONSTRUCTORS(ContentViewUserData);
191 // static
192 ContentViewCoreImpl* ContentViewCoreImpl::FromWebContents(
193 content::WebContents* web_contents) {
194 ContentViewCoreImpl::ContentViewUserData* data =
195 static_cast<ContentViewCoreImpl::ContentViewUserData*>(
196 web_contents->GetUserData(kContentViewUserDataKey));
197 return data ? data->get() : NULL;
200 // static
201 ContentViewCore* ContentViewCore::FromWebContents(
202 content::WebContents* web_contents) {
203 return ContentViewCoreImpl::FromWebContents(web_contents);
206 // static
207 ContentViewCore* ContentViewCore::GetNativeContentViewCore(JNIEnv* env,
208 jobject obj) {
209 return reinterpret_cast<ContentViewCore*>(
210 Java_ContentViewCore_getNativeContentViewCore(env, obj));
213 ContentViewCoreImpl::ContentViewCoreImpl(
214 JNIEnv* env,
215 jobject obj,
216 WebContents* web_contents,
217 ui::ViewAndroid* view_android,
218 ui::WindowAndroid* window_android,
219 jobject java_bridge_retained_object_set)
220 : WebContentsObserver(web_contents),
221 java_ref_(env, obj),
222 web_contents_(static_cast<WebContentsImpl*>(web_contents)),
223 root_layer_(cc::SolidColorLayer::Create()),
224 dpi_scale_(GetPrimaryDisplayDeviceScaleFactor()),
225 view_android_(view_android),
226 window_android_(window_android),
227 device_orientation_(0),
228 accessibility_enabled_(false) {
229 CHECK(web_contents) <<
230 "A ContentViewCoreImpl should be created with a valid WebContents.";
231 DCHECK(view_android_);
232 DCHECK(window_android_);
234 root_layer_->SetBackgroundColor(GetBackgroundColor(env, obj));
235 gfx::Size physical_size(
236 Java_ContentViewCore_getPhysicalBackingWidthPix(env, obj),
237 Java_ContentViewCore_getPhysicalBackingHeightPix(env, obj));
238 root_layer_->SetBounds(physical_size);
239 root_layer_->SetIsDrawable(true);
241 // Currently, the only use case we have for overriding a user agent involves
242 // spoofing a desktop Linux user agent for "Request desktop site".
243 // Automatically set it for all WebContents so that it is available when a
244 // NavigationEntry requires the user agent to be overridden.
245 const char kLinuxInfoStr[] = "X11; Linux x86_64";
246 std::string product = content::GetContentClient()->GetProduct();
247 std::string spoofed_ua =
248 BuildUserAgentFromOSAndProduct(kLinuxInfoStr, product);
249 web_contents->SetUserAgentOverride(spoofed_ua);
251 java_bridge_dispatcher_host_ =
252 new GinJavaBridgeDispatcherHost(web_contents,
253 java_bridge_retained_object_set);
255 InitWebContents();
258 ContentViewCoreImpl::~ContentViewCoreImpl() {
259 JNIEnv* env = base::android::AttachCurrentThread();
260 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
261 java_ref_.reset();
262 if (!j_obj.is_null()) {
263 Java_ContentViewCore_onNativeContentViewCoreDestroyed(
264 env, j_obj.obj(), reinterpret_cast<intptr_t>(this));
268 base::android::ScopedJavaLocalRef<jobject>
269 ContentViewCoreImpl::GetWebContentsAndroid(JNIEnv* env, jobject obj) {
270 return web_contents_->GetJavaWebContents();
273 void ContentViewCoreImpl::OnJavaContentViewCoreDestroyed(JNIEnv* env,
274 jobject obj) {
275 DCHECK(env->IsSameObject(java_ref_.get(env).obj(), obj));
276 java_ref_.reset();
277 // Java peer has gone, ContentViewCore is not functional and waits to
278 // be destroyed with WebContents.
279 // We need to reset WebContentsViewAndroid's reference, otherwise, there
280 // could have call in when swapping the WebContents,
281 // see http://crbug.com/383939 .
282 DCHECK(web_contents_);
283 static_cast<WebContentsViewAndroid*>(
284 static_cast<WebContentsImpl*>(web_contents_)->GetView())->
285 SetContentViewCore(NULL);
288 void ContentViewCoreImpl::InitWebContents() {
289 DCHECK(web_contents_);
290 static_cast<WebContentsViewAndroid*>(
291 static_cast<WebContentsImpl*>(web_contents_)->GetView())->
292 SetContentViewCore(this);
293 DCHECK(!web_contents_->GetUserData(kContentViewUserDataKey));
294 web_contents_->SetUserData(kContentViewUserDataKey,
295 new ContentViewUserData(this));
298 void ContentViewCoreImpl::RenderViewReady() {
299 JNIEnv* env = AttachCurrentThread();
300 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
301 if (!obj.is_null())
302 Java_ContentViewCore_onRenderProcessChange(env, obj.obj());
304 if (device_orientation_ != 0)
305 SendOrientationChangeEventInternal();
308 void ContentViewCoreImpl::RenderViewHostChanged(RenderViewHost* old_host,
309 RenderViewHost* new_host) {
310 int old_pid = 0;
311 if (old_host) {
312 old_pid = GetRenderProcessIdFromRenderViewHost(old_host);
314 RenderWidgetHostViewAndroid* view =
315 static_cast<RenderWidgetHostViewAndroid*>(old_host->GetView());
316 if (view)
317 view->SetContentViewCore(NULL);
319 view = static_cast<RenderWidgetHostViewAndroid*>(new_host->GetView());
320 if (view)
321 view->SetContentViewCore(this);
323 int new_pid = GetRenderProcessIdFromRenderViewHost(
324 web_contents_->GetRenderViewHost());
325 if (new_pid != old_pid) {
326 // Notify the Java side that the renderer process changed.
327 JNIEnv* env = AttachCurrentThread();
328 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
329 if (!obj.is_null()) {
330 Java_ContentViewCore_onRenderProcessChange(env, obj.obj());
334 SetFocusInternal(HasFocus());
335 SetAccessibilityEnabledInternal(accessibility_enabled_);
338 RenderWidgetHostViewAndroid*
339 ContentViewCoreImpl::GetRenderWidgetHostViewAndroid() const {
340 RenderWidgetHostView* rwhv = NULL;
341 if (web_contents_) {
342 rwhv = web_contents_->GetRenderWidgetHostView();
343 if (web_contents_->ShowingInterstitialPage()) {
344 rwhv = web_contents_->GetInterstitialPage()
345 ->GetMainFrame()
346 ->GetRenderViewHost()
347 ->GetView();
350 return static_cast<RenderWidgetHostViewAndroid*>(rwhv);
353 ScopedJavaLocalRef<jobject> ContentViewCoreImpl::GetJavaObject() {
354 JNIEnv* env = AttachCurrentThread();
355 return java_ref_.get(env);
358 jint ContentViewCoreImpl::GetBackgroundColor(JNIEnv* env, jobject obj) {
359 RenderWidgetHostViewAndroid* rwhva = GetRenderWidgetHostViewAndroid();
360 if (!rwhva)
361 return SK_ColorWHITE;
362 return rwhva->GetCachedBackgroundColor();
365 void ContentViewCoreImpl::PauseOrResumeGeolocation(bool should_pause) {
366 if (should_pause)
367 web_contents_->GetGeolocationServiceContext()->PauseUpdates();
368 else
369 web_contents_->GetGeolocationServiceContext()->ResumeUpdates();
372 // All positions and sizes are in CSS pixels.
373 // Note that viewport_width/height is a best effort based.
374 // ContentViewCore has the actual information about the physical viewport size.
375 void ContentViewCoreImpl::UpdateFrameInfo(
376 const gfx::Vector2dF& scroll_offset,
377 float page_scale_factor,
378 const gfx::Vector2dF& page_scale_factor_limits,
379 const gfx::SizeF& content_size,
380 const gfx::SizeF& viewport_size,
381 const gfx::Vector2dF& controls_offset,
382 const gfx::Vector2dF& content_offset) {
383 JNIEnv* env = AttachCurrentThread();
384 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
385 if (obj.is_null())
386 return;
388 window_android_->set_content_offset(
389 gfx::ScaleVector2d(content_offset, dpi_scale_));
391 Java_ContentViewCore_updateFrameInfo(
392 env, obj.obj(),
393 scroll_offset.x(),
394 scroll_offset.y(),
395 page_scale_factor,
396 page_scale_factor_limits.x(),
397 page_scale_factor_limits.y(),
398 content_size.width(),
399 content_size.height(),
400 viewport_size.width(),
401 viewport_size.height(),
402 controls_offset.y(),
403 content_offset.y());
406 void ContentViewCoreImpl::SetTitle(const base::string16& title) {
407 JNIEnv* env = AttachCurrentThread();
408 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
409 if (obj.is_null())
410 return;
411 ScopedJavaLocalRef<jstring> jtitle =
412 ConvertUTF8ToJavaString(env, base::UTF16ToUTF8(title));
413 Java_ContentViewCore_setTitle(env, obj.obj(), jtitle.obj());
416 void ContentViewCoreImpl::OnBackgroundColorChanged(SkColor color) {
417 root_layer_->SetBackgroundColor(color);
419 JNIEnv* env = AttachCurrentThread();
420 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
421 if (obj.is_null())
422 return;
423 Java_ContentViewCore_onBackgroundColorChanged(env, obj.obj(), color);
426 void ContentViewCoreImpl::ShowSelectPopupMenu(
427 RenderFrameHost* frame,
428 const gfx::Rect& bounds,
429 const std::vector<MenuItem>& items,
430 int selected_item,
431 bool multiple) {
432 JNIEnv* env = AttachCurrentThread();
433 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
434 if (j_obj.is_null())
435 return;
437 ScopedJavaLocalRef<jobject> bounds_rect(CreateJavaRect(env, bounds));
439 // For multi-select list popups we find the list of previous selections by
440 // iterating through the items. But for single selection popups we take the
441 // given |selected_item| as is.
442 ScopedJavaLocalRef<jintArray> selected_array;
443 if (multiple) {
444 scoped_ptr<jint[]> native_selected_array(new jint[items.size()]);
445 size_t selected_count = 0;
446 for (size_t i = 0; i < items.size(); ++i) {
447 if (items[i].checked)
448 native_selected_array[selected_count++] = i;
451 selected_array = ScopedJavaLocalRef<jintArray>(
452 env, env->NewIntArray(selected_count));
453 env->SetIntArrayRegion(selected_array.obj(), 0, selected_count,
454 native_selected_array.get());
455 } else {
456 selected_array = ScopedJavaLocalRef<jintArray>(env, env->NewIntArray(1));
457 jint value = selected_item;
458 env->SetIntArrayRegion(selected_array.obj(), 0, 1, &value);
461 ScopedJavaLocalRef<jintArray> enabled_array(env,
462 env->NewIntArray(items.size()));
463 std::vector<base::string16> labels;
464 labels.reserve(items.size());
465 for (size_t i = 0; i < items.size(); ++i) {
466 labels.push_back(items[i].label);
467 jint enabled =
468 (items[i].type == MenuItem::GROUP ? POPUP_ITEM_TYPE_GROUP :
469 (items[i].enabled ? POPUP_ITEM_TYPE_ENABLED :
470 POPUP_ITEM_TYPE_DISABLED));
471 env->SetIntArrayRegion(enabled_array.obj(), i, 1, &enabled);
473 ScopedJavaLocalRef<jobjectArray> items_array(
474 base::android::ToJavaArrayOfStrings(env, labels));
475 Java_ContentViewCore_showSelectPopup(env,
476 j_obj.obj(),
477 reinterpret_cast<intptr_t>(frame),
478 bounds_rect.obj(),
479 items_array.obj(),
480 enabled_array.obj(),
481 multiple,
482 selected_array.obj());
485 void ContentViewCoreImpl::HideSelectPopupMenu() {
486 JNIEnv* env = AttachCurrentThread();
487 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
488 if (!j_obj.is_null())
489 Java_ContentViewCore_hideSelectPopup(env, j_obj.obj());
492 void ContentViewCoreImpl::OnGestureEventAck(const blink::WebGestureEvent& event,
493 InputEventAckState ack_result) {
494 JNIEnv* env = AttachCurrentThread();
495 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
496 if (j_obj.is_null())
497 return;
499 switch (event.type) {
500 case WebInputEvent::GestureFlingStart:
501 if (ack_result == INPUT_EVENT_ACK_STATE_CONSUMED) {
502 // The view expects the fling velocity in pixels/s.
503 Java_ContentViewCore_onFlingStartEventConsumed(env, j_obj.obj(),
504 event.data.flingStart.velocityX * dpi_scale(),
505 event.data.flingStart.velocityY * dpi_scale());
506 } else {
507 // If a scroll ends with a fling, a SCROLL_END event is never sent.
508 // However, if that fling went unconsumed, we still need to let the
509 // listeners know that scrolling has ended.
510 Java_ContentViewCore_onScrollEndEventAck(env, j_obj.obj());
513 if (ack_result == INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS) {
514 // The view expects the fling velocity in pixels/s.
515 Java_ContentViewCore_onFlingStartEventHadNoConsumer(env, j_obj.obj(),
516 event.data.flingStart.velocityX * dpi_scale(),
517 event.data.flingStart.velocityY * dpi_scale());
519 break;
520 case WebInputEvent::GestureFlingCancel:
521 Java_ContentViewCore_onFlingCancelEventAck(env, j_obj.obj());
522 break;
523 case WebInputEvent::GestureScrollBegin:
524 Java_ContentViewCore_onScrollBeginEventAck(env, j_obj.obj());
525 break;
526 case WebInputEvent::GestureScrollUpdate:
527 if (ack_result == INPUT_EVENT_ACK_STATE_CONSUMED)
528 Java_ContentViewCore_onScrollUpdateGestureConsumed(env, j_obj.obj());
529 break;
530 case WebInputEvent::GestureScrollEnd:
531 Java_ContentViewCore_onScrollEndEventAck(env, j_obj.obj());
532 break;
533 case WebInputEvent::GesturePinchBegin:
534 Java_ContentViewCore_onPinchBeginEventAck(env, j_obj.obj());
535 break;
536 case WebInputEvent::GesturePinchEnd:
537 Java_ContentViewCore_onPinchEndEventAck(env, j_obj.obj());
538 break;
539 case WebInputEvent::GestureTap:
540 Java_ContentViewCore_onSingleTapEventAck(
541 env,
542 j_obj.obj(),
543 ack_result == INPUT_EVENT_ACK_STATE_CONSUMED,
544 event.x * dpi_scale(),
545 event.y * dpi_scale());
546 break;
547 default:
548 break;
552 bool ContentViewCoreImpl::FilterInputEvent(const blink::WebInputEvent& event) {
553 if (event.type != WebInputEvent::GestureTap &&
554 event.type != WebInputEvent::GestureDoubleTap &&
555 event.type != WebInputEvent::GestureLongTap &&
556 event.type != WebInputEvent::GestureLongPress)
557 return false;
559 JNIEnv* env = AttachCurrentThread();
560 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
561 if (j_obj.is_null())
562 return false;
564 const blink::WebGestureEvent& gesture =
565 static_cast<const blink::WebGestureEvent&>(event);
566 int gesture_type = ToGestureEventType(event.type);
567 return Java_ContentViewCore_filterTapOrPressEvent(env,
568 j_obj.obj(),
569 gesture_type,
570 gesture.x * dpi_scale(),
571 gesture.y * dpi_scale());
573 // TODO(jdduke): Also report double-tap UMA, crbug/347568.
576 bool ContentViewCoreImpl::HasFocus() {
577 JNIEnv* env = AttachCurrentThread();
578 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
579 if (obj.is_null())
580 return false;
581 return Java_ContentViewCore_hasFocus(env, obj.obj());
584 void ContentViewCoreImpl::OnSelectionChanged(const std::string& text) {
585 JNIEnv* env = AttachCurrentThread();
586 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
587 if (obj.is_null())
588 return;
589 ScopedJavaLocalRef<jstring> jtext = ConvertUTF8ToJavaString(env, text);
590 Java_ContentViewCore_onSelectionChanged(env, obj.obj(), jtext.obj());
593 void ContentViewCoreImpl::OnSelectionEvent(ui::SelectionEventType event,
594 const gfx::PointF& position) {
595 JNIEnv* env = AttachCurrentThread();
596 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
597 if (j_obj.is_null())
598 return;
599 Java_ContentViewCore_onSelectionEvent(env, j_obj.obj(), event,
600 position.x() * dpi_scale(),
601 position.y() * dpi_scale());
604 scoped_ptr<ui::TouchHandleDrawable>
605 ContentViewCoreImpl::CreatePopupTouchHandleDrawable() {
606 JNIEnv* env = AttachCurrentThread();
607 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
608 if (obj.is_null()) {
609 NOTREACHED();
610 return scoped_ptr<ui::TouchHandleDrawable>();
612 return scoped_ptr<ui::TouchHandleDrawable>(new PopupTouchHandleDrawable(
613 Java_ContentViewCore_createPopupTouchHandleDrawable(env, obj.obj()),
614 dpi_scale_));
617 void ContentViewCoreImpl::ShowPastePopup(int x_dip, int y_dip) {
618 RenderWidgetHostViewAndroid* view = GetRenderWidgetHostViewAndroid();
619 if (!view)
620 return;
622 view->OnShowingPastePopup(gfx::PointF(x_dip, y_dip));
624 JNIEnv* env = AttachCurrentThread();
625 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
626 if (obj.is_null())
627 return;
628 Java_ContentViewCore_showPastePopupWithFeedback(
629 env, obj.obj(), static_cast<jint>(x_dip * dpi_scale()),
630 static_cast<jint>(y_dip * dpi_scale()));
633 void ContentViewCoreImpl::GetScaledContentBitmap(
634 float scale,
635 SkColorType color_type,
636 gfx::Rect src_subrect,
637 ReadbackRequestCallback& result_callback) {
638 RenderWidgetHostViewAndroid* view = GetRenderWidgetHostViewAndroid();
639 if (!view) {
640 result_callback.Run(SkBitmap(), READBACK_FAILED);
641 return;
644 view->GetScaledContentBitmap(scale, color_type, src_subrect,
645 result_callback);
648 void ContentViewCoreImpl::StartContentIntent(const GURL& content_url) {
649 JNIEnv* env = AttachCurrentThread();
650 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
651 if (j_obj.is_null())
652 return;
653 ScopedJavaLocalRef<jstring> jcontent_url =
654 ConvertUTF8ToJavaString(env, content_url.spec());
655 Java_ContentViewCore_startContentIntent(env,
656 j_obj.obj(),
657 jcontent_url.obj());
660 void ContentViewCoreImpl::ShowDisambiguationPopup(
661 const gfx::Rect& rect_pixels,
662 const SkBitmap& zoomed_bitmap) {
663 JNIEnv* env = AttachCurrentThread();
665 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
666 if (obj.is_null())
667 return;
669 ScopedJavaLocalRef<jobject> rect_object(CreateJavaRect(env, rect_pixels));
671 ScopedJavaLocalRef<jobject> java_bitmap =
672 gfx::ConvertToJavaBitmap(&zoomed_bitmap);
673 DCHECK(!java_bitmap.is_null());
675 Java_ContentViewCore_showDisambiguationPopup(env,
676 obj.obj(),
677 rect_object.obj(),
678 java_bitmap.obj());
681 ScopedJavaLocalRef<jobject> ContentViewCoreImpl::CreateTouchEventSynthesizer() {
682 JNIEnv* env = AttachCurrentThread();
684 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
685 if (obj.is_null())
686 return ScopedJavaLocalRef<jobject>();
687 return Java_ContentViewCore_createTouchEventSynthesizer(env, obj.obj());
690 bool ContentViewCoreImpl::ShouldBlockMediaRequest(const GURL& url) {
691 JNIEnv* env = AttachCurrentThread();
693 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
694 if (obj.is_null())
695 return true;
696 ScopedJavaLocalRef<jstring> j_url = ConvertUTF8ToJavaString(env, url.spec());
697 return Java_ContentViewCore_shouldBlockMediaRequest(env, obj.obj(),
698 j_url.obj());
701 void ContentViewCoreImpl::DidStopFlinging() {
702 JNIEnv* env = AttachCurrentThread();
704 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
705 if (!obj.is_null())
706 Java_ContentViewCore_onNativeFlingStopped(env, obj.obj());
709 ScopedJavaLocalRef<jobject> ContentViewCoreImpl::GetContext() const {
710 JNIEnv* env = AttachCurrentThread();
712 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
713 if (obj.is_null())
714 return ScopedJavaLocalRef<jobject>();
716 return Java_ContentViewCore_getContext(env, obj.obj());
719 gfx::Size ContentViewCoreImpl::GetViewSize() const {
720 gfx::Size size = GetViewportSizeDip();
721 if (DoTopControlsShrinkBlinkSize())
722 size.Enlarge(0, -GetTopControlsHeightDip());
723 return size;
726 gfx::Size ContentViewCoreImpl::GetPhysicalBackingSize() const {
727 JNIEnv* env = AttachCurrentThread();
728 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
729 if (j_obj.is_null())
730 return gfx::Size();
731 return gfx::Size(
732 Java_ContentViewCore_getPhysicalBackingWidthPix(env, j_obj.obj()),
733 Java_ContentViewCore_getPhysicalBackingHeightPix(env, j_obj.obj()));
736 gfx::Size ContentViewCoreImpl::GetViewportSizePix() const {
737 JNIEnv* env = AttachCurrentThread();
738 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
739 if (j_obj.is_null())
740 return gfx::Size();
741 return gfx::Size(
742 Java_ContentViewCore_getViewportWidthPix(env, j_obj.obj()),
743 Java_ContentViewCore_getViewportHeightPix(env, j_obj.obj()));
746 int ContentViewCoreImpl::GetTopControlsHeightPix() const {
747 JNIEnv* env = AttachCurrentThread();
748 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
749 if (j_obj.is_null())
750 return 0;
751 return Java_ContentViewCore_getTopControlsHeightPix(env, j_obj.obj());
754 gfx::Size ContentViewCoreImpl::GetViewportSizeDip() const {
755 return gfx::ToCeiledSize(
756 gfx::ScaleSize(GetViewportSizePix(), 1.0f / dpi_scale()));
759 bool ContentViewCoreImpl::DoTopControlsShrinkBlinkSize() const {
760 JNIEnv* env = AttachCurrentThread();
761 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
762 if (j_obj.is_null())
763 return false;
764 return Java_ContentViewCore_doTopControlsShrinkBlinkSize(env, j_obj.obj());
767 float ContentViewCoreImpl::GetTopControlsHeightDip() const {
768 return GetTopControlsHeightPix() / dpi_scale();
771 void ContentViewCoreImpl::AttachLayer(scoped_refptr<cc::Layer> layer) {
772 root_layer_->InsertChild(layer, 0);
773 root_layer_->SetIsDrawable(false);
776 void ContentViewCoreImpl::RemoveLayer(scoped_refptr<cc::Layer> layer) {
777 layer->RemoveFromParent();
779 if (!root_layer_->children().size())
780 root_layer_->SetIsDrawable(true);
783 void ContentViewCoreImpl::MoveRangeSelectionExtent(const gfx::PointF& extent) {
784 if (!web_contents_)
785 return;
787 web_contents_->MoveRangeSelectionExtent(gfx::Point(extent.x(), extent.y()));
790 void ContentViewCoreImpl::SelectBetweenCoordinates(const gfx::PointF& base,
791 const gfx::PointF& extent) {
792 if (!web_contents_)
793 return;
795 gfx::Point base_point = gfx::Point(base.x(), base.y());
796 gfx::Point extent_point = gfx::Point(extent.x(), extent.y());
797 if (base_point == extent_point)
798 return;
800 web_contents_->SelectRange(base_point, extent_point);
803 ui::ViewAndroid* ContentViewCoreImpl::GetViewAndroid() const {
804 return view_android_;
807 ui::WindowAndroid* ContentViewCoreImpl::GetWindowAndroid() const {
808 return window_android_;
811 const scoped_refptr<cc::Layer>& ContentViewCoreImpl::GetLayer() const {
812 return root_layer_;
815 // ----------------------------------------------------------------------------
816 // Methods called from Java via JNI
817 // ----------------------------------------------------------------------------
819 void ContentViewCoreImpl::SelectPopupMenuItems(JNIEnv* env,
820 jobject obj,
821 jlong selectPopupSourceFrame,
822 jintArray indices) {
823 RenderFrameHostImpl* rfhi =
824 reinterpret_cast<RenderFrameHostImpl*>(selectPopupSourceFrame);
825 DCHECK(rfhi);
826 if (indices == NULL) {
827 rfhi->DidCancelPopupMenu();
828 return;
831 int selected_count = env->GetArrayLength(indices);
832 std::vector<int> selected_indices;
833 jint* indices_ptr = env->GetIntArrayElements(indices, NULL);
834 for (int i = 0; i < selected_count; ++i)
835 selected_indices.push_back(indices_ptr[i]);
836 env->ReleaseIntArrayElements(indices, indices_ptr, JNI_ABORT);
837 rfhi->DidSelectPopupMenuItems(selected_indices);
840 WebContents* ContentViewCoreImpl::GetWebContents() const {
841 return web_contents_;
844 void ContentViewCoreImpl::SetFocus(JNIEnv* env, jobject obj, jboolean focused) {
845 SetFocusInternal(focused);
848 void ContentViewCoreImpl::SetFocusInternal(bool focused) {
849 if (!GetRenderWidgetHostViewAndroid())
850 return;
852 if (focused)
853 GetRenderWidgetHostViewAndroid()->Focus();
854 else
855 GetRenderWidgetHostViewAndroid()->Blur();
858 void ContentViewCoreImpl::SendOrientationChangeEvent(JNIEnv* env,
859 jobject obj,
860 jint orientation) {
861 if (device_orientation_ != orientation) {
862 device_orientation_ = orientation;
863 SendOrientationChangeEventInternal();
867 jboolean ContentViewCoreImpl::OnTouchEvent(JNIEnv* env,
868 jobject obj,
869 jobject motion_event,
870 jlong time_ms,
871 jint android_action,
872 jint pointer_count,
873 jint history_size,
874 jint action_index,
875 jfloat pos_x_0,
876 jfloat pos_y_0,
877 jfloat pos_x_1,
878 jfloat pos_y_1,
879 jint pointer_id_0,
880 jint pointer_id_1,
881 jfloat touch_major_0,
882 jfloat touch_major_1,
883 jfloat touch_minor_0,
884 jfloat touch_minor_1,
885 jfloat orientation_0,
886 jfloat orientation_1,
887 jfloat raw_pos_x,
888 jfloat raw_pos_y,
889 jint android_tool_type_0,
890 jint android_tool_type_1,
891 jint android_button_state,
892 jint android_meta_state,
893 jboolean is_touch_handle_event) {
894 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
895 // Avoid synthesizing a touch event if it cannot be forwarded.
896 if (!rwhv)
897 return false;
899 MotionEventAndroid::Pointer pointer0(pointer_id_0,
900 pos_x_0,
901 pos_y_0,
902 touch_major_0,
903 touch_minor_0,
904 orientation_0,
905 android_tool_type_0);
906 MotionEventAndroid::Pointer pointer1(pointer_id_1,
907 pos_x_1,
908 pos_y_1,
909 touch_major_1,
910 touch_minor_1,
911 orientation_1,
912 android_tool_type_1);
913 MotionEventAndroid event(1.f / dpi_scale(),
914 env,
915 motion_event,
916 time_ms,
917 android_action,
918 pointer_count,
919 history_size,
920 action_index,
921 android_button_state,
922 android_meta_state,
923 raw_pos_x - pos_x_0,
924 raw_pos_y - pos_y_0,
925 pointer0,
926 pointer1);
928 return is_touch_handle_event ? rwhv->OnTouchHandleEvent(event)
929 : rwhv->OnTouchEvent(event);
932 float ContentViewCoreImpl::GetDpiScale() const {
933 return dpi_scale_;
936 jboolean ContentViewCoreImpl::SendMouseMoveEvent(JNIEnv* env,
937 jobject obj,
938 jlong time_ms,
939 jfloat x,
940 jfloat y) {
941 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
942 if (!rwhv)
943 return false;
945 blink::WebMouseEvent event = WebMouseEventBuilder::Build(
946 WebInputEvent::MouseMove,
947 blink::WebMouseEvent::ButtonNone,
948 time_ms / 1000.0, x / dpi_scale(), y / dpi_scale(), 0, 1);
950 rwhv->SendMouseEvent(event);
951 return true;
954 jboolean ContentViewCoreImpl::SendMouseWheelEvent(JNIEnv* env,
955 jobject obj,
956 jlong time_ms,
957 jfloat x,
958 jfloat y,
959 jfloat vertical_axis,
960 jfloat horizontal_axis) {
961 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
962 if (!rwhv)
963 return false;
965 WebMouseWheelEventBuilder::Direction direction;
966 if (vertical_axis > 0) {
967 direction = WebMouseWheelEventBuilder::DIRECTION_UP;
968 } else if (vertical_axis < 0) {
969 direction = WebMouseWheelEventBuilder::DIRECTION_DOWN;
970 } else if (horizontal_axis > 0) {
971 direction = WebMouseWheelEventBuilder::DIRECTION_RIGHT;
972 } else if (horizontal_axis < 0) {
973 direction = WebMouseWheelEventBuilder::DIRECTION_LEFT;
974 } else {
975 return false;
977 blink::WebMouseWheelEvent event = WebMouseWheelEventBuilder::Build(
978 direction, time_ms / 1000.0, x / dpi_scale(), y / dpi_scale());
980 rwhv->SendMouseWheelEvent(event);
981 return true;
984 WebGestureEvent ContentViewCoreImpl::MakeGestureEvent(
985 WebInputEvent::Type type, int64 time_ms, float x, float y) const {
986 return WebGestureEventBuilder::Build(
987 type, time_ms / 1000.0, x / dpi_scale(), y / dpi_scale());
990 void ContentViewCoreImpl::SendGestureEvent(
991 const blink::WebGestureEvent& event) {
992 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
993 if (rwhv)
994 rwhv->SendGestureEvent(event);
997 void ContentViewCoreImpl::ScrollBegin(JNIEnv* env,
998 jobject obj,
999 jlong time_ms,
1000 jfloat x,
1001 jfloat y,
1002 jfloat hintx,
1003 jfloat hinty) {
1004 WebGestureEvent event = MakeGestureEvent(
1005 WebInputEvent::GestureScrollBegin, time_ms, x, y);
1006 event.data.scrollBegin.deltaXHint = hintx / dpi_scale();
1007 event.data.scrollBegin.deltaYHint = hinty / dpi_scale();
1009 SendGestureEvent(event);
1012 void ContentViewCoreImpl::ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms) {
1013 WebGestureEvent event = MakeGestureEvent(
1014 WebInputEvent::GestureScrollEnd, time_ms, 0, 0);
1015 SendGestureEvent(event);
1018 void ContentViewCoreImpl::ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
1019 jfloat x, jfloat y, jfloat dx, jfloat dy) {
1020 WebGestureEvent event = MakeGestureEvent(
1021 WebInputEvent::GestureScrollUpdate, time_ms, x, y);
1022 event.data.scrollUpdate.deltaX = -dx / dpi_scale();
1023 event.data.scrollUpdate.deltaY = -dy / dpi_scale();
1025 SendGestureEvent(event);
1028 void ContentViewCoreImpl::FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
1029 jfloat x, jfloat y, jfloat vx, jfloat vy) {
1030 WebGestureEvent event = MakeGestureEvent(
1031 WebInputEvent::GestureFlingStart, time_ms, x, y);
1032 event.data.flingStart.velocityX = vx / dpi_scale();
1033 event.data.flingStart.velocityY = vy / dpi_scale();
1035 SendGestureEvent(event);
1038 void ContentViewCoreImpl::FlingCancel(JNIEnv* env, jobject obj, jlong time_ms) {
1039 WebGestureEvent event = MakeGestureEvent(
1040 WebInputEvent::GestureFlingCancel, time_ms, 0, 0);
1041 event.data.flingCancel.preventBoosting = true;
1043 SendGestureEvent(event);
1046 void ContentViewCoreImpl::SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
1047 jfloat x, jfloat y) {
1048 // Tap gestures should always be preceded by a TapDown, ensuring consistency
1049 // with the touch-based gesture detection pipeline.
1050 WebGestureEvent tap_down_event = MakeGestureEvent(
1051 WebInputEvent::GestureTapDown, time_ms, x, y);
1052 tap_down_event.data.tap.tapCount = 1;
1053 SendGestureEvent(tap_down_event);
1055 WebGestureEvent tap_event = MakeGestureEvent(
1056 WebInputEvent::GestureTap, time_ms, x, y);
1057 tap_event.data.tap.tapCount = 1;
1058 SendGestureEvent(tap_event);
1061 void ContentViewCoreImpl::DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
1062 jfloat x, jfloat y) {
1063 WebGestureEvent event = MakeGestureEvent(
1064 WebInputEvent::GestureDoubleTap, time_ms, x, y);
1065 // Set the tap count to 1 even for DoubleTap, in order to be consistent with
1066 // double tap behavior on a mobile viewport. See crbug.com/234986 for context.
1067 event.data.tap.tapCount = 1;
1069 SendGestureEvent(event);
1072 void ContentViewCoreImpl::LongPress(JNIEnv* env, jobject obj, jlong time_ms,
1073 jfloat x, jfloat y) {
1074 WebGestureEvent event = MakeGestureEvent(
1075 WebInputEvent::GestureLongPress, time_ms, x, y);
1077 SendGestureEvent(event);
1080 void ContentViewCoreImpl::PinchBegin(JNIEnv* env, jobject obj, jlong time_ms,
1081 jfloat x, jfloat y) {
1082 WebGestureEvent event = MakeGestureEvent(
1083 WebInputEvent::GesturePinchBegin, time_ms, x, y);
1084 SendGestureEvent(event);
1087 void ContentViewCoreImpl::PinchEnd(JNIEnv* env, jobject obj, jlong time_ms) {
1088 WebGestureEvent event = MakeGestureEvent(
1089 WebInputEvent::GesturePinchEnd, time_ms, 0, 0);
1090 SendGestureEvent(event);
1093 void ContentViewCoreImpl::PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
1094 jfloat anchor_x, jfloat anchor_y,
1095 jfloat delta) {
1096 WebGestureEvent event = MakeGestureEvent(
1097 WebInputEvent::GesturePinchUpdate, time_ms, anchor_x, anchor_y);
1098 event.data.pinchUpdate.scale = delta;
1100 SendGestureEvent(event);
1103 void ContentViewCoreImpl::SelectBetweenCoordinates(JNIEnv* env, jobject obj,
1104 jfloat x1, jfloat y1,
1105 jfloat x2, jfloat y2) {
1106 SelectBetweenCoordinates(gfx::PointF(x1 / dpi_scale(), y1 / dpi_scale()),
1107 gfx::PointF(x2 / dpi_scale(), y2 / dpi_scale()));
1110 void ContentViewCoreImpl::MoveCaret(JNIEnv* env, jobject obj,
1111 jfloat x, jfloat y) {
1112 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
1113 if (rwhv)
1114 rwhv->MoveCaret(gfx::Point(x / dpi_scale_, y / dpi_scale_));
1117 void ContentViewCoreImpl::DismissTextHandles(JNIEnv* env, jobject obj) {
1118 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
1119 if (rwhv)
1120 rwhv->DismissTextHandles();
1123 void ContentViewCoreImpl::SetTextHandlesTemporarilyHidden(JNIEnv* env,
1124 jobject obj,
1125 jboolean hidden) {
1126 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
1127 if (rwhv)
1128 rwhv->SetTextHandlesTemporarilyHidden(hidden);
1131 void ContentViewCoreImpl::ResetGestureDetection(JNIEnv* env, jobject obj) {
1132 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
1133 if (rwhv)
1134 rwhv->ResetGestureDetection();
1137 void ContentViewCoreImpl::SetDoubleTapSupportEnabled(JNIEnv* env,
1138 jobject obj,
1139 jboolean enabled) {
1140 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
1141 if (rwhv)
1142 rwhv->SetDoubleTapSupportEnabled(enabled);
1145 void ContentViewCoreImpl::SetMultiTouchZoomSupportEnabled(JNIEnv* env,
1146 jobject obj,
1147 jboolean enabled) {
1148 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
1149 if (rwhv)
1150 rwhv->SetMultiTouchZoomSupportEnabled(enabled);
1153 void ContentViewCoreImpl::SetAllowJavascriptInterfacesInspection(
1154 JNIEnv* env,
1155 jobject obj,
1156 jboolean allow) {
1157 java_bridge_dispatcher_host_->SetAllowObjectContentsInspection(allow);
1160 void ContentViewCoreImpl::AddJavascriptInterface(
1161 JNIEnv* env,
1162 jobject /* obj */,
1163 jobject object,
1164 jstring name,
1165 jclass safe_annotation_clazz) {
1166 ScopedJavaLocalRef<jobject> scoped_object(env, object);
1167 ScopedJavaLocalRef<jclass> scoped_clazz(env, safe_annotation_clazz);
1168 java_bridge_dispatcher_host_->AddNamedObject(
1169 ConvertJavaStringToUTF8(env, name), scoped_object, scoped_clazz);
1172 void ContentViewCoreImpl::RemoveJavascriptInterface(JNIEnv* env,
1173 jobject /* obj */,
1174 jstring name) {
1175 java_bridge_dispatcher_host_->RemoveNamedObject(
1176 ConvertJavaStringToUTF8(env, name));
1179 void ContentViewCoreImpl::WasResized(JNIEnv* env, jobject obj) {
1180 RenderWidgetHostViewAndroid* view = GetRenderWidgetHostViewAndroid();
1181 gfx::Size physical_size(
1182 Java_ContentViewCore_getPhysicalBackingWidthPix(env, obj),
1183 Java_ContentViewCore_getPhysicalBackingHeightPix(env, obj));
1184 root_layer_->SetBounds(physical_size);
1186 if (view) {
1187 RenderWidgetHostImpl* host = RenderWidgetHostImpl::From(
1188 view->GetRenderWidgetHost());
1189 host->SendScreenRects();
1190 view->WasResized();
1194 long ContentViewCoreImpl::GetNativeImeAdapter(JNIEnv* env, jobject obj) {
1195 RenderWidgetHostViewAndroid* rwhva = GetRenderWidgetHostViewAndroid();
1196 if (!rwhva)
1197 return 0;
1198 return rwhva->GetNativeImeAdapter();
1201 void ContentViewCoreImpl::UpdateImeAdapter(long native_ime_adapter,
1202 int text_input_type,
1203 int text_input_flags,
1204 const std::string& text,
1205 int selection_start,
1206 int selection_end,
1207 int composition_start,
1208 int composition_end,
1209 bool show_ime_if_needed,
1210 bool is_non_ime_change) {
1211 JNIEnv* env = AttachCurrentThread();
1212 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1213 if (obj.is_null())
1214 return;
1216 ScopedJavaLocalRef<jstring> jstring_text = ConvertUTF8ToJavaString(env, text);
1217 Java_ContentViewCore_updateImeAdapter(env,
1218 obj.obj(),
1219 native_ime_adapter,
1220 text_input_type,
1221 text_input_flags,
1222 jstring_text.obj(),
1223 selection_start,
1224 selection_end,
1225 composition_start,
1226 composition_end,
1227 show_ime_if_needed,
1228 is_non_ime_change);
1231 void ContentViewCoreImpl::SetAccessibilityEnabled(JNIEnv* env, jobject obj,
1232 bool enabled) {
1233 SetAccessibilityEnabledInternal(enabled);
1236 bool ContentViewCoreImpl::IsFullscreenRequiredForOrientationLock() const {
1237 JNIEnv* env = AttachCurrentThread();
1238 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1239 if (obj.is_null())
1240 return true;
1241 return Java_ContentViewCore_isFullscreenRequiredForOrientationLock(env,
1242 obj.obj());
1245 void ContentViewCoreImpl::SetAccessibilityEnabledInternal(bool enabled) {
1246 accessibility_enabled_ = enabled;
1247 BrowserAccessibilityStateImpl* accessibility_state =
1248 BrowserAccessibilityStateImpl::GetInstance();
1249 if (enabled) {
1250 // This enables accessibility globally unless it was explicitly disallowed
1251 // by a command-line flag.
1252 accessibility_state->OnScreenReaderDetected();
1253 // If it was actually enabled globally, enable it for this RenderWidget now.
1254 if (accessibility_state->IsAccessibleBrowser() && web_contents_)
1255 web_contents_->AddAccessibilityMode(AccessibilityModeComplete);
1256 } else {
1257 accessibility_state->ResetAccessibilityMode();
1258 if (web_contents_) {
1259 web_contents_->SetAccessibilityMode(
1260 accessibility_state->accessibility_mode());
1265 void ContentViewCoreImpl::SendOrientationChangeEventInternal() {
1266 RenderWidgetHostViewAndroid* rwhv = GetRenderWidgetHostViewAndroid();
1267 if (rwhv)
1268 rwhv->UpdateScreenInfo(GetViewAndroid());
1270 static_cast<WebContentsImpl*>(web_contents())->
1271 screen_orientation_dispatcher_host()->OnOrientationChange();
1274 void ContentViewCoreImpl::ExtractSmartClipData(JNIEnv* env,
1275 jobject obj,
1276 jint x,
1277 jint y,
1278 jint width,
1279 jint height) {
1280 gfx::Rect rect(
1281 static_cast<int>(x / dpi_scale()),
1282 static_cast<int>(y / dpi_scale()),
1283 static_cast<int>((width > 0 && width < dpi_scale()) ?
1284 1 : (int)(width / dpi_scale())),
1285 static_cast<int>((height > 0 && height < dpi_scale()) ?
1286 1 : (int)(height / dpi_scale())));
1287 GetWebContents()->Send(new ViewMsg_ExtractSmartClipData(
1288 GetWebContents()->GetRoutingID(), rect));
1291 jint ContentViewCoreImpl::GetCurrentRenderProcessId(JNIEnv* env, jobject obj) {
1292 return GetRenderProcessIdFromRenderViewHost(
1293 web_contents_->GetRenderViewHost());
1296 void ContentViewCoreImpl::SetBackgroundOpaque(JNIEnv* env, jobject jobj,
1297 jboolean opaque) {
1298 if (GetRenderWidgetHostViewAndroid()) {
1299 if (opaque)
1300 GetRenderWidgetHostViewAndroid()->SetBackgroundColorToDefault();
1301 else
1302 GetRenderWidgetHostViewAndroid()->SetBackgroundColor(SK_ColorTRANSPARENT);
1306 void ContentViewCoreImpl::RequestTextSurroundingSelection(
1307 int max_length,
1308 const base::Callback<
1309 void(const base::string16& content, int start_offset, int end_offset)>&
1310 callback) {
1311 DCHECK(!callback.is_null());
1312 RenderFrameHost* focused_frame = web_contents_->GetFocusedFrame();
1313 if (!focused_frame)
1314 return;
1315 if (GetRenderWidgetHostViewAndroid()) {
1316 GetRenderWidgetHostViewAndroid()->SetTextSurroundingSelectionCallback(
1317 callback);
1318 focused_frame->Send(new FrameMsg_TextSurroundingSelectionRequest(
1319 focused_frame->GetRoutingID(), max_length));
1323 void ContentViewCoreImpl::OnShowUnhandledTapUIIfNeeded(int x_dip, int y_dip) {
1324 JNIEnv* env = AttachCurrentThread();
1325 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1326 if (obj.is_null())
1327 return;
1328 Java_ContentViewCore_onShowUnhandledTapUIIfNeeded(
1329 env, obj.obj(), static_cast<jint>(x_dip * dpi_scale()),
1330 static_cast<jint>(y_dip * dpi_scale()));
1333 void ContentViewCoreImpl::OnSmartClipDataExtracted(
1334 const base::string16& text,
1335 const base::string16& html,
1336 const gfx::Rect& clip_rect) {
1337 JNIEnv* env = AttachCurrentThread();
1338 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
1339 if (obj.is_null())
1340 return;
1341 ScopedJavaLocalRef<jstring> jtext = ConvertUTF16ToJavaString(env, text);
1342 ScopedJavaLocalRef<jstring> jhtml = ConvertUTF16ToJavaString(env, html);
1343 ScopedJavaLocalRef<jobject> clip_rect_object(CreateJavaRect(env, clip_rect));
1344 Java_ContentViewCore_onSmartClipDataExtracted(
1345 env, obj.obj(), jtext.obj(), jhtml.obj(), clip_rect_object.obj());
1348 void ContentViewCoreImpl::WebContentsDestroyed() {
1349 WebContentsViewAndroid* wcva = static_cast<WebContentsViewAndroid*>(
1350 static_cast<WebContentsImpl*>(web_contents())->GetView());
1351 DCHECK(wcva);
1352 wcva->SetContentViewCore(NULL);
1355 // This is called for each ContentView.
1356 jlong Init(JNIEnv* env,
1357 jobject obj,
1358 jlong native_web_contents,
1359 jlong view_android,
1360 jlong window_android,
1361 jobject retained_objects_set) {
1362 ContentViewCoreImpl* view = new ContentViewCoreImpl(
1363 env, obj,
1364 reinterpret_cast<WebContents*>(native_web_contents),
1365 reinterpret_cast<ui::ViewAndroid*>(view_android),
1366 reinterpret_cast<ui::WindowAndroid*>(window_android),
1367 retained_objects_set);
1368 return reinterpret_cast<intptr_t>(view);
1371 bool RegisterContentViewCore(JNIEnv* env) {
1372 return RegisterNativesImpl(env);
1375 } // namespace content