Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / render_widget_host_view_android.h
1 // Copyright (c) 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_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_
7
8 #include <map>
9 #include <queue>
10
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/i18n/rtl.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/process/process.h"
17 #include "cc/layers/delegated_frame_resource_collection.h"
18 #include "cc/output/begin_frame_args.h"
19 #include "content/browser/accessibility/browser_accessibility_manager.h"
20 #include "content/browser/renderer_host/delegated_frame_evictor.h"
21 #include "content/browser/renderer_host/image_transport_factory_android.h"
22 #include "content/browser/renderer_host/ime_adapter_android.h"
23 #include "content/browser/renderer_host/input/gesture_text_selector.h"
24 #include "content/browser/renderer_host/input/touch_selection_controller.h"
25 #include "content/browser/renderer_host/render_widget_host_view_base.h"
26 #include "content/common/content_export.h"
27 #include "gpu/command_buffer/common/mailbox.h"
28 #include "third_party/skia/include/core/SkColor.h"
29 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
30 #include "ui/base/android/window_android_observer.h"
31 #include "ui/events/gesture_detection/filtered_gesture_provider.h"
32 #include "ui/gfx/size.h"
33 #include "ui/gfx/vector2d_f.h"
34
35 struct ViewHostMsg_TextInputState_Params;
36
37 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
38 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
39
40 namespace cc {
41 class CopyOutputResult;
42 class DelegatedFrameProvider;
43 class DelegatedRendererLayer;
44 class Layer;
45 }
46
47 namespace blink {
48 class WebExternalTextureLayer;
49 class WebTouchEvent;
50 class WebMouseEvent;
51 }
52
53 namespace content {
54 class ContentViewCoreImpl;
55 class OverscrollGlow;
56 class RenderWidgetHost;
57 class RenderWidgetHostImpl;
58 struct DidOverscrollParams;
59 struct NativeWebKeyboardEvent;
60
61 class ReadbackRequest {
62  public:
63   explicit ReadbackRequest(
64       float scale,
65       SkColorType color_type,
66       gfx::Rect src_subrect,
67       const base::Callback<void(bool, const SkBitmap&)>& result_callback);
68   ~ReadbackRequest();
69   float GetScale() { return scale_; }
70   SkColorType GetColorFormat() { return color_type_; }
71   const gfx::Rect GetCaptureRect() { return src_subrect_; }
72   const base::Callback<void(bool, const SkBitmap&)>& GetResultCallback() {
73     return result_callback_;
74   }
75
76  private:
77   ReadbackRequest();
78   float scale_;
79   SkColorType color_type_;
80   gfx::Rect src_subrect_;
81   base::Callback<void(bool, const SkBitmap&)> result_callback_;
82 };
83
84 // -----------------------------------------------------------------------------
85 // See comments in render_widget_host_view.h about this class and its members.
86 // -----------------------------------------------------------------------------
87 class CONTENT_EXPORT RenderWidgetHostViewAndroid
88     : public RenderWidgetHostViewBase,
89       public cc::DelegatedFrameResourceCollectionClient,
90       public ImageTransportFactoryAndroidObserver,
91       public ui::GestureProviderClient,
92       public ui::WindowAndroidObserver,
93       public DelegatedFrameEvictorClient,
94       public GestureTextSelectorClient,
95       public TouchSelectionControllerClient {
96  public:
97   RenderWidgetHostViewAndroid(RenderWidgetHostImpl* widget,
98                               ContentViewCoreImpl* content_view_core);
99   virtual ~RenderWidgetHostViewAndroid();
100
101   // RenderWidgetHostView implementation.
102   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
103   virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE;
104   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
105                            const gfx::Rect& pos) OVERRIDE;
106   virtual void InitAsFullscreen(
107       RenderWidgetHostView* reference_host_view) OVERRIDE;
108   virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
109   virtual void WasShown() OVERRIDE;
110   virtual void WasHidden() OVERRIDE;
111   virtual void SetSize(const gfx::Size& size) OVERRIDE;
112   virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
113   virtual gfx::Vector2dF GetLastScrollOffset() const OVERRIDE;
114   virtual gfx::NativeView GetNativeView() const OVERRIDE;
115   virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
116   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
117   virtual void MovePluginWindows(
118       const std::vector<WebPluginGeometry>& moves) OVERRIDE;
119   virtual void Focus() OVERRIDE;
120   virtual void Blur() OVERRIDE;
121   virtual bool HasFocus() const OVERRIDE;
122   virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
123   virtual void Show() OVERRIDE;
124   virtual void Hide() OVERRIDE;
125   virtual bool IsShowing() OVERRIDE;
126   virtual gfx::Rect GetViewBounds() const OVERRIDE;
127   virtual gfx::Size GetPhysicalBackingSize() const OVERRIDE;
128   virtual float GetTopControlsLayoutHeight() const OVERRIDE;
129   virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
130   virtual void SetIsLoading(bool is_loading) OVERRIDE;
131   virtual void TextInputTypeChanged(ui::TextInputType type,
132                                     ui::TextInputMode input_mode,
133                                     bool can_compose_inline) OVERRIDE;
134   virtual void ImeCancelComposition() OVERRIDE;
135   virtual void FocusedNodeChanged(bool is_editable_node) OVERRIDE;
136   virtual void RenderProcessGone(base::TerminationStatus status,
137                                  int error_code) OVERRIDE;
138   virtual void Destroy() OVERRIDE;
139   virtual void SetTooltipText(const base::string16& tooltip_text) OVERRIDE;
140   virtual void SelectionChanged(const base::string16& text,
141                                 size_t offset,
142                                 const gfx::Range& range) OVERRIDE;
143   virtual void SelectionBoundsChanged(
144       const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
145   virtual void AcceleratedSurfaceInitialized(int host_id,
146                                              int route_id) OVERRIDE;
147   virtual void AcceleratedSurfaceBuffersSwapped(
148       const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
149       int gpu_host_id) OVERRIDE;
150   virtual void AcceleratedSurfacePostSubBuffer(
151       const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
152       int gpu_host_id) OVERRIDE;
153   virtual void AcceleratedSurfaceSuspend() OVERRIDE;
154   virtual void AcceleratedSurfaceRelease() OVERRIDE;
155   virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
156   virtual void SetBackgroundOpaque(bool transparent) OVERRIDE;
157   virtual void CopyFromCompositingSurface(
158       const gfx::Rect& src_subrect,
159       const gfx::Size& dst_size,
160       CopyFromCompositingSurfaceCallback& callback,
161       const SkColorType color_type) OVERRIDE;
162   virtual void CopyFromCompositingSurfaceToVideoFrame(
163       const gfx::Rect& src_subrect,
164       const scoped_refptr<media::VideoFrame>& target,
165       const base::Callback<void(bool)>& callback) OVERRIDE;
166   virtual bool CanCopyToVideoFrame() const OVERRIDE;
167   virtual void GetScreenInfo(blink::WebScreenInfo* results) OVERRIDE;
168   virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
169   virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
170   virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
171                                       InputEventAckState ack_result) OVERRIDE;
172   virtual InputEventAckState FilterInputEvent(
173       const blink::WebInputEvent& input_event) OVERRIDE;
174   virtual void OnSetNeedsFlushInput() OVERRIDE;
175   virtual void GestureEventAck(const blink::WebGestureEvent& event,
176                                InputEventAckState ack_result) OVERRIDE;
177   virtual BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
178       BrowserAccessibilityDelegate* delegate) OVERRIDE;
179   virtual bool LockMouse() OVERRIDE;
180   virtual void UnlockMouse() OVERRIDE;
181   virtual void OnSwapCompositorFrame(
182       uint32 output_surface_id,
183       scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
184   virtual void DidOverscroll(const DidOverscrollParams& params) OVERRIDE;
185   virtual void DidStopFlinging() OVERRIDE;
186   virtual void ShowDisambiguationPopup(const gfx::Rect& rect_pixels,
187                                        const SkBitmap& zoomed_bitmap) OVERRIDE;
188   virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget()
189       OVERRIDE;
190   virtual void LockCompositingSurface() OVERRIDE;
191   virtual void UnlockCompositingSurface() OVERRIDE;
192   virtual void OnTextSurroundingSelectionResponse(const base::string16& content,
193                                                   size_t start_offset,
194                                                   size_t end_offset) OVERRIDE;
195
196   // cc::DelegatedFrameResourceCollectionClient implementation.
197   virtual void UnusedResourcesAreAvailable() OVERRIDE;
198
199   // ui::GestureProviderClient implementation.
200   virtual void OnGestureEvent(const ui::GestureEventData& gesture) OVERRIDE;
201
202   // ui::WindowAndroidObserver implementation.
203   virtual void OnCompositingDidCommit() OVERRIDE;
204   virtual void OnAttachCompositor() OVERRIDE;
205   virtual void OnDetachCompositor() OVERRIDE;
206   virtual void OnVSync(base::TimeTicks frame_time,
207                        base::TimeDelta vsync_period) OVERRIDE;
208   virtual void OnAnimate(base::TimeTicks begin_frame_time) OVERRIDE;
209
210   // ImageTransportFactoryAndroidObserver implementation.
211   virtual void OnLostResources() OVERRIDE;
212
213   // DelegatedFrameEvictor implementation
214   virtual void EvictDelegatedFrame() OVERRIDE;
215
216   virtual SkColorType PreferredReadbackFormat() OVERRIDE;
217
218   // GestureTextSelectorClient implementation.
219   virtual void ShowSelectionHandlesAutomatically() OVERRIDE;
220   virtual void SelectRange(float x1, float y1, float x2, float y2) OVERRIDE;
221   virtual void Unselect() OVERRIDE;
222   virtual void LongPress(base::TimeTicks time, float x, float y) OVERRIDE;
223
224   // Non-virtual methods
225   void SetContentViewCore(ContentViewCoreImpl* content_view_core);
226   SkColor GetCachedBackgroundColor() const;
227   void SendKeyEvent(const NativeWebKeyboardEvent& event);
228   void SendMouseEvent(const blink::WebMouseEvent& event);
229   void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event);
230   void SendGestureEvent(const blink::WebGestureEvent& event);
231
232   void OnTextInputStateChanged(const ViewHostMsg_TextInputState_Params& params);
233   void OnDidChangeBodyBackgroundColor(SkColor color);
234   void OnStartContentIntent(const GURL& content_url);
235   void OnSetNeedsBeginFrame(bool enabled);
236   void OnSmartClipDataExtracted(const base::string16& text,
237                                 const base::string16& html,
238                                 const gfx::Rect rect);
239
240   bool OnTouchEvent(const ui::MotionEvent& event);
241   bool OnTouchHandleEvent(const ui::MotionEvent& event);
242   void ResetGestureDetection();
243   void SetDoubleTapSupportEnabled(bool enabled);
244   void SetMultiTouchZoomSupportEnabled(bool enabled);
245
246   long GetNativeImeAdapter();
247
248   void WasResized();
249
250   void GetScaledContentBitmap(
251       float scale,
252       SkColorType color_type,
253       gfx::Rect src_subrect,
254       const base::Callback<void(bool, const SkBitmap&)>& result_callback);
255
256   scoped_refptr<cc::DelegatedRendererLayer>
257       CreateDelegatedLayerForFrameProvider() const;
258
259   bool HasValidFrame() const;
260
261   void MoveCaret(const gfx::Point& point);
262   void HideTextHandles();
263   void OnShowingPastePopup(const gfx::PointF& point);
264
265   void SynchronousFrameMetadata(
266       const cc::CompositorFrameMetadata& frame_metadata);
267
268   void SetOverlayVideoMode(bool enabled);
269
270   typedef base::Callback<
271       void(const base::string16& content, int start_offset, int end_offset)>
272       TextSurroundingSelectionCallback;
273   void SetTextSurroundingSelectionCallback(
274       const TextSurroundingSelectionCallback& callback);
275
276  private:
277   // TouchSelectionControllerClient implementation.
278   virtual bool SupportsAnimation() const OVERRIDE;
279   virtual void SetNeedsAnimate() OVERRIDE;
280   virtual void MoveCaret(const gfx::PointF& position) OVERRIDE;
281   virtual void SelectBetweenCoordinates(const gfx::PointF& start,
282                                         const gfx::PointF& end) OVERRIDE;
283   virtual void OnSelectionEvent(SelectionEventType event,
284                                 const gfx::PointF& anchor_position) OVERRIDE;
285   virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() OVERRIDE;
286
287   void RunAckCallbacks();
288
289   void DestroyDelegatedContent();
290   void SwapDelegatedFrame(uint32 output_surface_id,
291                           scoped_ptr<cc::DelegatedFrameData> frame_data);
292   void SendDelegatedFrameAck(uint32 output_surface_id);
293   void SendReturnedDelegatedResources(uint32 output_surface_id);
294
295   void OnFrameMetadataUpdated(
296       const cc::CompositorFrameMetadata& frame_metadata);
297   void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata);
298
299   void AttachLayers();
300   void RemoveLayers();
301
302   // Called after async screenshot task completes. Scales and crops the result
303   // of the copy.
304   static void PrepareTextureCopyOutputResult(
305       const gfx::Size& dst_size_in_pixel,
306       const SkColorType color_type,
307       const base::TimeTicks& start_time,
308       const base::Callback<void(bool, const SkBitmap&)>& callback,
309       scoped_ptr<cc::CopyOutputResult> result);
310   static void PrepareTextureCopyOutputResultForDelegatedReadback(
311       const gfx::Size& dst_size_in_pixel,
312       const SkColorType color_type,
313       const base::TimeTicks& start_time,
314       scoped_refptr<cc::Layer> readback_layer,
315       const base::Callback<void(bool, const SkBitmap&)>& callback,
316       scoped_ptr<cc::CopyOutputResult> result);
317
318   // DevTools ScreenCast support for Android WebView.
319   void SynchronousCopyContents(
320       const gfx::Rect& src_subrect_in_pixel,
321       const gfx::Size& dst_size_in_pixel,
322       const base::Callback<void(bool, const SkBitmap&)>& callback,
323       const SkColorType color_type);
324
325   bool IsReadbackConfigSupported(SkColorType color_type);
326
327   // If we have locks on a frame during a ContentViewCore swap or a context
328   // lost, the frame is no longer valid and we can safely release all the locks.
329   // Use this method to release all the locks.
330   void ReleaseLocksOnSurface();
331
332   // Drop any incoming frames from the renderer when there are locks on the
333   // current frame.
334   void RetainFrame(uint32 output_surface_id,
335                    scoped_ptr<cc::CompositorFrame> frame);
336
337   void InternalSwapCompositorFrame(uint32 output_surface_id,
338                                    scoped_ptr<cc::CompositorFrame> frame);
339
340   enum VSyncRequestType {
341     FLUSH_INPUT = 1 << 0,
342     BEGIN_FRAME = 1 << 1,
343     PERSISTENT_BEGIN_FRAME = 1 << 2
344   };
345   void RequestVSyncUpdate(uint32 requests);
346   void StartObservingRootWindow();
347   void StopObservingRootWindow();
348   void SendBeginFrame(base::TimeTicks frame_time, base::TimeDelta vsync_period);
349   bool Animate(base::TimeTicks frame_time);
350
351   // Handles all unprocessed and pending readback requests.
352   void AbortPendingReadbackRequests();
353
354   // The model object.
355   RenderWidgetHostImpl* host_;
356
357   // Used to control action dispatch at the next |OnVSync()| call.
358   uint32 outstanding_vsync_requests_;
359
360   bool is_showing_;
361
362   // ContentViewCoreImpl is our interface to the view system.
363   ContentViewCoreImpl* content_view_core_;
364
365   ImeAdapterAndroid ime_adapter_android_;
366
367   // Body background color of the underlying document.
368   SkColor cached_background_color_;
369
370   scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
371   scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
372   scoped_refptr<cc::DelegatedRendererLayer> layer_;
373
374   // The most recent texture size that was pushed to the texture layer.
375   gfx::Size texture_size_in_layer_;
376
377   // The most recent content size that was pushed to the texture layer.
378   gfx::Size content_size_in_layer_;
379
380   // The output surface id of the last received frame.
381   uint32_t last_output_surface_id_;
382
383
384   std::queue<base::Closure> ack_callbacks_;
385
386   const bool overscroll_effect_enabled_;
387   // Used to render overscroll overlays.
388   scoped_ptr<OverscrollGlow> overscroll_effect_;
389
390   // Provides gesture synthesis given a stream of touch events (derived from
391   // Android MotionEvent's) and touch event acks.
392   ui::FilteredGestureProvider gesture_provider_;
393
394   // Handles gesture based text selection
395   GestureTextSelector gesture_text_selector_;
396
397   // Manages selection handle rendering and manipulation.
398   // This will always be NULL if |content_view_core_| is NULL.
399   scoped_ptr<TouchSelectionController> selection_controller_;
400
401   int accelerated_surface_route_id_;
402
403   // Size to use if we have no backing ContentViewCore
404   gfx::Size default_size_;
405
406   const bool using_synchronous_compositor_;
407
408   scoped_ptr<DelegatedFrameEvictor> frame_evictor_;
409
410   size_t locks_on_frame_count_;
411   bool observing_root_window_;
412
413   struct LastFrameInfo {
414     LastFrameInfo(uint32 output_id,
415                   scoped_ptr<cc::CompositorFrame> output_frame);
416     ~LastFrameInfo();
417     uint32 output_surface_id;
418     scoped_ptr<cc::CompositorFrame> frame;
419   };
420
421   scoped_ptr<LastFrameInfo> last_frame_info_;
422
423   TextSurroundingSelectionCallback text_surrounding_selection_callback_;
424
425   // List of readbackrequests waiting for arrival of a valid frame.
426   std::queue<ReadbackRequest> readbacks_waiting_for_frame_;
427
428   // The last scroll offset of the view.
429   gfx::Vector2dF last_scroll_offset_;
430
431   base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_;
432
433   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid);
434 };
435
436 } // namespace content
437
438 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_