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