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_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"
23#include "url/gurl.h"
24
25namespace ui {
26class ViewAndroid;
27class WindowAndroid;
28}
29
30namespace content {
31
32class GinJavaBridgeDispatcherHost;
33class RenderFrameHost;
34class RenderWidgetHostViewAndroid;
35struct MenuItem;
36
37// TODO(jrg): this is a shell.  Upstream the rest.
38class ContentViewCoreImpl : public ContentViewCore,
39                            public WebContentsObserver {
40 public:
41  static ContentViewCoreImpl* FromWebContents(WebContents* web_contents);
42  ContentViewCoreImpl(JNIEnv* env,
43                      jobject obj,
44                      WebContents* web_contents,
45                      ui::ViewAndroid* view_android,
46                      ui::WindowAndroid* window_android,
47                      jobject java_bridge_retained_object_set);
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 ShowPastePopup(int x, int y) OVERRIDE;
56  virtual void GetScaledContentBitmap(
57      float scale,
58      SkColorType color_type,
59      gfx::Rect src_subrect,
60      const base::Callback<void(bool, const SkBitmap&)>& result_callback)
61      OVERRIDE;
62  virtual float GetDpiScale() const OVERRIDE;
63  virtual void PauseOrResumeGeolocation(bool should_pause) OVERRIDE;
64  virtual void RequestTextSurroundingSelection(
65      int max_length,
66      const base::Callback<void(const base::string16& content,
67                                int start_offset,
68                                int end_offset)>& callback) OVERRIDE;
69
70  // --------------------------------------------------------------------------
71  // Methods called from Java via JNI
72  // --------------------------------------------------------------------------
73
74  base::android::ScopedJavaLocalRef<jobject> GetWebContentsAndroid(JNIEnv* env,
75                                                                   jobject obj);
76
77  void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
78
79  // Notifies the ContentViewCore that items were selected in the currently
80  // showing select popup.
81  void SelectPopupMenuItems(JNIEnv* env, jobject obj,
82                            jlong selectPopupSourceFrame,
83                            jintArray indices);
84
85  void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
86  jboolean OnTouchEvent(JNIEnv* env,
87                        jobject obj,
88                        jobject motion_event,
89                        jlong time_ms,
90                        jint android_action,
91                        jint pointer_count,
92                        jint history_size,
93                        jint action_index,
94                        jfloat pos_x_0,
95                        jfloat pos_y_0,
96                        jfloat pos_x_1,
97                        jfloat pos_y_1,
98                        jint pointer_id_0,
99                        jint pointer_id_1,
100                        jfloat touch_major_0,
101                        jfloat touch_major_1,
102                        jfloat touch_minor_0,
103                        jfloat touch_minor_1,
104                        jfloat orientation_0,
105                        jfloat orientation_1,
106                        jfloat raw_pos_x,
107                        jfloat raw_pos_y,
108                        jint android_tool_type_0,
109                        jint android_tool_type_1,
110                        jint android_button_state,
111                        jint android_meta_state,
112                        jboolean is_touch_handle_event);
113  jboolean SendMouseMoveEvent(JNIEnv* env,
114                              jobject obj,
115                              jlong time_ms,
116                              jfloat x,
117                              jfloat y);
118  jboolean SendMouseWheelEvent(JNIEnv* env,
119                               jobject obj,
120                               jlong time_ms,
121                               jfloat x,
122                               jfloat y,
123                               jfloat vertical_axis);
124  void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms,
125                   jfloat x, jfloat y, jfloat hintx, jfloat hinty);
126  void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
127  void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
128                jfloat x, jfloat y, jfloat dx, jfloat dy);
129  void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
130                  jfloat x, jfloat y, jfloat vx, jfloat vy);
131  void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
132  void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
133                 jfloat x, jfloat y);
134  void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
135                 jfloat x, jfloat y) ;
136  void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
137                 jfloat x, jfloat y);
138  void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
139  void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
140  void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
141               jfloat x, jfloat y, jfloat delta);
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  void DismissTextHandles(JNIEnv* env, jobject obj);
147  void SetTextHandlesTemporarilyHidden(JNIEnv* env,
148                                       jobject obj,
149                                       jboolean hidden);
150
151  void ResetGestureDetection(JNIEnv* env, jobject obj);
152  void SetDoubleTapSupportEnabled(JNIEnv* env, jobject obj, jboolean enabled);
153  void SetMultiTouchZoomSupportEnabled(JNIEnv* env,
154                                       jobject obj,
155                                       jboolean enabled);
156
157  void PostMessageToFrame(JNIEnv* env, jobject obj, jstring frame_id,
158      jstring message, jstring source_origin, jstring target_origin);
159  long GetNativeImeAdapter(JNIEnv* env, jobject obj);
160  void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
161
162  jint GetBackgroundColor(JNIEnv* env, jobject obj);
163  void SetBackgroundColor(JNIEnv* env, jobject obj, jint color);
164  void SetAllowJavascriptInterfacesInspection(JNIEnv* env,
165                                              jobject obj,
166                                              jboolean allow);
167  void AddJavascriptInterface(JNIEnv* env,
168                              jobject obj,
169                              jobject object,
170                              jstring name,
171                              jclass safe_annotation_clazz);
172  void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name);
173  void WasResized(JNIEnv* env, jobject obj);
174
175  void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
176
177  void ExtractSmartClipData(JNIEnv* env,
178                            jobject obj,
179                            jint x,
180                            jint y,
181                            jint width,
182                            jint height);
183
184  void SetBackgroundOpaque(JNIEnv* env, jobject jobj, jboolean opaque);
185
186  jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj);
187
188  // --------------------------------------------------------------------------
189  // Public methods that call to Java via JNI
190  // --------------------------------------------------------------------------
191
192  void OnSmartClipDataExtracted(const base::string16& text,
193                                const base::string16& html,
194                                const gfx::Rect& clip_rect);
195
196  // Creates a popup menu with |items|.
197  // |multiple| defines if it should support multi-select.
198  // If not |multiple|, |selected_item| sets the initially selected item.
199  // Otherwise, item's "checked" flag selects it.
200  void ShowSelectPopupMenu(RenderFrameHost* frame,
201                           const gfx::Rect& bounds,
202                           const std::vector<MenuItem>& items,
203                           int selected_item,
204                           bool multiple);
205  // Hides a visible popup menu.
206  void HideSelectPopupMenu();
207
208  // All sizes and offsets are in CSS pixels as cached by the renderer.
209  void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
210                       float page_scale_factor,
211                       const gfx::Vector2dF& page_scale_factor_limits,
212                       const gfx::SizeF& content_size,
213                       const gfx::SizeF& viewport_size,
214                       const gfx::Vector2dF& controls_offset,
215                       const gfx::Vector2dF& content_offset);
216
217  void UpdateImeAdapter(long native_ime_adapter,
218                        int text_input_type,
219                        int text_input_flags,
220                        const std::string& text,
221                        int selection_start,
222                        int selection_end,
223                        int composition_start,
224                        int composition_end,
225                        bool show_ime_if_needed,
226                        bool is_non_ime_change);
227  void SetTitle(const base::string16& title);
228  void OnBackgroundColorChanged(SkColor color);
229
230  bool HasFocus();
231  void OnGestureEventAck(const blink::WebGestureEvent& event,
232                         InputEventAckState ack_result);
233  bool FilterInputEvent(const blink::WebInputEvent& event);
234  void OnSelectionChanged(const std::string& text);
235  void OnSelectionEvent(SelectionEventType event,
236                        const gfx::PointF& anchor_position);
237  scoped_ptr<TouchHandleDrawable> CreatePopupTouchHandleDrawable();
238
239  void StartContentIntent(const GURL& content_url);
240
241  // Shows the disambiguation popup
242  // |rect_pixels|   --> window coordinates which |zoomed_bitmap| represents
243  // |zoomed_bitmap| --> magnified image of potential touch targets
244  void ShowDisambiguationPopup(
245      const gfx::Rect& rect_pixels, const SkBitmap& zoomed_bitmap);
246
247  // Creates a java-side touch event, used for injecting touch event for
248  // testing/benchmarking purposes
249  base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer();
250
251  base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
252
253  // Returns the context that the ContentViewCore was created with, it would
254  // typically be an Activity context for an on screen view.
255  base::android::ScopedJavaLocalRef<jobject> GetContext();
256
257  // Returns True if the given media should be blocked to load.
258  bool ShouldBlockMediaRequest(const GURL& url);
259
260  void DidStopFlinging();
261
262  // Returns the viewport size after accounting for the viewport offset.
263  gfx::Size GetViewSize() const;
264
265  void SetAccessibilityEnabledInternal(bool enabled);
266
267  bool IsFullscreenRequiredForOrientationLock() const;
268
269  // --------------------------------------------------------------------------
270  // Methods called from native code
271  // --------------------------------------------------------------------------
272
273  gfx::Size GetPhysicalBackingSize() const;
274  gfx::Size GetViewportSizeDip() const;
275  float GetTopControlsLayoutHeightDip() const;
276
277  void AttachLayer(scoped_refptr<cc::Layer> layer);
278  void RemoveLayer(scoped_refptr<cc::Layer> layer);
279
280  void SelectBetweenCoordinates(const gfx::PointF& start,
281                                const gfx::PointF& end);
282
283 private:
284  class ContentViewUserData;
285
286  friend class ContentViewUserData;
287  virtual ~ContentViewCoreImpl();
288
289  // WebContentsObserver implementation.
290  virtual void RenderViewReady() OVERRIDE;
291  virtual void RenderViewHostChanged(RenderViewHost* old_host,
292                                     RenderViewHost* new_host) OVERRIDE;
293  virtual void WebContentsDestroyed() OVERRIDE;
294
295  // --------------------------------------------------------------------------
296  // Other private methods and data
297  // --------------------------------------------------------------------------
298
299  void InitWebContents();
300
301  RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
302
303  blink::WebGestureEvent MakeGestureEvent(
304      blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
305
306  gfx::Size GetViewportSizePix() const;
307  int GetTopControlsLayoutHeightPix() const;
308
309  void SendGestureEvent(const blink::WebGestureEvent& event);
310
311  // Update focus state of the RenderWidgetHostView.
312  void SetFocusInternal(bool focused);
313
314  // Send device_orientation_ to renderer.
315  void SendOrientationChangeEventInternal();
316
317  float dpi_scale() const { return dpi_scale_; }
318
319  // A weak reference to the Java ContentViewCore object.
320  JavaObjectWeakGlobalRef java_ref_;
321
322  // Reference to the current WebContents used to determine how and what to
323  // display in the ContentViewCore.
324  WebContentsImpl* web_contents_;
325
326  // A compositor layer containing any layer that should be shown.
327  scoped_refptr<cc::Layer> root_layer_;
328
329  // Device scale factor.
330  float dpi_scale_;
331
332  // The Android view that can be used to add and remove decoration layers
333  // like AutofillPopup.
334  ui::ViewAndroid* view_android_;
335
336  // The owning window that has a hold of main application activity.
337  ui::WindowAndroid* window_android_;
338
339  // The cache of device's current orientation set from Java side, this value
340  // will be sent to Renderer once it is ready.
341  int device_orientation_;
342
343  bool accessibility_enabled_;
344
345  // Manages injecting Java objects.
346  scoped_refptr<GinJavaBridgeDispatcherHost> java_bridge_dispatcher_host_;
347
348  DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
349};
350
351bool RegisterContentViewCore(JNIEnv* env);
352
353}  // namespace content
354
355#endif  // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_
356