- add sources.
[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/layers/delegated_renderer_layer_client.h"
19 #include "cc/layers/texture_layer_client.h"
20 #include "cc/output/begin_frame_args.h"
21 #include "content/browser/accessibility/browser_accessibility_manager.h"
22 #include "content/browser/renderer_host/image_transport_factory_android.h"
23 #include "content/browser/renderer_host/ime_adapter_android.h"
24 #include "content/browser/renderer_host/render_widget_host_view_base.h"
25 #include "gpu/command_buffer/common/mailbox.h"
26 #include "third_party/skia/include/core/SkColor.h"
27 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
28 #include "ui/gfx/size.h"
29 #include "ui/gfx/vector2d_f.h"
30
31 struct ViewHostMsg_TextInputState_Params;
32
33 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
34 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
35
36 namespace cc {
37 class CopyOutputResult;
38 class DelegatedFrameProvider;
39 class DelegatedRendererLayer;
40 class Layer;
41 class SingleReleaseCallback;
42 class TextureLayer;
43 }
44
45 namespace WebKit {
46 class WebExternalTextureLayer;
47 class WebTouchEvent;
48 class WebMouseEvent;
49 }
50
51 namespace content {
52 class ContentViewCoreImpl;
53 class OverscrollGlow;
54 class RenderWidgetHost;
55 class RenderWidgetHostImpl;
56 struct NativeWebKeyboardEvent;
57
58 // -----------------------------------------------------------------------------
59 // See comments in render_widget_host_view.h about this class and its members.
60 // -----------------------------------------------------------------------------
61 class RenderWidgetHostViewAndroid
62     : public RenderWidgetHostViewBase,
63       public BrowserAccessibilityDelegate,
64       public cc::TextureLayerClient,
65       public cc::DelegatedRendererLayerClient,
66       public cc::DelegatedFrameResourceCollectionClient,
67       public ImageTransportFactoryAndroidObserver {
68  public:
69   RenderWidgetHostViewAndroid(RenderWidgetHostImpl* widget,
70                               ContentViewCoreImpl* content_view_core);
71   virtual ~RenderWidgetHostViewAndroid();
72
73   // RenderWidgetHostView implementation.
74   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
75   virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE;
76   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
77                            const gfx::Rect& pos) OVERRIDE;
78   virtual void InitAsFullscreen(
79       RenderWidgetHostView* reference_host_view) OVERRIDE;
80   virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
81   virtual void WasShown() OVERRIDE;
82   virtual void WasHidden() OVERRIDE;
83   virtual void SetSize(const gfx::Size& size) OVERRIDE;
84   virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
85   virtual gfx::NativeView GetNativeView() const OVERRIDE;
86   virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
87   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
88   virtual void MovePluginWindows(
89       const gfx::Vector2d& scroll_offset,
90       const std::vector<WebPluginGeometry>& moves) OVERRIDE;
91   virtual void Focus() OVERRIDE;
92   virtual void Blur() OVERRIDE;
93   virtual bool HasFocus() const OVERRIDE;
94   virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
95   virtual void Show() OVERRIDE;
96   virtual void Hide() OVERRIDE;
97   virtual bool IsShowing() OVERRIDE;
98   virtual gfx::Rect GetViewBounds() const OVERRIDE;
99   virtual gfx::Size GetPhysicalBackingSize() const OVERRIDE;
100   virtual float GetOverdrawBottomHeight() const OVERRIDE;
101   virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
102   virtual void SetIsLoading(bool is_loading) OVERRIDE;
103   virtual void TextInputTypeChanged(ui::TextInputType type,
104                                     ui::TextInputMode input_mode,
105                                     bool can_compose_inline) OVERRIDE;
106   virtual void ImeCancelComposition() OVERRIDE;
107   virtual void DidUpdateBackingStore(
108       const gfx::Rect& scroll_rect,
109       const gfx::Vector2d& scroll_delta,
110       const std::vector<gfx::Rect>& copy_rects,
111       const ui::LatencyInfo& latency_info) OVERRIDE;
112   virtual void RenderProcessGone(base::TerminationStatus status,
113                                  int error_code) OVERRIDE;
114   virtual void Destroy() OVERRIDE;
115   virtual void SetTooltipText(const string16& tooltip_text) OVERRIDE;
116   virtual void SelectionChanged(const string16& text,
117                                 size_t offset,
118                                 const gfx::Range& range) OVERRIDE;
119   virtual void SelectionBoundsChanged(
120       const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
121   virtual void ScrollOffsetChanged() OVERRIDE;
122   virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
123   virtual void OnAcceleratedCompositingStateChange() OVERRIDE;
124   virtual void AcceleratedSurfaceBuffersSwapped(
125       const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
126       int gpu_host_id) OVERRIDE;
127   virtual void AcceleratedSurfacePostSubBuffer(
128       const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
129       int gpu_host_id) OVERRIDE;
130   virtual void AcceleratedSurfaceSuspend() OVERRIDE;
131   virtual void AcceleratedSurfaceRelease() OVERRIDE;
132   virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
133   virtual void SetBackground(const SkBitmap& background) OVERRIDE;
134   virtual void CopyFromCompositingSurface(
135       const gfx::Rect& src_subrect,
136       const gfx::Size& dst_size,
137       const base::Callback<void(bool, const SkBitmap&)>& callback) OVERRIDE;
138   virtual void CopyFromCompositingSurfaceToVideoFrame(
139       const gfx::Rect& src_subrect,
140       const scoped_refptr<media::VideoFrame>& target,
141       const base::Callback<void(bool)>& callback) OVERRIDE;
142   virtual bool CanCopyToVideoFrame() const OVERRIDE;
143   virtual void GetScreenInfo(WebKit::WebScreenInfo* results) OVERRIDE;
144   virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
145   virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
146   virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
147                                       InputEventAckState ack_result) OVERRIDE;
148   virtual void SetHasHorizontalScrollbar(
149       bool has_horizontal_scrollbar) OVERRIDE;
150   virtual void SetScrollOffsetPinning(
151       bool is_pinned_to_left, bool is_pinned_to_right) OVERRIDE;
152   virtual void UnhandledWheelEvent(
153       const WebKit::WebMouseWheelEvent& event) OVERRIDE;
154   virtual InputEventAckState FilterInputEvent(
155       const WebKit::WebInputEvent& input_event) OVERRIDE;
156   virtual void OnSetNeedsFlushInput() OVERRIDE;
157   virtual void GestureEventAck(int gesture_event_type,
158                                InputEventAckState ack_result) OVERRIDE;
159   virtual void OnAccessibilityEvents(
160       const std::vector<AccessibilityHostMsg_EventParams>&
161           params) OVERRIDE;
162   virtual bool LockMouse() OVERRIDE;
163   virtual void UnlockMouse() OVERRIDE;
164   virtual void HasTouchEventHandlers(bool need_touch_events) OVERRIDE;
165   virtual void OnSwapCompositorFrame(
166       uint32 output_surface_id,
167       scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
168   virtual void OnOverscrolled(gfx::Vector2dF accumulated_overscroll,
169                               gfx::Vector2dF current_fling_velocity) OVERRIDE;
170   virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
171                                        const SkBitmap& zoomed_bitmap) OVERRIDE;
172   virtual SyntheticGesture* CreateSmoothScrollGesture(
173       bool scroll_down, int pixels_to_scroll, int mouse_event_x,
174       int mouse_event_y) OVERRIDE;
175   virtual SyntheticGesture* CreatePinchGesture(
176       bool zoom_in, int pixels_to_move, int anchor_x,
177       int anchor_y) OVERRIDE;
178
179   // Implementation of BrowserAccessibilityDelegate:
180   virtual void SetAccessibilityFocus(int acc_obj_id) OVERRIDE;
181   virtual void AccessibilityDoDefaultAction(int acc_obj_id) OVERRIDE;
182   virtual void AccessibilityScrollToMakeVisible(
183       int acc_obj_id, gfx::Rect subfocus) OVERRIDE;
184   virtual void AccessibilityScrollToPoint(
185       int acc_obj_id, gfx::Point point) OVERRIDE;
186   virtual void AccessibilitySetTextSelection(
187       int acc_obj_id, int start_offset, int end_offset) OVERRIDE;
188   virtual gfx::Point GetLastTouchEventLocation() const OVERRIDE;
189   virtual void FatalAccessibilityTreeError() OVERRIDE;
190
191   // cc::TextureLayerClient implementation.
192   virtual unsigned PrepareTexture() OVERRIDE;
193   virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE;
194   virtual bool PrepareTextureMailbox(
195       cc::TextureMailbox* mailbox,
196       scoped_ptr<cc::SingleReleaseCallback>* release_callback,
197       bool use_shared_memory) OVERRIDE;
198
199   // cc::DelegatedRendererLayerClient implementation.
200   virtual void DidCommitFrameData() OVERRIDE;
201
202   // cc::DelegatedFrameResourceCollectionClient implementation.
203   virtual void UnusedResourcesAreAvailable() OVERRIDE;
204
205   // ImageTransportFactoryAndroidObserver implementation.
206   virtual void OnLostResources() OVERRIDE;
207
208   // Non-virtual methods
209   void SetContentViewCore(ContentViewCoreImpl* content_view_core);
210   SkColor GetCachedBackgroundColor() const;
211   void SendKeyEvent(const NativeWebKeyboardEvent& event);
212   void SendTouchEvent(const WebKit::WebTouchEvent& event);
213   void SendMouseEvent(const WebKit::WebMouseEvent& event);
214   void SendMouseWheelEvent(const WebKit::WebMouseWheelEvent& event);
215   void SendGestureEvent(const WebKit::WebGestureEvent& event);
216   void SendBeginFrame(const cc::BeginFrameArgs& args);
217
218   void OnTextInputStateChanged(const ViewHostMsg_TextInputState_Params& params);
219   void OnDidChangeBodyBackgroundColor(SkColor color);
220   void OnStartContentIntent(const GURL& content_url);
221   void OnSetNeedsBeginFrame(bool enabled);
222
223   int GetNativeImeAdapter();
224
225   void WasResized();
226
227   WebKit::WebGLId GetScaledContentTexture(float scale, gfx::Size* out_size);
228   bool PopulateBitmapWithContents(jobject jbitmap);
229
230   bool HasValidFrame() const;
231
232   // Select all text between the given coordinates.
233   void SelectRange(const gfx::Point& start, const gfx::Point& end);
234
235   void MoveCaret(const gfx::Point& point);
236
237   void RequestContentClipping(const gfx::Rect& clipping,
238                               const gfx::Size& content_size);
239
240   // Returns true when animation ticks are still needed. This avoids a separate
241   // round-trip for requesting follow-up animation.
242   bool Animate(base::TimeTicks frame_time);
243
244   void SynchronousFrameMetadata(
245       const cc::CompositorFrameMetadata& frame_metadata);
246
247  private:
248   void BuffersSwapped(const gpu::Mailbox& mailbox,
249                       uint32_t output_surface_id,
250                       const base::Closure& ack_callback);
251
252   void RunAckCallbacks();
253
254   void DestroyDelegatedContent();
255   void SwapDelegatedFrame(uint32 output_surface_id,
256                           scoped_ptr<cc::DelegatedFrameData> frame_data);
257   void SendDelegatedFrameAck(uint32 output_surface_id);
258
259   void UpdateContentViewCoreFrameMetadata(
260       const cc::CompositorFrameMetadata& frame_metadata);
261   void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata);
262   void ResetClipping();
263   void ClipContents(const gfx::Rect& clipping, const gfx::Size& content_size);
264
265   void AttachLayers();
266   void RemoveLayers();
267
268   void CreateOverscrollEffectIfNecessary();
269   void UpdateAnimationSize(const cc::CompositorFrameMetadata& frame_metadata);
270   void ScheduleAnimationIfNecessary();
271
272   // Called after async screenshot task completes. Scales and crops the result
273   // of the copy.
274   static void PrepareTextureCopyOutputResult(
275       const gfx::Size& dst_size_in_pixel,
276       const base::Callback<void(bool, const SkBitmap&)>& callback,
277       scoped_ptr<cc::CopyOutputResult> result);
278   static void PrepareBitmapCopyOutputResult(
279       const gfx::Size& dst_size_in_pixel,
280       const base::Callback<void(bool, const SkBitmap&)>& callback,
281       scoped_ptr<cc::CopyOutputResult> result);
282
283   // The model object.
284   RenderWidgetHostImpl* host_;
285
286   // Used to track whether this render widget needs a BeginFrame.
287   bool needs_begin_frame_;
288
289   // Whether or not this widget is potentially attached to the view hierarchy.
290   // This view may not actually be attached if this is true, but it should be
291   // treated as such, because as soon as a ContentViewCore is set the layer
292   // will be attached automatically.
293   bool are_layers_attached_;
294
295   // ContentViewCoreImpl is our interface to the view system.
296   ContentViewCoreImpl* content_view_core_;
297
298   ImeAdapterAndroid ime_adapter_android_;
299
300   // Body background color of the underlying document.
301   SkColor cached_background_color_;
302
303   // The texture layer for this view when using browser-side compositing.
304   scoped_refptr<cc::TextureLayer> texture_layer_;
305
306   scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
307   scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
308   scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
309
310   // The layer used for rendering the contents of this view.
311   // It is either owned by texture_layer_ or surface_texture_transport_
312   // depending on the mode.
313   scoped_refptr<cc::Layer> layer_;
314
315   // The most recent texture id that was pushed to the texture layer.
316   unsigned int texture_id_in_layer_;
317
318   // The most recent texture size that was pushed to the texture layer.
319   gfx::Size texture_size_in_layer_;
320
321   // The most recent content size that was pushed to the texture layer.
322   gfx::Size content_size_in_layer_;
323
324   // The mailbox of the previously received frame.
325   gpu::Mailbox current_mailbox_;
326
327   // The output surface id of the last received frame.
328   uint32_t last_output_surface_id_;
329
330   base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_;
331
332   std::queue<base::Closure> ack_callbacks_;
333
334   // Used to render overscroll overlays.
335   bool overscroll_effect_enabled_;
336   scoped_ptr<OverscrollGlow> overscroll_effect_;
337
338   bool flush_input_requested_;
339
340   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid);
341 };
342
343 } // namespace content
344
345 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_