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
,
44 ui::ViewAndroid
* view_android
,
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 ExtractSmartClipData(JNIEnv
* env
,
181 void SetBackgroundOpaque(JNIEnv
* env
, jobject jobj
, jboolean opaque
);
182 void SetDrawsContent(JNIEnv
* env
, jobject jobj
, jboolean draws
);
184 jint
GetCurrentRenderProcessId(JNIEnv
* env
, jobject obj
);
186 // --------------------------------------------------------------------------
187 // Public methods that call to Java via JNI
188 // --------------------------------------------------------------------------
190 void OnSmartClipDataExtracted(const base::string16
& text
,
191 const base::string16
& html
,
192 const gfx::Rect
& clip_rect
);
194 // Creates a popup menu with |items|.
195 // |multiple| defines if it should support multi-select.
196 // If not |multiple|, |selected_item| sets the initially selected item.
197 // Otherwise, item's "checked" flag selects it.
198 void ShowSelectPopupMenu(RenderFrameHost
* frame
,
199 const gfx::Rect
& bounds
,
200 const std::vector
<MenuItem
>& items
,
203 // Hides a visible popup menu.
204 void HideSelectPopupMenu();
206 // All sizes and offsets are in CSS pixels as cached by the renderer.
207 void UpdateFrameInfo(const gfx::Vector2dF
& scroll_offset
,
208 float page_scale_factor
,
209 const gfx::Vector2dF
& page_scale_factor_limits
,
210 const gfx::SizeF
& content_size
,
211 const gfx::SizeF
& viewport_size
,
212 const gfx::Vector2dF
& controls_offset
,
213 const gfx::Vector2dF
& content_offset
,
214 bool is_mobile_optimized_hint
);
216 void UpdateImeAdapter(long native_ime_adapter
,
218 int text_input_flags
,
219 const std::string
& text
,
222 int composition_start
,
224 bool show_ime_if_needed
,
225 bool is_non_ime_change
);
226 void SetTitle(const base::string16
& title
);
227 void OnBackgroundColorChanged(SkColor color
);
230 void OnGestureEventAck(const blink::WebGestureEvent
& event
,
231 InputEventAckState ack_result
);
232 bool FilterInputEvent(const blink::WebInputEvent
& event
);
233 void OnSelectionChanged(const std::string
& text
);
234 void OnSelectionEvent(ui::SelectionEventType event
,
235 const gfx::PointF
& anchor_position
);
236 scoped_ptr
<ui::TouchHandleDrawable
> CreatePopupTouchHandleDrawable();
238 void StartContentIntent(const GURL
& content_url
);
240 // Shows the disambiguation popup
241 // |rect_pixels| --> window coordinates which |zoomed_bitmap| represents
242 // |zoomed_bitmap| --> magnified image of potential touch targets
243 void ShowDisambiguationPopup(
244 const gfx::Rect
& rect_pixels
, const SkBitmap
& zoomed_bitmap
);
246 // Creates a java-side touch event, used for injecting touch event for
247 // testing/benchmarking purposes
248 base::android::ScopedJavaLocalRef
<jobject
> CreateTouchEventSynthesizer();
250 // Returns True if the given media should be blocked to load.
251 bool ShouldBlockMediaRequest(const GURL
& url
);
253 void DidStopFlinging();
255 // Returns the context with which the ContentViewCore was created, typically
256 // the Activity context.
257 base::android::ScopedJavaLocalRef
<jobject
> GetContext() const;
259 // Returns the viewport size after accounting for the viewport offset.
260 gfx::Size
GetViewSize() const;
262 void SetAccessibilityEnabledInternal(bool enabled
);
264 bool IsFullscreenRequiredForOrientationLock() const;
266 // --------------------------------------------------------------------------
267 // Methods called from native code
268 // --------------------------------------------------------------------------
270 gfx::Size
GetPhysicalBackingSize() const;
271 gfx::Size
GetViewportSizeDip() const;
272 bool DoTopControlsShrinkBlinkSize() const;
273 float GetTopControlsHeightDip() const;
275 void AttachLayer(scoped_refptr
<cc::Layer
> layer
);
276 void RemoveLayer(scoped_refptr
<cc::Layer
> layer
);
278 void MoveRangeSelectionExtent(const gfx::PointF
& extent
);
280 void SelectBetweenCoordinates(const gfx::PointF
& base
,
281 const gfx::PointF
& extent
);
283 void OnShowUnhandledTapUIIfNeeded(int x_dip
, int y_dip
);
286 class ContentViewUserData
;
288 friend class ContentViewUserData
;
289 ~ContentViewCoreImpl() override
;
291 // WebContentsObserver implementation.
292 void RenderViewReady() override
;
293 void RenderViewHostChanged(RenderViewHost
* old_host
,
294 RenderViewHost
* new_host
) override
;
295 void WebContentsDestroyed() override
;
297 // --------------------------------------------------------------------------
298 // Other private methods and data
299 // --------------------------------------------------------------------------
301 void InitWebContents();
303 RenderWidgetHostViewAndroid
* GetRenderWidgetHostViewAndroid() const;
305 blink::WebGestureEvent
MakeGestureEvent(
306 blink::WebInputEvent::Type type
, int64 time_ms
, float x
, float y
) const;
308 gfx::Size
GetViewportSizePix() const;
309 int GetTopControlsHeightPix() const;
311 void SendGestureEvent(const blink::WebGestureEvent
& event
);
313 // Update focus state of the RenderWidgetHostView.
314 void SetFocusInternal(bool focused
);
316 // Send device_orientation_ to renderer.
317 void SendOrientationChangeEventInternal();
319 float dpi_scale() const { return dpi_scale_
; }
321 // A weak reference to the Java ContentViewCore object.
322 JavaObjectWeakGlobalRef java_ref_
;
324 // Reference to the current WebContents used to determine how and what to
325 // display in the ContentViewCore.
326 WebContentsImpl
* web_contents_
;
328 // A compositor layer containing any layer that should be shown.
329 scoped_refptr
<cc::Layer
> root_layer_
;
331 // Device scale factor.
334 // The Android view that can be used to add and remove decoration layers
335 // like AutofillPopup.
336 ui::ViewAndroid
* view_android_
;
338 // The owning window that has a hold of main application activity.
339 ui::WindowAndroid
* window_android_
;
341 // The cache of device's current orientation set from Java side, this value
342 // will be sent to Renderer once it is ready.
343 int device_orientation_
;
345 bool accessibility_enabled_
;
347 // Manages injecting Java objects.
348 scoped_refptr
<GinJavaBridgeDispatcherHost
> java_bridge_dispatcher_host_
;
350 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl
);
353 bool RegisterContentViewCore(JNIEnv
* env
);
355 } // namespace content
357 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_