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/renderer_host/render_widget_host_view_android.h"
17 #include "content/browser/web_contents/web_contents_impl.h"
18 #include "content/public/browser/android/content_view_core.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "third_party/WebKit/public/web/WebInputEvent.h"
21 #include "ui/gfx/geometry/rect.h"
22 #include "ui/gfx/geometry/rect_f.h"
32 class GinJavaBridgeDispatcherHost
;
33 class RenderFrameHost
;
34 class RenderWidgetHostViewAndroid
;
37 class ContentViewCoreImpl
: public ContentViewCore
,
38 public WebContentsObserver
{
40 static ContentViewCoreImpl
* FromWebContents(WebContents
* web_contents
);
41 ContentViewCoreImpl(JNIEnv
* env
,
43 WebContents
* web_contents
,
45 ui::WindowAndroid
* window_android
,
46 jobject java_bridge_retained_object_set
);
48 // ContentViewCore implementation.
49 base::android::ScopedJavaLocalRef
<jobject
> GetJavaObject() override
;
50 WebContents
* GetWebContents() const override
;
51 ui::ViewAndroid
* GetViewAndroid() const override
;
52 ui::WindowAndroid
* GetWindowAndroid() const override
;
53 const scoped_refptr
<cc::Layer
>& GetLayer() const override
;
54 void ShowPastePopup(int x
, int y
) override
;
55 void GetScaledContentBitmap(
57 SkColorType color_type
,
58 gfx::Rect src_subrect
,
59 ReadbackRequestCallback
& result_callback
) override
;
60 float GetDpiScale() const override
;
61 void PauseOrResumeGeolocation(bool should_pause
) override
;
62 void RequestTextSurroundingSelection(
64 const base::Callback
<void(const base::string16
& content
,
66 int end_offset
)>& callback
) override
;
68 // --------------------------------------------------------------------------
69 // Methods called from Java via JNI
70 // --------------------------------------------------------------------------
72 base::android::ScopedJavaLocalRef
<jobject
> GetWebContentsAndroid(JNIEnv
* env
,
75 void OnJavaContentViewCoreDestroyed(JNIEnv
* env
, jobject obj
);
77 // Notifies the ContentViewCore that items were selected in the currently
78 // showing select popup.
79 void SelectPopupMenuItems(JNIEnv
* env
, jobject obj
,
80 jlong selectPopupSourceFrame
,
83 void SendOrientationChangeEvent(JNIEnv
* env
, jobject obj
, jint orientation
);
84 jboolean
OnTouchEvent(JNIEnv
* env
,
100 jfloat touch_minor_0
,
101 jfloat touch_minor_1
,
102 jfloat orientation_0
,
103 jfloat orientation_1
,
106 jint android_tool_type_0
,
107 jint android_tool_type_1
,
108 jint android_button_state
,
109 jint android_meta_state
,
110 jboolean is_touch_handle_event
);
111 jboolean
SendMouseMoveEvent(JNIEnv
* env
,
116 jboolean
SendMouseWheelEvent(JNIEnv
* env
,
121 jfloat vertical_axis
,
122 jfloat horizontal_axis
);
123 void ScrollBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
,
124 jfloat x
, jfloat y
, jfloat hintx
, jfloat hinty
);
125 void ScrollEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
126 void ScrollBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
127 jfloat x
, jfloat y
, jfloat dx
, jfloat dy
);
128 void FlingStart(JNIEnv
* env
, jobject obj
, jlong time_ms
,
129 jfloat x
, jfloat y
, jfloat vx
, jfloat vy
);
130 void FlingCancel(JNIEnv
* env
, jobject obj
, jlong time_ms
);
131 void SingleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
133 void DoubleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
134 jfloat x
, jfloat y
) ;
135 void LongPress(JNIEnv
* env
, jobject obj
, jlong time_ms
,
137 void PinchBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
, jfloat x
, jfloat y
);
138 void PinchEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
139 void PinchBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
140 jfloat x
, jfloat y
, jfloat delta
);
141 void SelectBetweenCoordinates(JNIEnv
* env
, jobject obj
,
142 jfloat x1
, jfloat y1
,
143 jfloat x2
, jfloat y2
);
144 void MoveCaret(JNIEnv
* env
, jobject obj
, jfloat x
, jfloat y
);
145 void DismissTextHandles(JNIEnv
* env
, jobject obj
);
146 void SetTextHandlesTemporarilyHidden(JNIEnv
* env
,
150 void ResetGestureDetection(JNIEnv
* env
, jobject obj
);
151 void SetDoubleTapSupportEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
);
152 void SetMultiTouchZoomSupportEnabled(JNIEnv
* env
,
156 long GetNativeImeAdapter(JNIEnv
* env
, jobject obj
);
157 void SetFocus(JNIEnv
* env
, jobject obj
, jboolean focused
);
159 jint
GetBackgroundColor(JNIEnv
* env
, jobject obj
);
160 void SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
);
161 void SetAllowJavascriptInterfacesInspection(JNIEnv
* env
,
164 void AddJavascriptInterface(JNIEnv
* env
,
168 jclass safe_annotation_clazz
);
169 void RemoveJavascriptInterface(JNIEnv
* env
, jobject obj
, jstring name
);
170 void WasResized(JNIEnv
* env
, jobject obj
);
172 void SetAccessibilityEnabled(JNIEnv
* env
, jobject obj
, bool enabled
);
174 void SetTextTrackSettings(JNIEnv
* env
,
176 jstring textTrackBackgroundColor
,
177 jstring textTrackFontFamily
,
178 jstring textTrackFontStyle
,
179 jstring textTrackFontVariant
,
180 jstring textTrackTextColor
,
181 jstring textTrackTextShadow
,
182 jstring textTrackTextSize
);
184 void ExtractSmartClipData(JNIEnv
* env
,
191 void SetBackgroundOpaque(JNIEnv
* env
, jobject jobj
, jboolean opaque
);
192 void SetDrawsContent(JNIEnv
* env
, jobject jobj
, jboolean draws
);
194 jint
GetCurrentRenderProcessId(JNIEnv
* env
, jobject obj
);
196 // --------------------------------------------------------------------------
197 // Public methods that call to Java via JNI
198 // --------------------------------------------------------------------------
200 void OnSmartClipDataExtracted(const base::string16
& text
,
201 const base::string16
& html
,
202 const gfx::Rect
& clip_rect
);
204 // Creates a popup menu with |items|.
205 // |multiple| defines if it should support multi-select.
206 // If not |multiple|, |selected_item| sets the initially selected item.
207 // Otherwise, item's "checked" flag selects it.
208 void ShowSelectPopupMenu(RenderFrameHost
* frame
,
209 const gfx::Rect
& bounds
,
210 const std::vector
<MenuItem
>& items
,
213 // Hides a visible popup menu.
214 void HideSelectPopupMenu();
216 // All sizes and offsets are in CSS pixels as cached by the renderer.
217 void UpdateFrameInfo(const gfx::Vector2dF
& scroll_offset
,
218 float page_scale_factor
,
219 const gfx::Vector2dF
& page_scale_factor_limits
,
220 const gfx::SizeF
& content_size
,
221 const gfx::SizeF
& viewport_size
,
222 const gfx::Vector2dF
& controls_offset
,
223 const gfx::Vector2dF
& content_offset
,
224 bool is_mobile_optimized_hint
);
226 void UpdateImeAdapter(long native_ime_adapter
,
228 int text_input_flags
,
229 const std::string
& text
,
232 int composition_start
,
234 bool show_ime_if_needed
,
235 bool is_non_ime_change
);
236 void SetTitle(const base::string16
& title
);
237 void OnBackgroundColorChanged(SkColor color
);
240 void OnGestureEventAck(const blink::WebGestureEvent
& event
,
241 InputEventAckState ack_result
);
242 bool FilterInputEvent(const blink::WebInputEvent
& event
);
243 void OnSelectionChanged(const std::string
& text
);
244 void OnSelectionEvent(ui::SelectionEventType event
,
245 const gfx::PointF
& anchor_position
);
246 scoped_ptr
<ui::TouchHandleDrawable
> CreatePopupTouchHandleDrawable();
248 void StartContentIntent(const GURL
& content_url
);
250 // Shows the disambiguation popup
251 // |rect_pixels| --> window coordinates which |zoomed_bitmap| represents
252 // |zoomed_bitmap| --> magnified image of potential touch targets
253 void ShowDisambiguationPopup(
254 const gfx::Rect
& rect_pixels
, const SkBitmap
& zoomed_bitmap
);
256 // Creates a java-side touch event, used for injecting touch event for
257 // testing/benchmarking purposes
258 base::android::ScopedJavaLocalRef
<jobject
> CreateTouchEventSynthesizer();
260 // Returns True if the given media should be blocked to load.
261 bool ShouldBlockMediaRequest(const GURL
& url
);
263 void DidStopFlinging();
265 // Returns the context with which the ContentViewCore was created, typically
266 // the Activity context.
267 base::android::ScopedJavaLocalRef
<jobject
> GetContext() const;
269 // Returns the viewport size after accounting for the viewport offset.
270 gfx::Size
GetViewSize() const;
272 void SetAccessibilityEnabledInternal(bool enabled
);
274 bool IsFullscreenRequiredForOrientationLock() const;
276 // --------------------------------------------------------------------------
277 // Methods called from native code
278 // --------------------------------------------------------------------------
280 gfx::Size
GetPhysicalBackingSize() const;
281 gfx::Size
GetViewportSizeDip() const;
282 bool DoTopControlsShrinkBlinkSize() const;
283 float GetTopControlsHeightDip() const;
285 void AttachLayer(scoped_refptr
<cc::Layer
> layer
);
286 void RemoveLayer(scoped_refptr
<cc::Layer
> layer
);
288 void MoveRangeSelectionExtent(const gfx::PointF
& extent
);
290 void SelectBetweenCoordinates(const gfx::PointF
& base
,
291 const gfx::PointF
& extent
);
293 void OnShowUnhandledTapUIIfNeeded(int x_dip
, int y_dip
);
296 class ContentViewUserData
;
298 friend class ContentViewUserData
;
299 ~ContentViewCoreImpl() override
;
301 // WebContentsObserver implementation.
302 void RenderViewReady() override
;
303 void RenderViewHostChanged(RenderViewHost
* old_host
,
304 RenderViewHost
* new_host
) override
;
305 void WebContentsDestroyed() override
;
307 // --------------------------------------------------------------------------
308 // Other private methods and data
309 // --------------------------------------------------------------------------
311 void InitWebContents();
313 RenderWidgetHostViewAndroid
* GetRenderWidgetHostViewAndroid() const;
315 blink::WebGestureEvent
MakeGestureEvent(
316 blink::WebInputEvent::Type type
, int64 time_ms
, float x
, float y
) const;
318 gfx::Size
GetViewportSizePix() const;
319 int GetTopControlsHeightPix() const;
321 void SendGestureEvent(const blink::WebGestureEvent
& event
);
323 // Update focus state of the RenderWidgetHostView.
324 void SetFocusInternal(bool focused
);
326 // Send device_orientation_ to renderer.
327 void SendOrientationChangeEventInternal();
329 float dpi_scale() const { return dpi_scale_
; }
331 // A weak reference to the Java ContentViewCore object.
332 JavaObjectWeakGlobalRef java_ref_
;
334 // Reference to the current WebContents used to determine how and what to
335 // display in the ContentViewCore.
336 WebContentsImpl
* web_contents_
;
338 // A compositor layer containing any layer that should be shown.
339 scoped_refptr
<cc::Layer
> root_layer_
;
341 // Device scale factor.
344 // The Android view that can be used to add and remove decoration layers
345 // like AutofillPopup.
346 scoped_ptr
<ui::ViewAndroid
> view_android_
;
348 // The owning window that has a hold of main application activity.
349 ui::WindowAndroid
* window_android_
;
351 // The cache of device's current orientation set from Java side, this value
352 // will be sent to Renderer once it is ready.
353 int device_orientation_
;
355 bool accessibility_enabled_
;
357 // Manages injecting Java objects.
358 scoped_refptr
<GinJavaBridgeDispatcherHost
> java_bridge_dispatcher_host_
;
360 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl
);
363 bool RegisterContentViewCore(JNIEnv
* env
);
365 } // namespace content
367 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_