Upstream version 7.36.149.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/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                       WebContents* web_contents,
46                       ui::ViewAndroid* view_android,
47                       ui::WindowAndroid* window_android);
48
49   // ContentViewCore implementation.
50   virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() OVERRIDE;
51   virtual WebContents* GetWebContents() const OVERRIDE;
52   virtual ui::ViewAndroid* GetViewAndroid() const OVERRIDE;
53   virtual ui::WindowAndroid* GetWindowAndroid() const OVERRIDE;
54   virtual scoped_refptr<cc::Layer> GetLayer() const OVERRIDE;
55   virtual void LoadUrl(NavigationController::LoadURLParams& params) OVERRIDE;
56   virtual void ShowPastePopup(int x, int y) OVERRIDE;
57   virtual void GetScaledContentBitmap(
58       float scale,
59       jobject bitmap_config,
60       gfx::Rect src_subrect,
61       const base::Callback<void(bool, const SkBitmap&)>& result_callback)
62       OVERRIDE;
63   virtual float GetDpiScale() const OVERRIDE;
64   virtual void PauseVideo() OVERRIDE;
65   virtual void PauseOrResumeGeolocation(bool should_pause) OVERRIDE;
66
67   // --------------------------------------------------------------------------
68   // Methods called from Java via JNI
69   // --------------------------------------------------------------------------
70
71   base::android::ScopedJavaLocalRef<jobject> GetWebContentsAndroid(JNIEnv* env,
72                                                                    jobject obj);
73
74   void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj);
75
76   // Notifies the ContentViewCore that items were selected in the currently
77   // showing select popup.
78   void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices);
79
80   void LoadUrl(
81       JNIEnv* env, jobject obj,
82       jstring url,
83       jint load_url_type,
84       jint transition_type,
85       jstring j_referrer_url,
86       jint referrer_policy,
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   jboolean IsIncognito(JNIEnv* env, jobject obj);
95   void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation);
96   jboolean OnTouchEvent(JNIEnv* env,
97                         jobject obj,
98                         jobject motion_event,
99                         jlong time_ms,
100                         jint android_action,
101                         jint pointer_count,
102                         jint history_size,
103                         jint action_index,
104                         jfloat pos_x_0,
105                         jfloat pos_y_0,
106                         jfloat pos_x_1,
107                         jfloat pos_y_1,
108                         jint pointer_id_0,
109                         jint pointer_id_1,
110                         jfloat touch_major_0,
111                         jfloat touch_major_1);
112   jboolean SendMouseMoveEvent(JNIEnv* env,
113                               jobject obj,
114                               jlong time_ms,
115                               jfloat x,
116                               jfloat y);
117   jboolean SendMouseWheelEvent(JNIEnv* env,
118                                jobject obj,
119                                jlong time_ms,
120                                jfloat x,
121                                jfloat y,
122                                jfloat vertical_axis);
123   void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms,
124                    jfloat x, jfloat y, jfloat hintx, jfloat hinty);
125   void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms);
126   void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms,
127                 jfloat x, jfloat y, jfloat dx, jfloat dy);
128   void FlingStart(JNIEnv* env, jobject obj, jlong time_ms,
129                   jfloat x, jfloat y, jfloat vx, jfloat vy);
130   void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms);
131   void SingleTap(JNIEnv* env, jobject obj, jlong time_ms,
132                  jfloat x, jfloat y);
133   void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms,
134                  jfloat x, jfloat y) ;
135   void LongPress(JNIEnv* env, jobject obj, jlong time_ms,
136                  jfloat x, jfloat y);
137   void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y);
138   void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms);
139   void PinchBy(JNIEnv* env, jobject obj, jlong time_ms,
140                jfloat x, jfloat y, jfloat delta);
141   void SelectBetweenCoordinates(JNIEnv* env, jobject obj,
142                                 jfloat x1, jfloat y1,
143                                 jfloat x2, jfloat y2);
144   void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y);
145
146   void ResetGestureDetection(JNIEnv* env, jobject obj);
147   void SetDoubleTapSupportEnabled(JNIEnv* env, jobject obj, jboolean enabled);
148   void SetMultiTouchZoomSupportEnabled(JNIEnv* env,
149                                        jobject obj,
150                                        jboolean enabled);
151
152   void LoadIfNecessary(JNIEnv* env, jobject obj);
153   void RequestRestoreLoad(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   long GetNativeImeAdapter(JNIEnv* env, jobject obj);
165   void SetFocus(JNIEnv* env, jobject obj, jboolean focused);
166   void ScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj);
167
168   jint GetBackgroundColor(JNIEnv* env, jobject obj);
169   void SetBackgroundColor(JNIEnv* env, jobject obj, jint color);
170   void OnShow(JNIEnv* env, jobject obj);
171   void OnHide(JNIEnv* env, jobject obj);
172   void ClearSslPreferences(JNIEnv* env, jobject /* obj */);
173   void SetUseDesktopUserAgent(JNIEnv* env,
174                               jobject /* obj */,
175                               jboolean state,
176                               jboolean reload_on_state_change);
177   bool GetUseDesktopUserAgent(JNIEnv* env, jobject /* obj */);
178   void Show();
179   void Hide();
180   void SetAllowJavascriptInterfacesInspection(JNIEnv* env,
181                                               jobject obj,
182                                               jboolean allow);
183   void AddJavascriptInterface(JNIEnv* env,
184                               jobject obj,
185                               jobject object,
186                               jstring name,
187                               jclass safe_annotation_clazz,
188                               jobject retained_object_set);
189   void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name);
190   int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history);
191   void GetDirectedNavigationHistory(JNIEnv* env,
192                                     jobject obj,
193                                     jobject history,
194                                     jboolean is_forward,
195                                     jint max_entries);
196   base::android::ScopedJavaLocalRef<jstring>
197       GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj);
198   void WasResized(JNIEnv* env, jobject obj);
199   jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj);
200   void ExitFullscreen(JNIEnv* env, jobject obj);
201   void UpdateTopControlsState(JNIEnv* env,
202                               jobject obj,
203                               bool enable_hiding,
204                               bool enable_showing,
205                               bool animate);
206   void ShowImeIfNeeded(JNIEnv* env, jobject obj);
207
208   void ShowInterstitialPage(JNIEnv* env,
209                             jobject obj,
210                             jstring jurl,
211                             jlong delegate);
212   jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj);
213
214   void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled);
215
216   void ExtractSmartClipData(JNIEnv* env,
217                             jobject obj,
218                             jint x,
219                             jint y,
220                             jint width,
221                             jint height);
222
223   jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj);
224
225   // --------------------------------------------------------------------------
226   // Public methods that call to Java via JNI
227   // --------------------------------------------------------------------------
228
229   void OnSmartClipDataExtracted(const base::string16& result);
230
231   // Creates a popup menu with |items|.
232   // |multiple| defines if it should support multi-select.
233   // If not |multiple|, |selected_item| sets the initially selected item.
234   // Otherwise, item's "checked" flag selects it.
235   void ShowSelectPopupMenu(const gfx::Rect& bounds,
236                            const std::vector<MenuItem>& items,
237                            int selected_item,
238                            bool multiple);
239   // Hides a visible popup menu.
240   void HideSelectPopupMenu();
241
242   // All sizes and offsets are in CSS pixels as cached by the renderer.
243   void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset,
244                        float page_scale_factor,
245                        const gfx::Vector2dF& page_scale_factor_limits,
246                        const gfx::SizeF& content_size,
247                        const gfx::SizeF& viewport_size,
248                        const gfx::Vector2dF& controls_offset,
249                        const gfx::Vector2dF& content_offset,
250                        float overdraw_bottom_height);
251
252   void UpdateImeAdapter(long native_ime_adapter, int text_input_type,
253                         const std::string& text,
254                         int selection_start, int selection_end,
255                         int composition_start, int composition_end,
256                         bool show_ime_if_needed, bool is_non_ime_change);
257   void SetTitle(const base::string16& title);
258   void OnBackgroundColorChanged(SkColor color);
259
260   bool HasFocus();
261   void OnGestureEventAck(const blink::WebGestureEvent& event,
262                          InputEventAckState ack_result);
263   bool FilterInputEvent(const blink::WebInputEvent& event);
264   void OnSelectionChanged(const std::string& text);
265   void OnSelectionBoundsChanged(
266       const ViewHostMsg_SelectionBounds_Params& params);
267
268   void StartContentIntent(const GURL& content_url);
269
270   // Shows the disambiguation popup
271   // |target_rect|   --> window coordinates which |zoomed_bitmap| represents
272   // |zoomed_bitmap| --> magnified image of potential touch targets
273   void ShowDisambiguationPopup(
274       const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap);
275
276   // Creates a java-side touch event, used for injecting touch event for
277   // testing/benchmarking purposes
278   base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer();
279
280   base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient();
281
282   // Returns the context that the ContentViewCore was created with, it would
283   // typically be an Activity context for an on screen view.
284   base::android::ScopedJavaLocalRef<jobject> GetContext();
285
286   // Returns True if the given media should be blocked to load.
287   bool ShouldBlockMediaRequest(const GURL& url);
288
289   void DidStopFlinging();
290
291   // Returns the viewport size after accounting for the viewport offset.
292   gfx::Size GetViewSize() const;
293
294   void SetAccessibilityEnabledInternal(bool enabled);
295
296   // --------------------------------------------------------------------------
297   // Methods called from native code
298   // --------------------------------------------------------------------------
299
300   gfx::Size GetPhysicalBackingSize() const;
301   gfx::Size GetViewportSizeDip() const;
302   gfx::Size GetViewportSizeOffsetDip() const;
303   float GetOverdrawBottomHeightDip() const;
304
305   void AttachLayer(scoped_refptr<cc::Layer> layer);
306   void RemoveLayer(scoped_refptr<cc::Layer> layer);
307
308  private:
309   class ContentViewUserData;
310
311   friend class ContentViewUserData;
312   virtual ~ContentViewCoreImpl();
313
314   // NotificationObserver implementation.
315   virtual void Observe(int type,
316                        const NotificationSource& source,
317                        const NotificationDetails& details) OVERRIDE;
318
319   // WebContentsObserver implementation.
320   virtual void RenderViewReady() OVERRIDE;
321   virtual void WebContentsDestroyed() OVERRIDE;
322
323   // --------------------------------------------------------------------------
324   // Other private methods and data
325   // --------------------------------------------------------------------------
326
327   void InitWebContents();
328
329   RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
330
331   blink::WebGestureEvent MakeGestureEvent(
332       blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const;
333
334   gfx::Size GetViewportSizePix() const;
335   gfx::Size GetViewportSizeOffsetPix() const;
336
337   void DeleteScaledSnapshotTexture();
338
339   bool OnMotionEvent(const ui::MotionEvent& event);
340   void SendGestureEvent(const blink::WebGestureEvent& event);
341
342   // Update focus state of the RenderWidgetHostView.
343   void SetFocusInternal(bool focused);
344
345   // Send device_orientation_ to renderer.
346   void SendOrientationChangeEventInternal();
347
348   float dpi_scale() const { return dpi_scale_; }
349
350   // A weak reference to the Java ContentViewCore object.
351   JavaObjectWeakGlobalRef java_ref_;
352
353   NotificationRegistrar notification_registrar_;
354
355   // Reference to the current WebContents used to determine how and what to
356   // display in the ContentViewCore.
357   WebContentsImpl* web_contents_;
358
359   // A compositor layer containing any layer that should be shown.
360   scoped_refptr<cc::Layer> root_layer_;
361
362   // Device scale factor.
363   float dpi_scale_;
364
365   // The Android view that can be used to add and remove decoration layers
366   // like AutofillPopup.
367   ui::ViewAndroid* view_android_;
368
369   // The owning window that has a hold of main application activity.
370   ui::WindowAndroid* window_android_;
371
372   // The cache of device's current orientation set from Java side, this value
373   // will be sent to Renderer once it is ready.
374   int device_orientation_;
375
376   bool geolocation_needs_pause_;
377
378   bool accessibility_enabled_;
379
380   DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl);
381 };
382
383 bool RegisterContentViewCore(JNIEnv* env);
384
385 }  // namespace content
386
387 #endif  // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_