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.
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/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"
26#include "url/gurl.h"
27
28namespace ui {
29class ViewAndroid;
30class WindowAndroid;
31}
32
33namespace content {
34class RenderWidgetHostViewAndroid;
35struct MenuItem;
36
37// TODO(jrg): this is a shell.  Upstream the rest.
38class ContentViewCoreImpl : public ContentViewCore,
39                            public NotificationObserver,
40                            public WebContentsObserver {
41 public:
42  static ContentViewCoreImpl* FromWebContents(WebContents* web_contents);
43  ContentViewCoreImpl(JNIEnv* env,
44                      jobject obj,
45                      bool hardware_accelerated,
46                      WebContents* web_contents,
47                      ui::ViewAndroid* view_android,
48                      ui::WindowAndroid* window_android);
49
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(
60      float scale,
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;
66  virtual void PauseOrResumeGeolocation(bool should_pause) OVERRIDE;
67
68  // --------------------------------------------------------------------------
69  // Methods called from Java via JNI
70  // --------------------------------------------------------------------------
71
72  base::android::ScopedJavaLocalRef<jobject> GetWebContentsAndroid(JNIEnv* env,
73                                                                   jobject obj);
74
75  void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
76
77  // Notifies the ContentViewCore that items were selected in the currently
78  // showing select popup.
79  void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices);
80
81  void LoadUrl(
82      JNIEnv* env, jobject obj,
83      jstring url,
84      jint load_url_type,
85      jint transition_type,
86      jint ua_override_option,
87      jstring extra_headers,
88      jbyteArray post_data,
89      jstring base_url_for_data_url,
90      jstring virtual_url_for_data_url,
91      jboolean can_load_local_resources);
92  base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const;
93  base::android::ScopedJavaLocalRef<jstring> GetTitle(
94      JNIEnv* env, jobject obj) const;
95  jboolean IsIncognito(JNIEnv* env, jobject obj);
96  void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
97  jboolean SendTouchEvent(JNIEnv* env,
98                          jobject obj,
99                          jlong time_ms,
100                          jint type,
101                          jobjectArray pts);
102  jboolean SendMouseMoveEvent(JNIEnv* env,
103                              jobject obj,
104                              jlong time_ms,
105                              jfloat x,
106                              jfloat y);
107  jboolean SendMouseWheelEvent(JNIEnv* env,
108                               jobject obj,
109                               jlong time_ms,
110                               jfloat x,
111                               jfloat y,
112                               jfloat vertical_axis);
113  void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
114  void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
115  void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
116                jfloat x, jfloat y, jfloat dx, jfloat dy);
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 TapCancel(JNIEnv* env, jobject obj, jlong time_ms,
128                 jfloat x, jfloat y);
129  void TapDown(JNIEnv* env, jobject obj, jlong time_ms,
130               jfloat x, jfloat y);
131  void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
132                 jfloat x, jfloat y) ;
133  void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
134                 jfloat x, jfloat y,
135                 jboolean disambiguation_popup_tap);
136  void LongTap(JNIEnv* env, jobject obj, jlong time_ms,
137               jfloat x, jfloat y,
138               jboolean disambiguation_popup_tap);
139  void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
140  void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
141  void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
142               jfloat x, jfloat y, jfloat delta);
143  void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
144                                jfloat x1, jfloat y1,
145                                jfloat x2, jfloat y2);
146  void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
147
148  void LoadIfNecessary(JNIEnv* env, jobject obj);
149  void RequestRestoreLoad(JNIEnv* env, jobject obj);
150  void StopLoading(JNIEnv* env, jobject obj);
151  void Reload(JNIEnv* env, jobject obj, jboolean check_for_repost);
152  void ReloadIgnoringCache(JNIEnv* env, jobject obj, jboolean check_for_repost);
153  void CancelPendingReload(JNIEnv* env, jobject obj);
154  void ContinuePendingReload(JNIEnv* env, jobject obj);
155  void ClearHistory(JNIEnv* env, jobject obj);
156  void EvaluateJavaScript(JNIEnv* env,
157                          jobject obj,
158                          jstring script,
159                          jobject callback,
160                          jboolean start_renderer);
161  int GetNativeImeAdapter(JNIEnv* env, jobject obj);
162  void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
163  void ScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj);
164  void UndoScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj);
165
166  jint GetBackgroundColor(JNIEnv* env, jobject obj);
167  void SetBackgroundColor(JNIEnv* env, jobject obj, jint color);
168  void OnShow(JNIEnv* env, jobject obj);
169  void OnHide(JNIEnv* env, jobject obj);
170  void ClearSslPreferences(JNIEnv* env, jobject /* obj */);
171  void SetUseDesktopUserAgent(JNIEnv* env,
172                              jobject /* obj */,
173                              jboolean state,
174                              jboolean reload_on_state_change);
175  bool GetUseDesktopUserAgent(JNIEnv* env, jobject /* obj */);
176  void Show();
177  void Hide();
178  void AddJavascriptInterface(JNIEnv* env,
179                              jobject obj,
180                              jobject object,
181                              jstring name,
182                              jclass safe_annotation_clazz,
183                              jobject retained_object_set);
184  void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name);
185  int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history);
186  void GetDirectedNavigationHistory(JNIEnv* env,
187                                    jobject obj,
188                                    jobject history,
189                                    jboolean is_forward,
190                                    jint max_entries);
191  base::android::ScopedJavaLocalRef<jstring>
192      GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj);
193  void UpdateVSyncParameters(JNIEnv* env, jobject obj, jlong timebase_micros,
194                             jlong interval_micros);
195  void OnVSync(JNIEnv* env, jobject /* obj */, jlong frame_time_micros);
196  jboolean OnAnimate(JNIEnv* env, jobject /* obj */, jlong frame_time_micros);
197  jboolean PopulateBitmapFromCompositor(JNIEnv* env,
198                                        jobject obj,
199                                        jobject jbitmap);
200  void WasResized(JNIEnv* env, jobject obj);
201  jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj);
202  void ExitFullscreen(JNIEnv* env, jobject obj);
203  void UpdateTopControlsState(JNIEnv* env,
204                              jobject obj,
205                              bool enable_hiding,
206                              bool enable_showing,
207                              bool animate);
208  void ShowImeIfNeeded(JNIEnv* env, jobject obj);
209
210  void ShowInterstitialPage(JNIEnv* env,
211                            jobject obj,
212                            jstring jurl,
213                            jint delegate);
214  jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj);
215
216  void AttachExternalVideoSurface(JNIEnv* env,
217                                  jobject obj,
218                                  jint player_id,
219                                  jobject jsurface);
220  void DetachExternalVideoSurface(JNIEnv* env, jobject obj, jint player_id);
221  void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
222  void SendActionAfterDoubleTapUma(JNIEnv* env,
223                                   jobject obj,
224                                   jint type,
225                                   jboolean has_delay,
226                                   jint count);
227  void SendSingleTapUma(JNIEnv* env, jobject obj, jint type, jint count);
228
229  void ExtractSmartClipData(JNIEnv* env,
230                            jobject obj,
231                            jint x,
232                            jint y,
233                            jint width,
234                            jint height);
235  // --------------------------------------------------------------------------
236  // Public methods that call to Java via JNI
237  // --------------------------------------------------------------------------
238
239  void OnSmartClipDataExtracted(const string16& result);
240
241  // Creates a popup menu with |items|.
242  // |multiple| defines if it should support multi-select.
243  // If not |multiple|, |selected_item| sets the initially selected item.
244  // Otherwise, item's "checked" flag selects it.
245  void ShowSelectPopupMenu(const std::vector<MenuItem>& items,
246                           int selected_item,
247                           bool multiple);
248
249  void OnTabCrashed();
250
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);
260
261  void UpdateImeAdapter(int 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 require_ack);
266  void SetTitle(const base::string16& title);
267  void OnBackgroundColorChanged(SkColor color);
268
269  bool HasFocus();
270  void ConfirmTouchEvent(InputEventAckState ack_result);
271  void UnhandledFlingStartEvent();
272  void OnScrollUpdateGestureConsumed();
273  void HasTouchEventHandlers(bool need_touch_events);
274  void OnSelectionChanged(const std::string& text);
275  void OnSelectionBoundsChanged(
276      const ViewHostMsg_SelectionBounds_Params& params);
277
278  void StartContentIntent(const GURL& content_url);
279
280  // Shows the disambiguation popup
281  // |target_rect|   --> window coordinates which |zoomed_bitmap| represents
282  // |zoomed_bitmap| --> magnified image of potential touch targets
283  void ShowDisambiguationPopup(
284      const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
285
286  // Creates a java-side touch event, used for injecting touch event for
287  // testing/benchmarking purposes
288  base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer();
289
290  // Notifies the java object about the external surface, requesting for one if
291  // necessary.
292  void NotifyExternalSurface(
293      int player_id, bool is_request, const gfx::RectF& rect);
294
295  base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
296
297  // Returns the context that the ContentViewCore was created with, it would
298  // typically be an Activity context for an on screen view.
299  base::android::ScopedJavaLocalRef<jobject> GetContext();
300
301  // Returns True if the given media should be blocked to load.
302  bool ShouldBlockMediaRequest(const GURL& url);
303
304  // --------------------------------------------------------------------------
305  // Methods called from native code
306  // --------------------------------------------------------------------------
307
308  gfx::Size GetPhysicalBackingSize() const;
309  gfx::Size GetViewportSizeDip() const;
310  gfx::Size GetViewportSizeOffsetDip() const;
311  float GetOverdrawBottomHeightDip() const;
312
313  void AttachLayer(scoped_refptr<cc::Layer> layer);
314  void RemoveLayer(scoped_refptr<cc::Layer> layer);
315  void AddBeginFrameSubscriber();
316  void RemoveBeginFrameSubscriber();
317  void SetNeedsAnimate();
318
319 private:
320  class ContentViewUserData;
321
322  friend class ContentViewUserData;
323  virtual ~ContentViewCoreImpl();
324
325  // NotificationObserver implementation.
326  virtual void Observe(int type,
327                       const NotificationSource& source,
328                       const NotificationDetails& details) OVERRIDE;
329
330  // WebContentsObserver implementation.
331  virtual void RenderViewReady() OVERRIDE;
332  virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
333
334  // --------------------------------------------------------------------------
335  // Other private methods and data
336  // --------------------------------------------------------------------------
337
338  void InitWebContents();
339
340  RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
341
342  float GetTouchPaddingDip();
343
344  blink::WebGestureEvent MakeGestureEvent(
345      blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
346
347  void SendBeginFrame(base::TimeTicks frame_time);
348
349  gfx::Size GetViewportSizePix() const;
350  gfx::Size GetViewportSizeOffsetPix() const;
351
352  void DeleteScaledSnapshotTexture();
353
354  void SendGestureEvent(const blink::WebGestureEvent& event);
355
356  // Update focus state of the RenderWidgetHostView.
357  void SetFocusInternal(bool focused);
358
359  // Send device_orientation_ to renderer.
360  void SendOrientationChangeEventInternal();
361
362  // A weak reference to the Java ContentViewCore object.
363  JavaObjectWeakGlobalRef java_ref_;
364
365  NotificationRegistrar notification_registrar_;
366
367  // Reference to the current WebContents used to determine how and what to
368  // display in the ContentViewCore.
369  WebContentsImpl* web_contents_;
370
371  // A compositor layer containing any layer that should be shown.
372  scoped_refptr<cc::Layer> root_layer_;
373
374  // Device scale factor.
375  float dpi_scale_;
376
377  // Variables used to keep track of frame timestamps and deadlines.
378  base::TimeDelta vsync_interval_;
379  base::TimeDelta expected_browser_composite_time_;
380
381  // The Android view that can be used to add and remove decoration layers
382  // like AutofillPopup.
383  ui::ViewAndroid* view_android_;
384
385  // The owning window that has a hold of main application activity.
386  ui::WindowAndroid* window_android_;
387
388  // The cache of device's current orientation set from Java side, this value
389  // will be sent to Renderer once it is ready.
390  int device_orientation_;
391
392  bool geolocation_needs_pause_;
393
394  DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
395};
396
397bool RegisterContentViewCore(JNIEnv* env);
398
399}  // namespace content
400
401#endif  // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_
402