Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / render_widget_host_view_base.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_BASE_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_
7
8 #if defined(OS_MACOSX)
9 #include <OpenGL/OpenGL.h>
10 #endif
11
12 #include <string>
13 #include <vector>
14
15 #include "base/memory/scoped_ptr.h"
16 #include "base/callback_forward.h"
17 #include "base/process/kill.h"
18 #include "base/timer/timer.h"
19 #include "cc/output/compositor_frame.h"
20 #include "content/browser/renderer_host/event_with_latency_info.h"
21 #include "content/common/content_export.h"
22 #include "content/common/input/input_event_ack_state.h"
23 #include "content/public/browser/render_widget_host_view.h"
24 #include "ipc/ipc_listener.h"
25 #include "third_party/WebKit/public/platform/WebScreenOrientationType.h"
26 #include "third_party/WebKit/public/web/WebPopupType.h"
27 #include "third_party/WebKit/public/web/WebTextDirection.h"
28 #include "ui/base/ime/text_input_mode.h"
29 #include "ui/base/ime/text_input_type.h"
30 #include "ui/gfx/display.h"
31 #include "ui/gfx/native_widget_types.h"
32 #include "ui/gfx/range/range.h"
33 #include "ui/gfx/rect.h"
34 #include "ui/surface/transport_dib.h"
35
36 class SkBitmap;
37
38 struct AccessibilityHostMsg_EventParams;
39 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
40 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
41 struct ViewHostMsg_SelectionBounds_Params;
42 struct ViewHostMsg_TextInputState_Params;
43
44 namespace media {
45 class VideoFrame;
46 }
47
48 namespace blink {
49 struct WebScreenInfo;
50 }
51
52 namespace content {
53 class BrowserAccessibilityDelegate;
54 class BrowserAccessibilityManager;
55 class SyntheticGesture;
56 class SyntheticGestureTarget;
57 class WebCursor;
58 struct DidOverscrollParams;
59 struct NativeWebKeyboardEvent;
60 struct WebPluginGeometry;
61
62 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
63 class CONTENT_EXPORT RenderWidgetHostViewBase : public RenderWidgetHostView,
64                                                 public IPC::Listener {
65  public:
66   virtual ~RenderWidgetHostViewBase();
67
68   // RenderWidgetHostView implementation.
69   virtual void SetBackgroundOpaque(bool opaque) OVERRIDE;
70   virtual bool GetBackgroundOpaque() OVERRIDE;
71   virtual ui::TextInputClient* GetTextInputClient() OVERRIDE;
72   virtual bool IsShowingContextMenu() const OVERRIDE;
73   virtual void SetShowingContextMenu(bool showing_menu) OVERRIDE;
74   virtual base::string16 GetSelectedText() const OVERRIDE;
75   virtual bool IsMouseLocked() OVERRIDE;
76   virtual gfx::Size GetVisibleViewportSize() const OVERRIDE;
77   virtual void SetInsets(const gfx::Insets& insets) OVERRIDE;
78   virtual void BeginFrameSubscription(
79       scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) OVERRIDE;
80   virtual void EndFrameSubscription() OVERRIDE;
81
82   // IPC::Listener implementation:
83   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
84
85   // Called by the host when the input flush has completed.
86   void OnDidFlushInput();
87
88   void SetPopupType(blink::WebPopupType popup_type);
89
90   blink::WebPopupType GetPopupType();
91
92   // Return a value that is incremented each time the renderer swaps a new frame
93   // to the view.
94   uint32 RendererFrameNumber();
95
96   // Called each time the RenderWidgetHost receives a new frame for display from
97   // the renderer.
98   void DidReceiveRendererFrame();
99
100   // Notification that a resize or move session ended on the native widget.
101   void UpdateScreenInfo(gfx::NativeView view);
102
103   // Tells if the display property (work area/scale factor) has
104   // changed since the last time.
105   bool HasDisplayPropertyChanged(gfx::NativeView view);
106
107   base::WeakPtr<RenderWidgetHostViewBase> GetWeakPtr();
108
109   //----------------------------------------------------------------------------
110   // The following methods can be overridden by derived classes.
111
112   // Notifies the View that the renderer text selection has changed.
113   virtual void SelectionChanged(const base::string16& text,
114                                 size_t offset,
115                                 const gfx::Range& range);
116
117   // The requested size of the renderer. May differ from GetViewBounds().size()
118   // when the view requires additional throttling.
119   virtual gfx::Size GetRequestedRendererSize() const;
120
121   // The size of the view's backing surface in non-DPI-adjusted pixels.
122   virtual gfx::Size GetPhysicalBackingSize() const;
123
124   // The height of the physical backing surface that is overdrawn opaquely in
125   // the browser, for example by an on-screen-keyboard (in DPI-adjusted pixels).
126   virtual float GetOverdrawBottomHeight() const;
127
128   // Called prior to forwarding input event messages to the renderer, giving
129   // the view a chance to perform in-process event filtering or processing.
130   // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event|
131   // being forwarded.
132   virtual InputEventAckState FilterInputEvent(
133       const blink::WebInputEvent& input_event);
134
135   // Called by the host when it requires an input flush; the flush call should
136   // by synchronized with BeginFrame.
137   virtual void OnSetNeedsFlushInput();
138
139   virtual void WheelEventAck(const blink::WebMouseWheelEvent& event,
140                              InputEventAckState ack_result);
141
142   virtual void GestureEventAck(const blink::WebGestureEvent& event,
143                                InputEventAckState ack_result);
144
145   // Create a platform specific SyntheticGestureTarget implementation that will
146   // be used to inject synthetic input events.
147   virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget();
148
149   // Return true if frame subscription is supported on this platform.
150   virtual bool CanSubscribeFrame() const;
151
152   // Create a BrowserAccessibilityManager for this view.
153   virtual BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
154       BrowserAccessibilityDelegate* delegate);
155
156   virtual void AccessibilityShowMenu(const gfx::Point& point);
157   virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds);
158   virtual gfx::AcceleratedWidget AccessibilityGetAcceleratedWidget();
159   virtual gfx::NativeViewAccessible AccessibilityGetNativeViewAccessible();
160
161   virtual SkColorType PreferredReadbackFormat();
162
163   // Informs that the focused DOM node has changed.
164   virtual void FocusedNodeChanged(bool is_editable_node) {}
165
166   virtual void OnSwapCompositorFrame(uint32 output_surface_id,
167                                      scoped_ptr<cc::CompositorFrame> frame) {}
168
169   // Because the associated remote WebKit instance can asynchronously
170   // prevent-default on a dispatched touch event, the touch events are queued in
171   // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
172   // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
173   // or ignored (when |ack_result| is CONSUMED).
174   virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
175                                       InputEventAckState ack_result) {}
176
177   virtual void DidOverscroll(const DidOverscrollParams& params) {}
178
179   virtual void DidStopFlinging() {}
180
181   //----------------------------------------------------------------------------
182   // The following static methods are implemented by each platform.
183
184   static void GetDefaultScreenInfo(blink::WebScreenInfo* results);
185
186   //----------------------------------------------------------------------------
187   // The following pure virtual methods are implemented by derived classes.
188
189   // Perform all the initialization steps necessary for this object to represent
190   // a popup (such as a <select> dropdown), then shows the popup at |pos|.
191   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
192                            const gfx::Rect& pos) = 0;
193
194   // Perform all the initialization steps necessary for this object to represent
195   // a full screen window.
196   // |reference_host_view| is the view associated with the creating page that
197   // helps to position the full screen widget on the correct monitor.
198   virtual void InitAsFullscreen(RenderWidgetHostView* reference_host_view) = 0;
199
200   // Notifies the View that it has become visible.
201   virtual void WasShown() = 0;
202
203   // Notifies the View that it has been hidden.
204   virtual void WasHidden() = 0;
205
206   // Moves all plugin windows as described in the given list.
207   // |scroll_offset| is the scroll offset of the render view.
208   virtual void MovePluginWindows(
209       const std::vector<WebPluginGeometry>& moves) = 0;
210
211   // Take focus from the associated View component.
212   virtual void Blur() = 0;
213
214   // Sets the cursor to the one associated with the specified cursor_type
215   virtual void UpdateCursor(const WebCursor& cursor) = 0;
216
217   // Indicates whether the page has finished loading.
218   virtual void SetIsLoading(bool is_loading) = 0;
219
220   // Updates the type of the input method attached to the view.
221   virtual void TextInputStateChanged(
222       const ViewHostMsg_TextInputState_Params& params) = 0;
223
224   // Cancel the ongoing composition of the input method attached to the view.
225   virtual void ImeCancelComposition() = 0;
226
227   // Notifies the View that the renderer has ceased to exist.
228   virtual void RenderProcessGone(base::TerminationStatus status,
229                                  int error_code) = 0;
230
231   // Notifies the View that the renderer's host has ceased to exist.
232   // The default implementation of this is a no-op. This hack exists to fix
233   // a crash on the branch.
234   // TODO(ccameron): Clean this up.
235   // http://crbug.com/404828
236   virtual void RenderWidgetHostGone() {}
237
238   // Tells the View to destroy itself.
239   virtual void Destroy() = 0;
240
241   // Tells the View that the tooltip text for the current mouse position over
242   // the page has changed.
243   virtual void SetTooltipText(const base::string16& tooltip_text) = 0;
244
245   // Notifies the View that the renderer selection bounds has changed.
246   // |start_rect| and |end_rect| are the bounds end of the selection in the
247   // coordinate system of the render view. |start_direction| and |end_direction|
248   // indicates the direction at which the selection was made on touch devices.
249   virtual void SelectionBoundsChanged(
250       const ViewHostMsg_SelectionBounds_Params& params) = 0;
251
252   // Notifies the view that the scroll offset has changed.
253   virtual void ScrollOffsetChanged() = 0;
254
255   // Copies the contents of the compositing surface, providing a new SkBitmap
256   // result via an asynchronously-run |callback|. |src_subrect| is specified in
257   // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
258   // physical for Android), and is the region to be copied from this view. The
259   // copy is then scaled to a SkBitmap of size |dst_size|. |callback| is run
260   // with true on success, false otherwise. A smaller region than |src_subrect|
261   // may be copied if the underlying surface is smaller than |src_subrect|.
262   virtual void CopyFromCompositingSurface(
263       const gfx::Rect& src_subrect,
264       const gfx::Size& dst_size,
265       const base::Callback<void(bool, const SkBitmap&)>& callback,
266       const SkColorType color_type) = 0;
267
268   // Copies the contents of the compositing surface, populating the given
269   // |target| with YV12 image data. |src_subrect| is specified in layer space
270   // coordinates for the current platform (e.g., DIP for Aura/Mac, physical for
271   // Android), and is the region to be copied from this view. The copy is then
272   // scaled and letterboxed with black borders to fit |target|. Finally,
273   // |callback| is asynchronously run with true/false for
274   // success/failure. |target| must point to an allocated, YV12 video frame of
275   // the intended size. This operation will fail if there is no available
276   // compositing surface.
277   virtual void CopyFromCompositingSurfaceToVideoFrame(
278       const gfx::Rect& src_subrect,
279       const scoped_refptr<media::VideoFrame>& target,
280       const base::Callback<void(bool)>& callback) = 0;
281
282   // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
283   // succeed.
284   //
285   // TODO(nick): When VideoFrame copies are broadly implemented, this method
286   // should be renamed to HasCompositingSurface(), or unified with
287   // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
288   virtual bool CanCopyToVideoFrame() const = 0;
289
290   // Called when an accelerated compositing surface is initialized.
291   virtual void AcceleratedSurfaceInitialized(int host_id, int route_id) = 0;
292   // |params.window| and |params.surface_id| indicate which accelerated
293   // surface's buffers swapped. |params.renderer_id| and |params.route_id|
294   // are used to formulate a reply to the GPU process to prevent it from getting
295   // too far ahead. They may all be zero, in which case no flow control is
296   // enforced; this case is currently used for accelerated plugins.
297   virtual void AcceleratedSurfaceBuffersSwapped(
298       const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params_in_pixel,
299       int gpu_host_id) = 0;
300   // Similar to above, except |params.(x|y|width|height)| define the region
301   // of the surface that changed.
302   virtual void AcceleratedSurfacePostSubBuffer(
303       const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params_in_pixel,
304       int gpu_host_id) = 0;
305
306   // Release the accelerated surface temporarily. It will be recreated on the
307   // next swap buffers or post sub buffer.
308   virtual void AcceleratedSurfaceSuspend() = 0;
309
310   virtual void AcceleratedSurfaceRelease() = 0;
311
312   // Return true if the view has an accelerated surface that contains the last
313   // presented frame for the view. If |desired_size| is non-empty, true is
314   // returned only if the accelerated surface size matches.
315   virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) = 0;
316
317   // Compute the orientation type of the display assuming it is a mobile device.
318   static blink::WebScreenOrientationType GetOrientationTypeForMobile(
319       const gfx::Display& display);
320
321   // Compute the orientation type of the display assuming it is a desktop.
322   static blink::WebScreenOrientationType GetOrientationTypeForDesktop(
323       const gfx::Display& display);
324
325   virtual void GetScreenInfo(blink::WebScreenInfo* results) = 0;
326
327   // Gets the bounds of the window, in screen coordinates.
328   virtual gfx::Rect GetBoundsInRootWindow() = 0;
329
330   virtual gfx::GLSurfaceHandle GetCompositingSurface() = 0;
331
332   virtual void OnTextSurroundingSelectionResponse(const base::string16& content,
333                                                   size_t start_offset,
334                                                   size_t end_offset) {};
335
336 #if defined(OS_ANDROID)
337   virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
338                                        const SkBitmap& zoomed_bitmap) = 0;
339
340   // Instructs the view to not drop the surface even when the view is hidden.
341   virtual void LockCompositingSurface() = 0;
342   virtual void UnlockCompositingSurface() = 0;
343 #endif
344
345 #if defined(OS_MACOSX)
346   // Does any event handling necessary for plugin IME; should be called after
347   // the plugin has already had a chance to process the event. If plugin IME is
348   // not enabled, this is a no-op, so it is always safe to call.
349   // Returns true if the event was handled by IME.
350   virtual bool PostProcessEventForPluginIme(
351       const NativeWebKeyboardEvent& event) = 0;
352 #endif
353
354 #if defined(OS_MACOSX) || defined(USE_AURA)
355   // Updates the range of the marked text in an IME composition.
356   virtual void ImeCompositionRangeChanged(
357       const gfx::Range& range,
358       const std::vector<gfx::Rect>& character_bounds) = 0;
359 #endif
360
361 #if defined(OS_WIN)
362   virtual void SetParentNativeViewAccessible(
363       gfx::NativeViewAccessible accessible_parent) = 0;
364
365   // Returns an HWND that's given as the parent window for windowless Flash to
366   // workaround crbug.com/301548.
367   virtual gfx::NativeViewId GetParentForWindowlessPlugin() const = 0;
368
369   // The callback that DetachPluginsHelper calls for each child window. Call
370   // this directly if you want to do custom filtering on plugin windows first.
371   static void DetachPluginWindowsCallback(HWND window);
372 #endif
373
374  protected:
375   // Interface class only, do not construct.
376   RenderWidgetHostViewBase();
377
378 #if defined(OS_WIN)
379   // Shared implementation of MovePluginWindows for use by win and aura/wina.
380   static void MovePluginWindowsHelper(
381       HWND parent,
382       const std::vector<WebPluginGeometry>& moves);
383
384   static void PaintPluginWindowsHelper(
385       HWND parent,
386       const gfx::Rect& damaged_screen_rect);
387
388   // Needs to be called before the HWND backing the view goes away to avoid
389   // crashes in Windowed plugins.
390   static void DetachPluginsHelper(HWND parent);
391 #endif
392
393   // Whether this view is a popup and what kind of popup it is (select,
394   // autofill...).
395   blink::WebPopupType popup_type_;
396
397   // When false, the background of the web content is not fully opaque.
398   bool background_opaque_;
399
400   // While the mouse is locked, the cursor is hidden from the user. Mouse events
401   // are still generated. However, the position they report is the last known
402   // mouse position just as mouse lock was entered; the movement they report
403   // indicates what the change in position of the mouse would be had it not been
404   // locked.
405   bool mouse_locked_;
406
407   // Whether we are showing a context menu.
408   bool showing_context_menu_;
409
410   // A buffer containing the text inside and around the current selection range.
411   base::string16 selection_text_;
412
413   // The offset of the text stored in |selection_text_| relative to the start of
414   // the web page.
415   size_t selection_text_offset_;
416
417   // The current selection range relative to the start of the web page.
418   gfx::Range selection_range_;
419
420 protected:
421   // The scale factor of the display the renderer is currently on.
422   float current_device_scale_factor_;
423
424   // The orientation of the display the renderer is currently on.
425   gfx::Display::Rotation current_display_rotation_;
426
427   // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
428   // renderer.
429   bool pinch_zoom_enabled_;
430
431  private:
432   void FlushInput();
433
434   gfx::Rect current_display_area_;
435
436   uint32 renderer_frame_number_;
437
438   base::OneShotTimer<RenderWidgetHostViewBase> flush_input_timer_;
439
440   base::WeakPtrFactory<RenderWidgetHostViewBase> weak_factory_;
441
442   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase);
443 };
444
445 }  // namespace content
446
447 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_