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/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 WebContents* web_contents,
46 ui::ViewAndroid* view_android,
47 ui::WindowAndroid* window_android);
49 // ContentViewCore implementation.
50 virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() OVERRIDE;
51 virtual WebContents* GetWebContents() const OVERRIDE;
52 virtual ui::ViewAndroid* GetViewAndroid() const OVERRIDE;
53 virtual ui::WindowAndroid* GetWindowAndroid() const OVERRIDE;
54 virtual scoped_refptr<cc::Layer> GetLayer() const OVERRIDE;
55 virtual void LoadUrl(NavigationController::LoadURLParams& params) OVERRIDE;
56 virtual void ShowPastePopup(int x, int y) OVERRIDE;
57 virtual void GetScaledContentBitmap(
59 jobject bitmap_config,
60 gfx::Rect src_subrect,
61 const base::Callback<void(bool, const SkBitmap&)>& result_callback)
63 virtual float GetDpiScale() const OVERRIDE;
64 virtual void PauseVideo() OVERRIDE;
65 virtual void PauseOrResumeGeolocation(bool should_pause) OVERRIDE;
67 // --------------------------------------------------------------------------
68 // Methods called from Java via JNI
69 // --------------------------------------------------------------------------
71 base::android::ScopedJavaLocalRef<jobject> GetWebContentsAndroid(JNIEnv* env,
74 void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
76 // Notifies the ContentViewCore that items were selected in the currently
77 // showing select popup.
78 void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices);
81 JNIEnv* env, jobject obj,
85 jstring j_referrer_url,
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 jboolean IsIncognito(JNIEnv* env, jobject obj);
95 void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
96 jboolean OnTouchEvent(JNIEnv* env,
110 jfloat touch_major_0,
111 jfloat touch_major_1);
112 jboolean SendMouseMoveEvent(JNIEnv* env,
117 jboolean SendMouseWheelEvent(JNIEnv* env,
122 jfloat vertical_axis);
123 void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms,
124 jfloat x, jfloat y, jfloat hintx, jfloat hinty);
125 void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
126 void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
127 jfloat x, jfloat y, jfloat dx, jfloat dy);
128 void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
129 jfloat x, jfloat y, jfloat vx, jfloat vy);
130 void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
131 void SingleTap(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 void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
138 void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
139 void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
140 jfloat x, jfloat y, jfloat delta);
141 void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
142 jfloat x1, jfloat y1,
143 jfloat x2, jfloat y2);
144 void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
146 void ResetGestureDetection(JNIEnv* env, jobject obj);
147 void SetDoubleTapSupportEnabled(JNIEnv* env, jobject obj, jboolean enabled);
148 void SetMultiTouchZoomSupportEnabled(JNIEnv* env,
152 void LoadIfNecessary(JNIEnv* env, jobject obj);
153 void RequestRestoreLoad(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 long GetNativeImeAdapter(JNIEnv* env, jobject obj);
165 void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
166 void ScrollFocusedEditableNodeIntoView(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 SetAllowJavascriptInterfacesInspection(JNIEnv* env,
183 void AddJavascriptInterface(JNIEnv* env,
187 jclass safe_annotation_clazz,
188 jobject retained_object_set);
189 void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name);
190 int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history);
191 void GetDirectedNavigationHistory(JNIEnv* env,
196 base::android::ScopedJavaLocalRef<jstring>
197 GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj);
198 void WasResized(JNIEnv* env, jobject obj);
199 jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj);
200 void ExitFullscreen(JNIEnv* env, jobject obj);
201 void UpdateTopControlsState(JNIEnv* env,
206 void ShowImeIfNeeded(JNIEnv* env, jobject obj);
208 void ShowInterstitialPage(JNIEnv* env,
212 jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj);
214 void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
216 void ExtractSmartClipData(JNIEnv* env,
223 jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj);
225 // --------------------------------------------------------------------------
226 // Public methods that call to Java via JNI
227 // --------------------------------------------------------------------------
229 void OnSmartClipDataExtracted(const base::string16& result);
231 // Creates a popup menu with |items|.
232 // |multiple| defines if it should support multi-select.
233 // If not |multiple|, |selected_item| sets the initially selected item.
234 // Otherwise, item's "checked" flag selects it.
235 void ShowSelectPopupMenu(const gfx::Rect& bounds,
236 const std::vector<MenuItem>& items,
239 // Hides a visible popup menu.
240 void HideSelectPopupMenu();
242 // All sizes and offsets are in CSS pixels as cached by the renderer.
243 void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
244 float page_scale_factor,
245 const gfx::Vector2dF& page_scale_factor_limits,
246 const gfx::SizeF& content_size,
247 const gfx::SizeF& viewport_size,
248 const gfx::Vector2dF& controls_offset,
249 const gfx::Vector2dF& content_offset,
250 float overdraw_bottom_height);
252 void UpdateImeAdapter(long native_ime_adapter, int text_input_type,
253 const std::string& text,
254 int selection_start, int selection_end,
255 int composition_start, int composition_end,
256 bool show_ime_if_needed, bool is_non_ime_change);
257 void SetTitle(const base::string16& title);
258 void OnBackgroundColorChanged(SkColor color);
261 void OnGestureEventAck(const blink::WebGestureEvent& event,
262 InputEventAckState ack_result);
263 bool FilterInputEvent(const blink::WebInputEvent& event);
264 void OnSelectionChanged(const std::string& text);
265 void OnSelectionBoundsChanged(
266 const ViewHostMsg_SelectionBounds_Params& params);
268 void StartContentIntent(const GURL& content_url);
270 // Shows the disambiguation popup
271 // |target_rect| --> window coordinates which |zoomed_bitmap| represents
272 // |zoomed_bitmap| --> magnified image of potential touch targets
273 void ShowDisambiguationPopup(
274 const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
276 // Creates a java-side touch event, used for injecting touch event for
277 // testing/benchmarking purposes
278 base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer();
280 base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
282 // Returns the context that the ContentViewCore was created with, it would
283 // typically be an Activity context for an on screen view.
284 base::android::ScopedJavaLocalRef<jobject> GetContext();
286 // Returns True if the given media should be blocked to load.
287 bool ShouldBlockMediaRequest(const GURL& url);
289 void DidStopFlinging();
291 // Returns the viewport size after accounting for the viewport offset.
292 gfx::Size GetViewSize() const;
294 void SetAccessibilityEnabledInternal(bool enabled);
296 // --------------------------------------------------------------------------
297 // Methods called from native code
298 // --------------------------------------------------------------------------
300 gfx::Size GetPhysicalBackingSize() const;
301 gfx::Size GetViewportSizeDip() const;
302 gfx::Size GetViewportSizeOffsetDip() const;
303 float GetOverdrawBottomHeightDip() const;
305 void AttachLayer(scoped_refptr<cc::Layer> layer);
306 void RemoveLayer(scoped_refptr<cc::Layer> layer);
309 class ContentViewUserData;
311 friend class ContentViewUserData;
312 virtual ~ContentViewCoreImpl();
314 // NotificationObserver implementation.
315 virtual void Observe(int type,
316 const NotificationSource& source,
317 const NotificationDetails& details) OVERRIDE;
319 // WebContentsObserver implementation.
320 virtual void RenderViewReady() OVERRIDE;
321 virtual void WebContentsDestroyed() OVERRIDE;
323 // --------------------------------------------------------------------------
324 // Other private methods and data
325 // --------------------------------------------------------------------------
327 void InitWebContents();
329 RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
331 blink::WebGestureEvent MakeGestureEvent(
332 blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
334 gfx::Size GetViewportSizePix() const;
335 gfx::Size GetViewportSizeOffsetPix() const;
337 void DeleteScaledSnapshotTexture();
339 bool OnMotionEvent(const ui::MotionEvent& event);
340 void SendGestureEvent(const blink::WebGestureEvent& event);
342 // Update focus state of the RenderWidgetHostView.
343 void SetFocusInternal(bool focused);
345 // Send device_orientation_ to renderer.
346 void SendOrientationChangeEventInternal();
348 float dpi_scale() const { return dpi_scale_; }
350 // A weak reference to the Java ContentViewCore object.
351 JavaObjectWeakGlobalRef java_ref_;
353 NotificationRegistrar notification_registrar_;
355 // Reference to the current WebContents used to determine how and what to
356 // display in the ContentViewCore.
357 WebContentsImpl* web_contents_;
359 // A compositor layer containing any layer that should be shown.
360 scoped_refptr<cc::Layer> root_layer_;
362 // Device scale factor.
365 // The Android view that can be used to add and remove decoration layers
366 // like AutofillPopup.
367 ui::ViewAndroid* view_android_;
369 // The owning window that has a hold of main application activity.
370 ui::WindowAndroid* window_android_;
372 // The cache of device's current orientation set from Java side, this value
373 // will be sent to Renderer once it is ready.
374 int device_orientation_;
376 bool geolocation_needs_pause_;
378 bool accessibility_enabled_;
380 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
383 bool RegisterContentViewCore(JNIEnv* env);
385 } // namespace content
387 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_