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