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/events/gesture_detection/filtered_gesture_provider.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 ui::GestureProviderClient,
41 public NotificationObserver,
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 void ShowPastePopup(int x, int y) OVERRIDE;
59 virtual void GetScaledContentBitmap(
61 jobject bitmap_config,
62 gfx::Rect src_subrect,
63 const base::Callback<void(bool, const SkBitmap&)>& result_callback)
65 virtual float GetDpiScale() const OVERRIDE;
66 virtual void PauseVideo() 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 OnTouchEvent(JNIEnv* env,
100 jobject motion_event,
112 jfloat touch_major_0,
113 jfloat touch_major_1);
114 jboolean SendMouseMoveEvent(JNIEnv* env,
119 jboolean SendMouseWheelEvent(JNIEnv* env,
124 jfloat vertical_axis);
125 void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms,
126 jfloat x, jfloat y, jfloat hintx, jfloat hinty);
127 void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
128 void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
129 jfloat x, jfloat y, jfloat dx, jfloat dy);
130 void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
131 jfloat x, jfloat y, jfloat vx, jfloat vy);
132 void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
133 void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
135 void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
136 jfloat x, jfloat y) ;
137 void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
139 void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
140 void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
141 void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
142 jfloat x, jfloat y, jfloat delta);
143 void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
144 jfloat x1, jfloat y1,
145 jfloat x2, jfloat y2);
146 void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
148 void ResetGestureDetectors(JNIEnv* env, jobject obj);
149 void IgnoreRemainingTouchEvents(JNIEnv* env, jobject obj);
150 void OnWindowFocusLost(JNIEnv* env, jobject obj);
151 void SetDoubleTapSupportForPageEnabled(JNIEnv* env,
154 void SetDoubleTapSupportEnabled(JNIEnv* env, jobject obj, jboolean enabled);
155 void SetMultiTouchZoomSupportEnabled(JNIEnv* env,
159 void LoadIfNecessary(JNIEnv* env, jobject obj);
160 void RequestRestoreLoad(JNIEnv* env, jobject obj);
161 void StopLoading(JNIEnv* env, jobject obj);
162 void Reload(JNIEnv* env, jobject obj, jboolean check_for_repost);
163 void ReloadIgnoringCache(JNIEnv* env, jobject obj, jboolean check_for_repost);
164 void CancelPendingReload(JNIEnv* env, jobject obj);
165 void ContinuePendingReload(JNIEnv* env, jobject obj);
166 void ClearHistory(JNIEnv* env, jobject obj);
167 void EvaluateJavaScript(JNIEnv* env,
171 jboolean start_renderer);
172 int GetNativeImeAdapter(JNIEnv* env, jobject obj);
173 void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
174 void ScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj);
176 jint GetBackgroundColor(JNIEnv* env, jobject obj);
177 void SetBackgroundColor(JNIEnv* env, jobject obj, jint color);
178 void OnShow(JNIEnv* env, jobject obj);
179 void OnHide(JNIEnv* env, jobject obj);
180 void ClearSslPreferences(JNIEnv* env, jobject /* obj */);
181 void SetUseDesktopUserAgent(JNIEnv* env,
184 jboolean reload_on_state_change);
185 bool GetUseDesktopUserAgent(JNIEnv* env, jobject /* obj */);
188 void SetAllowJavascriptInterfacesInspection(JNIEnv* env,
191 void AddJavascriptInterface(JNIEnv* env,
195 jclass safe_annotation_clazz,
196 jobject retained_object_set);
197 void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name);
198 int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history);
199 void GetDirectedNavigationHistory(JNIEnv* env,
204 base::android::ScopedJavaLocalRef<jstring>
205 GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj);
206 void UpdateVSyncParameters(JNIEnv* env, jobject obj, jlong timebase_micros,
207 jlong interval_micros);
208 void OnVSync(JNIEnv* env, jobject /* obj */, jlong frame_time_micros);
209 jboolean OnAnimate(JNIEnv* env, jobject /* obj */, jlong frame_time_micros);
210 void WasResized(JNIEnv* env, jobject obj);
211 jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj);
212 void ExitFullscreen(JNIEnv* env, jobject obj);
213 void UpdateTopControlsState(JNIEnv* env,
218 void ShowImeIfNeeded(JNIEnv* env, jobject obj);
220 void ShowInterstitialPage(JNIEnv* env,
224 jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj);
226 void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
228 void ExtractSmartClipData(JNIEnv* env,
235 jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj);
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(long 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 OnGestureEventAck(const blink::WebGestureEvent& event,
274 InputEventAckState ack_result);
275 bool FilterInputEvent(const blink::WebInputEvent& event);
276 void OnSelectionChanged(const std::string& text);
277 void OnSelectionBoundsChanged(
278 const ViewHostMsg_SelectionBounds_Params& params);
279 void OnSelectionRootBoundsChanged(const gfx::Rect& bounds);
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 base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
295 // Returns the context that the ContentViewCore was created with, it would
296 // typically be an Activity context for an on screen view.
297 base::android::ScopedJavaLocalRef<jobject> GetContext();
299 // Returns True if the given media should be blocked to load.
300 bool ShouldBlockMediaRequest(const GURL& url);
302 void DidStopFlinging();
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);
315 void AddBeginFrameSubscriber();
316 void RemoveBeginFrameSubscriber();
317 void SetNeedsAnimate();
320 class ContentViewUserData;
322 friend class ContentViewUserData;
323 virtual ~ContentViewCoreImpl();
325 // NotificationObserver implementation.
326 virtual void Observe(int type,
327 const NotificationSource& source,
328 const NotificationDetails& details) OVERRIDE;
330 // WebContentsObserver implementation.
331 virtual void RenderViewReady() OVERRIDE;
332 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
334 // ui::GestureProviderClient implementation.
335 virtual void OnGestureEvent(const ui::GestureEventData& gesture) OVERRIDE;
337 // --------------------------------------------------------------------------
338 // Other private methods and data
339 // --------------------------------------------------------------------------
341 void InitWebContents();
343 RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
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 // Utility method for synthesizing a touch cancel event and dispatching it
364 // through the touch pipeline.
365 void CancelActiveTouchSequenceIfNecessary();
367 float dpi_scale() const { return dpi_scale_; }
369 // A weak reference to the Java ContentViewCore object.
370 JavaObjectWeakGlobalRef java_ref_;
372 NotificationRegistrar notification_registrar_;
374 // Reference to the current WebContents used to determine how and what to
375 // display in the ContentViewCore.
376 WebContentsImpl* web_contents_;
378 // A compositor layer containing any layer that should be shown.
379 scoped_refptr<cc::Layer> root_layer_;
381 // Device scale factor.
384 // Variables used to keep track of frame timestamps and deadlines.
385 base::TimeDelta vsync_interval_;
386 base::TimeDelta expected_browser_composite_time_;
388 // The Android view that can be used to add and remove decoration layers
389 // like AutofillPopup.
390 ui::ViewAndroid* view_android_;
392 // The owning window that has a hold of main application activity.
393 ui::WindowAndroid* window_android_;
395 // Provides gesture synthesis given a stream of touch events (derived from
396 // Android MotionEvent's) and touch event acks.
397 ui::FilteredGestureProvider gesture_provider_;
399 // The cache of device's current orientation set from Java side, this value
400 // will be sent to Renderer once it is ready.
401 int device_orientation_;
403 bool geolocation_needs_pause_;
405 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
408 bool RegisterContentViewCore(JNIEnv* env);
410 } // namespace content
412 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_