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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_
9 #include <OpenGL/OpenGL.h>
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/web/WebPopupType.h"
26 #include "third_party/WebKit/public/web/WebTextDirection.h"
27 #include "ui/base/ime/text_input_mode.h"
28 #include "ui/base/ime/text_input_type.h"
29 #include "ui/gfx/native_widget_types.h"
30 #include "ui/gfx/range/range.h"
31 #include "ui/gfx/rect.h"
32 #include "ui/surface/transport_dib.h"
36 struct AccessibilityHostMsg_EventParams
;
37 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
;
38 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
;
39 struct ViewHostMsg_TextInputState_Params
;
40 struct ViewHostMsg_SelectionBounds_Params
;
51 class BrowserAccessibilityManager
;
52 class SyntheticGesture
;
53 class SyntheticGestureTarget
;
55 struct WebPluginGeometry
;
56 struct NativeWebKeyboardEvent
;
58 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
59 class CONTENT_EXPORT RenderWidgetHostViewBase
: public RenderWidgetHostView
,
60 public IPC::Listener
{
62 virtual ~RenderWidgetHostViewBase();
64 // RenderWidgetHostView implementation.
65 virtual void SetBackground(const SkBitmap
& background
) OVERRIDE
;
66 virtual const SkBitmap
& GetBackground() OVERRIDE
;
67 virtual bool IsShowingContextMenu() const OVERRIDE
;
68 virtual void SetShowingContextMenu(bool showing_menu
) OVERRIDE
;
69 virtual base::string16
GetSelectedText() const OVERRIDE
;
70 virtual bool IsMouseLocked() OVERRIDE
;
71 virtual gfx::Size
GetVisibleViewportSize() const OVERRIDE
;
72 virtual void SetInsets(const gfx::Insets
& insets
) OVERRIDE
;
73 virtual void BeginFrameSubscription(
74 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
) OVERRIDE
;
75 virtual void EndFrameSubscription() OVERRIDE
;
77 // IPC::Listener implementation:
78 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
80 // Called when a mousewheel event was not processed by the renderer.
81 // virtual for testing.
82 virtual void UnhandledWheelEvent(const blink::WebMouseWheelEvent
& event
);
84 // Called by the host when the input flush has completed.
85 void OnDidFlushInput();
87 void SetPopupType(blink::WebPopupType popup_type
);
89 blink::WebPopupType
GetPopupType();
91 // Get the BrowserAccessibilityManager if it exists, may return NULL.
92 BrowserAccessibilityManager
* GetBrowserAccessibilityManager() const;
94 void SetBrowserAccessibilityManager(BrowserAccessibilityManager
* manager
);
96 // Return a value that is incremented each time the renderer swaps a new frame
98 uint32
RendererFrameNumber();
100 // Called each time the RenderWidgetHost receives a new frame for display from
102 void DidReceiveRendererFrame();
104 // Notification that a resize or move session ended on the native widget.
105 void UpdateScreenInfo(gfx::NativeView view
);
107 // Tells if the display property (work area/scale factor) has
108 // changed since the last time.
109 bool HasDisplayPropertyChanged(gfx::NativeView view
);
111 //----------------------------------------------------------------------------
112 // The following methods can be overridden by derived classes.
114 // Notifies the View that the renderer text selection has changed.
115 virtual void SelectionChanged(const base::string16
& text
,
117 const gfx::Range
& range
);
119 // The size of the view's backing surface in non-DPI-adjusted pixels.
120 virtual gfx::Size
GetPhysicalBackingSize() const;
122 // The height of the physical backing surface that is overdrawn opaquely in
123 // the browser, for example by an on-screen-keyboard (in DPI-adjusted pixels).
124 virtual float GetOverdrawBottomHeight() const;
126 // Called prior to forwarding input event messages to the renderer, giving
127 // the view a chance to perform in-process event filtering or processing.
128 // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event|
130 virtual InputEventAckState
FilterInputEvent(
131 const blink::WebInputEvent
& input_event
);
133 // Called by the host when it requires an input flush; the flush call should
134 // by synchronized with BeginFrame.
135 virtual void OnSetNeedsFlushInput();
137 virtual void GestureEventAck(const blink::WebGestureEvent
& event
,
138 InputEventAckState ack_result
);
140 // Create a platform specific SyntheticGestureTarget implementation that will
141 // be used to inject synthetic input events.
142 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget();
144 // Return true if frame subscription is supported on this platform.
145 virtual bool CanSubscribeFrame() const;
147 // Create a BrowserAccessibilityManager for this view if it's possible to
148 // create one and if one doesn't exist already. Some ports may not create
149 // one depending on the current state.
150 virtual void CreateBrowserAccessibilityManagerIfNeeded();
152 virtual void OnAccessibilitySetFocus(int acc_obj_id
);
153 virtual void AccessibilityShowMenu(int acc_obj_id
);
154 virtual gfx::Point
AccessibilityOriginInScreen(const gfx::Rect
& bounds
);
156 virtual SkBitmap::Config
PreferredReadbackFormat();
158 // Informs that the focused DOM node has changed.
159 virtual void FocusedNodeChanged(bool is_editable_node
) {}
161 virtual void OnSwapCompositorFrame(uint32 output_surface_id
,
162 scoped_ptr
<cc::CompositorFrame
> frame
) {}
164 // Because the associated remote WebKit instance can asynchronously
165 // prevent-default on a dispatched touch event, the touch events are queued in
166 // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
167 // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
168 // or ignored (when |ack_result| is CONSUMED).
169 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
170 InputEventAckState ack_result
) {}
172 virtual void DidStopFlinging() {}
174 //----------------------------------------------------------------------------
175 // The following static methods are implemented by each platform.
177 static void GetDefaultScreenInfo(blink::WebScreenInfo
* results
);
179 //----------------------------------------------------------------------------
180 // The following pure virtual methods are implemented by derived classes.
182 // Perform all the initialization steps necessary for this object to represent
183 // a popup (such as a <select> dropdown), then shows the popup at |pos|.
184 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
185 const gfx::Rect
& pos
) = 0;
187 // Perform all the initialization steps necessary for this object to represent
188 // a full screen window.
189 // |reference_host_view| is the view associated with the creating page that
190 // helps to position the full screen widget on the correct monitor.
191 virtual void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) = 0;
193 // Notifies the View that it has become visible.
194 virtual void WasShown() = 0;
196 // Notifies the View that it has been hidden.
197 virtual void WasHidden() = 0;
199 // Moves all plugin windows as described in the given list.
200 // |scroll_offset| is the scroll offset of the render view.
201 virtual void MovePluginWindows(
202 const std::vector
<WebPluginGeometry
>& moves
) = 0;
204 // Take focus from the associated View component.
205 virtual void Blur() = 0;
207 // Sets the cursor to the one associated with the specified cursor_type
208 virtual void UpdateCursor(const WebCursor
& cursor
) = 0;
210 // Indicates whether the page has finished loading.
211 virtual void SetIsLoading(bool is_loading
) = 0;
213 // Updates the type of the input method attached to the view.
214 virtual void TextInputTypeChanged(ui::TextInputType type
,
215 ui::TextInputMode mode
,
216 bool can_compose_inline
) = 0;
218 // Cancel the ongoing composition of the input method attached to the view.
219 virtual void ImeCancelComposition() = 0;
221 // Notifies the View that the renderer has ceased to exist.
222 virtual void RenderProcessGone(base::TerminationStatus status
,
225 // Tells the View to destroy itself.
226 virtual void Destroy() = 0;
228 // Tells the View that the tooltip text for the current mouse position over
229 // the page has changed.
230 virtual void SetTooltipText(const base::string16
& tooltip_text
) = 0;
232 // Notifies the View that the renderer selection bounds has changed.
233 // |start_rect| and |end_rect| are the bounds end of the selection in the
234 // coordinate system of the render view. |start_direction| and |end_direction|
235 // indicates the direction at which the selection was made on touch devices.
236 virtual void SelectionBoundsChanged(
237 const ViewHostMsg_SelectionBounds_Params
& params
) = 0;
239 // Notifies the view that the scroll offset has changed.
240 virtual void ScrollOffsetChanged() = 0;
242 // Copies the contents of the compositing surface into the given
243 // (uninitialized) PlatformCanvas if any.
244 // The rectangle region specified with |src_subrect| is copied from the
245 // contents, scaled to |dst_size|, and written to |output|.
246 // |callback| is invoked with true on success, false otherwise. |output| can
247 // be initialized even on failure.
248 // A smaller region than |src_subrect| may be copied if the underlying surface
249 // is smaller than |src_subrect|.
250 // NOTE: |callback| is called asynchronously.
251 virtual void CopyFromCompositingSurface(
252 const gfx::Rect
& src_subrect
,
253 const gfx::Size
& dst_size
,
254 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
255 const SkBitmap::Config config
) = 0;
257 // Copies a given subset of the compositing surface's content into a YV12
258 // VideoFrame, and invokes a callback with a success/fail parameter. |target|
259 // must contain an allocated, YV12 video frame of the intended size. If the
260 // copy rectangle does not match |target|'s size, the copied content will be
261 // scaled and letterboxed with black borders. The copy will happen
262 // asynchronously. This operation will fail if there is no available
263 // compositing surface.
264 virtual void CopyFromCompositingSurfaceToVideoFrame(
265 const gfx::Rect
& src_subrect
,
266 const scoped_refptr
<media::VideoFrame
>& target
,
267 const base::Callback
<void(bool)>& callback
) = 0;
269 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
272 // TODO(nick): When VideoFrame copies are broadly implemented, this method
273 // should be renamed to HasCompositingSurface(), or unified with
274 // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
275 virtual bool CanCopyToVideoFrame() const = 0;
277 // Called when accelerated compositing state changes.
278 virtual void OnAcceleratedCompositingStateChange() = 0;
279 // Called when an accelerated compositing surface is initialized.
280 virtual void AcceleratedSurfaceInitialized(int host_id
, int route_id
) = 0;
281 // |params.window| and |params.surface_id| indicate which accelerated
282 // surface's buffers swapped. |params.renderer_id| and |params.route_id|
283 // are used to formulate a reply to the GPU process to prevent it from getting
284 // too far ahead. They may all be zero, in which case no flow control is
285 // enforced; this case is currently used for accelerated plugins.
286 virtual void AcceleratedSurfaceBuffersSwapped(
287 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params_in_pixel
,
288 int gpu_host_id
) = 0;
289 // Similar to above, except |params.(x|y|width|height)| define the region
290 // of the surface that changed.
291 virtual void AcceleratedSurfacePostSubBuffer(
292 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params_in_pixel
,
293 int gpu_host_id
) = 0;
295 // Release the accelerated surface temporarily. It will be recreated on the
296 // next swap buffers or post sub buffer.
297 virtual void AcceleratedSurfaceSuspend() = 0;
299 virtual void AcceleratedSurfaceRelease() = 0;
301 // Return true if the view has an accelerated surface that contains the last
302 // presented frame for the view. If |desired_size| is non-empty, true is
303 // returned only if the accelerated surface size matches.
304 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) = 0;
306 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) = 0;
308 // Gets the bounds of the window, in screen coordinates.
309 virtual gfx::Rect
GetBoundsInRootWindow() = 0;
311 virtual gfx::GLSurfaceHandle
GetCompositingSurface() = 0;
313 virtual void SetScrollOffsetPinning(
314 bool is_pinned_to_left
, bool is_pinned_to_right
) = 0;
316 #if defined(OS_ANDROID)
317 virtual void ShowDisambiguationPopup(const gfx::Rect
& target_rect
,
318 const SkBitmap
& zoomed_bitmap
) = 0;
320 // Notifies the View that the renderer selection root bounds has changed.
321 virtual void SelectionRootBoundsChanged(const gfx::Rect
& bounds
) = 0;
323 // Instructs the view to not drop the surface even when the view is hidden.
324 virtual void LockCompositingSurface() = 0;
325 virtual void UnlockCompositingSurface() = 0;
328 #if defined(OS_MACOSX)
329 // Does any event handling necessary for plugin IME; should be called after
330 // the plugin has already had a chance to process the event. If plugin IME is
331 // not enabled, this is a no-op, so it is always safe to call.
332 // Returns true if the event was handled by IME.
333 virtual bool PostProcessEventForPluginIme(
334 const NativeWebKeyboardEvent
& event
) = 0;
337 #if defined(OS_MACOSX) || defined(USE_AURA)
338 // Updates the range of the marked text in an IME composition.
339 virtual void ImeCompositionRangeChanged(
340 const gfx::Range
& range
,
341 const std::vector
<gfx::Rect
>& character_bounds
) = 0;
345 virtual void SetParentNativeViewAccessible(
346 gfx::NativeViewAccessible accessible_parent
) = 0;
348 // Returns an HWND that's given as the parent window for windowless Flash to
349 // workaround crbug.com/301548.
350 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const = 0;
352 // The callback that DetachPluginsHelper calls for each child window. Call
353 // this directly if you want to do custom filtering on plugin windows first.
354 static void DetachPluginWindowsCallback(HWND window
);
358 // Interface class only, do not construct.
359 RenderWidgetHostViewBase();
362 // Shared implementation of MovePluginWindows for use by win and aura/wina.
363 static void MovePluginWindowsHelper(
365 const std::vector
<WebPluginGeometry
>& moves
);
367 static void PaintPluginWindowsHelper(
369 const gfx::Rect
& damaged_screen_rect
);
371 // Needs to be called before the HWND backing the view goes away to avoid
372 // crashes in Windowed plugins.
373 static void DetachPluginsHelper(HWND parent
);
376 // Whether this view is a popup and what kind of popup it is (select,
378 blink::WebPopupType popup_type_
;
380 // A custom background to paint behind the web content. This will be tiled
381 // horizontally. Can be null, in which case we fall back to painting white.
382 SkBitmap background_
;
384 // While the mouse is locked, the cursor is hidden from the user. Mouse events
385 // are still generated. However, the position they report is the last known
386 // mouse position just as mouse lock was entered; the movement they report
387 // indicates what the change in position of the mouse would be had it not been
391 // Whether we are showing a context menu.
392 bool showing_context_menu_
;
394 // A buffer containing the text inside and around the current selection range.
395 base::string16 selection_text_
;
397 // The offset of the text stored in |selection_text_| relative to the start of
399 size_t selection_text_offset_
;
401 // The current selection range relative to the start of the web page.
402 gfx::Range selection_range_
;
405 // The scale factor of the display the renderer is currently on.
406 float current_device_scale_factor_
;
408 // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
410 bool pinch_zoom_enabled_
;
415 // Manager of the tree representation of the WebKit render tree.
416 scoped_ptr
<BrowserAccessibilityManager
> browser_accessibility_manager_
;
418 gfx::Rect current_display_area_
;
420 uint32 renderer_frame_number_
;
422 base::OneShotTimer
<RenderWidgetHostViewBase
> flush_input_timer_
;
424 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase
);
427 } // namespace content
429 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_