Upstream version 5.34.92.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(int gesture_event_type,
163                                InputEventAckState ack_result) OVERRIDE;
164   virtual void CreateBrowserAccessibilityManagerIfNeeded() OVERRIDE;
165   virtual bool LockMouse() OVERRIDE;
166   virtual void UnlockMouse() OVERRIDE;
167   virtual void HasTouchEventHandlers(bool need_touch_events) OVERRIDE;
168   virtual void OnSwapCompositorFrame(
169       uint32 output_surface_id,
170       scoped_ptr<cc::CompositorFrame> frame) OVERRIDE;
171   virtual void OnOverscrolled(gfx::Vector2dF accumulated_overscroll,
172                               gfx::Vector2dF current_fling_velocity) OVERRIDE;
173   virtual void DidStopFlinging() OVERRIDE;
174   virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
175                                        const SkBitmap& zoomed_bitmap) OVERRIDE;
176   virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget()
177       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::DelegatedFrameResourceCollectionClient implementation.
192   virtual void UnusedResourcesAreAvailable() OVERRIDE;
193
194   // ui::WindowAndroidObserver implementation.
195   virtual void OnCompositingDidCommit() OVERRIDE;
196   virtual void OnAttachCompositor() OVERRIDE {}
197   virtual void OnDetachCompositor() OVERRIDE;
198
199   // ImageTransportFactoryAndroidObserver implementation.
200   virtual void OnLostResources() OVERRIDE;
201
202   // DelegatedFrameEvictor implementation
203   virtual void EvictDelegatedFrame() OVERRIDE;
204
205   // Non-virtual methods
206   void SetContentViewCore(ContentViewCoreImpl* content_view_core);
207   SkColor GetCachedBackgroundColor() const;
208   void SendKeyEvent(const NativeWebKeyboardEvent& event);
209   void SendTouchEvent(const blink::WebTouchEvent& event);
210   void SendMouseEvent(const blink::WebMouseEvent& event);
211   void SendMouseWheelEvent(const blink::WebMouseWheelEvent& event);
212   void SendGestureEvent(const blink::WebGestureEvent& event);
213   void SendBeginFrame(const cc::BeginFrameArgs& args);
214
215   void OnTextInputStateChanged(const ViewHostMsg_TextInputState_Params& params);
216   void OnDidChangeBodyBackgroundColor(SkColor color);
217   void OnStartContentIntent(const GURL& content_url);
218   void OnSetNeedsBeginFrame(bool enabled);
219   void OnSmartClipDataExtracted(const base::string16& result);
220
221   void LockResources();
222   void UnlockResources();
223
224   int GetNativeImeAdapter();
225
226   void WasResized();
227
228   blink::WebGLId GetScaledContentTexture(float scale, gfx::Size* out_size);
229   bool PopulateBitmapWithContents(jobject jbitmap);
230
231   bool HasValidFrame() const;
232
233   // Select all text between the given coordinates.
234   void SelectRange(const gfx::Point& start, const gfx::Point& end);
235
236   void MoveCaret(const gfx::Point& point);
237
238   void RequestContentClipping(const gfx::Rect& clipping,
239                               const gfx::Size& content_size);
240
241   // Returns true when animation ticks are still needed. This avoids a separate
242   // round-trip for requesting follow-up animation.
243   bool Animate(base::TimeTicks frame_time);
244
245   void SynchronousFrameMetadata(
246       const cc::CompositorFrameMetadata& frame_metadata);
247
248   void SetOverlayVideoMode(bool enabled);
249
250  private:
251   void BuffersSwapped(const gpu::Mailbox& mailbox,
252                       uint32_t output_surface_id,
253                       const base::Closure& ack_callback);
254
255   void RunAckCallbacks();
256
257   void DestroyDelegatedContent();
258   void SwapDelegatedFrame(uint32 output_surface_id,
259                           scoped_ptr<cc::DelegatedFrameData> frame_data);
260   void SendDelegatedFrameAck(uint32 output_surface_id);
261
262   void UpdateContentViewCoreFrameMetadata(
263       const cc::CompositorFrameMetadata& frame_metadata);
264   void ComputeContentsSize(const cc::CompositorFrameMetadata& frame_metadata);
265   void ResetClipping();
266   void ClipContents(const gfx::Rect& clipping, const gfx::Size& content_size);
267
268   void AttachLayers();
269   void RemoveLayers();
270
271   void UpdateAnimationSize(const cc::CompositorFrameMetadata& frame_metadata);
272
273   // Called after async screenshot task completes. Scales and crops the result
274   // of the copy.
275   static void PrepareTextureCopyOutputResult(
276       const gfx::Size& dst_size_in_pixel,
277       const SkBitmap::Config config,
278       const base::TimeTicks& start_time,
279       const base::Callback<void(bool, const SkBitmap&)>& callback,
280       scoped_ptr<cc::CopyOutputResult> result);
281   static void PrepareBitmapCopyOutputResult(
282       const gfx::Size& dst_size_in_pixel,
283       const base::TimeTicks& start_time,
284       const base::Callback<void(bool, const SkBitmap&)>& callback,
285       scoped_ptr<cc::CopyOutputResult> result);
286
287   // DevTools ScreenCast support for Android WebView.
288   void SynchronousCopyContents(
289       const gfx::Rect& src_subrect_in_pixel,
290       const gfx::Size& dst_size_in_pixel,
291       const base::Callback<void(bool, const SkBitmap&)>& callback);
292
293   // The model object.
294   RenderWidgetHostImpl* host_;
295
296   // Used to track whether this render widget needs a BeginFrame.
297   bool needs_begin_frame_;
298
299   // Whether or not this widget is potentially attached to the view hierarchy.
300   // This view may not actually be attached if this is true, but it should be
301   // treated as such, because as soon as a ContentViewCore is set the layer
302   // will be attached automatically.
303   bool are_layers_attached_;
304
305   // ContentViewCoreImpl is our interface to the view system.
306   ContentViewCoreImpl* content_view_core_;
307
308   ImeAdapterAndroid ime_adapter_android_;
309
310   // Body background color of the underlying document.
311   SkColor cached_background_color_;
312
313   // The texture layer for this view when using browser-side compositing.
314   scoped_refptr<cc::TextureLayer> texture_layer_;
315
316   scoped_refptr<cc::DelegatedFrameResourceCollection> resource_collection_;
317   scoped_refptr<cc::DelegatedFrameProvider> frame_provider_;
318   scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
319
320   // The layer used for rendering the contents of this view.
321   // It is either owned by texture_layer_ or surface_texture_transport_
322   // depending on the mode.
323   scoped_refptr<cc::Layer> layer_;
324
325   // The most recent texture id that was pushed to the texture layer.
326   unsigned int texture_id_in_layer_;
327
328   // The most recent texture size that was pushed to the texture layer.
329   gfx::Size texture_size_in_layer_;
330
331   // The most recent content size that was pushed to the texture layer.
332   gfx::Size content_size_in_layer_;
333
334   // The mailbox of the previously received frame.
335   gpu::Mailbox current_mailbox_;
336
337   // The output surface id of the last received frame.
338   uint32_t last_output_surface_id_;
339
340   base::WeakPtrFactory<RenderWidgetHostViewAndroid> weak_ptr_factory_;
341
342   std::queue<base::Closure> ack_callbacks_;
343
344   const bool overscroll_effect_enabled_;
345   // Used to render overscroll overlays.
346   // Note: |overscroll_effect_| will never be NULL, even if it's never enabled.
347   scoped_ptr<OverscrollGlow> overscroll_effect_;
348
349   bool flush_input_requested_;
350
351   int accelerated_surface_route_id_;
352
353   // Size to use if we have no backing ContentViewCore
354   gfx::Size default_size_;
355
356   const bool using_synchronous_compositor_;
357
358   scoped_ptr<DelegatedFrameEvictor> frame_evictor_;
359
360   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAndroid);
361 };
362
363 } // namespace content
364
365 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_ANDROID_H_