3bb12913975c29ed0cc4252d502437080f3359df
[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 JavaBridgeDispatcherHostManager;
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       jobject bitmap_config,
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   base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const;
98   jboolean IsIncognito(JNIEnv* env, jobject obj);
99   void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
100   jboolean OnTouchEvent(JNIEnv* env,
101                         jobject obj,
102                         jobject motion_event,
103                         jlong time_ms,
104                         jint android_action,
105                         jint pointer_count,
106                         jint history_size,
107                         jint action_index,
108                         jfloat pos_x_0,
109                         jfloat pos_y_0,
110                         jfloat pos_x_1,
111                         jfloat pos_y_1,
112                         jint pointer_id_0,
113                         jint pointer_id_1,
114                         jfloat touch_major_0,
115                         jfloat touch_major_1,
116                         jfloat raw_pos_x,
117                         jfloat raw_pos_y);
118   jboolean SendMouseMoveEvent(JNIEnv* env,
119                               jobject obj,
120                               jlong time_ms,
121                               jfloat x,
122                               jfloat y);
123   jboolean SendMouseWheelEvent(JNIEnv* env,
124                                jobject obj,
125                                jlong time_ms,
126                                jfloat x,
127                                jfloat y,
128                                jfloat vertical_axis);
129   void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms,
130                    jfloat x, jfloat y, jfloat hintx, jfloat hinty);
131   void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
132   void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
133                 jfloat x, jfloat y, jfloat dx, jfloat dy);
134   void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
135                   jfloat x, jfloat y, jfloat vx, jfloat vy);
136   void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
137   void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
138                  jfloat x, jfloat y);
139   void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
140                  jfloat x, jfloat y) ;
141   void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
142                  jfloat x, jfloat y);
143   void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
144   void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
145   void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
146                jfloat x, jfloat y, jfloat delta);
147   void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
148                                 jfloat x1, jfloat y1,
149                                 jfloat x2, jfloat y2);
150   void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
151
152   void ResetGestureDetection(JNIEnv* env, jobject obj);
153   void SetDoubleTapSupportEnabled(JNIEnv* env, jobject obj, jboolean enabled);
154   void SetMultiTouchZoomSupportEnabled(JNIEnv* env,
155                                        jobject obj,
156                                        jboolean enabled);
157
158   void LoadIfNecessary(JNIEnv* env, jobject obj);
159   void RequestRestoreLoad(JNIEnv* env, jobject obj);
160   void Reload(JNIEnv* env, jobject obj, jboolean check_for_repost);
161   void ReloadIgnoringCache(JNIEnv* env, jobject obj, jboolean check_for_repost);
162   void CancelPendingReload(JNIEnv* env, jobject obj);
163   void ContinuePendingReload(JNIEnv* env, jobject obj);
164   void AddStyleSheetByURL(JNIEnv* env, jobject obj, jstring url);
165   void ClearHistory(JNIEnv* env, jobject obj);
166   void EvaluateJavaScript(JNIEnv* env,
167                           jobject obj,
168                           jstring script,
169                           jobject callback,
170                           jboolean start_renderer);
171   long GetNativeImeAdapter(JNIEnv* env, jobject obj);
172   void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
173   void ScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj);
174   void SelectWordAroundCaret(JNIEnv* env, jobject obj);
175
176   jint GetBackgroundColor(JNIEnv* env, jobject obj);
177   void SetBackgroundColor(JNIEnv* env, jobject obj, jint color);
178   void OnShow(JNIEnv* env, jobject obj);
179   void OnHide(JNIEnv* env, jobject obj);
180   void ClearSslPreferences(JNIEnv* env, jobject /* obj */);
181   void SetUseDesktopUserAgent(JNIEnv* env,
182                               jobject /* obj */,
183                               jboolean state,
184                               jboolean reload_on_state_change);
185   bool GetUseDesktopUserAgent(JNIEnv* env, jobject /* obj */);
186   void Show();
187   void Hide();
188   void SetAllowJavascriptInterfacesInspection(JNIEnv* env,
189                                               jobject obj,
190                                               jboolean allow);
191   void AddJavascriptInterface(JNIEnv* env,
192                               jobject obj,
193                               jobject object,
194                               jstring name,
195                               jclass safe_annotation_clazz);
196   void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name);
197   int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history);
198   void GetDirectedNavigationHistory(JNIEnv* env,
199                                     jobject obj,
200                                     jobject history,
201                                     jboolean is_forward,
202                                     jint max_entries);
203   base::android::ScopedJavaLocalRef<jstring>
204       GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj);
205   void WasResized(JNIEnv* env, jobject obj);
206   jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj);
207   void ExitFullscreen(JNIEnv* env, jobject obj);
208   void UpdateTopControlsState(JNIEnv* env,
209                               jobject obj,
210                               bool enable_hiding,
211                               bool enable_showing,
212                               bool animate);
213   void ShowImeIfNeeded(JNIEnv* env, jobject obj);
214
215   void ShowInterstitialPage(JNIEnv* env,
216                             jobject obj,
217                             jstring jurl,
218                             jlong delegate);
219   jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj);
220
221   void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
222
223   void ExtractSmartClipData(JNIEnv* env,
224                             jobject obj,
225                             jint x,
226                             jint y,
227                             jint width,
228                             jint height);
229
230   void SetBackgroundOpaque(JNIEnv* env, jobject jobj, jboolean opaque);
231
232   jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj);
233
234   // --------------------------------------------------------------------------
235   // Public methods that call to Java via JNI
236   // --------------------------------------------------------------------------
237
238   void OnSmartClipDataExtracted(const base::string16& result);
239
240   // Creates a popup menu with |items|.
241   // |multiple| defines if it should support multi-select.
242   // If not |multiple|, |selected_item| sets the initially selected item.
243   // Otherwise, item's "checked" flag selects it.
244   void ShowSelectPopupMenu(const gfx::Rect& bounds,
245                            const std::vector<MenuItem>& items,
246                            int selected_item,
247                            bool multiple);
248   // Hides a visible popup menu.
249   void HideSelectPopupMenu();
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(long 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 is_non_ime_change);
266   void SetTitle(const base::string16& title);
267   void OnBackgroundColorChanged(SkColor color);
268
269   bool HasFocus();
270   void OnGestureEventAck(const blink::WebGestureEvent& event,
271                          InputEventAckState ack_result);
272   bool FilterInputEvent(const blink::WebInputEvent& event);
273   void OnSelectionChanged(const std::string& text);
274   void OnSelectionBoundsChanged(
275       const ViewHostMsg_SelectionBounds_Params& params);
276
277   void StartContentIntent(const GURL& content_url);
278
279   // Shows the disambiguation popup
280   // |target_rect|   --> window coordinates which |zoomed_bitmap| represents
281   // |zoomed_bitmap| --> magnified image of potential touch targets
282   void ShowDisambiguationPopup(
283       const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
284
285   // Creates a java-side touch event, used for injecting touch event for
286   // testing/benchmarking purposes
287   base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer();
288
289   base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
290
291   // Returns the context that the ContentViewCore was created with, it would
292   // typically be an Activity context for an on screen view.
293   base::android::ScopedJavaLocalRef<jobject> GetContext();
294
295   // Returns True if the given media should be blocked to load.
296   bool ShouldBlockMediaRequest(const GURL& url);
297
298   void DidStopFlinging();
299
300   // Returns the viewport size after accounting for the viewport offset.
301   gfx::Size GetViewSize() const;
302
303   void SetAccessibilityEnabledInternal(bool enabled);
304
305   // --------------------------------------------------------------------------
306   // Methods called from native code
307   // --------------------------------------------------------------------------
308
309   gfx::Size GetPhysicalBackingSize() const;
310   gfx::Size GetViewportSizeDip() const;
311   gfx::Size GetViewportSizeOffsetDip() const;
312   float GetOverdrawBottomHeightDip() const;
313
314   void AttachLayer(scoped_refptr<cc::Layer> layer);
315   void RemoveLayer(scoped_refptr<cc::Layer> layer);
316
317  private:
318   class ContentViewUserData;
319
320   friend class ContentViewUserData;
321   virtual ~ContentViewCoreImpl();
322
323   // WebContentsObserver implementation.
324   virtual void RenderViewReady() OVERRIDE;
325   virtual void RenderViewHostChanged(RenderViewHost* old_host,
326                                      RenderViewHost* new_host) OVERRIDE;
327   virtual void WebContentsDestroyed() OVERRIDE;
328
329   // --------------------------------------------------------------------------
330   // Other private methods and data
331   // --------------------------------------------------------------------------
332
333   void InitWebContents();
334
335   RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
336
337   blink::WebGestureEvent MakeGestureEvent(
338       blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
339
340   gfx::Size GetViewportSizePix() const;
341   gfx::Size GetViewportSizeOffsetPix() const;
342
343   void DeleteScaledSnapshotTexture();
344
345   bool OnMotionEvent(const ui::MotionEvent& event);
346   void SendGestureEvent(const blink::WebGestureEvent& event);
347
348   // Update focus state of the RenderWidgetHostView.
349   void SetFocusInternal(bool focused);
350
351   // Send device_orientation_ to renderer.
352   void SendOrientationChangeEventInternal();
353
354   float dpi_scale() const { return dpi_scale_; }
355
356   // A weak reference to the Java ContentViewCore object.
357   JavaObjectWeakGlobalRef java_ref_;
358
359   // Reference to the current WebContents used to determine how and what to
360   // display in the ContentViewCore.
361   WebContentsImpl* web_contents_;
362
363   // A compositor layer containing any layer that should be shown.
364   scoped_refptr<cc::Layer> root_layer_;
365
366   // Device scale factor.
367   float dpi_scale_;
368
369   // The Android view that can be used to add and remove decoration layers
370   // like AutofillPopup.
371   ui::ViewAndroid* view_android_;
372
373   // The owning window that has a hold of main application activity.
374   ui::WindowAndroid* window_android_;
375
376   // The cache of device's current orientation set from Java side, this value
377   // will be sent to Renderer once it is ready.
378   int device_orientation_;
379
380   bool accessibility_enabled_;
381
382   // Manages injecting Java objects.
383   scoped_ptr<JavaBridgeDispatcherHostManager>
384       java_bridge_dispatcher_host_manager_;
385
386   DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
387 };
388
389 bool RegisterContentViewCore(JNIEnv* env);
390
391 }  // namespace content
392
393 #endif  // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_