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_helper.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/i18n/rtl.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/process/process.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/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/web_contents_observer.h"
23 #include "third_party/WebKit/public/web/WebInputEvent.h"
24 #include "ui/gfx/rect.h"
25 #include "ui/gfx/rect_f.h"
34 class RenderWidgetHostViewAndroid
;
37 // TODO(jrg): this is a shell. Upstream the rest.
38 class ContentViewCoreImpl
: public ContentViewCore
,
39 public NotificationObserver
,
40 public WebContentsObserver
{
42 static ContentViewCoreImpl
* FromWebContents(WebContents
* web_contents
);
43 ContentViewCoreImpl(JNIEnv
* env
,
45 bool hardware_accelerated
,
46 WebContents
* web_contents
,
47 ui::ViewAndroid
* view_android
,
48 ui::WindowAndroid
* window_android
);
50 // ContentViewCore implementation.
51 virtual base::android::ScopedJavaLocalRef
<jobject
> GetJavaObject() OVERRIDE
;
52 virtual WebContents
* GetWebContents() const OVERRIDE
;
53 virtual ui::ViewAndroid
* GetViewAndroid() const OVERRIDE
;
54 virtual ui::WindowAndroid
* GetWindowAndroid() const OVERRIDE
;
55 virtual scoped_refptr
<cc::Layer
> GetLayer() const OVERRIDE
;
56 virtual void LoadUrl(NavigationController::LoadURLParams
& params
) OVERRIDE
;
57 virtual jint
GetCurrentRenderProcessId(JNIEnv
* env
, jobject obj
) OVERRIDE
;
58 virtual void ShowPastePopup(int x
, int y
) OVERRIDE
;
59 virtual unsigned int GetScaledContentTexture(
61 gfx::Size
* out_size
) OVERRIDE
;
62 virtual float GetDpiScale() const OVERRIDE
;
63 virtual void RequestContentClipping(const gfx::Rect
& clipping
,
64 const gfx::Size
& content_size
) OVERRIDE
;
65 virtual void PauseVideo() OVERRIDE
;
66 virtual void ResumeVideo() OVERRIDE
;
67 virtual void PauseOrResumeGeolocation(bool should_pause
) 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
, jintArray indices
);
83 JNIEnv
* env
, jobject obj
,
87 jint ua_override_option
,
88 jstring extra_headers
,
90 jstring base_url_for_data_url
,
91 jstring virtual_url_for_data_url
,
92 jboolean can_load_local_resources
);
93 base::android::ScopedJavaLocalRef
<jstring
> GetURL(JNIEnv
* env
, jobject
) const;
94 base::android::ScopedJavaLocalRef
<jstring
> GetTitle(
95 JNIEnv
* env
, jobject obj
) const;
96 jboolean
IsIncognito(JNIEnv
* env
, jobject obj
);
97 void SendOrientationChangeEvent(JNIEnv
* env
, jobject obj
, jint orientation
);
98 jboolean
SendTouchEvent(JNIEnv
* env
,
103 jboolean
SendMouseMoveEvent(JNIEnv
* env
,
108 jboolean
SendMouseWheelEvent(JNIEnv
* env
,
113 jfloat vertical_axis
);
114 void ScrollBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
,
115 jfloat x
, jfloat y
, jfloat hintx
, jfloat hinty
);
116 void ScrollEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
117 void ScrollBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
118 jfloat x
, jfloat y
, jfloat dx
, jfloat dy
);
119 void FlingStart(JNIEnv
* env
, jobject obj
, jlong time_ms
,
120 jfloat x
, jfloat y
, jfloat vx
, jfloat vy
);
121 void FlingCancel(JNIEnv
* env
, jobject obj
, jlong time_ms
);
122 void SingleTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
124 jboolean disambiguation_popup_tap
);
125 void SingleTapUnconfirmed(JNIEnv
* env
, jobject obj
, jlong time_ms
,
127 void ShowPressState(JNIEnv
* env
, jobject obj
, jlong time_ms
,
129 void TapCancel(JNIEnv
* env
, jobject obj
, jlong time_ms
,
131 void TapDown(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 jboolean disambiguation_popup_tap
);
138 void LongTap(JNIEnv
* env
, jobject obj
, jlong time_ms
,
140 jboolean disambiguation_popup_tap
);
141 void PinchBegin(JNIEnv
* env
, jobject obj
, jlong time_ms
, jfloat x
, jfloat y
);
142 void PinchEnd(JNIEnv
* env
, jobject obj
, jlong time_ms
);
143 void PinchBy(JNIEnv
* env
, jobject obj
, jlong time_ms
,
144 jfloat x
, jfloat y
, jfloat delta
);
145 void SelectBetweenCoordinates(JNIEnv
* env
, jobject obj
,
146 jfloat x1
, jfloat y1
,
147 jfloat x2
, jfloat y2
);
148 void MoveCaret(JNIEnv
* env
, jobject obj
, jfloat x
, jfloat y
);
150 void LoadIfNecessary(JNIEnv
* env
, jobject obj
);
151 void RequestRestoreLoad(JNIEnv
* env
, jobject obj
);
152 void StopLoading(JNIEnv
* env
, jobject obj
);
153 void Reload(JNIEnv
* env
, jobject obj
, jboolean check_for_repost
);
154 void ReloadIgnoringCache(JNIEnv
* env
, jobject obj
, jboolean check_for_repost
);
155 void CancelPendingReload(JNIEnv
* env
, jobject obj
);
156 void ContinuePendingReload(JNIEnv
* env
, jobject obj
);
157 void ClearHistory(JNIEnv
* env
, jobject obj
);
158 void EvaluateJavaScript(JNIEnv
* env
,
162 jboolean start_renderer
);
163 int GetNativeImeAdapter(JNIEnv
* env
, jobject obj
);
164 void SetFocus(JNIEnv
* env
, jobject obj
, jboolean focused
);
165 void ScrollFocusedEditableNodeIntoView(JNIEnv
* env
, jobject obj
);
166 void UndoScrollFocusedEditableNodeIntoView(JNIEnv
* env
, jobject obj
);
168 jint
GetBackgroundColor(JNIEnv
* env
, jobject obj
);
169 void SetBackgroundColor(JNIEnv
* env
, jobject obj
, jint color
);
170 void OnShow(JNIEnv
* env
, jobject obj
);
171 void OnHide(JNIEnv
* env
, jobject obj
);
172 void ClearSslPreferences(JNIEnv
* env
, jobject
/* obj */);
173 void SetUseDesktopUserAgent(JNIEnv
* env
,
176 jboolean reload_on_state_change
);
177 bool GetUseDesktopUserAgent(JNIEnv
* env
, jobject
/* obj */);
180 void AddJavascriptInterface(JNIEnv
* env
,
184 jclass safe_annotation_clazz
,
185 jobject retained_object_set
);
186 void RemoveJavascriptInterface(JNIEnv
* env
, jobject obj
, jstring name
);
187 int GetNavigationHistory(JNIEnv
* env
, jobject obj
, jobject history
);
188 void GetDirectedNavigationHistory(JNIEnv
* env
,
193 base::android::ScopedJavaLocalRef
<jstring
>
194 GetOriginalUrlForActiveNavigationEntry(JNIEnv
* env
, jobject obj
);
195 void UpdateVSyncParameters(JNIEnv
* env
, jobject obj
, jlong timebase_micros
,
196 jlong interval_micros
);
197 void OnVSync(JNIEnv
* env
, jobject
/* obj */, jlong frame_time_micros
);
198 jboolean
OnAnimate(JNIEnv
* env
, jobject
/* obj */, jlong frame_time_micros
);
199 jboolean
PopulateBitmapFromCompositor(JNIEnv
* env
,
202 void WasResized(JNIEnv
* env
, jobject obj
);
203 jboolean
IsRenderWidgetHostViewReady(JNIEnv
* env
, jobject obj
);
204 void ExitFullscreen(JNIEnv
* env
, jobject obj
);
205 void UpdateTopControlsState(JNIEnv
* env
,
210 void ShowImeIfNeeded(JNIEnv
* env
, jobject obj
);
212 void ShowInterstitialPage(JNIEnv
* env
,
216 jboolean
IsShowingInterstitialPage(JNIEnv
* env
, jobject obj
);
218 void AttachExternalVideoSurface(JNIEnv
* env
,
222 void DetachExternalVideoSurface(JNIEnv
* env
, jobject obj
, jint player_id
);
223 void SetAccessibilityEnabled(JNIEnv
* env
, jobject obj
, bool enabled
);
224 void SendActionAfterDoubleTapUma(JNIEnv
* env
,
229 void SendSingleTapUma(JNIEnv
* env
, jobject obj
, jint type
, jint count
);
231 void ExtractSmartClipData(JNIEnv
* env
,
237 // --------------------------------------------------------------------------
238 // Public methods that call to Java via JNI
239 // --------------------------------------------------------------------------
241 void OnSmartClipDataExtracted(const base::string16
& result
);
243 // Creates a popup menu with |items|.
244 // |multiple| defines if it should support multi-select.
245 // If not |multiple|, |selected_item| sets the initially selected item.
246 // Otherwise, item's "checked" flag selects it.
247 void ShowSelectPopupMenu(const std::vector
<MenuItem
>& items
,
253 // All sizes and offsets are in CSS pixels as cached by the renderer.
254 void UpdateFrameInfo(const gfx::Vector2dF
& scroll_offset
,
255 float page_scale_factor
,
256 const gfx::Vector2dF
& page_scale_factor_limits
,
257 const gfx::SizeF
& content_size
,
258 const gfx::SizeF
& viewport_size
,
259 const gfx::Vector2dF
& controls_offset
,
260 const gfx::Vector2dF
& content_offset
,
261 float overdraw_bottom_height
);
263 void UpdateImeAdapter(int native_ime_adapter
, int text_input_type
,
264 const std::string
& text
,
265 int selection_start
, int selection_end
,
266 int composition_start
, int composition_end
,
267 bool show_ime_if_needed
, bool require_ack
);
268 void SetTitle(const base::string16
& title
);
269 void OnBackgroundColorChanged(SkColor color
);
272 void ConfirmTouchEvent(InputEventAckState ack_result
);
273 void UnhandledFlingStartEvent();
274 void OnScrollUpdateGestureConsumed();
275 void HasTouchEventHandlers(bool need_touch_events
);
276 void OnSelectionChanged(const std::string
& text
);
277 void OnSelectionBoundsChanged(
278 const ViewHostMsg_SelectionBounds_Params
& params
);
280 void StartContentIntent(const GURL
& content_url
);
282 // Shows the disambiguation popup
283 // |target_rect| --> window coordinates which |zoomed_bitmap| represents
284 // |zoomed_bitmap| --> magnified image of potential touch targets
285 void ShowDisambiguationPopup(
286 const gfx::Rect
& target_rect
, const SkBitmap
& zoomed_bitmap
);
288 // Creates a java-side touch event, used for injecting touch event for
289 // testing/benchmarking purposes
290 base::android::ScopedJavaLocalRef
<jobject
> CreateTouchEventSynthesizer();
292 // Notifies the java object about the external surface, requesting for one if
294 void NotifyExternalSurface(
295 int player_id
, bool is_request
, const gfx::RectF
& rect
);
297 base::android::ScopedJavaLocalRef
<jobject
> GetContentVideoViewClient();
299 // Returns the context that the ContentViewCore was created with, it would
300 // typically be an Activity context for an on screen view.
301 base::android::ScopedJavaLocalRef
<jobject
> GetContext();
303 // Returns True if the given media should be blocked to load.
304 bool ShouldBlockMediaRequest(const GURL
& url
);
306 // --------------------------------------------------------------------------
307 // Methods called from native code
308 // --------------------------------------------------------------------------
310 gfx::Size
GetPhysicalBackingSize() const;
311 gfx::Size
GetViewportSizeDip() const;
312 gfx::Size
GetViewportSizeOffsetDip() const;
313 float GetOverdrawBottomHeightDip() const;
315 void AttachLayer(scoped_refptr
<cc::Layer
> layer
);
316 void RemoveLayer(scoped_refptr
<cc::Layer
> layer
);
317 void AddBeginFrameSubscriber();
318 void RemoveBeginFrameSubscriber();
319 void SetNeedsAnimate();
322 class ContentViewUserData
;
324 friend class ContentViewUserData
;
325 virtual ~ContentViewCoreImpl();
327 // NotificationObserver implementation.
328 virtual void Observe(int type
,
329 const NotificationSource
& source
,
330 const NotificationDetails
& details
) OVERRIDE
;
332 // WebContentsObserver implementation.
333 virtual void RenderViewReady() OVERRIDE
;
335 // --------------------------------------------------------------------------
336 // Other private methods and data
337 // --------------------------------------------------------------------------
339 void InitWebContents();
341 RenderWidgetHostViewAndroid
* GetRenderWidgetHostViewAndroid();
343 float GetTouchPaddingDip();
345 blink::WebGestureEvent
MakeGestureEvent(
346 blink::WebInputEvent::Type type
, int64 time_ms
, float x
, float y
) const;
348 void SendBeginFrame(base::TimeTicks frame_time
);
350 gfx::Size
GetViewportSizePix() const;
351 gfx::Size
GetViewportSizeOffsetPix() const;
353 void DeleteScaledSnapshotTexture();
355 void SendGestureEvent(const blink::WebGestureEvent
& event
);
357 // Update focus state of the RenderWidgetHostView.
358 void SetFocusInternal(bool focused
);
360 // Send device_orientation_ to renderer.
361 void SendOrientationChangeEventInternal();
363 // A weak reference to the Java ContentViewCore object.
364 JavaObjectWeakGlobalRef java_ref_
;
366 NotificationRegistrar notification_registrar_
;
368 // Reference to the current WebContents used to determine how and what to
369 // display in the ContentViewCore.
370 WebContentsImpl
* web_contents_
;
372 // A compositor layer containing any layer that should be shown.
373 scoped_refptr
<cc::Layer
> root_layer_
;
375 // Device scale factor.
378 // Variables used to keep track of frame timestamps and deadlines.
379 base::TimeDelta vsync_interval_
;
380 base::TimeDelta expected_browser_composite_time_
;
382 // The Android view that can be used to add and remove decoration layers
383 // like AutofillPopup.
384 ui::ViewAndroid
* view_android_
;
386 // The owning window that has a hold of main application activity.
387 ui::WindowAndroid
* window_android_
;
389 // The cache of device's current orientation set from Java side, this value
390 // will be sent to Renderer once it is ready.
391 int device_orientation_
;
393 bool geolocation_needs_pause_
;
395 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl
);
398 bool RegisterContentViewCore(JNIEnv
* env
);
400 } // namespace content
402 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_