Upstream version 5.34.104.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_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/input/touch_disposition_gesture_filter.h"
18 #include "content/browser/renderer_host/render_widget_host_view_android.h"
19 #include "content/browser/web_contents/web_contents_impl.h"
20 #include "content/public/browser/android/content_view_core.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "content/public/browser/web_contents_observer.h"
24 #include "third_party/WebKit/public/web/WebInputEvent.h"
25 #include "ui/gfx/rect.h"
26 #include "ui/gfx/rect_f.h"
27 #include "url/gurl.h"
28
29 namespace ui {
30 class ViewAndroid;
31 class WindowAndroid;
32 }
33
34 namespace content {
35 class RenderWidgetHostViewAndroid;
36 struct MenuItem;
37
38 // TODO(jrg): this is a shell.  Upstream the rest.
39 class ContentViewCoreImpl : public ContentViewCore,
40                             public NotificationObserver,
41                             public TouchDispositionGestureFilterClient,
42                             public WebContentsObserver {
43  public:
44   static ContentViewCoreImpl* FromWebContents(WebContents* web_contents);
45   ContentViewCoreImpl(JNIEnv* env,
46                       jobject obj,
47                       WebContents* web_contents,
48                       ui::ViewAndroid* view_android,
49                       ui::WindowAndroid* window_android);
50
51   // ContentViewCore implementation.
52   virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() OVERRIDE;
53   virtual WebContents* GetWebContents() const OVERRIDE;
54   virtual ui::ViewAndroid* GetViewAndroid() const OVERRIDE;
55   virtual ui::WindowAndroid* GetWindowAndroid() const OVERRIDE;
56   virtual scoped_refptr<cc::Layer> GetLayer() const OVERRIDE;
57   virtual void LoadUrl(NavigationController::LoadURLParams& params) OVERRIDE;
58   virtual jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj) OVERRIDE;
59   virtual void ShowPastePopup(int x, int y) OVERRIDE;
60   virtual void GetScaledContentBitmap(
61       float scale,
62       gfx::Size* out_size,
63       const base::Callback<void(bool, const SkBitmap&)>& result_callback)
64       OVERRIDE;
65   virtual float GetDpiScale() const OVERRIDE;
66   virtual void RequestContentClipping(const gfx::Rect& clipping,
67                                       const gfx::Size& content_size) OVERRIDE;
68   virtual void PauseVideo() OVERRIDE;
69   virtual void PauseOrResumeGeolocation(bool should_pause) OVERRIDE;
70
71   // --------------------------------------------------------------------------
72   // Methods called from Java via JNI
73   // --------------------------------------------------------------------------
74
75   base::android::ScopedJavaLocalRef<jobject> GetWebContentsAndroid(JNIEnv* env,
76                                                                    jobject obj);
77
78   void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
79
80   // Notifies the ContentViewCore that items were selected in the currently
81   // showing select popup.
82   void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices);
83
84   void LoadUrl(
85       JNIEnv* env, jobject obj,
86       jstring url,
87       jint load_url_type,
88       jint transition_type,
89       jint ua_override_option,
90       jstring extra_headers,
91       jbyteArray post_data,
92       jstring base_url_for_data_url,
93       jstring virtual_url_for_data_url,
94       jboolean can_load_local_resources);
95   base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const;
96   base::android::ScopedJavaLocalRef<jstring> GetTitle(
97       JNIEnv* env, jobject obj) const;
98   jboolean IsIncognito(JNIEnv* env, jobject obj);
99   void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
100   void OnTouchEventHandlingBegin(JNIEnv* env,
101                                  jobject obj,
102                                  jobject motion_event);
103   void OnTouchEventHandlingEnd(JNIEnv* env, jobject obj);
104   jboolean SendMouseMoveEvent(JNIEnv* env,
105                               jobject obj,
106                               jlong time_ms,
107                               jfloat x,
108                               jfloat y);
109   jboolean SendMouseWheelEvent(JNIEnv* env,
110                                jobject obj,
111                                jlong time_ms,
112                                jfloat x,
113                                jfloat y,
114                                jfloat vertical_axis);
115   void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms,
116                    jfloat x, jfloat y, jfloat hintx, jfloat hinty);
117   void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
118   void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
119                 jfloat x, jfloat y, jfloat dx, jfloat dy);
120   void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
121                   jfloat x, jfloat y, jfloat vx, jfloat vy);
122   void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
123   void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
124                  jfloat x, jfloat y,
125                  jboolean disambiguation_popup_tap);
126   void SingleTapUnconfirmed(JNIEnv* env, jobject obj, jlong time_ms,
127                             jfloat x, jfloat y);
128   void ShowPress(JNIEnv* env, jobject obj, jlong time_ms,
129                  jfloat x, jfloat y);
130   void TapCancel(JNIEnv* env, jobject obj, jlong time_ms,
131                  jfloat x, jfloat y);
132   void TapDown(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                  jboolean disambiguation_popup_tap);
139   void LongTap(JNIEnv* env, jobject obj, jlong time_ms,
140                jfloat x, jfloat y,
141                jboolean disambiguation_popup_tap);
142   void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
143   void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
144   void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
145                jfloat x, jfloat y, jfloat delta);
146   void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
147                                 jfloat x1, jfloat y1,
148                                 jfloat x2, jfloat y2);
149   void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
150
151   void LoadIfNecessary(JNIEnv* env, jobject obj);
152   void RequestRestoreLoad(JNIEnv* env, jobject obj);
153   void StopLoading(JNIEnv* env, jobject obj);
154   void Reload(JNIEnv* env, jobject obj, jboolean check_for_repost);
155   void ReloadIgnoringCache(JNIEnv* env, jobject obj, jboolean check_for_repost);
156   void CancelPendingReload(JNIEnv* env, jobject obj);
157   void ContinuePendingReload(JNIEnv* env, jobject obj);
158   void ClearHistory(JNIEnv* env, jobject obj);
159   void EvaluateJavaScript(JNIEnv* env,
160                           jobject obj,
161                           jstring script,
162                           jobject callback,
163                           jboolean start_renderer);
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   void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
225   void SendActionAfterDoubleTapUma(JNIEnv* env,
226                                    jobject obj,
227                                    jint type,
228                                    jboolean has_delay,
229                                    jint count);
230   void SendSingleTapUma(JNIEnv* env, jobject obj, jint type, jint count);
231
232   void ExtractSmartClipData(JNIEnv* env,
233                             jobject obj,
234                             jint x,
235                             jint y,
236                             jint width,
237                             jint height);
238   // --------------------------------------------------------------------------
239   // Public methods that call to Java via JNI
240   // --------------------------------------------------------------------------
241
242   void OnSmartClipDataExtracted(const base::string16& result);
243
244   // Creates a popup menu with |items|.
245   // |multiple| defines if it should support multi-select.
246   // If not |multiple|, |selected_item| sets the initially selected item.
247   // Otherwise, item's "checked" flag selects it.
248   void ShowSelectPopupMenu(const std::vector<MenuItem>& items,
249                            int selected_item,
250                            bool multiple);
251
252   void OnTabCrashed();
253
254   // All sizes and offsets are in CSS pixels as cached by the renderer.
255   void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
256                        float page_scale_factor,
257                        const gfx::Vector2dF& page_scale_factor_limits,
258                        const gfx::SizeF& content_size,
259                        const gfx::SizeF& viewport_size,
260                        const gfx::Vector2dF& controls_offset,
261                        const gfx::Vector2dF& content_offset,
262                        float overdraw_bottom_height);
263
264   void UpdateImeAdapter(int native_ime_adapter, int text_input_type,
265                         const std::string& text,
266                         int selection_start, int selection_end,
267                         int composition_start, int composition_end,
268                         bool show_ime_if_needed, bool require_ack);
269   void SetTitle(const base::string16& title);
270   void OnBackgroundColorChanged(SkColor color);
271
272   bool HasFocus();
273   void ConfirmTouchEvent(InputEventAckState ack_result);
274   void OnGestureEventAck(const blink::WebGestureEvent& event,
275                          InputEventAckState ack_result);
276   bool FilterInputEvent(const blink::WebInputEvent& event);
277   void OnSelectionChanged(const std::string& text);
278   void OnSelectionBoundsChanged(
279       const ViewHostMsg_SelectionBounds_Params& params);
280
281   void StartContentIntent(const GURL& content_url);
282
283   // Shows the disambiguation popup
284   // |target_rect|   --> window coordinates which |zoomed_bitmap| represents
285   // |zoomed_bitmap| --> magnified image of potential touch targets
286   void ShowDisambiguationPopup(
287       const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
288
289   // Creates a java-side touch event, used for injecting touch event for
290   // testing/benchmarking purposes
291   base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer();
292
293   // Notifies the java object about the external surface, requesting for one if
294   // necessary.
295   void NotifyExternalSurface(
296       int player_id, bool is_request, const gfx::RectF& rect);
297
298   base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
299
300   // Returns the context that the ContentViewCore was created with, it would
301   // typically be an Activity context for an on screen view.
302   base::android::ScopedJavaLocalRef<jobject> GetContext();
303
304   // Returns True if the given media should be blocked to load.
305   bool ShouldBlockMediaRequest(const GURL& url);
306
307   void DidStopFlinging();
308
309   // --------------------------------------------------------------------------
310   // Methods called from native code
311   // --------------------------------------------------------------------------
312
313   gfx::Size GetPhysicalBackingSize() const;
314   gfx::Size GetViewportSizeDip() const;
315   gfx::Size GetViewportSizeOffsetDip() const;
316   float GetOverdrawBottomHeightDip() const;
317
318   void AttachLayer(scoped_refptr<cc::Layer> layer);
319   void RemoveLayer(scoped_refptr<cc::Layer> layer);
320   void AddBeginFrameSubscriber();
321   void RemoveBeginFrameSubscriber();
322   void SetNeedsAnimate();
323
324  private:
325   class ContentViewUserData;
326
327   friend class ContentViewUserData;
328   virtual ~ContentViewCoreImpl();
329
330   // NotificationObserver implementation.
331   virtual void Observe(int type,
332                        const NotificationSource& source,
333                        const NotificationDetails& details) OVERRIDE;
334
335   // WebContentsObserver implementation.
336   virtual void RenderViewReady() OVERRIDE;
337   virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
338
339   // TouchDispositionGestureFilterClient implementation.
340   virtual void ForwardGestureEvent(
341       const blink::WebGestureEvent& event) OVERRIDE;
342
343   // --------------------------------------------------------------------------
344   // Other private methods and data
345   // --------------------------------------------------------------------------
346
347   void InitWebContents();
348
349   RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
350
351   float GetTouchPaddingDip();
352
353   blink::WebGestureEvent MakeGestureEvent(
354       blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
355
356   void SendBeginFrame(base::TimeTicks frame_time);
357
358   gfx::Size GetViewportSizePix() const;
359   gfx::Size GetViewportSizeOffsetPix() const;
360
361   void DeleteScaledSnapshotTexture();
362
363   void SendGestureEvent(const blink::WebGestureEvent& event);
364   void SendSyntheticGestureEvent(const blink::WebGestureEvent& event);
365
366   // Update focus state of the RenderWidgetHostView.
367   void SetFocusInternal(bool focused);
368
369   // Send device_orientation_ to renderer.
370   void SendOrientationChangeEventInternal();
371
372   // A weak reference to the Java ContentViewCore object.
373   JavaObjectWeakGlobalRef java_ref_;
374
375   NotificationRegistrar notification_registrar_;
376
377   // Reference to the current WebContents used to determine how and what to
378   // display in the ContentViewCore.
379   WebContentsImpl* web_contents_;
380
381   // A compositor layer containing any layer that should be shown.
382   scoped_refptr<cc::Layer> root_layer_;
383
384   // Device scale factor.
385   float dpi_scale_;
386
387   // Variables used to keep track of frame timestamps and deadlines.
388   base::TimeDelta vsync_interval_;
389   base::TimeDelta expected_browser_composite_time_;
390
391   // The Android view that can be used to add and remove decoration layers
392   // like AutofillPopup.
393   ui::ViewAndroid* view_android_;
394
395   // The owning window that has a hold of main application activity.
396   ui::WindowAndroid* window_android_;
397
398   // The cache of device's current orientation set from Java side, this value
399   // will be sent to Renderer once it is ready.
400   int device_orientation_;
401
402   bool geolocation_needs_pause_;
403
404   // Handles gesture dispatch as the generating touch events are ack'ed.
405   TouchDispositionGestureFilter touch_disposition_gesture_filter_;
406   bool handling_touch_event_;
407   blink::WebTouchEvent pending_touch_event_;
408   GestureEventPacket pending_gesture_packet_;
409
410   DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
411 };
412
413 bool RegisterContentViewCore(JNIEnv* env);
414
415 }  // namespace content
416
417 #endif  // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_