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/callback_forward.h"
16 #include "base/memory/scoped_ptr.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/readback_types.h"
24 #include "content/public/browser/render_widget_host_view.h"
25 #include "ipc/ipc_listener.h"
26 #include "third_party/WebKit/public/platform/WebScreenOrientationType.h"
27 #include "third_party/WebKit/public/web/WebPopupType.h"
28 #include "third_party/WebKit/public/web/WebTextDirection.h"
29 #include "ui/base/ime/text_input_mode.h"
30 #include "ui/base/ime/text_input_type.h"
31 #include "ui/gfx/display.h"
32 #include "ui/gfx/geometry/rect.h"
33 #include "ui/gfx/native_widget_types.h"
34 #include "ui/gfx/range/range.h"
35 #include "ui/surface/transport_dib.h"
39 struct AccessibilityHostMsg_EventParams
;
40 struct ViewHostMsg_SelectionBounds_Params
;
51 class BrowserAccessibilityDelegate
;
52 class BrowserAccessibilityManager
;
53 class SyntheticGesture
;
54 class SyntheticGestureTarget
;
56 struct DidOverscrollParams
;
57 struct NativeWebKeyboardEvent
;
58 struct WebPluginGeometry
;
60 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
61 class CONTENT_EXPORT RenderWidgetHostViewBase
: public RenderWidgetHostView
,
62 public IPC::Listener
{
64 ~RenderWidgetHostViewBase() override
;
66 // RenderWidgetHostView implementation.
67 void SetBackgroundColor(SkColor color
) override
;
68 void SetBackgroundColorToDefault() final
;
69 bool GetBackgroundOpaque() override
;
70 ui::TextInputClient
* GetTextInputClient() override
;
71 void WasUnOccluded() override
{}
72 void WasOccluded() override
{}
73 bool IsShowingContextMenu() const override
;
74 void SetShowingContextMenu(bool showing_menu
) override
;
75 base::string16
GetSelectedText() const override
;
76 bool IsMouseLocked() override
;
77 gfx::Size
GetVisibleViewportSize() const override
;
78 void SetInsets(const gfx::Insets
& insets
) override
;
79 void BeginFrameSubscription(
80 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
) override
;
81 void EndFrameSubscription() override
;
83 // IPC::Listener implementation:
84 bool OnMessageReceived(const IPC::Message
& msg
) override
;
86 // Called by the host when the input flush has completed.
87 void OnDidFlushInput();
89 void SetPopupType(blink::WebPopupType popup_type
);
91 blink::WebPopupType
GetPopupType();
93 // Return a value that is incremented each time the renderer swaps a new frame
95 uint32
RendererFrameNumber();
97 // Called each time the RenderWidgetHost receives a new frame for display from
99 void DidReceiveRendererFrame();
101 // Notification that a resize or move session ended on the native widget.
102 void UpdateScreenInfo(gfx::NativeView view
);
104 // Tells if the display property (work area/scale factor) has
105 // changed since the last time.
106 bool HasDisplayPropertyChanged(gfx::NativeView view
);
108 base::WeakPtr
<RenderWidgetHostViewBase
> GetWeakPtr();
110 //----------------------------------------------------------------------------
111 // The following methods can be overridden by derived classes.
113 // Notifies the View that the renderer text selection has changed.
114 virtual void SelectionChanged(const base::string16
& text
,
116 const gfx::Range
& range
);
118 // The requested size of the renderer. May differ from GetViewBounds().size()
119 // when the view requires additional throttling.
120 virtual gfx::Size
GetRequestedRendererSize() const;
122 // The size of the view's backing surface in non-DPI-adjusted pixels.
123 virtual gfx::Size
GetPhysicalBackingSize() const;
125 // Whether or not Blink's viewport size should be shrunk by the height of the
127 virtual bool DoTopControlsShrinkBlinkSize() const;
129 // The height of the URL-bar top controls.
130 virtual float GetTopControlsHeight() const;
132 // Called prior to forwarding input event messages to the renderer, giving
133 // the view a chance to perform in-process event filtering or processing.
134 // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event|
136 virtual InputEventAckState
FilterInputEvent(
137 const blink::WebInputEvent
& input_event
);
139 // Called by the host when it requires an input flush; the flush call should
140 // by synchronized with BeginFrame.
141 virtual void OnSetNeedsFlushInput();
143 virtual void WheelEventAck(const blink::WebMouseWheelEvent
& event
,
144 InputEventAckState ack_result
);
146 virtual void GestureEventAck(const blink::WebGestureEvent
& event
,
147 InputEventAckState ack_result
);
149 // Create a platform specific SyntheticGestureTarget implementation that will
150 // be used to inject synthetic input events.
151 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget();
153 // Return true if frame subscription is supported on this platform.
154 virtual bool CanSubscribeFrame() const;
156 // Create a BrowserAccessibilityManager for this view.
157 virtual BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
158 BrowserAccessibilityDelegate
* delegate
);
160 virtual void AccessibilityShowMenu(const gfx::Point
& point
);
161 virtual gfx::Point
AccessibilityOriginInScreen(const gfx::Rect
& bounds
);
162 virtual gfx::AcceleratedWidget
AccessibilityGetAcceleratedWidget();
163 virtual gfx::NativeViewAccessible
AccessibilityGetNativeViewAccessible();
165 virtual SkColorType
PreferredReadbackFormat();
167 // Informs that the focused DOM node has changed.
168 virtual void FocusedNodeChanged(bool is_editable_node
) {}
170 virtual void OnSwapCompositorFrame(uint32 output_surface_id
,
171 scoped_ptr
<cc::CompositorFrame
> frame
) {}
173 // Because the associated remote WebKit instance can asynchronously
174 // prevent-default on a dispatched touch event, the touch events are queued in
175 // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
176 // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
177 // or ignored (when |ack_result| is CONSUMED).
178 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
179 InputEventAckState ack_result
) {}
181 virtual void DidOverscroll(const DidOverscrollParams
& params
) {}
183 virtual void DidStopFlinging() {}
185 //----------------------------------------------------------------------------
186 // The following static methods are implemented by each platform.
188 static void GetDefaultScreenInfo(blink::WebScreenInfo
* results
);
190 //----------------------------------------------------------------------------
191 // The following pure virtual methods are implemented by derived classes.
193 // Perform all the initialization steps necessary for this object to represent
194 // a popup (such as a <select> dropdown), then shows the popup at |pos|.
195 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
196 const gfx::Rect
& bounds
) = 0;
198 // Perform all the initialization steps necessary for this object to represent
199 // a full screen window.
200 // |reference_host_view| is the view associated with the creating page that
201 // helps to position the full screen widget on the correct monitor.
202 virtual void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) = 0;
204 // Moves all plugin windows as described in the given list.
205 // |scroll_offset| is the scroll offset of the render view.
206 virtual void MovePluginWindows(
207 const std::vector
<WebPluginGeometry
>& moves
) = 0;
209 // Take focus from the associated View component.
210 virtual void Blur() = 0;
212 // Sets the cursor to the one associated with the specified cursor_type
213 virtual void UpdateCursor(const WebCursor
& cursor
) = 0;
215 // Indicates whether the page has finished loading.
216 virtual void SetIsLoading(bool is_loading
) = 0;
218 // Updates the type of the input method attached to the view.
219 virtual void TextInputTypeChanged(ui::TextInputType type
,
220 ui::TextInputMode mode
,
221 bool can_compose_inline
,
224 // Cancel the ongoing composition of the input method attached to the view.
225 virtual void ImeCancelComposition() = 0;
227 // Notifies the View that the renderer has ceased to exist.
228 virtual void RenderProcessGone(base::TerminationStatus status
,
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() {}
238 // Tells the View to destroy itself.
239 virtual void Destroy() = 0;
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;
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;
252 // Copies the contents of the compositing surface, providing a new SkBitmap
253 // result via an asynchronously-run |callback|. |src_subrect| is specified in
254 // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
255 // physical for Android), and is the region to be copied from this view. When
256 // |src_subrect| is empty then the whole surface will be copied. The copy is
257 // then scaled to a SkBitmap of size |dst_size|. If |dst_size| is empty then
258 // output will be unscaled. |callback| is run with true on success,
259 // false otherwise. A smaller region than |src_subrect| may be copied
260 // if the underlying surface is smaller than |src_subrect|.
261 virtual void CopyFromCompositingSurface(const gfx::Rect
& src_subrect
,
262 const gfx::Size
& dst_size
,
263 ReadbackRequestCallback
& callback
,
264 const SkColorType color_type
) = 0;
266 // Copies the contents of the compositing surface, populating the given
267 // |target| with YV12 image data. |src_subrect| is specified in layer space
268 // coordinates for the current platform (e.g., DIP for Aura/Mac, physical for
269 // Android), and is the region to be copied from this view. The copy is then
270 // scaled and letterboxed with black borders to fit |target|. Finally,
271 // |callback| is asynchronously run with true/false for
272 // success/failure. |target| must point to an allocated, YV12 video frame of
273 // the intended size. This operation will fail if there is no available
274 // compositing surface.
275 virtual void CopyFromCompositingSurfaceToVideoFrame(
276 const gfx::Rect
& src_subrect
,
277 const scoped_refptr
<media::VideoFrame
>& target
,
278 const base::Callback
<void(bool)>& callback
) = 0;
280 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
283 // TODO(nick): When VideoFrame copies are broadly implemented, this method
284 // should be renamed to HasCompositingSurface(), or unified with
285 // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
286 virtual bool CanCopyToVideoFrame() const = 0;
288 // DEPRECATED. Called when an accelerated compositing surface is initialized.
289 virtual void AcceleratedSurfaceInitialized(int route_id
) {}
291 // Return true if the view has an accelerated surface that contains the last
292 // presented frame for the view. If |desired_size| is non-empty, true is
293 // returned only if the accelerated surface size matches.
294 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) = 0;
296 // Compute the orientation type of the display assuming it is a mobile device.
297 static blink::WebScreenOrientationType
GetOrientationTypeForMobile(
298 const gfx::Display
& display
);
300 // Compute the orientation type of the display assuming it is a desktop.
301 static blink::WebScreenOrientationType
GetOrientationTypeForDesktop(
302 const gfx::Display
& display
);
304 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) = 0;
306 // Gets the bounds of the window, in screen coordinates.
307 virtual gfx::Rect
GetBoundsInRootWindow() = 0;
309 virtual gfx::GLSurfaceHandle
GetCompositingSurface() = 0;
311 // Called by the RenderFrameHost when it receives an IPC response to a
312 // TextSurroundingSelectionRequest.
313 virtual void OnTextSurroundingSelectionResponse(const base::string16
& content
,
317 // Called by the RenderWidgetHost when an ambiguous gesture is detected to
318 // show the disambiguation popup bubble.
319 virtual void ShowDisambiguationPopup(const gfx::Rect
& rect_pixels
,
320 const SkBitmap
& zoomed_bitmap
);
322 // Called by the WebContentsImpl when a user tries to navigate a new page on
324 virtual void OnDidNavigateMainFrameToNewPage();
326 #if defined(OS_ANDROID)
327 // Instructs the view to not drop the surface even when the view is hidden.
328 virtual void LockCompositingSurface() = 0;
329 virtual void UnlockCompositingSurface() = 0;
332 #if defined(OS_MACOSX)
333 // Does any event handling necessary for plugin IME; should be called after
334 // the plugin has already had a chance to process the event. If plugin IME is
335 // not enabled, this is a no-op, so it is always safe to call.
336 // Returns true if the event was handled by IME.
337 virtual bool PostProcessEventForPluginIme(
338 const NativeWebKeyboardEvent
& event
) = 0;
341 // Updates the range of the marked text in an IME composition.
342 virtual void ImeCompositionRangeChanged(
343 const gfx::Range
& range
,
344 const std::vector
<gfx::Rect
>& character_bounds
) = 0;
347 virtual void SetParentNativeViewAccessible(
348 gfx::NativeViewAccessible accessible_parent
) = 0;
350 // Returns an HWND that's given as the parent window for windowless Flash to
351 // workaround crbug.com/301548.
352 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const = 0;
354 // The callback that DetachPluginsHelper calls for each child window. Call
355 // this directly if you want to do custom filtering on plugin windows first.
356 static void DetachPluginWindowsCallback(HWND window
);
360 // Interface class only, do not construct.
361 RenderWidgetHostViewBase();
364 // Shared implementation of MovePluginWindows for use by win and aura/wina.
365 static void MovePluginWindowsHelper(
367 const std::vector
<WebPluginGeometry
>& moves
);
369 static void PaintPluginWindowsHelper(
371 const gfx::Rect
& damaged_screen_rect
);
373 // Needs to be called before the HWND backing the view goes away to avoid
374 // crashes in Windowed plugins.
375 static void DetachPluginsHelper(HWND parent
);
378 // Whether this view is a popup and what kind of popup it is (select,
380 blink::WebPopupType popup_type_
;
382 // The background color of the web content.
383 SkColor background_color_
;
385 // While the mouse is locked, the cursor is hidden from the user. Mouse events
386 // are still generated. However, the position they report is the last known
387 // mouse position just as mouse lock was entered; the movement they report
388 // indicates what the change in position of the mouse would be had it not been
392 // Whether we are showing a context menu.
393 bool showing_context_menu_
;
395 // A buffer containing the text inside and around the current selection range.
396 base::string16 selection_text_
;
398 // The offset of the text stored in |selection_text_| relative to the start of
400 size_t selection_text_offset_
;
402 // The current selection range relative to the start of the web page.
403 gfx::Range selection_range_
;
406 // The scale factor of the display the renderer is currently on.
407 float current_device_scale_factor_
;
409 // The orientation of the display the renderer is currently on.
410 gfx::Display::Rotation current_display_rotation_
;
412 // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
414 bool pinch_zoom_enabled_
;
419 gfx::Rect current_display_area_
;
421 uint32 renderer_frame_number_
;
423 base::OneShotTimer
<RenderWidgetHostViewBase
> flush_input_timer_
;
425 base::WeakPtrFactory
<RenderWidgetHostViewBase
> weak_factory_
;
427 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase
);
430 } // namespace content
432 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_