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
);
267 void StartContentIntent(const GURL
& content_url
);
269 // Shows the disambiguation popup
270 // |rect_pixels| --> window coordinates which |zoomed_bitmap| represents
271 // |zoomed_bitmap| --> magnified image of potential touch targets
272 void ShowDisambiguationPopup(
273 const gfx::Rect
& rect_pixels
, const SkBitmap
& zoomed_bitmap
);
275 // Creates a java-side touch event, used for injecting motion events for
276 // testing/benchmarking purposes.
277 base::android::ScopedJavaLocalRef
<jobject
> CreateMotionEventSynthesizer();
279 // Returns True if the given media should be blocked to load.
280 bool ShouldBlockMediaRequest(const GURL
& url
);
282 void DidStopFlinging();
284 // Returns the context with which the ContentViewCore was created, typically
285 // the Activity context.
286 base::android::ScopedJavaLocalRef
<jobject
> GetContext() const;
288 // Returns the viewport size after accounting for the viewport offset.
289 gfx::Size
GetViewSize() const;
291 void SetAccessibilityEnabledInternal(bool enabled
);
293 bool IsFullscreenRequiredForOrientationLock() const;
295 // --------------------------------------------------------------------------
296 // Methods called from native code
297 // --------------------------------------------------------------------------
299 gfx::Size
GetPhysicalBackingSize() const;
300 gfx::Size
GetViewportSizeDip() const;
301 bool DoTopControlsShrinkBlinkSize() const;
302 float GetTopControlsHeightDip() const;
304 void AttachLayer(scoped_refptr
<cc::Layer
> layer
);
305 void RemoveLayer(scoped_refptr
<cc::Layer
> layer
);
307 void MoveRangeSelectionExtent(const gfx::PointF
& extent
);
309 void SelectBetweenCoordinates(const gfx::PointF
& base
,
310 const gfx::PointF
& extent
);
312 void OnShowUnhandledTapUIIfNeeded(int x_dip
, int y_dip
);
314 // returns page density (dpi) X page scale
315 float GetScaleFactor() const;
317 class ContentViewUserData
;
319 friend class ContentViewUserData
;
320 ~ContentViewCoreImpl() override
;
322 // WebContentsObserver implementation.
323 void RenderViewReady() override
;
324 void RenderViewHostChanged(RenderViewHost
* old_host
,
325 RenderViewHost
* new_host
) override
;
326 void WebContentsDestroyed() override
;
328 // OverscrollRefreshHandler implementation.
329 bool PullStart() override
;
330 void PullUpdate(float delta
) override
;
331 void PullRelease(bool allow_refresh
) override
;
332 void PullReset() override
;
334 // --------------------------------------------------------------------------
335 // Other private methods and data
336 // --------------------------------------------------------------------------
338 void InitWebContents();
340 RenderWidgetHostViewAndroid
* GetRenderWidgetHostViewAndroid() const;
342 blink::WebGestureEvent
MakeGestureEvent(
343 blink::WebInputEvent::Type type
, int64 time_ms
, float x
, float y
) const;
345 gfx::Size
GetViewportSizePix() const;
346 int GetTopControlsHeightPix() const;
348 void SendGestureEvent(const blink::WebGestureEvent
& event
);
350 // Update focus state of the RenderWidgetHostView.
351 void SetFocusInternal(bool focused
);
353 // Send device_orientation_ to renderer.
354 void SendOrientationChangeEventInternal();
356 float dpi_scale() const { return dpi_scale_
; }
358 // A weak reference to the Java ContentViewCore object.
359 JavaObjectWeakGlobalRef java_ref_
;
361 // Reference to the current WebContents used to determine how and what to
362 // display in the ContentViewCore.
363 WebContentsImpl
* web_contents_
;
365 // A compositor layer containing any layer that should be shown.
366 scoped_refptr
<cc::Layer
> root_layer_
;
368 // Page scale factor.
371 // The Android view that can be used to add and remove decoration layers
372 // like AutofillPopup.
373 scoped_ptr
<ui::ViewAndroid
> view_android_
;
375 // Device scale factor.
376 const float dpi_scale_
;
378 // The owning window that has a hold of main application activity.
379 ui::WindowAndroid
* window_android_
;
381 // The cache of device's current orientation set from Java side, this value
382 // will be sent to Renderer once it is ready.
383 int device_orientation_
;
385 bool accessibility_enabled_
;
387 // Manages injecting Java objects.
388 scoped_refptr
<GinJavaBridgeDispatcherHost
> java_bridge_dispatcher_host_
;
390 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl
);
393 bool RegisterContentViewCore(JNIEnv
* env
);
395 } // namespace content
397 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_