81c78db351697ebe096ad7f1198f51445ea070be
[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/events/gesture_detection/filtered_gesture_provider.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 ui::GestureProviderClient,
41                             public NotificationObserver,
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 void ShowPastePopup(int x, int y) OVERRIDE;
59   virtual void GetScaledContentBitmap(
60       float scale,
61       jobject bitmap_config,
62       gfx::Rect src_subrect,
63       const base::Callback<void(bool, const SkBitmap&)>& result_callback)
64       OVERRIDE;
65   virtual float GetDpiScale() const OVERRIDE;
66   virtual void PauseVideo() OVERRIDE;
67   virtual void PauseOrResumeGeolocation(bool should_pause) OVERRIDE;
68
69   // --------------------------------------------------------------------------
70   // Methods called from Java via JNI
71   // --------------------------------------------------------------------------
72
73   base::android::ScopedJavaLocalRef<jobject> GetWebContentsAndroid(JNIEnv* env,
74                                                                    jobject obj);
75
76   void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
77
78   // Notifies the ContentViewCore that items were selected in the currently
79   // showing select popup.
80   void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices);
81
82   void LoadUrl(
83       JNIEnv* env, jobject obj,
84       jstring url,
85       jint load_url_type,
86       jint transition_type,
87       jint ua_override_option,
88       jstring extra_headers,
89       jbyteArray post_data,
90       jstring base_url_for_data_url,
91       jstring virtual_url_for_data_url,
92       jboolean can_load_local_resources);
93   base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const;
94   base::android::ScopedJavaLocalRef<jstring> GetTitle(
95       JNIEnv* env, jobject obj) const;
96   jboolean IsIncognito(JNIEnv* env, jobject obj);
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   jboolean SendMouseMoveEvent(JNIEnv* env,
115                               jobject obj,
116                               jlong time_ms,
117                               jfloat x,
118                               jfloat y);
119   jboolean SendMouseWheelEvent(JNIEnv* env,
120                                jobject obj,
121                                jlong time_ms,
122                                jfloat x,
123                                jfloat y,
124                                jfloat vertical_axis);
125   void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms,
126                    jfloat x, jfloat y, jfloat hintx, jfloat hinty);
127   void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
128   void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
129                 jfloat x, jfloat y, jfloat dx, jfloat dy);
130   void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
131                   jfloat x, jfloat y, jfloat vx, jfloat vy);
132   void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
133   void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
134                  jfloat x, jfloat y);
135   void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
136                  jfloat x, jfloat y) ;
137   void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
138                  jfloat x, jfloat y);
139   void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
140   void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
141   void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
142                jfloat x, jfloat y, jfloat delta);
143   void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
144                                 jfloat x1, jfloat y1,
145                                 jfloat x2, jfloat y2);
146   void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
147
148   void ResetGestureDetectors(JNIEnv* env, jobject obj);
149   void IgnoreRemainingTouchEvents(JNIEnv* env, jobject obj);
150   void OnWindowFocusLost(JNIEnv* env, jobject obj);
151   void SetDoubleTapSupportForPageEnabled(JNIEnv* env,
152                                          jobject obj,
153                                          jboolean enabled);
154   void SetDoubleTapSupportEnabled(JNIEnv* env, jobject obj, jboolean enabled);
155   void SetMultiTouchZoomSupportEnabled(JNIEnv* env,
156                                        jobject obj,
157                                        jboolean enabled);
158
159   void LoadIfNecessary(JNIEnv* env, jobject obj);
160   void RequestRestoreLoad(JNIEnv* env, jobject obj);
161   void StopLoading(JNIEnv* env, jobject obj);
162   void Reload(JNIEnv* env, jobject obj, jboolean check_for_repost);
163   void ReloadIgnoringCache(JNIEnv* env, jobject obj, jboolean check_for_repost);
164   void CancelPendingReload(JNIEnv* env, jobject obj);
165   void ContinuePendingReload(JNIEnv* env, jobject obj);
166   void ClearHistory(JNIEnv* env, jobject obj);
167   void EvaluateJavaScript(JNIEnv* env,
168                           jobject obj,
169                           jstring script,
170                           jobject callback,
171                           jboolean start_renderer);
172   int GetNativeImeAdapter(JNIEnv* env, jobject obj);
173   void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
174   void ScrollFocusedEditableNodeIntoView(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                               jobject retained_object_set);
197   void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name);
198   int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history);
199   void GetDirectedNavigationHistory(JNIEnv* env,
200                                     jobject obj,
201                                     jobject history,
202                                     jboolean is_forward,
203                                     jint max_entries);
204   base::android::ScopedJavaLocalRef<jstring>
205       GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj);
206   void UpdateVSyncParameters(JNIEnv* env, jobject obj, jlong timebase_micros,
207                              jlong interval_micros);
208   void OnVSync(JNIEnv* env, jobject /* obj */, jlong frame_time_micros);
209   jboolean OnAnimate(JNIEnv* env, jobject /* obj */, jlong frame_time_micros);
210   void WasResized(JNIEnv* env, jobject obj);
211   jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj);
212   void ExitFullscreen(JNIEnv* env, jobject obj);
213   void UpdateTopControlsState(JNIEnv* env,
214                               jobject obj,
215                               bool enable_hiding,
216                               bool enable_showing,
217                               bool animate);
218   void ShowImeIfNeeded(JNIEnv* env, jobject obj);
219
220   void ShowInterstitialPage(JNIEnv* env,
221                             jobject obj,
222                             jstring jurl,
223                             jint delegate);
224   jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj);
225
226   void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
227
228   void ExtractSmartClipData(JNIEnv* env,
229                             jobject obj,
230                             jint x,
231                             jint y,
232                             jint width,
233                             jint height);
234
235   jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj);
236
237   // --------------------------------------------------------------------------
238   // Public methods that call to Java via JNI
239   // --------------------------------------------------------------------------
240
241   void OnSmartClipDataExtracted(const base::string16& result);
242
243   // Creates a popup menu with |items|.
244   // |multiple| defines if it should support multi-select.
245   // If not |multiple|, |selected_item| sets the initially selected item.
246   // Otherwise, item's "checked" flag selects it.
247   void ShowSelectPopupMenu(const std::vector<MenuItem>& items,
248                            int selected_item,
249                            bool multiple);
250
251   void OnTabCrashed();
252
253   // All sizes and offsets are in CSS pixels as cached by the renderer.
254   void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
255                        float page_scale_factor,
256                        const gfx::Vector2dF& page_scale_factor_limits,
257                        const gfx::SizeF& content_size,
258                        const gfx::SizeF& viewport_size,
259                        const gfx::Vector2dF& controls_offset,
260                        const gfx::Vector2dF& content_offset,
261                        float overdraw_bottom_height);
262
263   void UpdateImeAdapter(long native_ime_adapter, int text_input_type,
264                         const std::string& text,
265                         int selection_start, int selection_end,
266                         int composition_start, int composition_end,
267                         bool show_ime_if_needed, bool require_ack);
268   void SetTitle(const base::string16& title);
269   void OnBackgroundColorChanged(SkColor color);
270
271   bool HasFocus();
272   void ConfirmTouchEvent(InputEventAckState ack_result);
273   void OnGestureEventAck(const blink::WebGestureEvent& event,
274                          InputEventAckState ack_result);
275   bool FilterInputEvent(const blink::WebInputEvent& event);
276   void OnSelectionChanged(const std::string& text);
277   void OnSelectionBoundsChanged(
278       const ViewHostMsg_SelectionBounds_Params& params);
279   void OnSelectionRootBoundsChanged(const gfx::Rect& bounds);
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   base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
294
295   // Returns the context that the ContentViewCore was created with, it would
296   // typically be an Activity context for an on screen view.
297   base::android::ScopedJavaLocalRef<jobject> GetContext();
298
299   // Returns True if the given media should be blocked to load.
300   bool ShouldBlockMediaRequest(const GURL& url);
301
302   void DidStopFlinging();
303
304   // --------------------------------------------------------------------------
305   // Methods called from native code
306   // --------------------------------------------------------------------------
307
308   gfx::Size GetPhysicalBackingSize() const;
309   gfx::Size GetViewportSizeDip() const;
310   gfx::Size GetViewportSizeOffsetDip() const;
311   float GetOverdrawBottomHeightDip() const;
312
313   void AttachLayer(scoped_refptr<cc::Layer> layer);
314   void RemoveLayer(scoped_refptr<cc::Layer> layer);
315   void AddBeginFrameSubscriber();
316   void RemoveBeginFrameSubscriber();
317   void SetNeedsAnimate();
318
319  private:
320   class ContentViewUserData;
321
322   friend class ContentViewUserData;
323   virtual ~ContentViewCoreImpl();
324
325   // NotificationObserver implementation.
326   virtual void Observe(int type,
327                        const NotificationSource& source,
328                        const NotificationDetails& details) OVERRIDE;
329
330   // WebContentsObserver implementation.
331   virtual void RenderViewReady() OVERRIDE;
332   virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
333
334   // ui::GestureProviderClient implementation.
335   virtual void OnGestureEvent(const ui::GestureEventData& gesture) OVERRIDE;
336
337   // --------------------------------------------------------------------------
338   // Other private methods and data
339   // --------------------------------------------------------------------------
340
341   void InitWebContents();
342
343   RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
344
345   blink::WebGestureEvent MakeGestureEvent(
346       blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
347
348   void SendBeginFrame(base::TimeTicks frame_time);
349
350   gfx::Size GetViewportSizePix() const;
351   gfx::Size GetViewportSizeOffsetPix() const;
352
353   void DeleteScaledSnapshotTexture();
354
355   void SendGestureEvent(const blink::WebGestureEvent& event);
356
357   // Update focus state of the RenderWidgetHostView.
358   void SetFocusInternal(bool focused);
359
360   // Send device_orientation_ to renderer.
361   void SendOrientationChangeEventInternal();
362
363   // Utility method for synthesizing a touch cancel event and dispatching it
364   // through the touch pipeline.
365   void CancelActiveTouchSequenceIfNecessary();
366
367   float dpi_scale() const { return dpi_scale_; }
368
369   // A weak reference to the Java ContentViewCore object.
370   JavaObjectWeakGlobalRef java_ref_;
371
372   NotificationRegistrar notification_registrar_;
373
374   // Reference to the current WebContents used to determine how and what to
375   // display in the ContentViewCore.
376   WebContentsImpl* web_contents_;
377
378   // A compositor layer containing any layer that should be shown.
379   scoped_refptr<cc::Layer> root_layer_;
380
381   // Device scale factor.
382   float dpi_scale_;
383
384   // Variables used to keep track of frame timestamps and deadlines.
385   base::TimeDelta vsync_interval_;
386   base::TimeDelta expected_browser_composite_time_;
387
388   // The Android view that can be used to add and remove decoration layers
389   // like AutofillPopup.
390   ui::ViewAndroid* view_android_;
391
392   // The owning window that has a hold of main application activity.
393   ui::WindowAndroid* window_android_;
394
395   // Provides gesture synthesis given a stream of touch events (derived from
396   // Android MotionEvent's) and touch event acks.
397   ui::FilteredGestureProvider gesture_provider_;
398
399   // The cache of device's current orientation set from Java side, this value
400   // will be sent to Renderer once it is ready.
401   int device_orientation_;
402
403   bool geolocation_needs_pause_;
404
405   DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
406 };
407
408 bool RegisterContentViewCore(JNIEnv* env);
409
410 }  // namespace content
411
412 #endif  // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_