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/input/touch_disposition_gesture_filter.h"
18 #include "content/browser/renderer_host/render_widget_host_view_android.h"
19 #include "content/browser/web_contents/web_contents_impl.h"
20 #include "content/public/browser/android/content_view_core.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "content/public/browser/web_contents_observer.h"
24 #include "third_party/WebKit/public/web/WebInputEvent.h"
25 #include "ui/gfx/rect.h"
26 #include "ui/gfx/rect_f.h"
35 class RenderWidgetHostViewAndroid;
38 // TODO(jrg): this is a shell. Upstream the rest.
39 class ContentViewCoreImpl : public ContentViewCore,
40 public NotificationObserver,
41 public TouchDispositionGestureFilterClient,
42 public WebContentsObserver {
44 static ContentViewCoreImpl* FromWebContents(WebContents* web_contents);
45 ContentViewCoreImpl(JNIEnv* env,
47 WebContents* web_contents,
48 ui::ViewAndroid* view_android,
49 ui::WindowAndroid* window_android);
51 // ContentViewCore implementation.
52 virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() OVERRIDE;
53 virtual WebContents* GetWebContents() const OVERRIDE;
54 virtual ui::ViewAndroid* GetViewAndroid() const OVERRIDE;
55 virtual ui::WindowAndroid* GetWindowAndroid() const OVERRIDE;
56 virtual scoped_refptr<cc::Layer> GetLayer() const OVERRIDE;
57 virtual void LoadUrl(NavigationController::LoadURLParams& params) OVERRIDE;
58 virtual jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj) OVERRIDE;
59 virtual void ShowPastePopup(int x, int y) OVERRIDE;
60 virtual void GetScaledContentBitmap(
63 const base::Callback<void(bool, const SkBitmap&)>& result_callback)
65 virtual float GetDpiScale() const OVERRIDE;
66 virtual void RequestContentClipping(const gfx::Rect& clipping,
67 const gfx::Size& content_size) OVERRIDE;
68 virtual void PauseVideo() OVERRIDE;
69 virtual void PauseOrResumeGeolocation(bool should_pause) OVERRIDE;
71 // --------------------------------------------------------------------------
72 // Methods called from Java via JNI
73 // --------------------------------------------------------------------------
75 base::android::ScopedJavaLocalRef<jobject> GetWebContentsAndroid(JNIEnv* env,
78 void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
80 // Notifies the ContentViewCore that items were selected in the currently
81 // showing select popup.
82 void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices);
85 JNIEnv* env, jobject obj,
89 jint ua_override_option,
90 jstring extra_headers,
92 jstring base_url_for_data_url,
93 jstring virtual_url_for_data_url,
94 jboolean can_load_local_resources);
95 base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const;
96 base::android::ScopedJavaLocalRef<jstring> GetTitle(
97 JNIEnv* env, jobject obj) const;
98 jboolean IsIncognito(JNIEnv* env, jobject obj);
99 void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
100 void OnTouchEventHandlingBegin(JNIEnv* env,
102 jobject motion_event);
103 void OnTouchEventHandlingEnd(JNIEnv* env, jobject obj);
104 jboolean SendMouseMoveEvent(JNIEnv* env,
109 jboolean SendMouseWheelEvent(JNIEnv* env,
114 jfloat vertical_axis);
115 void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms,
116 jfloat x, jfloat y, jfloat hintx, jfloat hinty);
117 void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
118 void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
119 jfloat x, jfloat y, jfloat dx, jfloat dy);
120 void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
121 jfloat x, jfloat y, jfloat vx, jfloat vy);
122 void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
123 void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
125 jboolean disambiguation_popup_tap);
126 void SingleTapUnconfirmed(JNIEnv* env, jobject obj, jlong time_ms,
128 void ShowPress(JNIEnv* env, jobject obj, jlong time_ms,
130 void TapCancel(JNIEnv* env, jobject obj, jlong time_ms,
132 void TapDown(JNIEnv* env, jobject obj, jlong time_ms,
134 void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
135 jfloat x, jfloat y) ;
136 void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
138 jboolean disambiguation_popup_tap);
139 void LongTap(JNIEnv* env, jobject obj, jlong time_ms,
141 jboolean disambiguation_popup_tap);
142 void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
143 void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
144 void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
145 jfloat x, jfloat y, jfloat delta);
146 void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
147 jfloat x1, jfloat y1,
148 jfloat x2, jfloat y2);
149 void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
151 void LoadIfNecessary(JNIEnv* env, jobject obj);
152 void RequestRestoreLoad(JNIEnv* env, jobject obj);
153 void StopLoading(JNIEnv* env, jobject obj);
154 void Reload(JNIEnv* env, jobject obj, jboolean check_for_repost);
155 void ReloadIgnoringCache(JNIEnv* env, jobject obj, jboolean check_for_repost);
156 void CancelPendingReload(JNIEnv* env, jobject obj);
157 void ContinuePendingReload(JNIEnv* env, jobject obj);
158 void ClearHistory(JNIEnv* env, jobject obj);
159 void EvaluateJavaScript(JNIEnv* env,
163 jboolean start_renderer);
164 int GetNativeImeAdapter(JNIEnv* env, jobject obj);
165 void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
166 void ScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj);
167 void UndoScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj);
169 jint GetBackgroundColor(JNIEnv* env, jobject obj);
170 void SetBackgroundColor(JNIEnv* env, jobject obj, jint color);
171 void OnShow(JNIEnv* env, jobject obj);
172 void OnHide(JNIEnv* env, jobject obj);
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 */);
181 void AddJavascriptInterface(JNIEnv* env,
185 jclass safe_annotation_clazz,
186 jobject retained_object_set);
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 UpdateVSyncParameters(JNIEnv* env, jobject obj, jlong timebase_micros,
197 jlong interval_micros);
198 void OnVSync(JNIEnv* env, jobject /* obj */, jlong frame_time_micros);
199 jboolean OnAnimate(JNIEnv* env, jobject /* obj */, jlong frame_time_micros);
200 jboolean PopulateBitmapFromCompositor(JNIEnv* env,
203 void WasResized(JNIEnv* env, jobject obj);
204 jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj);
205 void ExitFullscreen(JNIEnv* env, jobject obj);
206 void UpdateTopControlsState(JNIEnv* env,
211 void ShowImeIfNeeded(JNIEnv* env, jobject obj);
213 void ShowInterstitialPage(JNIEnv* env,
217 jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj);
219 void AttachExternalVideoSurface(JNIEnv* env,
223 void DetachExternalVideoSurface(JNIEnv* env, jobject obj, jint player_id);
224 void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
225 void SendActionAfterDoubleTapUma(JNIEnv* env,
230 void SendSingleTapUma(JNIEnv* env, jobject obj, jint type, jint count);
232 void ExtractSmartClipData(JNIEnv* env,
238 // --------------------------------------------------------------------------
239 // Public methods that call to Java via JNI
240 // --------------------------------------------------------------------------
242 void OnSmartClipDataExtracted(const base::string16& result);
244 // Creates a popup menu with |items|.
245 // |multiple| defines if it should support multi-select.
246 // If not |multiple|, |selected_item| sets the initially selected item.
247 // Otherwise, item's "checked" flag selects it.
248 void ShowSelectPopupMenu(const std::vector<MenuItem>& items,
254 // All sizes and offsets are in CSS pixels as cached by the renderer.
255 void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
256 float page_scale_factor,
257 const gfx::Vector2dF& page_scale_factor_limits,
258 const gfx::SizeF& content_size,
259 const gfx::SizeF& viewport_size,
260 const gfx::Vector2dF& controls_offset,
261 const gfx::Vector2dF& content_offset,
262 float overdraw_bottom_height);
264 void UpdateImeAdapter(int native_ime_adapter, int text_input_type,
265 const std::string& text,
266 int selection_start, int selection_end,
267 int composition_start, int composition_end,
268 bool show_ime_if_needed, bool require_ack);
269 void SetTitle(const base::string16& title);
270 void OnBackgroundColorChanged(SkColor color);
273 void ConfirmTouchEvent(InputEventAckState ack_result);
274 void OnGestureEventAck(const blink::WebGestureEvent& event,
275 InputEventAckState ack_result);
276 bool FilterInputEvent(const blink::WebInputEvent& event);
277 void OnSelectionChanged(const std::string& text);
278 void OnSelectionBoundsChanged(
279 const ViewHostMsg_SelectionBounds_Params& params);
281 void StartContentIntent(const GURL& content_url);
283 // Shows the disambiguation popup
284 // |target_rect| --> window coordinates which |zoomed_bitmap| represents
285 // |zoomed_bitmap| --> magnified image of potential touch targets
286 void ShowDisambiguationPopup(
287 const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
289 // Creates a java-side touch event, used for injecting touch event for
290 // testing/benchmarking purposes
291 base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer();
293 // Notifies the java object about the external surface, requesting for one if
295 void NotifyExternalSurface(
296 int player_id, bool is_request, const gfx::RectF& rect);
298 base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
300 // Returns the context that the ContentViewCore was created with, it would
301 // typically be an Activity context for an on screen view.
302 base::android::ScopedJavaLocalRef<jobject> GetContext();
304 // Returns True if the given media should be blocked to load.
305 bool ShouldBlockMediaRequest(const GURL& url);
307 void DidStopFlinging();
309 // --------------------------------------------------------------------------
310 // Methods called from native code
311 // --------------------------------------------------------------------------
313 gfx::Size GetPhysicalBackingSize() const;
314 gfx::Size GetViewportSizeDip() const;
315 gfx::Size GetViewportSizeOffsetDip() const;
316 float GetOverdrawBottomHeightDip() const;
318 void AttachLayer(scoped_refptr<cc::Layer> layer);
319 void RemoveLayer(scoped_refptr<cc::Layer> layer);
320 void AddBeginFrameSubscriber();
321 void RemoveBeginFrameSubscriber();
322 void SetNeedsAnimate();
325 class ContentViewUserData;
327 friend class ContentViewUserData;
328 virtual ~ContentViewCoreImpl();
330 // NotificationObserver implementation.
331 virtual void Observe(int type,
332 const NotificationSource& source,
333 const NotificationDetails& details) OVERRIDE;
335 // WebContentsObserver implementation.
336 virtual void RenderViewReady() OVERRIDE;
337 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
339 // TouchDispositionGestureFilterClient implementation.
340 virtual void ForwardGestureEvent(
341 const blink::WebGestureEvent& event) OVERRIDE;
343 // --------------------------------------------------------------------------
344 // Other private methods and data
345 // --------------------------------------------------------------------------
347 void InitWebContents();
349 RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
351 float GetTouchPaddingDip();
353 blink::WebGestureEvent MakeGestureEvent(
354 blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
356 void SendBeginFrame(base::TimeTicks frame_time);
358 gfx::Size GetViewportSizePix() const;
359 gfx::Size GetViewportSizeOffsetPix() const;
361 void DeleteScaledSnapshotTexture();
363 void SendGestureEvent(const blink::WebGestureEvent& event);
364 void SendSyntheticGestureEvent(const blink::WebGestureEvent& event);
366 // Update focus state of the RenderWidgetHostView.
367 void SetFocusInternal(bool focused);
369 // Send device_orientation_ to renderer.
370 void SendOrientationChangeEventInternal();
372 // A weak reference to the Java ContentViewCore object.
373 JavaObjectWeakGlobalRef java_ref_;
375 NotificationRegistrar notification_registrar_;
377 // Reference to the current WebContents used to determine how and what to
378 // display in the ContentViewCore.
379 WebContentsImpl* web_contents_;
381 // A compositor layer containing any layer that should be shown.
382 scoped_refptr<cc::Layer> root_layer_;
384 // Device scale factor.
387 // Variables used to keep track of frame timestamps and deadlines.
388 base::TimeDelta vsync_interval_;
389 base::TimeDelta expected_browser_composite_time_;
391 // The Android view that can be used to add and remove decoration layers
392 // like AutofillPopup.
393 ui::ViewAndroid* view_android_;
395 // The owning window that has a hold of main application activity.
396 ui::WindowAndroid* window_android_;
398 // The cache of device's current orientation set from Java side, this value
399 // will be sent to Renderer once it is ready.
400 int device_orientation_;
402 bool geolocation_needs_pause_;
404 // Handles gesture dispatch as the generating touch events are ack'ed.
405 TouchDispositionGestureFilter touch_disposition_gesture_filter_;
406 bool handling_touch_event_;
407 blink::WebTouchEvent pending_touch_event_;
408 GestureEventPacket pending_gesture_packet_;
410 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
413 bool RegisterContentViewCore(JNIEnv* env);
415 } // namespace content
417 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_