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"
31 class JavaBridgeDispatcherHostManager
;
32 class RenderWidgetHostViewAndroid
;
35 // TODO(jrg): this is a shell. Upstream the rest.
36 class ContentViewCoreImpl
: public ContentViewCore
,
37 public WebContentsObserver
{
39 static ContentViewCoreImpl
* FromWebContents(WebContents
* web_contents
);
40 ContentViewCoreImpl(JNIEnv
* env
,
42 WebContents
* web_contents
,
43 ui::ViewAndroid
* view_android
,
44 ui::WindowAndroid
* window_android
,
45 jobject java_bridge_retained_object_set
);
47 // ContentViewCore implementation.
48 virtual base::android::ScopedJavaLocalRef
<jobject
> GetJavaObject() OVERRIDE
;
49 virtual WebContents
* GetWebContents() const OVERRIDE
;
50 virtual ui::ViewAndroid
* GetViewAndroid() const OVERRIDE
;
51 virtual ui::WindowAndroid
* GetWindowAndroid() const OVERRIDE
;
52 virtual scoped_refptr
<cc::Layer
> GetLayer() const OVERRIDE
;
53 virtual void LoadUrl(NavigationController::LoadURLParams
& params
) OVERRIDE
;
54 virtual void ShowPastePopup(int x
, int y
) OVERRIDE
;
55 virtual void GetScaledContentBitmap(
57 jobject bitmap_config
,
58 gfx::Rect src_subrect
,
59 const base::Callback
<void(bool, const SkBitmap
&)>& result_callback
)
61 virtual float GetDpiScale() const OVERRIDE
;
62 virtual void PauseVideo() OVERRIDE
;
63 virtual void PauseOrResumeGeolocation(bool should_pause
) OVERRIDE
;
64 virtual 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
,
77 void OnJavaContentViewCoreDestroyed(JNIEnv
* env
, jobject obj
);
79 // Notifies the ContentViewCore that items were selected in the currently
80 // showing select popup.
81 void SelectPopupMenuItems(JNIEnv
* env
, jobject obj
, jintArray indices
);
84 JNIEnv
* env
, jobject obj
,
88 jstring j_referrer_url
,
90 jint ua_override_option
,
91 jstring extra_headers
,
93 jstring base_url_for_data_url
,
94 jstring virtual_url_for_data_url
,
95 jboolean can_load_local_resources
,
96 jboolean is_renderer_initiated
);
97 base::android::ScopedJavaLocalRef
<jstring
> GetURL(JNIEnv
* env
, jobject
) const;
98 jboolean
IsIncognito(JNIEnv
* env
, jobject obj
);
99 void SendOrientationChangeEvent(JNIEnv
* env
, jobject obj
, jint orientation
);
100 jboolean
OnTouchEvent(JNIEnv
* env
,
102 jobject motion_event
,
114 jfloat touch_major_0
,
115 jfloat touch_major_1
,
118 jboolean
SendMouseMoveEvent(JNIEnv
* env
,
123 jboolean
SendMouseWheelEvent(JNIEnv
* env
,
128 jfloat vertical_axis
);
129 void ScrollBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
,
130 jfloat x
, jfloat y
, jfloat hintx
, jfloat hinty
);
131 void ScrollEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
132 void ScrollBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
133 jfloat x
, jfloat y
, jfloat dx
, jfloat dy
);
134 void FlingStart(JNIEnv
* env
, jobject obj
, jlong time_ms
,
135 jfloat x
, jfloat y
, jfloat vx
, jfloat vy
);
136 void FlingCancel(JNIEnv
* env
, jobject obj
, jlong time_ms
);
137 void SingleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
139 void DoubleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
140 jfloat x
, jfloat y
) ;
141 void LongPress(JNIEnv
* env
, jobject obj
, jlong time_ms
,
143 void PinchBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
, jfloat x
, jfloat y
);
144 void PinchEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
145 void PinchBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
146 jfloat x
, jfloat y
, jfloat delta
);
147 void SelectBetweenCoordinates(JNIEnv
* env
, jobject obj
,
148 jfloat x1
, jfloat y1
,
149 jfloat x2
, jfloat y2
);
150 void MoveCaret(JNIEnv
* env
, jobject obj
, jfloat x
, jfloat y
);
152 void ResetGestureDetection(JNIEnv
* env
, jobject obj
);
153 void SetDoubleTapSupportEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
);
154 void SetMultiTouchZoomSupportEnabled(JNIEnv
* env
,
158 void LoadIfNecessary(JNIEnv
* env
, jobject obj
);
159 void RequestRestoreLoad(JNIEnv
* env
, jobject obj
);
160 void Reload(JNIEnv
* env
, jobject obj
, jboolean check_for_repost
);
161 void ReloadIgnoringCache(JNIEnv
* env
, jobject obj
, jboolean check_for_repost
);
162 void CancelPendingReload(JNIEnv
* env
, jobject obj
);
163 void ContinuePendingReload(JNIEnv
* env
, jobject obj
);
164 void ClearHistory(JNIEnv
* env
, jobject obj
);
165 void EvaluateJavaScript(JNIEnv
* env
,
169 jboolean start_renderer
);
170 long GetNativeImeAdapter(JNIEnv
* env
, jobject obj
);
171 void SetFocus(JNIEnv
* env
, jobject obj
, jboolean focused
);
172 void ScrollFocusedEditableNodeIntoView(JNIEnv
* env
, jobject obj
);
173 void SelectWordAroundCaret(JNIEnv
* env
, jobject obj
);
175 jint
GetBackgroundColor(JNIEnv
* env
, jobject obj
);
176 void SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
);
177 void OnShow(JNIEnv
* env
, jobject obj
);
178 void OnHide(JNIEnv
* env
, jobject obj
);
179 void ClearSslPreferences(JNIEnv
* env
, jobject
/* obj */);
180 void SetUseDesktopUserAgent(JNIEnv
* env
,
183 jboolean reload_on_state_change
);
184 bool GetUseDesktopUserAgent(JNIEnv
* env
, jobject
/* obj */);
187 void SetAllowJavascriptInterfacesInspection(JNIEnv
* env
,
190 void AddJavascriptInterface(JNIEnv
* env
,
194 jclass safe_annotation_clazz
);
195 void RemoveJavascriptInterface(JNIEnv
* env
, jobject obj
, jstring name
);
196 int GetNavigationHistory(JNIEnv
* env
, jobject obj
, jobject history
);
197 void GetDirectedNavigationHistory(JNIEnv
* env
,
202 base::android::ScopedJavaLocalRef
<jstring
>
203 GetOriginalUrlForActiveNavigationEntry(JNIEnv
* env
, jobject obj
);
204 void WasResized(JNIEnv
* env
, jobject obj
);
205 jboolean
IsRenderWidgetHostViewReady(JNIEnv
* env
, jobject obj
);
206 void ExitFullscreen(JNIEnv
* env
, jobject obj
);
207 void UpdateTopControlsState(JNIEnv
* env
,
212 void ShowImeIfNeeded(JNIEnv
* env
, jobject obj
);
214 void ShowInterstitialPage(JNIEnv
* env
,
218 jboolean
IsShowingInterstitialPage(JNIEnv
* env
, jobject obj
);
220 void SetAccessibilityEnabled(JNIEnv
* env
, jobject obj
, bool enabled
);
222 void ExtractSmartClipData(JNIEnv
* env
,
229 void SetBackgroundOpaque(JNIEnv
* env
, jobject jobj
, jboolean opaque
);
231 jint
GetCurrentRenderProcessId(JNIEnv
* env
, jobject obj
);
233 // --------------------------------------------------------------------------
234 // Public methods that call to Java via JNI
235 // --------------------------------------------------------------------------
237 void OnSmartClipDataExtracted(const base::string16
& result
);
239 // Creates a popup menu with |items|.
240 // |multiple| defines if it should support multi-select.
241 // If not |multiple|, |selected_item| sets the initially selected item.
242 // Otherwise, item's "checked" flag selects it.
243 void ShowSelectPopupMenu(const gfx::Rect
& bounds
,
244 const std::vector
<MenuItem
>& items
,
247 // Hides a visible popup menu.
248 void HideSelectPopupMenu();
250 // All sizes and offsets are in CSS pixels as cached by the renderer.
251 void UpdateFrameInfo(const gfx::Vector2dF
& scroll_offset
,
252 float page_scale_factor
,
253 const gfx::Vector2dF
& page_scale_factor_limits
,
254 const gfx::SizeF
& content_size
,
255 const gfx::SizeF
& viewport_size
,
256 const gfx::Vector2dF
& controls_offset
,
257 const gfx::Vector2dF
& content_offset
,
258 float overdraw_bottom_height
);
260 void UpdateImeAdapter(long native_ime_adapter
, int text_input_type
,
261 const std::string
& text
,
262 int selection_start
, int selection_end
,
263 int composition_start
, int composition_end
,
264 bool show_ime_if_needed
, bool is_non_ime_change
);
265 void SetTitle(const base::string16
& title
);
266 void OnBackgroundColorChanged(SkColor color
);
269 void OnGestureEventAck(const blink::WebGestureEvent
& event
,
270 InputEventAckState ack_result
);
271 bool FilterInputEvent(const blink::WebInputEvent
& event
);
272 void OnSelectionChanged(const std::string
& text
);
273 void OnSelectionBoundsChanged(
274 const ViewHostMsg_SelectionBounds_Params
& params
);
276 void StartContentIntent(const GURL
& content_url
);
278 // Shows the disambiguation popup
279 // |target_rect| --> window coordinates which |zoomed_bitmap| represents
280 // |zoomed_bitmap| --> magnified image of potential touch targets
281 void ShowDisambiguationPopup(
282 const gfx::Rect
& target_rect
, const SkBitmap
& zoomed_bitmap
);
284 // Creates a java-side touch event, used for injecting touch event for
285 // testing/benchmarking purposes
286 base::android::ScopedJavaLocalRef
<jobject
> CreateTouchEventSynthesizer();
288 base::android::ScopedJavaLocalRef
<jobject
> GetContentVideoViewClient();
290 // Returns the context that the ContentViewCore was created with, it would
291 // typically be an Activity context for an on screen view.
292 base::android::ScopedJavaLocalRef
<jobject
> GetContext();
294 // Returns True if the given media should be blocked to load.
295 bool ShouldBlockMediaRequest(const GURL
& url
);
297 void DidStopFlinging();
299 // Returns the viewport size after accounting for the viewport offset.
300 gfx::Size
GetViewSize() const;
302 void SetAccessibilityEnabledInternal(bool enabled
);
304 // --------------------------------------------------------------------------
305 // Methods called from native code
306 // --------------------------------------------------------------------------
308 gfx::Size
GetPhysicalBackingSize() const;
309 gfx::Size
GetViewportSizeDip() const;
310 gfx::Size
GetViewportSizeOffsetDip() const;
311 float GetOverdrawBottomHeightDip() const;
313 void AttachLayer(scoped_refptr
<cc::Layer
> layer
);
314 void RemoveLayer(scoped_refptr
<cc::Layer
> layer
);
317 class ContentViewUserData
;
319 friend class ContentViewUserData
;
320 virtual ~ContentViewCoreImpl();
322 // WebContentsObserver implementation.
323 virtual void RenderViewReady() OVERRIDE
;
324 virtual void RenderViewHostChanged(RenderViewHost
* old_host
,
325 RenderViewHost
* new_host
) OVERRIDE
;
326 virtual void WebContentsDestroyed() OVERRIDE
;
328 // --------------------------------------------------------------------------
329 // Other private methods and data
330 // --------------------------------------------------------------------------
332 void InitWebContents();
334 RenderWidgetHostViewAndroid
* GetRenderWidgetHostViewAndroid();
336 blink::WebGestureEvent
MakeGestureEvent(
337 blink::WebInputEvent::Type type
, int64 time_ms
, float x
, float y
) const;
339 gfx::Size
GetViewportSizePix() const;
340 gfx::Size
GetViewportSizeOffsetPix() const;
342 void DeleteScaledSnapshotTexture();
344 bool OnMotionEvent(const ui::MotionEvent
& event
);
345 void SendGestureEvent(const blink::WebGestureEvent
& event
);
347 // Update focus state of the RenderWidgetHostView.
348 void SetFocusInternal(bool focused
);
350 // Send device_orientation_ to renderer.
351 void SendOrientationChangeEventInternal();
353 float dpi_scale() const { return dpi_scale_
; }
355 // A weak reference to the Java ContentViewCore object.
356 JavaObjectWeakGlobalRef java_ref_
;
358 // Reference to the current WebContents used to determine how and what to
359 // display in the ContentViewCore.
360 WebContentsImpl
* web_contents_
;
362 // A compositor layer containing any layer that should be shown.
363 scoped_refptr
<cc::Layer
> root_layer_
;
365 // Device scale factor.
368 // The Android view that can be used to add and remove decoration layers
369 // like AutofillPopup.
370 ui::ViewAndroid
* view_android_
;
372 // The owning window that has a hold of main application activity.
373 ui::WindowAndroid
* window_android_
;
375 // The cache of device's current orientation set from Java side, this value
376 // will be sent to Renderer once it is ready.
377 int device_orientation_
;
379 bool accessibility_enabled_
;
381 // Manages injecting Java objects.
382 scoped_ptr
<JavaBridgeDispatcherHostManager
>
383 java_bridge_dispatcher_host_manager_
;
385 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl
);
388 bool RegisterContentViewCore(JNIEnv
* env
);
390 } // namespace content
392 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_