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 scoped_refptr
<cc::Layer
> GetLayer() const OVERRIDE
;
55 virtual void LoadUrl(NavigationController::LoadURLParams
& params
) OVERRIDE
;
56 virtual void ShowPastePopup(int x
, int y
) OVERRIDE
;
57 virtual void GetScaledContentBitmap(
59 SkColorType color_type
,
60 gfx::Rect src_subrect
,
61 const base::Callback
<void(bool, const SkBitmap
&)>& result_callback
)
63 virtual float GetDpiScale() const OVERRIDE
;
64 virtual void PauseVideo() OVERRIDE
;
65 virtual void PauseOrResumeGeolocation(bool should_pause
) OVERRIDE
;
66 virtual void RequestTextSurroundingSelection(
68 const base::Callback
<void(const base::string16
& content
,
70 int end_offset
)>& callback
) OVERRIDE
;
72 // --------------------------------------------------------------------------
73 // Methods called from Java via JNI
74 // --------------------------------------------------------------------------
76 base::android::ScopedJavaLocalRef
<jobject
> GetWebContentsAndroid(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
,
88 JNIEnv
* env
, jobject obj
,
92 jstring j_referrer_url
,
94 jint ua_override_option
,
95 jstring extra_headers
,
97 jstring base_url_for_data_url
,
98 jstring virtual_url_for_data_url
,
99 jboolean can_load_local_resources
,
100 jboolean is_renderer_initiated
);
101 void SendOrientationChangeEvent(JNIEnv
* env
, jobject obj
, jint orientation
);
102 jboolean
OnTouchEvent(JNIEnv
* env
,
104 jobject motion_event
,
116 jfloat touch_major_0
,
117 jfloat touch_major_1
,
120 jint android_tool_type_0
,
121 jint android_tool_type_1
,
122 jint android_button_state
,
123 jboolean is_touch_handle_event
);
124 jboolean
SendMouseMoveEvent(JNIEnv
* env
,
129 jboolean
SendMouseWheelEvent(JNIEnv
* env
,
134 jfloat vertical_axis
);
135 void ScrollBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
,
136 jfloat x
, jfloat y
, jfloat hintx
, jfloat hinty
);
137 void ScrollEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
138 void ScrollBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
139 jfloat x
, jfloat y
, jfloat dx
, jfloat dy
);
140 void FlingStart(JNIEnv
* env
, jobject obj
, jlong time_ms
,
141 jfloat x
, jfloat y
, jfloat vx
, jfloat vy
);
142 void FlingCancel(JNIEnv
* env
, jobject obj
, jlong time_ms
);
143 void SingleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
145 void DoubleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
146 jfloat x
, jfloat y
) ;
147 void LongPress(JNIEnv
* env
, jobject obj
, jlong time_ms
,
149 void PinchBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
, jfloat x
, jfloat y
);
150 void PinchEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
151 void PinchBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
152 jfloat x
, jfloat y
, jfloat delta
);
153 void SelectBetweenCoordinates(JNIEnv
* env
, jobject obj
,
154 jfloat x1
, jfloat y1
,
155 jfloat x2
, jfloat y2
);
156 void MoveCaret(JNIEnv
* env
, jobject obj
, jfloat x
, jfloat y
);
157 void HideTextHandles(JNIEnv
* env
, jobject obj
);
159 void ResetGestureDetection(JNIEnv
* env
, jobject obj
);
160 void SetDoubleTapSupportEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
);
161 void SetMultiTouchZoomSupportEnabled(JNIEnv
* env
,
165 void ClearHistory(JNIEnv
* env
, jobject obj
);
166 void PostMessageToFrame(JNIEnv
* env
, jobject obj
, jstring frame_id
,
167 jstring message
, jstring source_origin
, jstring target_origin
);
168 long GetNativeImeAdapter(JNIEnv
* env
, jobject obj
);
169 void SetFocus(JNIEnv
* env
, jobject obj
, jboolean focused
);
171 jint
GetBackgroundColor(JNIEnv
* env
, jobject obj
);
172 void SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
);
173 void ClearSslPreferences(JNIEnv
* env
, jobject
/* obj */);
174 void SetUseDesktopUserAgent(JNIEnv
* env
,
177 jboolean reload_on_state_change
);
178 bool GetUseDesktopUserAgent(JNIEnv
* env
, jobject
/* obj */);
179 void SetAllowJavascriptInterfacesInspection(JNIEnv
* env
,
182 void AddJavascriptInterface(JNIEnv
* env
,
186 jclass safe_annotation_clazz
);
187 void RemoveJavascriptInterface(JNIEnv
* env
, jobject obj
, jstring name
);
188 int GetNavigationHistory(JNIEnv
* env
, jobject obj
, jobject history
);
189 void GetDirectedNavigationHistory(JNIEnv
* env
,
194 base::android::ScopedJavaLocalRef
<jstring
>
195 GetOriginalUrlForActiveNavigationEntry(JNIEnv
* env
, jobject obj
);
196 void WasResized(JNIEnv
* env
, jobject obj
);
198 void SetAccessibilityEnabled(JNIEnv
* env
, jobject obj
, bool enabled
);
200 void ExtractSmartClipData(JNIEnv
* env
,
207 void SetBackgroundOpaque(JNIEnv
* env
, jobject jobj
, jboolean opaque
);
209 jint
GetCurrentRenderProcessId(JNIEnv
* env
, jobject obj
);
211 // --------------------------------------------------------------------------
212 // Public methods that call to Java via JNI
213 // --------------------------------------------------------------------------
215 void OnSmartClipDataExtracted(const base::string16
& text
,
216 const base::string16
& html
,
217 const gfx::Rect
& clip_rect
);
219 // Creates a popup menu with |items|.
220 // |multiple| defines if it should support multi-select.
221 // If not |multiple|, |selected_item| sets the initially selected item.
222 // Otherwise, item's "checked" flag selects it.
223 void ShowSelectPopupMenu(RenderFrameHost
* frame
,
224 const gfx::Rect
& bounds
,
225 const std::vector
<MenuItem
>& items
,
228 // Hides a visible popup menu.
229 void HideSelectPopupMenu();
231 // All sizes and offsets are in CSS pixels as cached by the renderer.
232 void UpdateFrameInfo(const gfx::Vector2dF
& scroll_offset
,
233 float page_scale_factor
,
234 const gfx::Vector2dF
& page_scale_factor_limits
,
235 const gfx::SizeF
& content_size
,
236 const gfx::SizeF
& viewport_size
,
237 const gfx::Vector2dF
& controls_offset
,
238 const gfx::Vector2dF
& content_offset
);
240 void UpdateImeAdapter(long native_ime_adapter
,
242 int text_input_flags
,
243 const std::string
& text
,
246 int composition_start
,
248 bool show_ime_if_needed
,
249 bool is_non_ime_change
);
250 void SetTitle(const base::string16
& title
);
251 void OnBackgroundColorChanged(SkColor color
);
254 void OnGestureEventAck(const blink::WebGestureEvent
& event
,
255 InputEventAckState ack_result
);
256 bool FilterInputEvent(const blink::WebInputEvent
& event
);
257 void OnSelectionChanged(const std::string
& text
);
258 void OnSelectionEvent(SelectionEventType event
,
259 const gfx::PointF
& anchor_position
);
260 scoped_ptr
<TouchHandleDrawable
> CreatePopupTouchHandleDrawable();
262 void StartContentIntent(const GURL
& content_url
);
264 // Shows the disambiguation popup
265 // |target_rect| --> window coordinates which |zoomed_bitmap| represents
266 // |zoomed_bitmap| --> magnified image of potential touch targets
267 void ShowDisambiguationPopup(
268 const gfx::Rect
& target_rect
, const SkBitmap
& zoomed_bitmap
);
270 // Creates a java-side touch event, used for injecting touch event for
271 // testing/benchmarking purposes
272 base::android::ScopedJavaLocalRef
<jobject
> CreateTouchEventSynthesizer();
274 base::android::ScopedJavaLocalRef
<jobject
> GetContentVideoViewClient();
276 // Returns the context that the ContentViewCore was created with, it would
277 // typically be an Activity context for an on screen view.
278 base::android::ScopedJavaLocalRef
<jobject
> GetContext();
280 // Returns True if the given media should be blocked to load.
281 bool ShouldBlockMediaRequest(const GURL
& url
);
283 void DidStopFlinging();
285 // Returns the viewport size after accounting for the viewport offset.
286 gfx::Size
GetViewSize() const;
288 void SetAccessibilityEnabledInternal(bool enabled
);
290 bool IsFullscreenRequiredForOrientationLock() const;
292 // --------------------------------------------------------------------------
293 // Methods called from native code
294 // --------------------------------------------------------------------------
296 gfx::Size
GetPhysicalBackingSize() const;
297 gfx::Size
GetViewportSizeDip() const;
298 float GetTopControlsLayoutHeightDip() const;
300 void AttachLayer(scoped_refptr
<cc::Layer
> layer
);
301 void RemoveLayer(scoped_refptr
<cc::Layer
> layer
);
303 void SelectBetweenCoordinates(const gfx::PointF
& start
,
304 const gfx::PointF
& end
);
307 class ContentViewUserData
;
309 friend class ContentViewUserData
;
310 virtual ~ContentViewCoreImpl();
312 // WebContentsObserver implementation.
313 virtual void RenderViewReady() OVERRIDE
;
314 virtual void RenderViewHostChanged(RenderViewHost
* old_host
,
315 RenderViewHost
* new_host
) OVERRIDE
;
316 virtual void WebContentsDestroyed() OVERRIDE
;
318 // --------------------------------------------------------------------------
319 // Other private methods and data
320 // --------------------------------------------------------------------------
322 void InitWebContents();
324 RenderWidgetHostViewAndroid
* GetRenderWidgetHostViewAndroid();
326 blink::WebGestureEvent
MakeGestureEvent(
327 blink::WebInputEvent::Type type
, int64 time_ms
, float x
, float y
) const;
329 gfx::Size
GetViewportSizePix() const;
330 int GetTopControlsLayoutHeightPix() const;
332 void SendGestureEvent(const blink::WebGestureEvent
& event
);
334 // Update focus state of the RenderWidgetHostView.
335 void SetFocusInternal(bool focused
);
337 // Send device_orientation_ to renderer.
338 void SendOrientationChangeEventInternal();
340 float dpi_scale() const { return dpi_scale_
; }
342 // A weak reference to the Java ContentViewCore object.
343 JavaObjectWeakGlobalRef java_ref_
;
345 // Reference to the current WebContents used to determine how and what to
346 // display in the ContentViewCore.
347 WebContentsImpl
* web_contents_
;
349 // A compositor layer containing any layer that should be shown.
350 scoped_refptr
<cc::Layer
> root_layer_
;
352 // Device scale factor.
355 // The Android view that can be used to add and remove decoration layers
356 // like AutofillPopup.
357 ui::ViewAndroid
* view_android_
;
359 // The owning window that has a hold of main application activity.
360 ui::WindowAndroid
* window_android_
;
362 // The cache of device's current orientation set from Java side, this value
363 // will be sent to Renderer once it is ready.
364 int device_orientation_
;
366 bool accessibility_enabled_
;
368 // Manages injecting Java objects.
369 scoped_ptr
<GinJavaBridgeDispatcherHost
>
370 java_bridge_dispatcher_host_
;
372 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl
);
375 bool RegisterContentViewCore(JNIEnv
* env
);
377 } // namespace content
379 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_