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
;
47 class BrowserAccessibilityDelegate
;
48 class BrowserAccessibilityManager
;
49 class SyntheticGesture
;
50 class SyntheticGestureTarget
;
52 struct DidOverscrollParams
;
53 struct NativeWebKeyboardEvent
;
54 struct WebPluginGeometry
;
56 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
57 class CONTENT_EXPORT RenderWidgetHostViewBase
: public RenderWidgetHostView
,
58 public IPC::Listener
{
60 ~RenderWidgetHostViewBase() override
;
62 // RenderWidgetHostView implementation.
63 void SetBackgroundColor(SkColor color
) override
;
64 void SetBackgroundColorToDefault() final
;
65 bool GetBackgroundOpaque() override
;
66 ui::TextInputClient
* GetTextInputClient() override
;
67 void WasUnOccluded() override
{}
68 void WasOccluded() override
{}
69 bool IsShowingContextMenu() const override
;
70 void SetShowingContextMenu(bool showing_menu
) override
;
71 base::string16
GetSelectedText() const override
;
72 bool IsMouseLocked() override
;
73 gfx::Size
GetVisibleViewportSize() const override
;
74 void SetInsets(const gfx::Insets
& insets
) override
;
75 void BeginFrameSubscription(
76 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
) override
;
77 void EndFrameSubscription() override
;
79 // IPC::Listener implementation:
80 bool OnMessageReceived(const IPC::Message
& msg
) override
;
82 void SetPopupType(blink::WebPopupType popup_type
);
84 blink::WebPopupType
GetPopupType();
86 // Return a value that is incremented each time the renderer swaps a new frame
88 uint32
RendererFrameNumber();
90 // Called each time the RenderWidgetHost receives a new frame for display from
92 void DidReceiveRendererFrame();
94 // Notification that a resize or move session ended on the native widget.
95 void UpdateScreenInfo(gfx::NativeView view
);
97 // Tells if the display property (work area/scale factor) has
98 // changed since the last time.
99 bool HasDisplayPropertyChanged(gfx::NativeView view
);
101 base::WeakPtr
<RenderWidgetHostViewBase
> GetWeakPtr();
103 //----------------------------------------------------------------------------
104 // The following methods can be overridden by derived classes.
106 // Notifies the View that the renderer text selection has changed.
107 virtual void SelectionChanged(const base::string16
& text
,
109 const gfx::Range
& range
);
111 // The requested size of the renderer. May differ from GetViewBounds().size()
112 // when the view requires additional throttling.
113 virtual gfx::Size
GetRequestedRendererSize() const;
115 // The size of the view's backing surface in non-DPI-adjusted pixels.
116 virtual gfx::Size
GetPhysicalBackingSize() const;
118 // Whether or not Blink's viewport size should be shrunk by the height of the
120 virtual bool DoTopControlsShrinkBlinkSize() const;
122 // The height of the URL-bar top controls.
123 virtual float GetTopControlsHeight() const;
125 // Called prior to forwarding input event messages to the renderer, giving
126 // the view a chance to perform in-process event filtering or processing.
127 // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event|
129 virtual InputEventAckState
FilterInputEvent(
130 const blink::WebInputEvent
& input_event
);
132 // Called by the host when it requires an input flush; the flush call should
133 // by synchronized with BeginFrame.
134 virtual void OnSetNeedsFlushInput();
136 virtual void WheelEventAck(const blink::WebMouseWheelEvent
& event
,
137 InputEventAckState ack_result
);
139 virtual void GestureEventAck(const blink::WebGestureEvent
& event
,
140 InputEventAckState ack_result
);
142 // Create a platform specific SyntheticGestureTarget implementation that will
143 // be used to inject synthetic input events.
144 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget();
146 // Return true if frame subscription is supported on this platform.
147 virtual bool CanSubscribeFrame() const;
149 // Create a BrowserAccessibilityManager for this view.
150 virtual BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
151 BrowserAccessibilityDelegate
* delegate
);
153 virtual void AccessibilityShowMenu(const gfx::Point
& point
);
154 virtual gfx::Point
AccessibilityOriginInScreen(const gfx::Rect
& bounds
);
155 virtual gfx::AcceleratedWidget
AccessibilityGetAcceleratedWidget();
156 virtual gfx::NativeViewAccessible
AccessibilityGetNativeViewAccessible();
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 DidOverscroll(const DidOverscrollParams
& params
) {}
174 virtual void DidStopFlinging() {}
176 // Returns the compositing surface ID namespace, or 0 if Surfaces are not
178 virtual uint32_t GetSurfaceIdNamespace();
180 //----------------------------------------------------------------------------
181 // The following static methods are implemented by each platform.
183 static void GetDefaultScreenInfo(blink::WebScreenInfo
* results
);
185 //----------------------------------------------------------------------------
186 // The following pure virtual methods are implemented by derived classes.
188 // Perform all the initialization steps necessary for this object to represent
189 // a popup (such as a <select> dropdown), then shows the popup at |pos|.
190 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
191 const gfx::Rect
& bounds
) = 0;
193 // Perform all the initialization steps necessary for this object to represent
194 // a full screen window.
195 // |reference_host_view| is the view associated with the creating page that
196 // helps to position the full screen widget on the correct monitor.
197 virtual void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) = 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 // Sets the cursor to the one associated with the specified cursor_type
205 virtual void UpdateCursor(const WebCursor
& cursor
) = 0;
207 // Indicates whether the page has finished loading.
208 virtual void SetIsLoading(bool is_loading
) = 0;
210 // Updates the type of the input method attached to the view.
211 virtual void TextInputTypeChanged(ui::TextInputType type
,
212 ui::TextInputMode mode
,
213 bool can_compose_inline
,
216 // Cancel the ongoing composition of the input method attached to the view.
217 virtual void ImeCancelComposition() = 0;
219 // Notifies the View that the renderer has ceased to exist.
220 virtual void RenderProcessGone(base::TerminationStatus status
,
223 // Notifies the View that the renderer's host has ceased to exist.
224 // The default implementation of this is a no-op. This hack exists to fix
225 // a crash on the branch.
226 // TODO(ccameron): Clean this up.
227 // http://crbug.com/404828
228 virtual void RenderWidgetHostGone() {}
230 // Tells the View to destroy itself.
231 virtual void Destroy() = 0;
233 // Tells the View that the tooltip text for the current mouse position over
234 // the page has changed.
235 virtual void SetTooltipText(const base::string16
& tooltip_text
) = 0;
237 // Notifies the View that the renderer selection bounds has changed.
238 // |start_rect| and |end_rect| are the bounds end of the selection in the
239 // coordinate system of the render view. |start_direction| and |end_direction|
240 // indicates the direction at which the selection was made on touch devices.
241 virtual void SelectionBoundsChanged(
242 const ViewHostMsg_SelectionBounds_Params
& params
) = 0;
244 // Copies the contents of the compositing surface, providing a new SkBitmap
245 // result via an asynchronously-run |callback|. |src_subrect| is specified in
246 // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
247 // physical for Android), and is the region to be copied from this view. When
248 // |src_subrect| is empty then the whole surface will be copied. The copy is
249 // then scaled to a SkBitmap of size |dst_size|. If |dst_size| is empty then
250 // output will be unscaled. |callback| is run with true on success,
251 // false otherwise. A smaller region than |src_subrect| may be copied
252 // if the underlying surface is smaller than |src_subrect|.
253 virtual void CopyFromCompositingSurface(
254 const gfx::Rect
& src_subrect
,
255 const gfx::Size
& dst_size
,
256 ReadbackRequestCallback
& callback
,
257 const SkColorType preferred_color_type
) = 0;
259 // Copies the contents of the compositing surface, populating the given
260 // |target| with YV12 image data. |src_subrect| is specified in layer space
261 // coordinates for the current platform (e.g., DIP for Aura/Mac, physical for
262 // Android), and is the region to be copied from this view. The copy is then
263 // scaled and letterboxed with black borders to fit |target|. Finally,
264 // |callback| is asynchronously run with true/false for
265 // success/failure. |target| must point to an allocated, YV12 video frame of
266 // the intended size. This operation will fail if there is no available
267 // compositing surface.
268 virtual void CopyFromCompositingSurfaceToVideoFrame(
269 const gfx::Rect
& src_subrect
,
270 const scoped_refptr
<media::VideoFrame
>& target
,
271 const base::Callback
<void(bool)>& callback
) = 0;
273 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
276 // TODO(nick): When VideoFrame copies are broadly implemented, this method
277 // should be renamed to HasCompositingSurface(), or unified with
278 // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
279 virtual bool CanCopyToVideoFrame() const = 0;
281 // DEPRECATED. Called when an accelerated compositing surface is initialized.
282 virtual void AcceleratedSurfaceInitialized(int route_id
) {}
284 // Return true if the view has an accelerated surface that contains the last
285 // presented frame for the view. If |desired_size| is non-empty, true is
286 // returned only if the accelerated surface size matches.
287 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) = 0;
289 // Compute the orientation type of the display assuming it is a mobile device.
290 static blink::WebScreenOrientationType
GetOrientationTypeForMobile(
291 const gfx::Display
& display
);
293 // Compute the orientation type of the display assuming it is a desktop.
294 static blink::WebScreenOrientationType
GetOrientationTypeForDesktop(
295 const gfx::Display
& display
);
297 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) = 0;
299 // Gets the bounds of the window, in screen coordinates.
300 virtual gfx::Rect
GetBoundsInRootWindow() = 0;
302 virtual gfx::GLSurfaceHandle
GetCompositingSurface() = 0;
304 // Called by the RenderFrameHost when it receives an IPC response to a
305 // TextSurroundingSelectionRequest.
306 virtual void OnTextSurroundingSelectionResponse(const base::string16
& content
,
310 // Called by the RenderWidgetHost when an ambiguous gesture is detected to
311 // show the disambiguation popup bubble.
312 virtual void ShowDisambiguationPopup(const gfx::Rect
& rect_pixels
,
313 const SkBitmap
& zoomed_bitmap
);
315 // Called by the WebContentsImpl when a user tries to navigate a new page on
317 virtual void OnDidNavigateMainFrameToNewPage();
319 #if defined(OS_ANDROID)
320 // Instructs the view to not drop the surface even when the view is hidden.
321 virtual void LockCompositingSurface() = 0;
322 virtual void UnlockCompositingSurface() = 0;
325 #if defined(OS_MACOSX)
326 // Does any event handling necessary for plugin IME; should be called after
327 // the plugin has already had a chance to process the event. If plugin IME is
328 // not enabled, this is a no-op, so it is always safe to call.
329 // Returns true if the event was handled by IME.
330 virtual bool PostProcessEventForPluginIme(
331 const NativeWebKeyboardEvent
& event
) = 0;
334 // Updates the range of the marked text in an IME composition.
335 virtual void ImeCompositionRangeChanged(
336 const gfx::Range
& range
,
337 const std::vector
<gfx::Rect
>& character_bounds
) = 0;
340 virtual void SetParentNativeViewAccessible(
341 gfx::NativeViewAccessible accessible_parent
) = 0;
343 // Returns an HWND that's given as the parent window for windowless Flash to
344 // workaround crbug.com/301548.
345 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const = 0;
347 // The callback that DetachPluginsHelper calls for each child window. Call
348 // this directly if you want to do custom filtering on plugin windows first.
349 static void DetachPluginWindowsCallback(HWND window
);
353 // Interface class only, do not construct.
354 RenderWidgetHostViewBase();
357 // Shared implementation of MovePluginWindows for use by win and aura/wina.
358 static void MovePluginWindowsHelper(
360 const std::vector
<WebPluginGeometry
>& moves
);
362 static void PaintPluginWindowsHelper(
364 const gfx::Rect
& damaged_screen_rect
);
366 // Needs to be called before the HWND backing the view goes away to avoid
367 // crashes in Windowed plugins.
368 static void DetachPluginsHelper(HWND parent
);
371 // Whether this view is a popup and what kind of popup it is (select,
373 blink::WebPopupType popup_type_
;
375 // The background color of the web content.
376 SkColor background_color_
;
378 // While the mouse is locked, the cursor is hidden from the user. Mouse events
379 // are still generated. However, the position they report is the last known
380 // mouse position just as mouse lock was entered; the movement they report
381 // indicates what the change in position of the mouse would be had it not been
385 // Whether we are showing a context menu.
386 bool showing_context_menu_
;
388 // A buffer containing the text inside and around the current selection range.
389 base::string16 selection_text_
;
391 // The offset of the text stored in |selection_text_| relative to the start of
393 size_t selection_text_offset_
;
395 // The current selection range relative to the start of the web page.
396 gfx::Range selection_range_
;
399 // The scale factor of the display the renderer is currently on.
400 float current_device_scale_factor_
;
402 // The orientation of the display the renderer is currently on.
403 gfx::Display::Rotation current_display_rotation_
;
405 // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
407 bool pinch_zoom_enabled_
;
412 gfx::Rect current_display_area_
;
414 uint32 renderer_frame_number_
;
416 base::OneShotTimer
<RenderWidgetHostViewBase
> flush_input_timer_
;
418 base::WeakPtrFactory
<RenderWidgetHostViewBase
> weak_factory_
;
420 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase
);
423 } // namespace content
425 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_