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
;
48 class BrowserAccessibilityDelegate
;
49 class BrowserAccessibilityManager
;
50 class SyntheticGesture
;
51 class SyntheticGestureTarget
;
53 struct DidOverscrollParams
;
54 struct NativeWebKeyboardEvent
;
55 struct WebPluginGeometry
;
57 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
58 class CONTENT_EXPORT RenderWidgetHostViewBase
: public RenderWidgetHostView
,
59 public IPC::Listener
{
61 ~RenderWidgetHostViewBase() override
;
63 // RenderWidgetHostView implementation.
64 void SetBackgroundColor(SkColor color
) override
;
65 void SetBackgroundColorToDefault() final
;
66 bool GetBackgroundOpaque() override
;
67 ui::TextInputClient
* GetTextInputClient() override
;
68 void WasUnOccluded() override
{}
69 void WasOccluded() override
{}
70 bool IsShowingContextMenu() const override
;
71 void SetShowingContextMenu(bool showing_menu
) override
;
72 base::string16
GetSelectedText() const override
;
73 bool IsMouseLocked() override
;
74 gfx::Size
GetVisibleViewportSize() const override
;
75 void SetInsets(const gfx::Insets
& insets
) override
;
76 void BeginFrameSubscription(
77 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
) override
;
78 void EndFrameSubscription() override
;
80 // IPC::Listener implementation:
81 bool OnMessageReceived(const IPC::Message
& msg
) override
;
83 void SetPopupType(blink::WebPopupType popup_type
);
85 blink::WebPopupType
GetPopupType();
87 // Return a value that is incremented each time the renderer swaps a new frame
89 uint32
RendererFrameNumber();
91 // Called each time the RenderWidgetHost receives a new frame for display from
93 void DidReceiveRendererFrame();
95 // Notification that a resize or move session ended on the native widget.
96 void UpdateScreenInfo(gfx::NativeView view
);
98 // Tells if the display property (work area/scale factor) has
99 // changed since the last time.
100 bool HasDisplayPropertyChanged(gfx::NativeView view
);
102 base::WeakPtr
<RenderWidgetHostViewBase
> GetWeakPtr();
104 //----------------------------------------------------------------------------
105 // The following methods can be overridden by derived classes.
107 // Notifies the View that the renderer text selection has changed.
108 virtual void SelectionChanged(const base::string16
& text
,
110 const gfx::Range
& range
);
112 // The requested size of the renderer. May differ from GetViewBounds().size()
113 // when the view requires additional throttling.
114 virtual gfx::Size
GetRequestedRendererSize() const;
116 // The size of the view's backing surface in non-DPI-adjusted pixels.
117 virtual gfx::Size
GetPhysicalBackingSize() const;
119 // Whether or not Blink's viewport size should be shrunk by the height of the
121 virtual bool DoTopControlsShrinkBlinkSize() const;
123 // The height of the URL-bar top controls.
124 virtual float GetTopControlsHeight() 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 WheelEventAck(const blink::WebMouseWheelEvent
& event
,
138 InputEventAckState ack_result
);
140 virtual void GestureEventAck(const blink::WebGestureEvent
& event
,
141 InputEventAckState ack_result
);
143 // Create a platform specific SyntheticGestureTarget implementation that will
144 // be used to inject synthetic input events.
145 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget();
147 // Return true if frame subscription is supported on this platform.
148 virtual bool CanSubscribeFrame() const;
150 // Create a BrowserAccessibilityManager for this view.
151 virtual BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
152 BrowserAccessibilityDelegate
* delegate
);
154 virtual void AccessibilityShowMenu(const gfx::Point
& point
);
155 virtual gfx::Point
AccessibilityOriginInScreen(const gfx::Rect
& bounds
);
156 virtual gfx::AcceleratedWidget
AccessibilityGetAcceleratedWidget();
157 virtual gfx::NativeViewAccessible
AccessibilityGetNativeViewAccessible();
159 // Informs that the focused DOM node has changed.
160 virtual void FocusedNodeChanged(bool is_editable_node
) {}
162 virtual void OnSwapCompositorFrame(uint32 output_surface_id
,
163 scoped_ptr
<cc::CompositorFrame
> frame
) {}
165 // Because the associated remote WebKit instance can asynchronously
166 // prevent-default on a dispatched touch event, the touch events are queued in
167 // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
168 // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
169 // or ignored (when |ack_result| is CONSUMED).
170 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
171 InputEventAckState ack_result
) {}
173 virtual void DidOverscroll(const DidOverscrollParams
& params
) {}
175 virtual void DidStopFlinging() {}
177 // Returns the compositing surface ID namespace, or 0 if Surfaces are not
179 virtual uint32_t GetSurfaceIdNamespace();
181 //----------------------------------------------------------------------------
182 // The following static methods are implemented by each platform.
184 static void GetDefaultScreenInfo(blink::WebScreenInfo
* results
);
186 //----------------------------------------------------------------------------
187 // The following pure virtual methods are implemented by derived classes.
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
& bounds
) = 0;
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;
200 // Moves all plugin windows as described in the given list.
201 // |scroll_offset| is the scroll offset of the render view.
202 virtual void MovePluginWindows(
203 const std::vector
<WebPluginGeometry
>& moves
) = 0;
205 // Sets the cursor to the one associated with the specified cursor_type
206 virtual void UpdateCursor(const WebCursor
& cursor
) = 0;
208 // Indicates whether the page has finished loading.
209 virtual void SetIsLoading(bool is_loading
) = 0;
211 // Updates the state of the input method attached to the view.
212 virtual void TextInputStateChanged(
213 const ViewHostMsg_TextInputState_Params
& params
) = 0;
215 // Cancel the ongoing composition of the input method attached to the view.
216 virtual void ImeCancelComposition() = 0;
218 // Notifies the View that the renderer has ceased to exist.
219 virtual void RenderProcessGone(base::TerminationStatus status
,
222 // Notifies the View that the renderer's host has ceased to exist.
223 // The default implementation of this is a no-op. This hack exists to fix
224 // a crash on the branch.
225 // TODO(ccameron): Clean this up.
226 // http://crbug.com/404828
227 virtual void RenderWidgetHostGone() {}
229 // Tells the View to destroy itself.
230 virtual void Destroy() = 0;
232 // Tells the View that the tooltip text for the current mouse position over
233 // the page has changed.
234 virtual void SetTooltipText(const base::string16
& tooltip_text
) = 0;
236 // Notifies the View that the renderer selection bounds has changed.
237 // |start_rect| and |end_rect| are the bounds end of the selection in the
238 // coordinate system of the render view. |start_direction| and |end_direction|
239 // indicates the direction at which the selection was made on touch devices.
240 virtual void SelectionBoundsChanged(
241 const ViewHostMsg_SelectionBounds_Params
& params
) = 0;
243 // Copies the contents of the compositing surface, providing a new SkBitmap
244 // result via an asynchronously-run |callback|. |src_subrect| is specified in
245 // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
246 // physical for Android), and is the region to be copied from this view. When
247 // |src_subrect| is empty then the whole surface will be copied. The copy is
248 // then scaled to a SkBitmap of size |dst_size|. If |dst_size| is empty then
249 // output will be unscaled. |callback| is run with true on success,
250 // false otherwise. A smaller region than |src_subrect| may be copied
251 // if the underlying surface is smaller than |src_subrect|.
252 virtual void CopyFromCompositingSurface(
253 const gfx::Rect
& src_subrect
,
254 const gfx::Size
& dst_size
,
255 ReadbackRequestCallback
& callback
,
256 const SkColorType preferred_color_type
) = 0;
258 // Copies the contents of the compositing surface, populating the given
259 // |target| with YV12 image data. |src_subrect| is specified in layer space
260 // coordinates for the current platform (e.g., DIP for Aura/Mac, physical for
261 // Android), and is the region to be copied from this view. The copy is then
262 // scaled and letterboxed with black borders to fit |target|. Finally,
263 // |callback| is asynchronously run with true/false for
264 // success/failure. |target| must point to an allocated, YV12 video frame of
265 // the intended size. This operation will fail if there is no available
266 // compositing surface.
267 virtual void CopyFromCompositingSurfaceToVideoFrame(
268 const gfx::Rect
& src_subrect
,
269 const scoped_refptr
<media::VideoFrame
>& target
,
270 const base::Callback
<void(bool)>& callback
) = 0;
272 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
275 // TODO(nick): When VideoFrame copies are broadly implemented, this method
276 // should be renamed to HasCompositingSurface(), or unified with
277 // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
278 virtual bool CanCopyToVideoFrame() const = 0;
280 // DEPRECATED. Called when an accelerated compositing surface is initialized.
281 virtual void AcceleratedSurfaceInitialized(int route_id
) {}
283 // Return true if the view has an accelerated surface that contains the last
284 // presented frame for the view. If |desired_size| is non-empty, true is
285 // returned only if the accelerated surface size matches.
286 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) = 0;
288 // Compute the orientation type of the display assuming it is a mobile device.
289 static blink::WebScreenOrientationType
GetOrientationTypeForMobile(
290 const gfx::Display
& display
);
292 // Compute the orientation type of the display assuming it is a desktop.
293 static blink::WebScreenOrientationType
GetOrientationTypeForDesktop(
294 const gfx::Display
& display
);
296 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) = 0;
298 // Gets the bounds of the window, in screen coordinates.
299 virtual gfx::Rect
GetBoundsInRootWindow() = 0;
301 virtual gfx::GLSurfaceHandle
GetCompositingSurface() = 0;
303 // Called by the RenderFrameHost when it receives an IPC response to a
304 // TextSurroundingSelectionRequest.
305 virtual void OnTextSurroundingSelectionResponse(const base::string16
& content
,
309 // Called by the RenderWidgetHost when an ambiguous gesture is detected to
310 // show the disambiguation popup bubble.
311 virtual void ShowDisambiguationPopup(const gfx::Rect
& rect_pixels
,
312 const SkBitmap
& zoomed_bitmap
);
314 // Called by the WebContentsImpl when a user tries to navigate a new page on
316 virtual void OnDidNavigateMainFrameToNewPage();
318 #if defined(OS_ANDROID)
319 // Instructs the view to not drop the surface even when the view is hidden.
320 virtual void LockCompositingSurface() = 0;
321 virtual void UnlockCompositingSurface() = 0;
324 #if defined(OS_MACOSX)
325 // Does any event handling necessary for plugin IME; should be called after
326 // the plugin has already had a chance to process the event. If plugin IME is
327 // not enabled, this is a no-op, so it is always safe to call.
328 // Returns true if the event was handled by IME.
329 virtual bool PostProcessEventForPluginIme(
330 const NativeWebKeyboardEvent
& event
) = 0;
333 // Updates the range of the marked text in an IME composition.
334 virtual void ImeCompositionRangeChanged(
335 const gfx::Range
& range
,
336 const std::vector
<gfx::Rect
>& character_bounds
) = 0;
339 virtual void SetParentNativeViewAccessible(
340 gfx::NativeViewAccessible accessible_parent
) = 0;
342 // Returns an HWND that's given as the parent window for windowless Flash to
343 // workaround crbug.com/301548.
344 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const = 0;
346 // The callback that DetachPluginsHelper calls for each child window. Call
347 // this directly if you want to do custom filtering on plugin windows first.
348 static void DetachPluginWindowsCallback(HWND window
);
352 // Interface class only, do not construct.
353 RenderWidgetHostViewBase();
356 // Shared implementation of MovePluginWindows for use by win and aura/wina.
357 static void MovePluginWindowsHelper(
359 const std::vector
<WebPluginGeometry
>& moves
);
361 static void PaintPluginWindowsHelper(
363 const gfx::Rect
& damaged_screen_rect
);
365 // Needs to be called before the HWND backing the view goes away to avoid
366 // crashes in Windowed plugins.
367 static void DetachPluginsHelper(HWND parent
);
370 // Whether this view is a popup and what kind of popup it is (select,
372 blink::WebPopupType popup_type_
;
374 // The background color of the web content.
375 SkColor background_color_
;
377 // While the mouse is locked, the cursor is hidden from the user. Mouse events
378 // are still generated. However, the position they report is the last known
379 // mouse position just as mouse lock was entered; the movement they report
380 // indicates what the change in position of the mouse would be had it not been
384 // Whether we are showing a context menu.
385 bool showing_context_menu_
;
387 // A buffer containing the text inside and around the current selection range.
388 base::string16 selection_text_
;
390 // The offset of the text stored in |selection_text_| relative to the start of
392 size_t selection_text_offset_
;
394 // The current selection range relative to the start of the web page.
395 gfx::Range selection_range_
;
398 // The scale factor of the display the renderer is currently on.
399 float current_device_scale_factor_
;
401 // The orientation of the display the renderer is currently on.
402 gfx::Display::Rotation current_display_rotation_
;
404 // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
406 bool pinch_zoom_enabled_
;
411 gfx::Rect current_display_area_
;
413 uint32 renderer_frame_number_
;
415 base::OneShotTimer
<RenderWidgetHostViewBase
> flush_input_timer_
;
417 base::WeakPtrFactory
<RenderWidgetHostViewBase
> weak_factory_
;
419 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase
);
422 } // namespace content
424 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_