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_
11 #include "base/callback_forward.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/process/kill.h"
14 #include "base/timer/timer.h"
15 #include "cc/output/compositor_frame.h"
16 #include "content/browser/renderer_host/event_with_latency_info.h"
17 #include "content/common/content_export.h"
18 #include "content/common/input/input_event_ack_state.h"
19 #include "content/public/browser/readback_types.h"
20 #include "content/public/browser/render_widget_host_view.h"
21 #include "ipc/ipc_listener.h"
22 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScreenOrientationType.h"
23 #include "third_party/WebKit/public/web/WebPopupType.h"
24 #include "third_party/WebKit/public/web/WebTextDirection.h"
25 #include "ui/base/ime/text_input_mode.h"
26 #include "ui/base/ime/text_input_type.h"
27 #include "ui/gfx/display.h"
28 #include "ui/gfx/geometry/rect.h"
29 #include "ui/gfx/native_widget_types.h"
30 #include "ui/gfx/range/range.h"
31 #include "ui/surface/transport_dib.h"
35 struct AccessibilityHostMsg_EventParams
;
36 struct ViewHostMsg_SelectionBounds_Params
;
37 struct ViewHostMsg_TextInputState_Params
;
46 class WebMouseWheelEvent
;
50 class BrowserAccessibilityDelegate
;
51 class BrowserAccessibilityManager
;
52 class SyntheticGesture
;
53 class SyntheticGestureTarget
;
55 struct DidOverscrollParams
;
56 struct NativeWebKeyboardEvent
;
57 struct WebPluginGeometry
;
59 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
60 class CONTENT_EXPORT RenderWidgetHostViewBase
: public RenderWidgetHostView
,
61 public IPC::Listener
{
63 ~RenderWidgetHostViewBase() override
;
65 // RenderWidgetHostView implementation.
66 void SetBackgroundColor(SkColor color
) override
;
67 void SetBackgroundColorToDefault() final
;
68 bool GetBackgroundOpaque() override
;
69 ui::TextInputClient
* GetTextInputClient() override
;
70 void WasUnOccluded() override
{}
71 void WasOccluded() override
{}
72 bool IsShowingContextMenu() const override
;
73 void SetShowingContextMenu(bool showing_menu
) override
;
74 base::string16
GetSelectedText() const override
;
75 bool IsMouseLocked() override
;
76 gfx::Size
GetVisibleViewportSize() const override
;
77 void SetInsets(const gfx::Insets
& insets
) override
;
78 void BeginFrameSubscription(
79 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
) override
;
80 void EndFrameSubscription() override
;
82 // IPC::Listener implementation:
83 bool OnMessageReceived(const IPC::Message
& msg
) override
;
85 void SetPopupType(blink::WebPopupType popup_type
);
87 blink::WebPopupType
GetPopupType();
89 // Return a value that is incremented each time the renderer swaps a new frame
91 uint32
RendererFrameNumber();
93 // Called each time the RenderWidgetHost receives a new frame for display from
95 void DidReceiveRendererFrame();
97 // Notification that a resize or move session ended on the native widget.
98 void UpdateScreenInfo(gfx::NativeView view
);
100 // Tells if the display property (work area/scale factor) has
101 // changed since the last time.
102 bool HasDisplayPropertyChanged(gfx::NativeView view
);
104 base::WeakPtr
<RenderWidgetHostViewBase
> GetWeakPtr();
106 //----------------------------------------------------------------------------
107 // The following methods can be overridden by derived classes.
109 // Notifies the View that the renderer text selection has changed.
110 virtual void SelectionChanged(const base::string16
& text
,
112 const gfx::Range
& range
);
114 // The requested size of the renderer. May differ from GetViewBounds().size()
115 // when the view requires additional throttling.
116 virtual gfx::Size
GetRequestedRendererSize() const;
118 // The size of the view's backing surface in non-DPI-adjusted pixels.
119 virtual gfx::Size
GetPhysicalBackingSize() const;
121 // Whether or not Blink's viewport size should be shrunk by the height of the
123 virtual bool DoTopControlsShrinkBlinkSize() const;
125 // The height of the URL-bar top controls.
126 virtual float GetTopControlsHeight() const;
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|
132 virtual InputEventAckState
FilterInputEvent(
133 const blink::WebInputEvent
& input_event
);
135 // Called by the host when it requires an input flush; the flush call should
136 // by synchronized with BeginFrame.
137 virtual void OnSetNeedsFlushInput();
139 virtual void WheelEventAck(const blink::WebMouseWheelEvent
& event
,
140 InputEventAckState ack_result
);
142 virtual void GestureEventAck(const blink::WebGestureEvent
& event
,
143 InputEventAckState ack_result
);
145 // Create a platform specific SyntheticGestureTarget implementation that will
146 // be used to inject synthetic input events.
147 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget();
149 // Return true if frame subscription is supported on this platform.
150 virtual bool CanSubscribeFrame() const;
152 // Create a BrowserAccessibilityManager for this view.
153 virtual BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
154 BrowserAccessibilityDelegate
* delegate
);
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();
161 // Informs that the focused DOM node has changed.
162 virtual void FocusedNodeChanged(bool is_editable_node
) {}
164 virtual void OnSwapCompositorFrame(uint32 output_surface_id
,
165 scoped_ptr
<cc::CompositorFrame
> frame
) {}
167 // This method exists to allow removing of displayed graphics, after a new
168 // page has been loaded, to prevent the displayed URL from being out of sync
169 // with what is visible on screen.
170 virtual void ClearCompositorFrame() = 0;
172 // Because the associated remote WebKit instance can asynchronously
173 // prevent-default on a dispatched touch event, the touch events are queued in
174 // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
175 // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
176 // or ignored (when |ack_result| is CONSUMED).
177 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
178 InputEventAckState ack_result
) {}
180 virtual void DidOverscroll(const DidOverscrollParams
& params
) {}
182 virtual void DidStopFlinging() {}
184 // Returns the compositing surface ID namespace, or 0 if Surfaces are not
186 virtual uint32_t GetSurfaceIdNamespace();
188 // When there are multiple RenderWidgetHostViews for a single page, input
189 // events need to be targeted to the correct one for handling. The following
190 // methods are invoked on the RenderWidgetHostView that should be able to
191 // properly handle the event (i.e. it has focus for keyboard events, or has
192 // been identified by hit testing mouse, touch or gesture events).
193 virtual uint32_t SurfaceIdNamespaceAtPoint(const gfx::Point
& point
,
194 gfx::Point
* transformed_point
);
195 virtual void ProcessKeyboardEvent(const NativeWebKeyboardEvent
& event
) {}
196 virtual void ProcessMouseEvent(const blink::WebMouseEvent
& event
) {}
197 virtual void ProcessMouseWheelEvent(const blink::WebMouseWheelEvent
& event
) {}
199 //----------------------------------------------------------------------------
200 // The following static methods are implemented by each platform.
202 static void GetDefaultScreenInfo(blink::WebScreenInfo
* results
);
204 //----------------------------------------------------------------------------
205 // The following pure virtual methods are implemented by derived classes.
207 // Perform all the initialization steps necessary for this object to represent
208 // a popup (such as a <select> dropdown), then shows the popup at |pos|.
209 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
210 const gfx::Rect
& bounds
) = 0;
212 // Perform all the initialization steps necessary for this object to represent
213 // a full screen window.
214 // |reference_host_view| is the view associated with the creating page that
215 // helps to position the full screen widget on the correct monitor.
216 virtual void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) = 0;
218 // Moves all plugin windows as described in the given list.
219 // |scroll_offset| is the scroll offset of the render view.
220 virtual void MovePluginWindows(
221 const std::vector
<WebPluginGeometry
>& moves
) = 0;
223 // Sets the cursor to the one associated with the specified cursor_type
224 virtual void UpdateCursor(const WebCursor
& cursor
) = 0;
226 // Indicates whether the page has finished loading.
227 virtual void SetIsLoading(bool is_loading
) = 0;
229 // Updates the state of the input method attached to the view.
230 virtual void TextInputStateChanged(
231 const ViewHostMsg_TextInputState_Params
& params
) = 0;
233 // Cancel the ongoing composition of the input method attached to the view.
234 virtual void ImeCancelComposition() = 0;
236 // Notifies the View that the renderer has ceased to exist.
237 virtual void RenderProcessGone(base::TerminationStatus status
,
240 // Notifies the View that the renderer's host has ceased to exist.
241 // The default implementation of this is a no-op. This hack exists to fix
242 // a crash on the branch.
243 // TODO(ccameron): Clean this up.
244 // http://crbug.com/404828
245 virtual void RenderWidgetHostGone() {}
247 // Tells the View to destroy itself.
248 virtual void Destroy() = 0;
250 // Tells the View that the tooltip text for the current mouse position over
251 // the page has changed.
252 virtual void SetTooltipText(const base::string16
& tooltip_text
) = 0;
254 // Notifies the View that the renderer selection bounds has changed.
255 // |start_rect| and |end_rect| are the bounds end of the selection in the
256 // coordinate system of the render view. |start_direction| and |end_direction|
257 // indicates the direction at which the selection was made on touch devices.
258 virtual void SelectionBoundsChanged(
259 const ViewHostMsg_SelectionBounds_Params
& params
) = 0;
261 // Copies the contents of the compositing surface, providing a new SkBitmap
262 // result via an asynchronously-run |callback|. |src_subrect| is specified in
263 // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
264 // physical for Android), and is the region to be copied from this view. When
265 // |src_subrect| is empty then the whole surface will be copied. The copy is
266 // then scaled to a SkBitmap of size |dst_size|. If |dst_size| is empty then
267 // output will be unscaled. |callback| is run with true on success,
268 // false otherwise. A smaller region than |src_subrect| may be copied
269 // if the underlying surface is smaller than |src_subrect|.
270 virtual void CopyFromCompositingSurface(
271 const gfx::Rect
& src_subrect
,
272 const gfx::Size
& dst_size
,
273 ReadbackRequestCallback
& callback
,
274 const SkColorType preferred_color_type
) = 0;
276 // Copies the contents of the compositing surface, populating the given
277 // |target| with YV12 image data. |src_subrect| is specified in layer space
278 // coordinates for the current platform (e.g., DIP for Aura/Mac, physical for
279 // Android), and is the region to be copied from this view. The copy is then
280 // scaled and letterboxed with black borders to fit |target|. Finally,
281 // |callback| is asynchronously run with true/false for
282 // success/failure. |target| must point to an allocated, YV12 video frame of
283 // the intended size. This operation will fail if there is no available
284 // compositing surface.
285 virtual void CopyFromCompositingSurfaceToVideoFrame(
286 const gfx::Rect
& src_subrect
,
287 const scoped_refptr
<media::VideoFrame
>& target
,
288 const base::Callback
<void(bool)>& callback
) = 0;
290 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
293 // TODO(nick): When VideoFrame copies are broadly implemented, this method
294 // should be renamed to HasCompositingSurface(), or unified with
295 // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
296 virtual bool CanCopyToVideoFrame() const = 0;
298 // DEPRECATED. Called when an accelerated compositing surface is initialized.
299 virtual void AcceleratedSurfaceInitialized(int route_id
) {}
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 // Compute the orientation type of the display assuming it is a mobile device.
307 static blink::WebScreenOrientationType
GetOrientationTypeForMobile(
308 const gfx::Display
& display
);
310 // Compute the orientation type of the display assuming it is a desktop.
311 static blink::WebScreenOrientationType
GetOrientationTypeForDesktop(
312 const gfx::Display
& display
);
314 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) = 0;
315 virtual bool GetScreenColorProfile(std::vector
<char>* color_profile
) = 0;
317 // Gets the bounds of the window, in screen coordinates.
318 virtual gfx::Rect
GetBoundsInRootWindow() = 0;
320 virtual gfx::GLSurfaceHandle
GetCompositingSurface() = 0;
322 // Called by the RenderFrameHost when it receives an IPC response to a
323 // TextSurroundingSelectionRequest.
324 virtual void OnTextSurroundingSelectionResponse(const base::string16
& content
,
328 // Called by the RenderWidgetHost when an ambiguous gesture is detected to
329 // show the disambiguation popup bubble.
330 virtual void ShowDisambiguationPopup(const gfx::Rect
& rect_pixels
,
331 const SkBitmap
& zoomed_bitmap
);
333 // Called by the WebContentsImpl when a user tries to navigate a new page on
335 virtual void OnDidNavigateMainFrameToNewPage();
337 #if defined(OS_ANDROID)
338 // Instructs the view to not drop the surface even when the view is hidden.
339 virtual void LockCompositingSurface() = 0;
340 virtual void UnlockCompositingSurface() = 0;
343 #if defined(OS_MACOSX)
344 // Does any event handling necessary for plugin IME; should be called after
345 // the plugin has already had a chance to process the event. If plugin IME is
346 // not enabled, this is a no-op, so it is always safe to call.
347 // Returns true if the event was handled by IME.
348 virtual bool PostProcessEventForPluginIme(
349 const NativeWebKeyboardEvent
& event
) = 0;
352 // Updates the range of the marked text in an IME composition.
353 virtual void ImeCompositionRangeChanged(
354 const gfx::Range
& range
,
355 const std::vector
<gfx::Rect
>& character_bounds
) = 0;
358 virtual void SetParentNativeViewAccessible(
359 gfx::NativeViewAccessible accessible_parent
) = 0;
361 // Returns an HWND that's given as the parent window for windowless Flash to
362 // workaround crbug.com/301548.
363 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const = 0;
365 // The callback that DetachPluginsHelper calls for each child window. Call
366 // this directly if you want to do custom filtering on plugin windows first.
367 static void DetachPluginWindowsCallback(HWND window
);
371 // Interface class only, do not construct.
372 RenderWidgetHostViewBase();
375 // Shared implementation of MovePluginWindows for use by win and aura/wina.
376 static void MovePluginWindowsHelper(
378 const std::vector
<WebPluginGeometry
>& moves
);
380 static void PaintPluginWindowsHelper(
382 const gfx::Rect
& damaged_screen_rect
);
384 // Needs to be called before the HWND backing the view goes away to avoid
385 // crashes in Windowed plugins.
386 static void DetachPluginsHelper(HWND parent
);
389 // Whether this view is a popup and what kind of popup it is (select,
391 blink::WebPopupType popup_type_
;
393 // The background color of the web content.
394 SkColor background_color_
;
396 // While the mouse is locked, the cursor is hidden from the user. Mouse events
397 // are still generated. However, the position they report is the last known
398 // mouse position just as mouse lock was entered; the movement they report
399 // indicates what the change in position of the mouse would be had it not been
403 // Whether we are showing a context menu.
404 bool showing_context_menu_
;
406 // A buffer containing the text inside and around the current selection range.
407 base::string16 selection_text_
;
409 // The offset of the text stored in |selection_text_| relative to the start of
411 size_t selection_text_offset_
;
413 // The current selection range relative to the start of the web page.
414 gfx::Range selection_range_
;
417 // The scale factor of the display the renderer is currently on.
418 float current_device_scale_factor_
;
420 // The orientation of the display the renderer is currently on.
421 gfx::Display::Rotation current_display_rotation_
;
423 // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
425 bool pinch_zoom_enabled_
;
430 gfx::Rect current_display_area_
;
432 uint32 renderer_frame_number_
;
434 base::OneShotTimer
<RenderWidgetHostViewBase
> flush_input_timer_
;
436 base::WeakPtrFactory
<RenderWidgetHostViewBase
> weak_factory_
;
438 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase
);
441 } // namespace content
443 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_