content_view_core_impl.h revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 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.
4
5#ifndef CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_
6#define CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_
7
8#include <vector>
9
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.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 "googleurl/src/gurl.h"
23#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
24#include "ui/gfx/rect.h"
25#include "ui/gfx/rect_f.h"
26
27struct WebMenuItem;
28
29namespace ui {
30class ViewAndroid;
31class WindowAndroid;
32}
33
34namespace content {
35class RenderWidgetHostViewAndroid;
36
37// TODO(jrg): this is a shell.  Upstream the rest.
38class ContentViewCoreImpl : public ContentViewCore,
39                            public NotificationObserver {
40 public:
41  static ContentViewCoreImpl* FromWebContents(WebContents* web_contents);
42  ContentViewCoreImpl(JNIEnv* env,
43                      jobject obj,
44                      bool hardware_accelerated,
45                      WebContents* web_contents,
46                      ui::ViewAndroid* view_android,
47                      ui::WindowAndroid* window_android);
48
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 jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj) OVERRIDE;
57  virtual void ShowPastePopup(int x, int y) OVERRIDE;
58  virtual unsigned int GetScaledContentTexture(
59      float scale,
60      gfx::Size* out_size) OVERRIDE;
61  virtual float GetDpiScale() const OVERRIDE;
62  virtual void RequestContentClipping(const gfx::Rect& clipping,
63                                      const gfx::Size& content_size) OVERRIDE;
64  virtual void AddFrameInfoCallback(
65      const UpdateFrameInfoCallback& callback) OVERRIDE;
66  virtual void RemoveFrameInfoCallback(
67      const UpdateFrameInfoCallback& callback) OVERRIDE;
68
69  // --------------------------------------------------------------------------
70  // Methods called from Java via JNI
71  // --------------------------------------------------------------------------
72
73  void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
74
75  // Notifies the ContentViewCore that items were selected in the currently
76  // showing select popup.
77  void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices);
78
79  void LoadUrl(
80      JNIEnv* env, jobject obj,
81      jstring url,
82      jint load_url_type,
83      jint transition_type,
84      jint ua_override_option,
85      jstring extra_headers,
86      jbyteArray post_data,
87      jstring base_url_for_data_url,
88      jstring virtual_url_for_data_url,
89      jboolean can_load_local_resources);
90  base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const;
91  base::android::ScopedJavaLocalRef<jstring> GetTitle(
92      JNIEnv* env, jobject obj) const;
93  jboolean IsIncognito(JNIEnv* env, jobject obj);
94  jboolean Crashed(JNIEnv* env, jobject obj) const { return tab_crashed_; }
95  void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
96  jboolean SendTouchEvent(JNIEnv* env,
97                          jobject obj,
98                          jlong time_ms,
99                          jint type,
100                          jobjectArray pts);
101  jboolean SendMouseMoveEvent(JNIEnv* env,
102                              jobject obj,
103                              jlong time_ms,
104                              jfloat x,
105                              jfloat y);
106  jboolean SendMouseWheelEvent(JNIEnv* env,
107                               jobject obj,
108                               jlong time_ms,
109                               jfloat x,
110                               jfloat y,
111                               jfloat vertical_axis);
112  void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
113  void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
114  void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
115                jfloat x, jfloat y, jfloat dx, jfloat dy,
116                jboolean last_input_event_for_vsync);
117  void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
118                  jfloat x, jfloat y, jfloat vx, jfloat vy);
119  void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
120  void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
121                 jfloat x, jfloat y,
122                 jboolean disambiguation_popup_tap);
123  void SingleTapUnconfirmed(JNIEnv* env, jobject obj, jlong time_ms,
124                            jfloat x, jfloat y);
125  void ShowPressState(JNIEnv* env, jobject obj, jlong time_ms,
126                      jfloat x, jfloat y);
127  void ShowPressCancel(JNIEnv* env, jobject obj, jlong time_ms,
128                       jfloat x, jfloat y);
129  void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
130                 jfloat x, jfloat y) ;
131  void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
132                 jfloat x, jfloat y,
133                 jboolean disambiguation_popup_tap);
134  void LongTap(JNIEnv* env, jobject obj, jlong time_ms,
135               jfloat x, jfloat y,
136               jboolean disambiguation_popup_tap);
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               jboolean last_input_event_for_vsync);
142  void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
143                                jfloat x1, jfloat y1,
144                                jfloat x2, jfloat y2);
145  void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
146
147  jboolean CanGoBack(JNIEnv* env, jobject obj);
148  jboolean CanGoForward(JNIEnv* env, jobject obj);
149  jboolean CanGoToOffset(JNIEnv* env, jobject obj, jint offset);
150  void GoBack(JNIEnv* env, jobject obj);
151  void GoForward(JNIEnv* env, jobject obj);
152  void GoToOffset(JNIEnv* env, jobject obj, jint offset);
153  void GoToNavigationIndex(JNIEnv* env, jobject obj, jint index);
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  jboolean NeedsReload(JNIEnv* env, jobject obj);
159  void ClearHistory(JNIEnv* env, jobject obj);
160  void EvaluateJavaScript(JNIEnv* env,
161                          jobject obj,
162                          jstring script,
163                          jobject callback);
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);
168
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,
175                              jobject /* obj */,
176                              jboolean state,
177                              jboolean reload_on_state_change);
178  bool GetUseDesktopUserAgent(JNIEnv* env, jobject /* obj */);
179  void Show();
180  void Hide();
181  void AddJavascriptInterface(JNIEnv* env,
182                              jobject obj,
183                              jobject object,
184                              jstring name,
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,
190                                    jobject obj,
191                                    jobject history,
192                                    jboolean is_forward,
193                                    jint max_entries);
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,
201                                        jobject obj,
202                                        jobject jbitmap);
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,
207                              jobject obj,
208                              bool enable_hiding,
209                              bool enable_showing,
210                              bool animate);
211  void ShowImeIfNeeded(JNIEnv* env, jobject obj);
212
213  void ShowInterstitialPage(JNIEnv* env,
214                            jobject obj,
215                            jstring jurl,
216                            jint delegate);
217  jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj);
218
219  void AttachExternalVideoSurface(JNIEnv* env,
220                                  jobject obj,
221                                  jint player_id,
222                                  jobject jsurface);
223  void DetachExternalVideoSurface(JNIEnv* env, jobject obj, jint player_id);
224
225  // --------------------------------------------------------------------------
226  // Public methods that call to Java via JNI
227  // --------------------------------------------------------------------------
228
229  // Creates a popup menu with |items|.
230  // |multiple| defines if it should support multi-select.
231  // If not |multiple|, |selected_item| sets the initially selected item.
232  // Otherwise, item's "checked" flag selects it.
233  void ShowSelectPopupMenu(const std::vector<WebMenuItem>& items,
234                           int selected_item,
235                           bool multiple);
236
237  void OnTabCrashed();
238
239  // All sizes and offsets are in CSS pixels as cached by the renderer.
240  void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
241                       float page_scale_factor,
242                       const gfx::Vector2dF& page_scale_factor_limits,
243                       const gfx::SizeF& content_size,
244                       const gfx::SizeF& viewport_size,
245                       const gfx::Vector2dF& controls_offset,
246                       const gfx::Vector2dF& content_offset,
247                       float overdraw_bottom_height);
248
249  void UpdateImeAdapter(int native_ime_adapter, int text_input_type,
250                        const std::string& text,
251                        int selection_start, int selection_end,
252                        int composition_start, int composition_end,
253                        bool show_ime_if_needed);
254  void ProcessImeBatchStateAck(bool is_begin);
255  void SetTitle(const string16& title);
256  void OnBackgroundColorChanged(SkColor color);
257
258  bool HasFocus();
259  void ConfirmTouchEvent(InputEventAckState ack_result);
260  void HasTouchEventHandlers(bool need_touch_events);
261  void OnSelectionChanged(const std::string& text);
262  void OnSelectionBoundsChanged(
263      const ViewHostMsg_SelectionBounds_Params& params);
264
265  void StartContentIntent(const GURL& content_url);
266
267  // Shows the disambiguation popup
268  // |target_rect|   --> window coordinates which |zoomed_bitmap| represents
269  // |zoomed_bitmap| --> magnified image of potential touch targets
270  void ShowDisambiguationPopup(
271      const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
272
273  // Creates a java-side smooth scroller. Used by
274  // chrome.gpuBenchmarking.smoothScrollBy.
275  base::android::ScopedJavaLocalRef<jobject> CreateSmoothScroller(
276      bool scroll_down, int mouse_event_x, int mouse_event_y);
277
278  // Notifies the java object about the external surface, requesting for one if
279  // necessary.
280  void NotifyExternalSurface(
281      int player_id, bool is_request, const gfx::RectF& rect);
282
283  base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
284
285  // Returns the context that the ContentViewCore was created with, it would
286  // typically be an Activity context for an on screen view.
287  base::android::ScopedJavaLocalRef<jobject> GetContext();
288
289  // --------------------------------------------------------------------------
290  // Methods called from native code
291  // --------------------------------------------------------------------------
292
293  gfx::Size GetPhysicalBackingSize() const;
294  gfx::Size GetViewportSizeDip() const;
295  gfx::Size GetViewportSizeOffsetDip() const;
296  float GetOverdrawBottomHeightDip() const;
297
298  void AttachLayer(scoped_refptr<cc::Layer> layer);
299  void RemoveLayer(scoped_refptr<cc::Layer> layer);
300  void SetVSyncNotificationEnabled(bool enabled);
301  void SetNeedsAnimate();
302
303 private:
304  class ContentViewUserData;
305
306  friend class ContentViewUserData;
307  virtual ~ContentViewCoreImpl();
308
309  // NotificationObserver implementation.
310  virtual void Observe(int type,
311                       const NotificationSource& source,
312                       const NotificationDetails& details) OVERRIDE;
313
314  // --------------------------------------------------------------------------
315  // Other private methods and data
316  // --------------------------------------------------------------------------
317
318  void InitWebContents();
319
320  RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
321
322  float GetTouchPaddingDip();
323
324  WebKit::WebGestureEvent MakeGestureEvent(
325      WebKit::WebInputEvent::Type type, long time_ms, float x, float y) const;
326
327  gfx::Size GetViewportSizePix() const;
328  gfx::Size GetViewportSizeOffsetPix() const;
329
330  void DeleteScaledSnapshotTexture();
331
332  void SendGestureEvent(const WebKit::WebGestureEvent& event);
333
334  // A weak reference to the Java ContentViewCore object.
335  JavaObjectWeakGlobalRef java_ref_;
336
337  NotificationRegistrar notification_registrar_;
338
339  // Reference to the current WebContents used to determine how and what to
340  // display in the ContentViewCore.
341  WebContentsImpl* web_contents_;
342
343  // A compositor layer containing any layer that should be shown.
344  scoped_refptr<cc::Layer> root_layer_;
345
346  // Whether the renderer backing this ContentViewCore has crashed.
347  bool tab_crashed_;
348
349  // Device scale factor.
350  float dpi_scale_;
351
352  // The Android view that can be used to add and remove decoration layers
353  // like AutofillPopup.
354  ui::ViewAndroid* view_android_;
355
356  // The owning window that has a hold of main application activity.
357  ui::WindowAndroid* window_android_;
358
359  std::vector<UpdateFrameInfoCallback> update_frame_info_callbacks_;
360
361  DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
362};
363
364bool RegisterContentViewCore(JNIEnv* env);
365
366}  // namespace content
367
368#endif  // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_
369