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/rect.h"
22 #include "ui/gfx/rect_f.h"
32 class GinJavaBridgeDispatcherHost
;
33 class RenderFrameHost
;
34 class RenderWidgetHostViewAndroid
;
37 // TODO(jrg): this is a shell. Upstream the rest.
38 class ContentViewCoreImpl
: public ContentViewCore
,
39 public WebContentsObserver
{
41 static ContentViewCoreImpl
* FromWebContents(WebContents
* web_contents
);
42 ContentViewCoreImpl(JNIEnv
* env
,
44 WebContents
* web_contents
,
45 ui::ViewAndroid
* view_android
,
46 ui::WindowAndroid
* window_android
,
47 jobject java_bridge_retained_object_set
);
49 // ContentViewCore implementation.
50 virtual base::android::ScopedJavaLocalRef
<jobject
> GetJavaObject() override
;
51 virtual WebContents
* GetWebContents() const override
;
52 virtual ui::ViewAndroid
* GetViewAndroid() const override
;
53 virtual ui::WindowAndroid
* GetWindowAndroid() const override
;
54 virtual const scoped_refptr
<cc::Layer
>& GetLayer() const override
;
55 virtual void ShowPastePopup(int x
, int y
) override
;
56 virtual void GetScaledContentBitmap(
58 SkColorType color_type
,
59 gfx::Rect src_subrect
,
60 ReadbackRequestCallback
& result_callback
) override
;
61 virtual float GetDpiScale() const override
;
62 virtual void PauseOrResumeGeolocation(bool should_pause
) override
;
63 virtual void RequestTextSurroundingSelection(
65 const base::Callback
<void(const base::string16
& content
,
67 int end_offset
)>& callback
) override
;
69 // --------------------------------------------------------------------------
70 // Methods called from Java via JNI
71 // --------------------------------------------------------------------------
73 base::android::ScopedJavaLocalRef
<jobject
> GetWebContentsAndroid(JNIEnv
* env
,
76 void OnJavaContentViewCoreDestroyed(JNIEnv
* env
, jobject obj
);
78 // Notifies the ContentViewCore that items were selected in the currently
79 // showing select popup.
80 void SelectPopupMenuItems(JNIEnv
* env
, jobject obj
,
81 jlong selectPopupSourceFrame
,
84 void SendOrientationChangeEvent(JNIEnv
* env
, jobject obj
, jint orientation
);
85 jboolean
OnTouchEvent(JNIEnv
* env
,
100 jfloat touch_major_1
,
101 jfloat touch_minor_0
,
102 jfloat touch_minor_1
,
103 jfloat orientation_0
,
104 jfloat orientation_1
,
107 jint android_tool_type_0
,
108 jint android_tool_type_1
,
109 jint android_button_state
,
110 jint android_meta_state
,
111 jboolean is_touch_handle_event
);
112 jboolean
SendMouseMoveEvent(JNIEnv
* env
,
117 jboolean
SendMouseWheelEvent(JNIEnv
* env
,
122 jfloat vertical_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
);
183 jint
GetCurrentRenderProcessId(JNIEnv
* env
, jobject obj
);
185 // --------------------------------------------------------------------------
186 // Public methods that call to Java via JNI
187 // --------------------------------------------------------------------------
189 void OnSmartClipDataExtracted(const base::string16
& text
,
190 const base::string16
& html
,
191 const gfx::Rect
& clip_rect
);
193 // Creates a popup menu with |items|.
194 // |multiple| defines if it should support multi-select.
195 // If not |multiple|, |selected_item| sets the initially selected item.
196 // Otherwise, item's "checked" flag selects it.
197 void ShowSelectPopupMenu(RenderFrameHost
* frame
,
198 const gfx::Rect
& bounds
,
199 const std::vector
<MenuItem
>& items
,
202 // Hides a visible popup menu.
203 void HideSelectPopupMenu();
205 // All sizes and offsets are in CSS pixels as cached by the renderer.
206 void UpdateFrameInfo(const gfx::Vector2dF
& scroll_offset
,
207 float page_scale_factor
,
208 const gfx::Vector2dF
& page_scale_factor_limits
,
209 const gfx::SizeF
& content_size
,
210 const gfx::SizeF
& viewport_size
,
211 const gfx::Vector2dF
& controls_offset
,
212 const gfx::Vector2dF
& content_offset
);
214 void UpdateImeAdapter(long native_ime_adapter
,
216 int text_input_flags
,
217 const std::string
& text
,
220 int composition_start
,
222 bool show_ime_if_needed
,
223 bool is_non_ime_change
);
224 void SetTitle(const base::string16
& title
);
225 void OnBackgroundColorChanged(SkColor color
);
228 void OnGestureEventAck(const blink::WebGestureEvent
& event
,
229 InputEventAckState ack_result
);
230 bool FilterInputEvent(const blink::WebInputEvent
& event
);
231 void OnSelectionChanged(const std::string
& text
);
232 void OnSelectionEvent(ui::SelectionEventType event
,
233 const gfx::PointF
& anchor_position
);
234 scoped_ptr
<ui::TouchHandleDrawable
> CreatePopupTouchHandleDrawable();
236 void StartContentIntent(const GURL
& content_url
);
238 // Shows the disambiguation popup
239 // |rect_pixels| --> window coordinates which |zoomed_bitmap| represents
240 // |zoomed_bitmap| --> magnified image of potential touch targets
241 void ShowDisambiguationPopup(
242 const gfx::Rect
& rect_pixels
, const SkBitmap
& zoomed_bitmap
);
244 // Creates a java-side touch event, used for injecting touch event for
245 // testing/benchmarking purposes
246 base::android::ScopedJavaLocalRef
<jobject
> CreateTouchEventSynthesizer();
248 // Returns True if the given media should be blocked to load.
249 bool ShouldBlockMediaRequest(const GURL
& url
);
251 void DidStopFlinging();
253 // Returns the context with which the ContentViewCore was created, typically
254 // the Activity context.
255 base::android::ScopedJavaLocalRef
<jobject
> GetContext() const;
257 // Returns the viewport size after accounting for the viewport offset.
258 gfx::Size
GetViewSize() const;
260 void SetAccessibilityEnabledInternal(bool enabled
);
262 bool IsFullscreenRequiredForOrientationLock() const;
264 // --------------------------------------------------------------------------
265 // Methods called from native code
266 // --------------------------------------------------------------------------
268 gfx::Size
GetPhysicalBackingSize() const;
269 gfx::Size
GetViewportSizeDip() const;
270 bool DoTopControlsShrinkBlinkSize() const;
271 float GetTopControlsHeightDip() const;
273 void AttachLayer(scoped_refptr
<cc::Layer
> layer
);
274 void RemoveLayer(scoped_refptr
<cc::Layer
> layer
);
276 void MoveRangeSelectionExtent(const gfx::PointF
& extent
);
278 void SelectBetweenCoordinates(const gfx::PointF
& base
,
279 const gfx::PointF
& extent
);
282 class ContentViewUserData
;
284 friend class ContentViewUserData
;
285 virtual ~ContentViewCoreImpl();
287 // WebContentsObserver implementation.
288 virtual void RenderViewReady() override
;
289 virtual void RenderViewHostChanged(RenderViewHost
* old_host
,
290 RenderViewHost
* new_host
) override
;
291 virtual void WebContentsDestroyed() override
;
293 // --------------------------------------------------------------------------
294 // Other private methods and data
295 // --------------------------------------------------------------------------
297 void InitWebContents();
299 RenderWidgetHostViewAndroid
* GetRenderWidgetHostViewAndroid() const;
301 blink::WebGestureEvent
MakeGestureEvent(
302 blink::WebInputEvent::Type type
, int64 time_ms
, float x
, float y
) const;
304 gfx::Size
GetViewportSizePix() const;
305 int GetTopControlsHeightPix() const;
307 void SendGestureEvent(const blink::WebGestureEvent
& event
);
309 // Update focus state of the RenderWidgetHostView.
310 void SetFocusInternal(bool focused
);
312 // Send device_orientation_ to renderer.
313 void SendOrientationChangeEventInternal();
315 float dpi_scale() const { return dpi_scale_
; }
317 // A weak reference to the Java ContentViewCore object.
318 JavaObjectWeakGlobalRef java_ref_
;
320 // Reference to the current WebContents used to determine how and what to
321 // display in the ContentViewCore.
322 WebContentsImpl
* web_contents_
;
324 // A compositor layer containing any layer that should be shown.
325 scoped_refptr
<cc::Layer
> root_layer_
;
327 // Device scale factor.
330 // The Android view that can be used to add and remove decoration layers
331 // like AutofillPopup.
332 ui::ViewAndroid
* view_android_
;
334 // The owning window that has a hold of main application activity.
335 ui::WindowAndroid
* window_android_
;
337 // The cache of device's current orientation set from Java side, this value
338 // will be sent to Renderer once it is ready.
339 int device_orientation_
;
341 bool accessibility_enabled_
;
343 // Manages injecting Java objects.
344 scoped_refptr
<GinJavaBridgeDispatcherHost
> java_bridge_dispatcher_host_
;
346 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl
);
349 bool RegisterContentViewCore(JNIEnv
* env
);
351 } // namespace content
353 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_