- add sources.
[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/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
28 namespace ui {
29 class ViewAndroid;
30 class WindowAndroid;
31 }
32
33 namespace content {
34 class RenderWidgetHostViewAndroid;
35 struct MenuItem;
36
37 // TODO(jrg): this is a shell.  Upstream the rest.
38 class 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
67   // --------------------------------------------------------------------------
68   // Methods called from Java via JNI
69   // --------------------------------------------------------------------------
70
71   void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
72
73   // Notifies the ContentViewCore that items were selected in the currently
74   // showing select popup.
75   void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices);
76
77   void LoadUrl(
78       JNIEnv* env, jobject obj,
79       jstring url,
80       jint load_url_type,
81       jint transition_type,
82       jint ua_override_option,
83       jstring extra_headers,
84       jbyteArray post_data,
85       jstring base_url_for_data_url,
86       jstring virtual_url_for_data_url,
87       jboolean can_load_local_resources);
88   base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const;
89   base::android::ScopedJavaLocalRef<jstring> GetTitle(
90       JNIEnv* env, jobject obj) const;
91   jboolean IsIncognito(JNIEnv* env, jobject obj);
92   jboolean Crashed(JNIEnv* env, jobject obj) const { return tab_crashed_; }
93   void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
94   jboolean SendTouchEvent(JNIEnv* env,
95                           jobject obj,
96                           jlong time_ms,
97                           jint type,
98                           jobjectArray pts);
99   jboolean SendMouseMoveEvent(JNIEnv* env,
100                               jobject obj,
101                               jlong time_ms,
102                               jfloat x,
103                               jfloat y);
104   jboolean SendMouseWheelEvent(JNIEnv* env,
105                                jobject obj,
106                                jlong time_ms,
107                                jfloat x,
108                                jfloat y,
109                                jfloat vertical_axis);
110   void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
111   void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
112   void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
113                 jfloat x, jfloat y, jfloat dx, jfloat dy);
114   void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
115                   jfloat x, jfloat y, jfloat vx, jfloat vy);
116   void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
117   void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
118                  jfloat x, jfloat y,
119                  jboolean disambiguation_popup_tap);
120   void SingleTapUnconfirmed(JNIEnv* env, jobject obj, jlong time_ms,
121                             jfloat x, jfloat y);
122   void ShowPressState(JNIEnv* env, jobject obj, jlong time_ms,
123                       jfloat x, jfloat y);
124   void ShowPressCancel(JNIEnv* env, jobject obj, jlong time_ms,
125                        jfloat x, jfloat y);
126   void TapDown(JNIEnv* env, jobject obj, jlong time_ms,
127                jfloat x, jfloat y);
128   void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
129                  jfloat x, jfloat y) ;
130   void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
131                  jfloat x, jfloat y,
132                  jboolean disambiguation_popup_tap);
133   void LongTap(JNIEnv* env, jobject obj, jlong time_ms,
134                jfloat x, jfloat y,
135                jboolean disambiguation_popup_tap);
136   void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
137   void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
138   void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
139                jfloat x, jfloat y, jfloat delta);
140   void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
141                                 jfloat x1, jfloat y1,
142                                 jfloat x2, jfloat y2);
143   void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
144
145   jboolean CanGoBack(JNIEnv* env, jobject obj);
146   jboolean CanGoForward(JNIEnv* env, jobject obj);
147   jboolean CanGoToOffset(JNIEnv* env, jobject obj, jint offset);
148   void GoBack(JNIEnv* env, jobject obj);
149   void GoForward(JNIEnv* env, jobject obj);
150   void GoToOffset(JNIEnv* env, jobject obj, jint offset);
151   void GoToNavigationIndex(JNIEnv* env, jobject obj, jint index);
152   void LoadIfNecessary(JNIEnv* env, jobject obj);
153   void RequestRestoreLoad(JNIEnv* env, jobject obj);
154   void StopLoading(JNIEnv* env, jobject obj);
155   void Reload(JNIEnv* env, jobject obj);
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
226   // --------------------------------------------------------------------------
227   // Public methods that call to Java via JNI
228   // --------------------------------------------------------------------------
229
230   // Creates a popup menu with |items|.
231   // |multiple| defines if it should support multi-select.
232   // If not |multiple|, |selected_item| sets the initially selected item.
233   // Otherwise, item's "checked" flag selects it.
234   void ShowSelectPopupMenu(const std::vector<MenuItem>& items,
235                            int selected_item,
236                            bool multiple);
237
238   void OnTabCrashed();
239
240   // All sizes and offsets are in CSS pixels as cached by the renderer.
241   void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
242                        float page_scale_factor,
243                        const gfx::Vector2dF& page_scale_factor_limits,
244                        const gfx::SizeF& content_size,
245                        const gfx::SizeF& viewport_size,
246                        const gfx::Vector2dF& controls_offset,
247                        const gfx::Vector2dF& content_offset,
248                        float overdraw_bottom_height);
249
250   void UpdateImeAdapter(int native_ime_adapter, int text_input_type,
251                         const std::string& text,
252                         int selection_start, int selection_end,
253                         int composition_start, int composition_end,
254                         bool show_ime_if_needed, bool require_ack);
255   void SetTitle(const string16& title);
256   void OnBackgroundColorChanged(SkColor color);
257
258   bool HasFocus();
259   void ConfirmTouchEvent(InputEventAckState ack_result);
260   void UnhandledFlingStartEvent();
261   void OnScrollUpdateGestureConsumed();
262   void HasTouchEventHandlers(bool need_touch_events);
263   void OnSelectionChanged(const std::string& text);
264   void OnSelectionBoundsChanged(
265       const ViewHostMsg_SelectionBounds_Params& params);
266
267   void StartContentIntent(const GURL& content_url);
268
269   // Shows the disambiguation popup
270   // |target_rect|   --> window coordinates which |zoomed_bitmap| represents
271   // |zoomed_bitmap| --> magnified image of potential touch targets
272   void ShowDisambiguationPopup(
273       const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
274
275   // Creates a java-side touch gesture, e.g. used by
276   // chrome.gpuBenchmarking.smoothScrollBy.
277   base::android::ScopedJavaLocalRef<jobject> CreateOnePointTouchGesture(
278       int start_x, int start_y, int delta_x, int delta_y);
279
280   // Creates a java-side touch gesture with two pointers, e.g. used by
281   // chrome.gpuBenchmarking.pinchBy.
282   base::android::ScopedJavaLocalRef<jobject> CreateTwoPointTouchGesture(
283       int start_x0, int start_y0, int delta_x0, int delta_y0,
284       int start_x1, int start_y1, int delta_x1, int delta_y1);
285
286   // Notifies the java object about the external surface, requesting for one if
287   // necessary.
288   void NotifyExternalSurface(
289       int player_id, bool is_request, const gfx::RectF& rect);
290
291   base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
292
293   // Returns the context that the ContentViewCore was created with, it would
294   // typically be an Activity context for an on screen view.
295   base::android::ScopedJavaLocalRef<jobject> GetContext();
296
297   // --------------------------------------------------------------------------
298   // Methods called from native code
299   // --------------------------------------------------------------------------
300
301   gfx::Size GetPhysicalBackingSize() const;
302   gfx::Size GetViewportSizeDip() const;
303   gfx::Size GetViewportSizeOffsetDip() const;
304   float GetOverdrawBottomHeightDip() const;
305
306   void AttachLayer(scoped_refptr<cc::Layer> layer);
307   void RemoveLayer(scoped_refptr<cc::Layer> layer);
308   void AddBeginFrameSubscriber();
309   void RemoveBeginFrameSubscriber();
310   void SetNeedsAnimate();
311
312  private:
313   class ContentViewUserData;
314
315   friend class ContentViewUserData;
316   virtual ~ContentViewCoreImpl();
317
318   // NotificationObserver implementation.
319   virtual void Observe(int type,
320                        const NotificationSource& source,
321                        const NotificationDetails& details) OVERRIDE;
322
323   // WebContentsObserver implementation.
324   virtual void RenderViewReady() OVERRIDE;
325
326   // --------------------------------------------------------------------------
327   // Other private methods and data
328   // --------------------------------------------------------------------------
329
330   void InitWebContents();
331
332   RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
333
334   float GetTouchPaddingDip();
335
336   WebKit::WebGestureEvent MakeGestureEvent(
337       WebKit::WebInputEvent::Type type, long time_ms, float x, float y) const;
338
339   void SendBeginFrame(base::TimeTicks frame_time);
340
341   gfx::Size GetViewportSizePix() const;
342   gfx::Size GetViewportSizeOffsetPix() const;
343
344   void DeleteScaledSnapshotTexture();
345
346   void SendGestureEvent(const WebKit::WebGestureEvent& event);
347
348   // Checks if there there is a corresponding renderer process and updates
349   // |tab_crashed_| accordingly.
350   void UpdateTabCrashedFlag();
351
352   // Update focus state of the RenderWidgetHostView.
353   void SetFocusInternal(bool focused);
354
355   // Send device_orientation_ to renderer.
356   void SendOrientationChangeEventInternal();
357
358   // A weak reference to the Java ContentViewCore object.
359   JavaObjectWeakGlobalRef java_ref_;
360
361   NotificationRegistrar notification_registrar_;
362
363   // Reference to the current WebContents used to determine how and what to
364   // display in the ContentViewCore.
365   WebContentsImpl* web_contents_;
366
367   // A compositor layer containing any layer that should be shown.
368   scoped_refptr<cc::Layer> root_layer_;
369
370   // Whether the renderer backing this ContentViewCore has crashed.
371   bool tab_crashed_;
372
373   // Device scale factor.
374   float dpi_scale_;
375
376   // Variables used to keep track of frame timestamps and deadlines.
377   base::TimeDelta vsync_interval_;
378   base::TimeDelta expected_browser_composite_time_;
379
380   // The Android view that can be used to add and remove decoration layers
381   // like AutofillPopup.
382   ui::ViewAndroid* view_android_;
383
384   // The owning window that has a hold of main application activity.
385   ui::WindowAndroid* window_android_;
386
387   // The cache of device's current orientation set from Java side, this value
388   // will be sent to Renderer once it is ready.
389   int device_orientation_;
390
391   DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
392 };
393
394 bool RegisterContentViewCore(JNIEnv* env);
395
396 }  // namespace content
397
398 #endif  // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_