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 #ifndef CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_
6 #define CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_weak_ref.h"
12 #include "base/compiler_specific.h"
13 #include "base/i18n/rtl.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/process/process.h"
16 #include "content/browser/android/overscroll_refresh.h"
17 #include "content/browser/renderer_host/render_widget_host_view_android.h"
18 #include "content/browser/web_contents/web_contents_impl.h"
19 #include "content/public/browser/android/content_view_core.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "third_party/WebKit/public/web/WebInputEvent.h"
22 #include "ui/gfx/geometry/rect.h"
23 #include "ui/gfx/geometry/rect_f.h"
33 class GinJavaBridgeDispatcherHost
;
34 class RenderFrameHost
;
35 class RenderWidgetHostViewAndroid
;
38 class ContentViewCoreImpl
: public ContentViewCore
,
39 public OverscrollRefreshHandler
,
40 public WebContentsObserver
{
42 static ContentViewCoreImpl
* FromWebContents(WebContents
* web_contents
);
43 ContentViewCoreImpl(JNIEnv
* env
,
45 WebContents
* web_contents
,
47 ui::WindowAndroid
* window_android
,
48 jobject java_bridge_retained_object_set
);
50 // ContentViewCore implementation.
51 base::android::ScopedJavaLocalRef
<jobject
> GetJavaObject() override
;
52 WebContents
* GetWebContents() const override
;
53 ui::ViewAndroid
* GetViewAndroid() const override
;
54 ui::WindowAndroid
* GetWindowAndroid() const override
;
55 const scoped_refptr
<cc::Layer
>& GetLayer() const override
;
56 void ShowPastePopup(int x
, int y
) override
;
57 void GetScaledContentBitmap(
59 SkColorType color_type
,
60 gfx::Rect src_subrect
,
61 ReadbackRequestCallback
& result_callback
) override
;
62 float GetDpiScale() const override
;
63 void PauseOrResumeGeolocation(bool should_pause
) override
;
64 void RequestTextSurroundingSelection(
66 const base::Callback
<void(const base::string16
& content
,
68 int end_offset
)>& callback
) override
;
70 // --------------------------------------------------------------------------
71 // Methods called from Java via JNI
72 // --------------------------------------------------------------------------
74 base::android::ScopedJavaLocalRef
<jobject
> GetWebContentsAndroid(JNIEnv
* env
,
77 void OnJavaContentViewCoreDestroyed(JNIEnv
* env
, jobject obj
);
79 // Notifies the ContentViewCore that items were selected in the currently
80 // showing select popup.
81 void SelectPopupMenuItems(JNIEnv
* env
, jobject obj
,
82 jlong selectPopupSourceFrame
,
85 void SendOrientationChangeEvent(JNIEnv
* env
, jobject obj
, jint orientation
);
86 jboolean
OnTouchEvent(JNIEnv
* env
,
100 jfloat touch_major_0
,
101 jfloat touch_major_1
,
102 jfloat touch_minor_0
,
103 jfloat touch_minor_1
,
104 jfloat orientation_0
,
105 jfloat orientation_1
,
108 jint android_tool_type_0
,
109 jint android_tool_type_1
,
110 jint android_button_state
,
111 jint android_meta_state
,
112 jboolean is_touch_handle_event
);
113 jboolean
SendMouseMoveEvent(JNIEnv
* env
,
118 jboolean
SendMouseWheelEvent(JNIEnv
* env
,
123 jfloat vertical_axis
,
124 jfloat horizontal_axis
);
125 void ScrollBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
,
126 jfloat x
, jfloat y
, jfloat hintx
, jfloat hinty
);
127 void ScrollEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
128 void ScrollBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
129 jfloat x
, jfloat y
, jfloat dx
, jfloat dy
);
130 void FlingStart(JNIEnv
* env
, jobject obj
, jlong time_ms
,
131 jfloat x
, jfloat y
, jfloat vx
, jfloat vy
);
132 void FlingCancel(JNIEnv
* env
, jobject obj
, jlong time_ms
);
133 void SingleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
135 void DoubleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
136 jfloat x
, jfloat y
) ;
137 void LongPress(JNIEnv
* env
, jobject obj
, jlong time_ms
,
139 void PinchBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
, jfloat x
, jfloat y
);
140 void PinchEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
141 void PinchBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
142 jfloat x
, jfloat y
, jfloat delta
);
143 void SelectBetweenCoordinates(JNIEnv
* env
, jobject obj
,
144 jfloat x1
, jfloat y1
,
145 jfloat x2
, jfloat y2
);
146 void MoveCaret(JNIEnv
* env
, jobject obj
, jfloat x
, jfloat y
);
147 void DismissTextHandles(JNIEnv
* env
, jobject obj
);
148 void SetTextHandlesTemporarilyHidden(JNIEnv
* env
,
152 void ResetGestureDetection(JNIEnv
* env
, jobject obj
);
153 void SetDoubleTapSupportEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
);
154 void SetMultiTouchZoomSupportEnabled(JNIEnv
* env
,
158 long GetNativeImeAdapter(JNIEnv
* env
, jobject obj
);
159 void SetFocus(JNIEnv
* env
, jobject obj
, jboolean focused
);
161 jint
GetBackgroundColor(JNIEnv
* env
, jobject obj
);
162 void SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
);
163 void SetAllowJavascriptInterfacesInspection(JNIEnv
* env
,
166 void AddJavascriptInterface(JNIEnv
* env
,
170 jclass safe_annotation_clazz
);
171 void RemoveJavascriptInterface(JNIEnv
* env
, jobject obj
, jstring name
);
172 void WasResized(JNIEnv
* env
, jobject obj
);
174 void SetAccessibilityEnabled(JNIEnv
* env
, jobject obj
, bool enabled
);
176 void SetTextTrackSettings(JNIEnv
* env
,
178 jstring textTrackBackgroundColor
,
179 jstring textTrackFontFamily
,
180 jstring textTrackFontStyle
,
181 jstring textTrackFontVariant
,
182 jstring textTrackTextColor
,
183 jstring textTrackTextShadow
,
184 jstring textTrackTextSize
);
186 void ExtractSmartClipData(JNIEnv
* env
,
193 void SetBackgroundOpaque(JNIEnv
* env
, jobject jobj
, jboolean opaque
);
194 void SetDrawsContent(JNIEnv
* env
, jobject jobj
, jboolean draws
);
196 jint
GetCurrentRenderProcessId(JNIEnv
* env
, jobject obj
);
198 // --------------------------------------------------------------------------
199 // Public methods that call to Java via JNI
200 // --------------------------------------------------------------------------
202 void OnSmartClipDataExtracted(const base::string16
& text
,
203 const base::string16
& html
,
204 const gfx::Rect
& clip_rect
);
206 // Creates a popup menu with |items|.
207 // |multiple| defines if it should support multi-select.
208 // If not |multiple|, |selected_item| sets the initially selected item.
209 // Otherwise, item's "checked" flag selects it.
210 void ShowSelectPopupMenu(RenderFrameHost
* frame
,
211 const gfx::Rect
& bounds
,
212 const std::vector
<MenuItem
>& items
,
215 // Hides a visible popup menu.
216 void HideSelectPopupMenu();
218 // All sizes and offsets are in CSS pixels as cached by the renderer.
219 void UpdateFrameInfo(const gfx::Vector2dF
& scroll_offset
,
220 float page_scale_factor
,
221 const gfx::Vector2dF
& page_scale_factor_limits
,
222 const gfx::SizeF
& content_size
,
223 const gfx::SizeF
& viewport_size
,
224 const gfx::Vector2dF
& controls_offset
,
225 const gfx::Vector2dF
& content_offset
,
226 bool is_mobile_optimized_hint
);
228 void UpdateImeAdapter(long native_ime_adapter
,
230 int text_input_flags
,
231 const std::string
& text
,
234 int composition_start
,
236 bool show_ime_if_needed
,
237 bool is_non_ime_change
);
238 void SetTitle(const base::string16
& title
);
239 void OnBackgroundColorChanged(SkColor color
);
242 void OnGestureEventAck(const blink::WebGestureEvent
& event
,
243 InputEventAckState ack_result
);
244 InputEventAckState
FilterInputEvent(const blink::WebInputEvent
& event
);
245 void OnSelectionChanged(const std::string
& text
);
246 void OnSelectionEvent(ui::SelectionEventType event
,
247 const gfx::PointF
& selection_anchor
,
248 const gfx::RectF
& selection_rect
);
249 scoped_ptr
<ui::TouchHandleDrawable
> CreatePopupTouchHandleDrawable();
251 void StartContentIntent(const GURL
& content_url
);
253 // Shows the disambiguation popup
254 // |rect_pixels| --> window coordinates which |zoomed_bitmap| represents
255 // |zoomed_bitmap| --> magnified image of potential touch targets
256 void ShowDisambiguationPopup(
257 const gfx::Rect
& rect_pixels
, const SkBitmap
& zoomed_bitmap
);
259 // Creates a java-side touch event, used for injecting touch event for
260 // testing/benchmarking purposes
261 base::android::ScopedJavaLocalRef
<jobject
> CreateTouchEventSynthesizer();
263 // Returns True if the given media should be blocked to load.
264 bool ShouldBlockMediaRequest(const GURL
& url
);
266 void DidStopFlinging();
268 // Returns the context with which the ContentViewCore was created, typically
269 // the Activity context.
270 base::android::ScopedJavaLocalRef
<jobject
> GetContext() const;
272 // Returns the viewport size after accounting for the viewport offset.
273 gfx::Size
GetViewSize() const;
275 void SetAccessibilityEnabledInternal(bool enabled
);
277 bool IsFullscreenRequiredForOrientationLock() const;
279 // --------------------------------------------------------------------------
280 // Methods called from native code
281 // --------------------------------------------------------------------------
283 gfx::Size
GetPhysicalBackingSize() const;
284 gfx::Size
GetViewportSizeDip() const;
285 bool DoTopControlsShrinkBlinkSize() const;
286 float GetTopControlsHeightDip() const;
288 void AttachLayer(scoped_refptr
<cc::Layer
> layer
);
289 void RemoveLayer(scoped_refptr
<cc::Layer
> layer
);
291 void MoveRangeSelectionExtent(const gfx::PointF
& extent
);
293 void SelectBetweenCoordinates(const gfx::PointF
& base
,
294 const gfx::PointF
& extent
);
296 void OnShowUnhandledTapUIIfNeeded(int x_dip
, int y_dip
);
299 class ContentViewUserData
;
301 friend class ContentViewUserData
;
302 ~ContentViewCoreImpl() override
;
304 // WebContentsObserver implementation.
305 void RenderViewReady() override
;
306 void RenderViewHostChanged(RenderViewHost
* old_host
,
307 RenderViewHost
* new_host
) override
;
308 void WebContentsDestroyed() override
;
310 // OverscrollRefreshHandler implementation.
311 bool PullStart() override
;
312 void PullUpdate(float delta
) override
;
313 void PullRelease(bool allow_refresh
) override
;
314 void PullReset() override
;
316 // --------------------------------------------------------------------------
317 // Other private methods and data
318 // --------------------------------------------------------------------------
320 void InitWebContents();
322 RenderWidgetHostViewAndroid
* GetRenderWidgetHostViewAndroid() const;
324 blink::WebGestureEvent
MakeGestureEvent(
325 blink::WebInputEvent::Type type
, int64 time_ms
, float x
, float y
) const;
327 gfx::Size
GetViewportSizePix() const;
328 int GetTopControlsHeightPix() const;
330 void SendGestureEvent(const blink::WebGestureEvent
& event
);
332 // Update focus state of the RenderWidgetHostView.
333 void SetFocusInternal(bool focused
);
335 // Send device_orientation_ to renderer.
336 void SendOrientationChangeEventInternal();
338 float dpi_scale() const { return dpi_scale_
; }
340 // A weak reference to the Java ContentViewCore object.
341 JavaObjectWeakGlobalRef java_ref_
;
343 // Reference to the current WebContents used to determine how and what to
344 // display in the ContentViewCore.
345 WebContentsImpl
* web_contents_
;
347 // A compositor layer containing any layer that should be shown.
348 scoped_refptr
<cc::Layer
> root_layer_
;
350 // Device scale factor.
353 // The Android view that can be used to add and remove decoration layers
354 // like AutofillPopup.
355 scoped_ptr
<ui::ViewAndroid
> view_android_
;
357 // The owning window that has a hold of main application activity.
358 ui::WindowAndroid
* window_android_
;
360 // The cache of device's current orientation set from Java side, this value
361 // will be sent to Renderer once it is ready.
362 int device_orientation_
;
364 bool accessibility_enabled_
;
366 // Manages injecting Java objects.
367 scoped_refptr
<GinJavaBridgeDispatcherHost
> java_bridge_dispatcher_host_
;
369 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl
);
372 bool RegisterContentViewCore(JNIEnv
* env
);
374 } // namespace content
376 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_