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 AddStyleSheetByURL(JNIEnv* env, jobject obj, jstring url);
165 void ClearHistory(JNIEnv* env, jobject obj);
166 void EvaluateJavaScript(JNIEnv* env,
170 jboolean start_renderer);
171 long GetNativeImeAdapter(JNIEnv* env, jobject obj);
172 void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
173 void ScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj);
174 void SelectWordAroundCaret(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 void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name);
197 int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history);
198 void GetDirectedNavigationHistory(JNIEnv* env,
203 base::android::ScopedJavaLocalRef<jstring>
204 GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj);
205 void WasResized(JNIEnv* env, jobject obj);
206 jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj);
207 void ExitFullscreen(JNIEnv* env, jobject obj);
208 void UpdateTopControlsState(JNIEnv* env,
213 void ShowImeIfNeeded(JNIEnv* env, jobject obj);
215 void ShowInterstitialPage(JNIEnv* env,
219 jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj);
221 void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
223 void ExtractSmartClipData(JNIEnv* env,
230 void SetBackgroundOpaque(JNIEnv* env, jobject jobj, jboolean opaque);
232 jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj);
234 // --------------------------------------------------------------------------
235 // Public methods that call to Java via JNI
236 // --------------------------------------------------------------------------
238 void OnSmartClipDataExtracted(const base::string16& result);
240 // Creates a popup menu with |items|.
241 // |multiple| defines if it should support multi-select.
242 // If not |multiple|, |selected_item| sets the initially selected item.
243 // Otherwise, item's "checked" flag selects it.
244 void ShowSelectPopupMenu(const gfx::Rect& bounds,
245 const std::vector<MenuItem>& items,
248 // Hides a visible popup menu.
249 void HideSelectPopupMenu();
251 // All sizes and offsets are in CSS pixels as cached by the renderer.
252 void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
253 float page_scale_factor,
254 const gfx::Vector2dF& page_scale_factor_limits,
255 const gfx::SizeF& content_size,
256 const gfx::SizeF& viewport_size,
257 const gfx::Vector2dF& controls_offset,
258 const gfx::Vector2dF& content_offset,
259 float overdraw_bottom_height);
261 void UpdateImeAdapter(long native_ime_adapter, int text_input_type,
262 const std::string& text,
263 int selection_start, int selection_end,
264 int composition_start, int composition_end,
265 bool show_ime_if_needed, bool is_non_ime_change);
266 void SetTitle(const base::string16& title);
267 void OnBackgroundColorChanged(SkColor color);
270 void OnGestureEventAck(const blink::WebGestureEvent& event,
271 InputEventAckState ack_result);
272 bool FilterInputEvent(const blink::WebInputEvent& event);
273 void OnSelectionChanged(const std::string& text);
274 void OnSelectionBoundsChanged(
275 const ViewHostMsg_SelectionBounds_Params& params);
277 void StartContentIntent(const GURL& content_url);
279 // Shows the disambiguation popup
280 // |target_rect| --> window coordinates which |zoomed_bitmap| represents
281 // |zoomed_bitmap| --> magnified image of potential touch targets
282 void ShowDisambiguationPopup(
283 const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
285 // Creates a java-side touch event, used for injecting touch event for
286 // testing/benchmarking purposes
287 base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer();
289 base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
291 // Returns the context that the ContentViewCore was created with, it would
292 // typically be an Activity context for an on screen view.
293 base::android::ScopedJavaLocalRef<jobject> GetContext();
295 // Returns True if the given media should be blocked to load.
296 bool ShouldBlockMediaRequest(const GURL& url);
298 void DidStopFlinging();
300 // Returns the viewport size after accounting for the viewport offset.
301 gfx::Size GetViewSize() const;
303 void SetAccessibilityEnabledInternal(bool enabled);
305 // --------------------------------------------------------------------------
306 // Methods called from native code
307 // --------------------------------------------------------------------------
309 gfx::Size GetPhysicalBackingSize() const;
310 gfx::Size GetViewportSizeDip() const;
311 gfx::Size GetViewportSizeOffsetDip() const;
312 float GetOverdrawBottomHeightDip() const;
314 void AttachLayer(scoped_refptr<cc::Layer> layer);
315 void RemoveLayer(scoped_refptr<cc::Layer> layer);
318 class ContentViewUserData;
320 friend class ContentViewUserData;
321 virtual ~ContentViewCoreImpl();
323 // WebContentsObserver implementation.
324 virtual void RenderViewReady() OVERRIDE;
325 virtual void RenderViewHostChanged(RenderViewHost* old_host,
326 RenderViewHost* new_host) OVERRIDE;
327 virtual void WebContentsDestroyed() OVERRIDE;
329 // --------------------------------------------------------------------------
330 // Other private methods and data
331 // --------------------------------------------------------------------------
333 void InitWebContents();
335 RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
337 blink::WebGestureEvent MakeGestureEvent(
338 blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
340 gfx::Size GetViewportSizePix() const;
341 gfx::Size GetViewportSizeOffsetPix() const;
343 void DeleteScaledSnapshotTexture();
345 bool OnMotionEvent(const ui::MotionEvent& event);
346 void SendGestureEvent(const blink::WebGestureEvent& event);
348 // Update focus state of the RenderWidgetHostView.
349 void SetFocusInternal(bool focused);
351 // Send device_orientation_ to renderer.
352 void SendOrientationChangeEventInternal();
354 float dpi_scale() const { return dpi_scale_; }
356 // A weak reference to the Java ContentViewCore object.
357 JavaObjectWeakGlobalRef java_ref_;
359 // Reference to the current WebContents used to determine how and what to
360 // display in the ContentViewCore.
361 WebContentsImpl* web_contents_;
363 // A compositor layer containing any layer that should be shown.
364 scoped_refptr<cc::Layer> root_layer_;
366 // Device scale factor.
369 // The Android view that can be used to add and remove decoration layers
370 // like AutofillPopup.
371 ui::ViewAndroid* view_android_;
373 // The owning window that has a hold of main application activity.
374 ui::WindowAndroid* window_android_;
376 // The cache of device's current orientation set from Java side, this value
377 // will be sent to Renderer once it is ready.
378 int device_orientation_;
380 bool accessibility_enabled_;
382 // Manages injecting Java objects.
383 scoped_ptr<JavaBridgeDispatcherHostManager>
384 java_bridge_dispatcher_host_manager_;
386 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
389 bool RegisterContentViewCore(JNIEnv* env);
391 } // namespace content
393 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_