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 preferred_color_type
,
60 const 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
,
76 base::android::ScopedJavaLocalRef
<jobject
> GetJavaWindowAndroid(JNIEnv
* env
,
79 void OnJavaContentViewCoreDestroyed(JNIEnv
* env
, jobject obj
);
81 // Notifies the ContentViewCore that items were selected in the currently
82 // showing select popup.
83 void SelectPopupMenuItems(JNIEnv
* env
, jobject obj
,
84 jlong selectPopupSourceFrame
,
87 void SendOrientationChangeEvent(JNIEnv
* env
, jobject obj
, jint orientation
);
88 jboolean
OnTouchEvent(JNIEnv
* env
,
102 jfloat touch_major_0
,
103 jfloat touch_major_1
,
104 jfloat touch_minor_0
,
105 jfloat touch_minor_1
,
106 jfloat orientation_0
,
107 jfloat orientation_1
,
110 jint android_tool_type_0
,
111 jint android_tool_type_1
,
112 jint android_button_state
,
113 jint android_meta_state
,
114 jboolean is_touch_handle_event
);
115 jboolean
SendMouseMoveEvent(JNIEnv
* env
,
120 jboolean
SendMouseWheelEvent(JNIEnv
* env
,
127 jfloat pixels_per_tick
);
128 void ScrollBegin(JNIEnv
* env
,
135 jboolean target_viewport
);
136 void ScrollEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
137 void ScrollBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
138 jfloat x
, jfloat y
, jfloat dx
, jfloat dy
);
139 void FlingStart(JNIEnv
* env
,
146 jboolean target_viewport
);
147 void FlingCancel(JNIEnv
* env
, jobject obj
, jlong time_ms
);
148 void SingleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
150 void DoubleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
151 jfloat x
, jfloat y
) ;
152 void LongPress(JNIEnv
* env
, jobject obj
, jlong time_ms
,
154 void PinchBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
, jfloat x
, jfloat y
);
155 void PinchEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
156 void PinchBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
157 jfloat x
, jfloat y
, jfloat delta
);
158 void SelectBetweenCoordinates(JNIEnv
* env
, jobject obj
,
159 jfloat x1
, jfloat y1
,
160 jfloat x2
, jfloat y2
);
161 void MoveCaret(JNIEnv
* env
, jobject obj
, jfloat x
, jfloat y
);
162 void DismissTextHandles(JNIEnv
* env
, jobject obj
);
163 void SetTextHandlesTemporarilyHidden(JNIEnv
* env
,
167 void ResetGestureDetection(JNIEnv
* env
, jobject obj
);
168 void SetDoubleTapSupportEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
);
169 void SetMultiTouchZoomSupportEnabled(JNIEnv
* env
,
173 long GetNativeImeAdapter(JNIEnv
* env
, jobject obj
);
174 void SetFocus(JNIEnv
* env
, jobject obj
, jboolean focused
);
176 jint
GetBackgroundColor(JNIEnv
* env
, jobject obj
);
177 void SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
);
178 void SetAllowJavascriptInterfacesInspection(JNIEnv
* env
,
181 void AddJavascriptInterface(JNIEnv
* env
,
185 jclass safe_annotation_clazz
);
186 void RemoveJavascriptInterface(JNIEnv
* env
, jobject obj
, jstring name
);
187 void WasResized(JNIEnv
* env
, jobject obj
);
189 void SetAccessibilityEnabled(JNIEnv
* env
, jobject obj
, bool enabled
);
191 void SetTextTrackSettings(JNIEnv
* env
,
193 jboolean textTracksEnabled
,
194 jstring textTrackBackgroundColor
,
195 jstring textTrackFontFamily
,
196 jstring textTrackFontStyle
,
197 jstring textTrackFontVariant
,
198 jstring textTrackTextColor
,
199 jstring textTrackTextShadow
,
200 jstring textTrackTextSize
);
202 void ExtractSmartClipData(JNIEnv
* env
,
209 void SetBackgroundOpaque(JNIEnv
* env
, jobject jobj
, jboolean opaque
);
211 jint
GetCurrentRenderProcessId(JNIEnv
* env
, jobject obj
);
213 // --------------------------------------------------------------------------
214 // Public methods that call to Java via JNI
215 // --------------------------------------------------------------------------
217 void OnSmartClipDataExtracted(const base::string16
& text
,
218 const base::string16
& html
,
219 const gfx::Rect
& clip_rect
);
221 // Creates a popup menu with |items|.
222 // |multiple| defines if it should support multi-select.
223 // If not |multiple|, |selected_item| sets the initially selected item.
224 // Otherwise, item's "checked" flag selects it.
225 void ShowSelectPopupMenu(RenderFrameHost
* frame
,
226 const gfx::Rect
& bounds
,
227 const std::vector
<MenuItem
>& items
,
230 // Hides a visible popup menu.
231 void HideSelectPopupMenu();
233 // All sizes and offsets are in CSS pixels as cached by the renderer.
234 void UpdateFrameInfo(const gfx::Vector2dF
& scroll_offset
,
235 float page_scale_factor
,
236 const gfx::Vector2dF
& page_scale_factor_limits
,
237 const gfx::SizeF
& content_size
,
238 const gfx::SizeF
& viewport_size
,
239 const gfx::Vector2dF
& controls_offset
,
240 const gfx::Vector2dF
& content_offset
,
241 bool is_mobile_optimized_hint
);
243 void ForceUpdateImeAdapter(long native_ime_adapter
);
244 void UpdateImeAdapter(long native_ime_adapter
,
246 int text_input_flags
,
247 const std::string
& text
,
250 int composition_start
,
252 bool show_ime_if_needed
,
253 bool is_non_ime_change
);
254 void SetTitle(const base::string16
& title
);
255 void OnBackgroundColorChanged(SkColor color
);
258 void RequestDisallowInterceptTouchEvent();
259 void OnGestureEventAck(const blink::WebGestureEvent
& event
,
260 InputEventAckState ack_result
);
261 bool FilterInputEvent(const blink::WebInputEvent
& event
);
262 void OnSelectionChanged(const std::string
& text
);
263 void OnSelectionEvent(ui::SelectionEventType event
,
264 const gfx::PointF
& selection_anchor
,
265 const gfx::RectF
& selection_rect
);
266 scoped_ptr
<ui::TouchHandleDrawable
> CreatePopupTouchHandleDrawable();
268 void StartContentIntent(const GURL
& content_url
);
270 // Shows the disambiguation popup
271 // |rect_pixels| --> window coordinates which |zoomed_bitmap| represents
272 // |zoomed_bitmap| --> magnified image of potential touch targets
273 void ShowDisambiguationPopup(
274 const gfx::Rect
& rect_pixels
, const SkBitmap
& zoomed_bitmap
);
276 // Creates a java-side touch event, used for injecting motion events for
277 // testing/benchmarking purposes.
278 base::android::ScopedJavaLocalRef
<jobject
> CreateMotionEventSynthesizer();
280 // Returns True if the given media should be blocked to load.
281 bool ShouldBlockMediaRequest(const GURL
& url
);
283 void DidStopFlinging();
285 // Returns the context with which the ContentViewCore was created, typically
286 // the Activity context.
287 base::android::ScopedJavaLocalRef
<jobject
> GetContext() const;
289 // Returns the viewport size after accounting for the viewport offset.
290 gfx::Size
GetViewSize() const;
292 void SetAccessibilityEnabledInternal(bool enabled
);
294 bool IsFullscreenRequiredForOrientationLock() const;
296 // --------------------------------------------------------------------------
297 // Methods called from native code
298 // --------------------------------------------------------------------------
300 gfx::Size
GetPhysicalBackingSize() const;
301 gfx::Size
GetViewportSizeDip() const;
302 bool DoTopControlsShrinkBlinkSize() const;
303 float GetTopControlsHeightDip() const;
305 void AttachLayer(scoped_refptr
<cc::Layer
> layer
);
306 void RemoveLayer(scoped_refptr
<cc::Layer
> layer
);
308 void MoveRangeSelectionExtent(const gfx::PointF
& extent
);
310 void SelectBetweenCoordinates(const gfx::PointF
& base
,
311 const gfx::PointF
& extent
);
313 void OnShowUnhandledTapUIIfNeeded(int x_dip
, int y_dip
);
315 // returns page density (dpi) X page scale
316 float GetScaleFactor() const;
318 class ContentViewUserData
;
320 friend class ContentViewUserData
;
321 ~ContentViewCoreImpl() override
;
323 // WebContentsObserver implementation.
324 void RenderViewReady() override
;
325 void RenderViewHostChanged(RenderViewHost
* old_host
,
326 RenderViewHost
* new_host
) override
;
327 void WebContentsDestroyed() override
;
329 // OverscrollRefreshHandler implementation.
330 bool PullStart() override
;
331 void PullUpdate(float delta
) override
;
332 void PullRelease(bool allow_refresh
) override
;
333 void PullReset() override
;
335 // --------------------------------------------------------------------------
336 // Other private methods and data
337 // --------------------------------------------------------------------------
339 void InitWebContents();
341 RenderWidgetHostViewAndroid
* GetRenderWidgetHostViewAndroid() const;
343 blink::WebGestureEvent
MakeGestureEvent(
344 blink::WebInputEvent::Type type
, int64 time_ms
, float x
, float y
) const;
346 gfx::Size
GetViewportSizePix() const;
347 int GetTopControlsHeightPix() const;
349 void SendGestureEvent(const blink::WebGestureEvent
& event
);
351 // Update focus state of the RenderWidgetHostView.
352 void SetFocusInternal(bool focused
);
354 // Send device_orientation_ to renderer.
355 void SendOrientationChangeEventInternal();
357 float dpi_scale() const { return dpi_scale_
; }
359 // A weak reference to the Java ContentViewCore object.
360 JavaObjectWeakGlobalRef java_ref_
;
362 // Reference to the current WebContents used to determine how and what to
363 // display in the ContentViewCore.
364 WebContentsImpl
* web_contents_
;
366 // A compositor layer containing any layer that should be shown.
367 scoped_refptr
<cc::Layer
> root_layer_
;
369 // Page scale factor.
372 // The Android view that can be used to add and remove decoration layers
373 // like AutofillPopup.
374 scoped_ptr
<ui::ViewAndroid
> view_android_
;
376 // Device scale factor.
377 const float dpi_scale_
;
379 // The owning window that has a hold of main application activity.
380 ui::WindowAndroid
* window_android_
;
382 // The cache of device's current orientation set from Java side, this value
383 // will be sent to Renderer once it is ready.
384 int device_orientation_
;
386 bool accessibility_enabled_
;
388 // Manages injecting Java objects.
389 scoped_refptr
<GinJavaBridgeDispatcherHost
> java_bridge_dispatcher_host_
;
391 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl
);
394 bool RegisterContentViewCore(JNIEnv
* env
);
396 } // namespace content
398 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_