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 GinJavaBridgeDispatcherHost;
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 SkColorType color_type,
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 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,
116 jint android_tool_type_0,
117 jint android_tool_type_1,
118 jint android_button_state,
119 jboolean is_touch_handle_event);
120 jboolean SendMouseMoveEvent(JNIEnv* env,
125 jboolean SendMouseWheelEvent(JNIEnv* env,
130 jfloat vertical_axis);
131 void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms,
132 jfloat x, jfloat y, jfloat hintx, jfloat hinty);
133 void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
134 void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
135 jfloat x, jfloat y, jfloat dx, jfloat dy);
136 void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
137 jfloat x, jfloat y, jfloat vx, jfloat vy);
138 void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
139 void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
141 void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
142 jfloat x, jfloat y) ;
143 void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
145 void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
146 void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
147 void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
148 jfloat x, jfloat y, jfloat delta);
149 void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
150 jfloat x1, jfloat y1,
151 jfloat x2, jfloat y2);
152 void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
153 void HideTextHandles(JNIEnv* env, jobject obj);
155 void ResetGestureDetection(JNIEnv* env, jobject obj);
156 void SetDoubleTapSupportEnabled(JNIEnv* env, jobject obj, jboolean enabled);
157 void SetMultiTouchZoomSupportEnabled(JNIEnv* env,
161 void ClearHistory(JNIEnv* env, jobject obj);
162 void PostMessageToFrame(JNIEnv* env, jobject obj, jstring frame_id,
163 jstring message, jstring source_origin, jstring target_origin);
164 long GetNativeImeAdapter(JNIEnv* env, jobject obj);
165 void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
167 jint GetBackgroundColor(JNIEnv* env, jobject obj);
168 void SetBackgroundColor(JNIEnv* env, jobject obj, jint color);
169 void ClearSslPreferences(JNIEnv* env, jobject /* obj */);
170 void SetUseDesktopUserAgent(JNIEnv* env,
173 jboolean reload_on_state_change);
174 bool GetUseDesktopUserAgent(JNIEnv* env, jobject /* obj */);
175 void SetAllowJavascriptInterfacesInspection(JNIEnv* env,
178 void AddJavascriptInterface(JNIEnv* env,
182 jclass safe_annotation_clazz);
183 void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name);
184 int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history);
185 void GetDirectedNavigationHistory(JNIEnv* env,
190 base::android::ScopedJavaLocalRef<jstring>
191 GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj);
192 void WasResized(JNIEnv* env, jobject obj);
194 void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
196 void ExtractSmartClipData(JNIEnv* env,
203 void SetBackgroundOpaque(JNIEnv* env, jobject jobj, jboolean opaque);
205 jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj);
207 // --------------------------------------------------------------------------
208 // Public methods that call to Java via JNI
209 // --------------------------------------------------------------------------
211 void OnSmartClipDataExtracted(const base::string16& text,
212 const base::string16& html,
213 const gfx::Rect& clip_rect);
215 // Creates a popup menu with |items|.
216 // |multiple| defines if it should support multi-select.
217 // If not |multiple|, |selected_item| sets the initially selected item.
218 // Otherwise, item's "checked" flag selects it.
219 void ShowSelectPopupMenu(const gfx::Rect& bounds,
220 const std::vector<MenuItem>& items,
223 // Hides a visible popup menu.
224 void HideSelectPopupMenu();
226 // All sizes and offsets are in CSS pixels as cached by the renderer.
227 void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
228 float page_scale_factor,
229 const gfx::Vector2dF& page_scale_factor_limits,
230 const gfx::SizeF& content_size,
231 const gfx::SizeF& viewport_size,
232 const gfx::Vector2dF& controls_offset,
233 const gfx::Vector2dF& content_offset,
234 float overdraw_bottom_height);
236 void UpdateImeAdapter(long native_ime_adapter,
238 int text_input_flags,
239 const std::string& text,
242 int composition_start,
244 bool show_ime_if_needed,
245 bool is_non_ime_change);
246 void SetTitle(const base::string16& title);
247 void OnBackgroundColorChanged(SkColor color);
250 void OnGestureEventAck(const blink::WebGestureEvent& event,
251 InputEventAckState ack_result);
252 bool FilterInputEvent(const blink::WebInputEvent& event);
253 void OnSelectionChanged(const std::string& text);
254 void OnSelectionEvent(SelectionEventType event,
255 const gfx::PointF& anchor_position);
256 scoped_ptr<TouchHandleDrawable> CreatePopupTouchHandleDrawable();
258 void StartContentIntent(const GURL& content_url);
260 // Shows the disambiguation popup
261 // |target_rect| --> window coordinates which |zoomed_bitmap| represents
262 // |zoomed_bitmap| --> magnified image of potential touch targets
263 void ShowDisambiguationPopup(
264 const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
266 // Creates a java-side touch event, used for injecting touch event for
267 // testing/benchmarking purposes
268 base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer();
270 base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
272 // Returns the context that the ContentViewCore was created with, it would
273 // typically be an Activity context for an on screen view.
274 base::android::ScopedJavaLocalRef<jobject> GetContext();
276 // Returns True if the given media should be blocked to load.
277 bool ShouldBlockMediaRequest(const GURL& url);
279 void DidStopFlinging();
281 // Returns the viewport size after accounting for the viewport offset.
282 gfx::Size GetViewSize() const;
284 void SetAccessibilityEnabledInternal(bool enabled);
286 bool IsFullscreenRequiredForOrientationLock() const;
288 // --------------------------------------------------------------------------
289 // Methods called from native code
290 // --------------------------------------------------------------------------
292 gfx::Size GetPhysicalBackingSize() const;
293 gfx::Size GetViewportSizeDip() const;
294 gfx::Size GetViewportSizeOffsetDip() const;
295 float GetOverdrawBottomHeightDip() const;
297 void AttachLayer(scoped_refptr<cc::Layer> layer);
298 void RemoveLayer(scoped_refptr<cc::Layer> layer);
300 void SelectBetweenCoordinates(const gfx::PointF& start,
301 const gfx::PointF& end);
304 class ContentViewUserData;
306 friend class ContentViewUserData;
307 virtual ~ContentViewCoreImpl();
309 // WebContentsObserver implementation.
310 virtual void RenderViewReady() OVERRIDE;
311 virtual void RenderViewHostChanged(RenderViewHost* old_host,
312 RenderViewHost* new_host) OVERRIDE;
313 virtual void WebContentsDestroyed() OVERRIDE;
315 // --------------------------------------------------------------------------
316 // Other private methods and data
317 // --------------------------------------------------------------------------
319 void InitWebContents();
321 RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
323 blink::WebGestureEvent MakeGestureEvent(
324 blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
326 gfx::Size GetViewportSizePix() const;
327 gfx::Size GetViewportSizeOffsetPix() const;
329 void DeleteScaledSnapshotTexture();
331 bool OnMotionEvent(const ui::MotionEvent& event);
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_