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/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 bool hardware_accelerated,
46 WebContents* web_contents,
47 ui::ViewAndroid* view_android,
48 ui::WindowAndroid* window_android);
50 // ContentViewCore implementation.
51 virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() OVERRIDE;
52 virtual WebContents* GetWebContents() const OVERRIDE;
53 virtual ui::ViewAndroid* GetViewAndroid() const OVERRIDE;
54 virtual ui::WindowAndroid* GetWindowAndroid() const OVERRIDE;
55 virtual scoped_refptr<cc::Layer> GetLayer() const OVERRIDE;
56 virtual void LoadUrl(NavigationController::LoadURLParams& params) OVERRIDE;
57 virtual jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj) OVERRIDE;
58 virtual void ShowPastePopup(int x, int y) OVERRIDE;
59 virtual unsigned int GetScaledContentTexture(
61 gfx::Size* out_size) OVERRIDE;
62 virtual float GetDpiScale() const OVERRIDE;
63 virtual void RequestContentClipping(const gfx::Rect& clipping,
64 const gfx::Size& content_size) OVERRIDE;
65 virtual void PauseVideo() OVERRIDE;
67 // --------------------------------------------------------------------------
68 // Methods called from Java via JNI
69 // --------------------------------------------------------------------------
71 void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
73 // Notifies the ContentViewCore that items were selected in the currently
74 // showing select popup.
75 void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices);
78 JNIEnv* env, jobject obj,
82 jint ua_override_option,
83 jstring extra_headers,
85 jstring base_url_for_data_url,
86 jstring virtual_url_for_data_url,
87 jboolean can_load_local_resources);
88 base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const;
89 base::android::ScopedJavaLocalRef<jstring> GetTitle(
90 JNIEnv* env, jobject obj) const;
91 jboolean IsIncognito(JNIEnv* env, jobject obj);
92 jboolean Crashed(JNIEnv* env, jobject obj) const { return tab_crashed_; }
93 void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
94 jboolean SendTouchEvent(JNIEnv* env,
99 jboolean SendMouseMoveEvent(JNIEnv* env,
104 jboolean SendMouseWheelEvent(JNIEnv* env,
109 jfloat vertical_axis);
110 void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
111 void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
112 void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
113 jfloat x, jfloat y, jfloat dx, jfloat dy);
114 void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
115 jfloat x, jfloat y, jfloat vx, jfloat vy);
116 void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
117 void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
119 jboolean disambiguation_popup_tap);
120 void SingleTapUnconfirmed(JNIEnv* env, jobject obj, jlong time_ms,
122 void ShowPressState(JNIEnv* env, jobject obj, jlong time_ms,
124 void ShowPressCancel(JNIEnv* env, jobject obj, jlong time_ms,
126 void TapDown(JNIEnv* env, jobject obj, jlong time_ms,
128 void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
129 jfloat x, jfloat y) ;
130 void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
132 jboolean disambiguation_popup_tap);
133 void LongTap(JNIEnv* env, jobject obj, jlong time_ms,
135 jboolean disambiguation_popup_tap);
136 void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
137 void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
138 void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
139 jfloat x, jfloat y, jfloat delta);
140 void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
141 jfloat x1, jfloat y1,
142 jfloat x2, jfloat y2);
143 void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
145 jboolean CanGoBack(JNIEnv* env, jobject obj);
146 jboolean CanGoForward(JNIEnv* env, jobject obj);
147 jboolean CanGoToOffset(JNIEnv* env, jobject obj, jint offset);
148 void GoBack(JNIEnv* env, jobject obj);
149 void GoForward(JNIEnv* env, jobject obj);
150 void GoToOffset(JNIEnv* env, jobject obj, jint offset);
151 void GoToNavigationIndex(JNIEnv* env, jobject obj, jint index);
152 void LoadIfNecessary(JNIEnv* env, jobject obj);
153 void RequestRestoreLoad(JNIEnv* env, jobject obj);
154 void StopLoading(JNIEnv* env, jobject obj);
155 void Reload(JNIEnv* env, jobject obj);
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);
226 // --------------------------------------------------------------------------
227 // Public methods that call to Java via JNI
228 // --------------------------------------------------------------------------
230 // Creates a popup menu with |items|.
231 // |multiple| defines if it should support multi-select.
232 // If not |multiple|, |selected_item| sets the initially selected item.
233 // Otherwise, item's "checked" flag selects it.
234 void ShowSelectPopupMenu(const std::vector<MenuItem>& items,
240 // All sizes and offsets are in CSS pixels as cached by the renderer.
241 void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
242 float page_scale_factor,
243 const gfx::Vector2dF& page_scale_factor_limits,
244 const gfx::SizeF& content_size,
245 const gfx::SizeF& viewport_size,
246 const gfx::Vector2dF& controls_offset,
247 const gfx::Vector2dF& content_offset,
248 float overdraw_bottom_height);
250 void UpdateImeAdapter(int native_ime_adapter, int text_input_type,
251 const std::string& text,
252 int selection_start, int selection_end,
253 int composition_start, int composition_end,
254 bool show_ime_if_needed, bool require_ack);
255 void SetTitle(const string16& title);
256 void OnBackgroundColorChanged(SkColor color);
259 void ConfirmTouchEvent(InputEventAckState ack_result);
260 void UnhandledFlingStartEvent();
261 void OnScrollUpdateGestureConsumed();
262 void HasTouchEventHandlers(bool need_touch_events);
263 void OnSelectionChanged(const std::string& text);
264 void OnSelectionBoundsChanged(
265 const ViewHostMsg_SelectionBounds_Params& params);
267 void StartContentIntent(const GURL& content_url);
269 // Shows the disambiguation popup
270 // |target_rect| --> window coordinates which |zoomed_bitmap| represents
271 // |zoomed_bitmap| --> magnified image of potential touch targets
272 void ShowDisambiguationPopup(
273 const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
275 // Creates a java-side touch gesture, e.g. used by
276 // chrome.gpuBenchmarking.smoothScrollBy.
277 base::android::ScopedJavaLocalRef<jobject> CreateOnePointTouchGesture(
278 int start_x, int start_y, int delta_x, int delta_y);
280 // Creates a java-side touch gesture with two pointers, e.g. used by
281 // chrome.gpuBenchmarking.pinchBy.
282 base::android::ScopedJavaLocalRef<jobject> CreateTwoPointTouchGesture(
283 int start_x0, int start_y0, int delta_x0, int delta_y0,
284 int start_x1, int start_y1, int delta_x1, int delta_y1);
286 // Notifies the java object about the external surface, requesting for one if
288 void NotifyExternalSurface(
289 int player_id, bool is_request, const gfx::RectF& rect);
291 base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
293 // Returns the context that the ContentViewCore was created with, it would
294 // typically be an Activity context for an on screen view.
295 base::android::ScopedJavaLocalRef<jobject> GetContext();
297 // --------------------------------------------------------------------------
298 // Methods called from native code
299 // --------------------------------------------------------------------------
301 gfx::Size GetPhysicalBackingSize() const;
302 gfx::Size GetViewportSizeDip() const;
303 gfx::Size GetViewportSizeOffsetDip() const;
304 float GetOverdrawBottomHeightDip() const;
306 void AttachLayer(scoped_refptr<cc::Layer> layer);
307 void RemoveLayer(scoped_refptr<cc::Layer> layer);
308 void AddBeginFrameSubscriber();
309 void RemoveBeginFrameSubscriber();
310 void SetNeedsAnimate();
313 class ContentViewUserData;
315 friend class ContentViewUserData;
316 virtual ~ContentViewCoreImpl();
318 // NotificationObserver implementation.
319 virtual void Observe(int type,
320 const NotificationSource& source,
321 const NotificationDetails& details) OVERRIDE;
323 // WebContentsObserver implementation.
324 virtual void RenderViewReady() OVERRIDE;
326 // --------------------------------------------------------------------------
327 // Other private methods and data
328 // --------------------------------------------------------------------------
330 void InitWebContents();
332 RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
334 float GetTouchPaddingDip();
336 WebKit::WebGestureEvent MakeGestureEvent(
337 WebKit::WebInputEvent::Type type, long time_ms, float x, float y) const;
339 void SendBeginFrame(base::TimeTicks frame_time);
341 gfx::Size GetViewportSizePix() const;
342 gfx::Size GetViewportSizeOffsetPix() const;
344 void DeleteScaledSnapshotTexture();
346 void SendGestureEvent(const WebKit::WebGestureEvent& event);
348 // Checks if there there is a corresponding renderer process and updates
349 // |tab_crashed_| accordingly.
350 void UpdateTabCrashedFlag();
352 // Update focus state of the RenderWidgetHostView.
353 void SetFocusInternal(bool focused);
355 // Send device_orientation_ to renderer.
356 void SendOrientationChangeEventInternal();
358 // A weak reference to the Java ContentViewCore object.
359 JavaObjectWeakGlobalRef java_ref_;
361 NotificationRegistrar notification_registrar_;
363 // Reference to the current WebContents used to determine how and what to
364 // display in the ContentViewCore.
365 WebContentsImpl* web_contents_;
367 // A compositor layer containing any layer that should be shown.
368 scoped_refptr<cc::Layer> root_layer_;
370 // Whether the renderer backing this ContentViewCore has crashed.
373 // Device scale factor.
376 // Variables used to keep track of frame timestamps and deadlines.
377 base::TimeDelta vsync_interval_;
378 base::TimeDelta expected_browser_composite_time_;
380 // The Android view that can be used to add and remove decoration layers
381 // like AutofillPopup.
382 ui::ViewAndroid* view_android_;
384 // The owning window that has a hold of main application activity.
385 ui::WindowAndroid* window_android_;
387 // The cache of device's current orientation set from Java side, this value
388 // will be sent to Renderer once it is ready.
389 int device_orientation_;
391 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
394 bool RegisterContentViewCore(JNIEnv* env);
396 } // namespace content
398 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_