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 void SetPopupType(blink::WebPopupType popup_type
);
88 blink::WebPopupType
GetPopupType();
90 // Return a value that is incremented each time the renderer swaps a new frame
92 uint32
RendererFrameNumber();
94 // Called each time the RenderWidgetHost receives a new frame for display from
96 void DidReceiveRendererFrame();
98 // Notification that a resize or move session ended on the native widget.
99 void UpdateScreenInfo(gfx::NativeView view
);
101 // Tells if the display property (work area/scale factor) has
102 // changed since the last time.
103 bool HasDisplayPropertyChanged(gfx::NativeView view
);
105 base::WeakPtr
<RenderWidgetHostViewBase
> GetWeakPtr();
107 //----------------------------------------------------------------------------
108 // The following methods can be overridden by derived classes.
110 // Notifies the View that the renderer text selection has changed.
111 virtual void SelectionChanged(const base::string16
& text
,
113 const gfx::Range
& range
);
115 // The requested size of the renderer. May differ from GetViewBounds().size()
116 // when the view requires additional throttling.
117 virtual gfx::Size
GetRequestedRendererSize() const;
119 // The size of the view's backing surface in non-DPI-adjusted pixels.
120 virtual gfx::Size
GetPhysicalBackingSize() const;
122 // Whether or not Blink's viewport size should be shrunk by the height of the
124 virtual bool DoTopControlsShrinkBlinkSize() const;
126 // The height of the URL-bar top controls.
127 virtual float GetTopControlsHeight() const;
129 // Called prior to forwarding input event messages to the renderer, giving
130 // the view a chance to perform in-process event filtering or processing.
131 // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event|
133 virtual InputEventAckState
FilterInputEvent(
134 const blink::WebInputEvent
& input_event
);
136 // Called by the host when it requires an input flush; the flush call should
137 // by synchronized with BeginFrame.
138 virtual void OnSetNeedsFlushInput();
140 virtual void WheelEventAck(const blink::WebMouseWheelEvent
& event
,
141 InputEventAckState ack_result
);
143 virtual void GestureEventAck(const blink::WebGestureEvent
& event
,
144 InputEventAckState ack_result
);
146 // Create a platform specific SyntheticGestureTarget implementation that will
147 // be used to inject synthetic input events.
148 virtual scoped_ptr
<SyntheticGestureTarget
> CreateSyntheticGestureTarget();
150 // Return true if frame subscription is supported on this platform.
151 virtual bool CanSubscribeFrame() const;
153 // Create a BrowserAccessibilityManager for this view.
154 virtual BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
155 BrowserAccessibilityDelegate
* delegate
);
157 virtual void AccessibilityShowMenu(const gfx::Point
& point
);
158 virtual gfx::Point
AccessibilityOriginInScreen(const gfx::Rect
& bounds
);
159 virtual gfx::AcceleratedWidget
AccessibilityGetAcceleratedWidget();
160 virtual gfx::NativeViewAccessible
AccessibilityGetNativeViewAccessible();
162 // Informs that the focused DOM node has changed.
163 virtual void FocusedNodeChanged(bool is_editable_node
) {}
165 virtual void OnSwapCompositorFrame(uint32 output_surface_id
,
166 scoped_ptr
<cc::CompositorFrame
> frame
) {}
168 // Because the associated remote WebKit instance can asynchronously
169 // prevent-default on a dispatched touch event, the touch events are queued in
170 // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
171 // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
172 // or ignored (when |ack_result| is CONSUMED).
173 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo
& touch
,
174 InputEventAckState ack_result
) {}
176 virtual void DidOverscroll(const DidOverscrollParams
& params
) {}
178 virtual void DidStopFlinging() {}
180 // Returns the compositing surface ID namespace, or 0 if Surfaces are not
182 virtual uint32_t GetSurfaceIdNamespace();
184 //----------------------------------------------------------------------------
185 // The following static methods are implemented by each platform.
187 static void GetDefaultScreenInfo(blink::WebScreenInfo
* results
);
189 //----------------------------------------------------------------------------
190 // The following pure virtual methods are implemented by derived classes.
192 // Perform all the initialization steps necessary for this object to represent
193 // a popup (such as a <select> dropdown), then shows the popup at |pos|.
194 virtual void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
195 const gfx::Rect
& bounds
) = 0;
197 // Perform all the initialization steps necessary for this object to represent
198 // a full screen window.
199 // |reference_host_view| is the view associated with the creating page that
200 // helps to position the full screen widget on the correct monitor.
201 virtual void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) = 0;
203 // Moves all plugin windows as described in the given list.
204 // |scroll_offset| is the scroll offset of the render view.
205 virtual void MovePluginWindows(
206 const std::vector
<WebPluginGeometry
>& moves
) = 0;
208 // Sets the cursor to the one associated with the specified cursor_type
209 virtual void UpdateCursor(const WebCursor
& cursor
) = 0;
211 // Indicates whether the page has finished loading.
212 virtual void SetIsLoading(bool is_loading
) = 0;
214 // Updates the type of the input method attached to the view.
215 virtual void TextInputTypeChanged(ui::TextInputType type
,
216 ui::TextInputMode mode
,
217 bool can_compose_inline
,
220 // Cancel the ongoing composition of the input method attached to the view.
221 virtual void ImeCancelComposition() = 0;
223 // Notifies the View that the renderer has ceased to exist.
224 virtual void RenderProcessGone(base::TerminationStatus status
,
227 // Notifies the View that the renderer's host has ceased to exist.
228 // The default implementation of this is a no-op. This hack exists to fix
229 // a crash on the branch.
230 // TODO(ccameron): Clean this up.
231 // http://crbug.com/404828
232 virtual void RenderWidgetHostGone() {}
234 // Tells the View to destroy itself.
235 virtual void Destroy() = 0;
237 // Tells the View that the tooltip text for the current mouse position over
238 // the page has changed.
239 virtual void SetTooltipText(const base::string16
& tooltip_text
) = 0;
241 // Notifies the View that the renderer selection bounds has changed.
242 // |start_rect| and |end_rect| are the bounds end of the selection in the
243 // coordinate system of the render view. |start_direction| and |end_direction|
244 // indicates the direction at which the selection was made on touch devices.
245 virtual void SelectionBoundsChanged(
246 const ViewHostMsg_SelectionBounds_Params
& params
) = 0;
248 // Copies the contents of the compositing surface, providing a new SkBitmap
249 // result via an asynchronously-run |callback|. |src_subrect| is specified in
250 // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
251 // physical for Android), and is the region to be copied from this view. When
252 // |src_subrect| is empty then the whole surface will be copied. The copy is
253 // then scaled to a SkBitmap of size |dst_size|. If |dst_size| is empty then
254 // output will be unscaled. |callback| is run with true on success,
255 // false otherwise. A smaller region than |src_subrect| may be copied
256 // if the underlying surface is smaller than |src_subrect|.
257 virtual void CopyFromCompositingSurface(
258 const gfx::Rect
& src_subrect
,
259 const gfx::Size
& dst_size
,
260 ReadbackRequestCallback
& callback
,
261 const SkColorType preferred_color_type
) = 0;
263 // Copies the contents of the compositing surface, populating the given
264 // |target| with YV12 image data. |src_subrect| is specified in layer space
265 // coordinates for the current platform (e.g., DIP for Aura/Mac, physical for
266 // Android), and is the region to be copied from this view. The copy is then
267 // scaled and letterboxed with black borders to fit |target|. Finally,
268 // |callback| is asynchronously run with true/false for
269 // success/failure. |target| must point to an allocated, YV12 video frame of
270 // the intended size. This operation will fail if there is no available
271 // compositing surface.
272 virtual void CopyFromCompositingSurfaceToVideoFrame(
273 const gfx::Rect
& src_subrect
,
274 const scoped_refptr
<media::VideoFrame
>& target
,
275 const base::Callback
<void(bool)>& callback
) = 0;
277 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
280 // TODO(nick): When VideoFrame copies are broadly implemented, this method
281 // should be renamed to HasCompositingSurface(), or unified with
282 // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
283 virtual bool CanCopyToVideoFrame() const = 0;
285 // DEPRECATED. Called when an accelerated compositing surface is initialized.
286 virtual void AcceleratedSurfaceInitialized(int route_id
) {}
288 // Return true if the view has an accelerated surface that contains the last
289 // presented frame for the view. If |desired_size| is non-empty, true is
290 // returned only if the accelerated surface size matches.
291 virtual bool HasAcceleratedSurface(const gfx::Size
& desired_size
) = 0;
293 // Compute the orientation type of the display assuming it is a mobile device.
294 static blink::WebScreenOrientationType
GetOrientationTypeForMobile(
295 const gfx::Display
& display
);
297 // Compute the orientation type of the display assuming it is a desktop.
298 static blink::WebScreenOrientationType
GetOrientationTypeForDesktop(
299 const gfx::Display
& display
);
301 virtual void GetScreenInfo(blink::WebScreenInfo
* results
) = 0;
303 // Gets the bounds of the window, in screen coordinates.
304 virtual gfx::Rect
GetBoundsInRootWindow() = 0;
306 virtual gfx::GLSurfaceHandle
GetCompositingSurface() = 0;
308 // Called by the RenderFrameHost when it receives an IPC response to a
309 // TextSurroundingSelectionRequest.
310 virtual void OnTextSurroundingSelectionResponse(const base::string16
& content
,
314 // Called by the RenderWidgetHost when an ambiguous gesture is detected to
315 // show the disambiguation popup bubble.
316 virtual void ShowDisambiguationPopup(const gfx::Rect
& rect_pixels
,
317 const SkBitmap
& zoomed_bitmap
);
319 // Called by the WebContentsImpl when a user tries to navigate a new page on
321 virtual void OnDidNavigateMainFrameToNewPage();
323 #if defined(OS_ANDROID)
324 // Instructs the view to not drop the surface even when the view is hidden.
325 virtual void LockCompositingSurface() = 0;
326 virtual void UnlockCompositingSurface() = 0;
329 #if defined(OS_MACOSX)
330 // Does any event handling necessary for plugin IME; should be called after
331 // the plugin has already had a chance to process the event. If plugin IME is
332 // not enabled, this is a no-op, so it is always safe to call.
333 // Returns true if the event was handled by IME.
334 virtual bool PostProcessEventForPluginIme(
335 const NativeWebKeyboardEvent
& event
) = 0;
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;
344 virtual void SetParentNativeViewAccessible(
345 gfx::NativeViewAccessible accessible_parent
) = 0;
347 // Returns an HWND that's given as the parent window for windowless Flash to
348 // workaround crbug.com/301548.
349 virtual gfx::NativeViewId
GetParentForWindowlessPlugin() const = 0;
351 // The callback that DetachPluginsHelper calls for each child window. Call
352 // this directly if you want to do custom filtering on plugin windows first.
353 static void DetachPluginWindowsCallback(HWND window
);
357 // Interface class only, do not construct.
358 RenderWidgetHostViewBase();
361 // Shared implementation of MovePluginWindows for use by win and aura/wina.
362 static void MovePluginWindowsHelper(
364 const std::vector
<WebPluginGeometry
>& moves
);
366 static void PaintPluginWindowsHelper(
368 const gfx::Rect
& damaged_screen_rect
);
370 // Needs to be called before the HWND backing the view goes away to avoid
371 // crashes in Windowed plugins.
372 static void DetachPluginsHelper(HWND parent
);
375 // Whether this view is a popup and what kind of popup it is (select,
377 blink::WebPopupType popup_type_
;
379 // The background color of the web content.
380 SkColor background_color_
;
382 // While the mouse is locked, the cursor is hidden from the user. Mouse events
383 // are still generated. However, the position they report is the last known
384 // mouse position just as mouse lock was entered; the movement they report
385 // indicates what the change in position of the mouse would be had it not been
389 // Whether we are showing a context menu.
390 bool showing_context_menu_
;
392 // A buffer containing the text inside and around the current selection range.
393 base::string16 selection_text_
;
395 // The offset of the text stored in |selection_text_| relative to the start of
397 size_t selection_text_offset_
;
399 // The current selection range relative to the start of the web page.
400 gfx::Range selection_range_
;
403 // The scale factor of the display the renderer is currently on.
404 float current_device_scale_factor_
;
406 // The orientation of the display the renderer is currently on.
407 gfx::Display::Rotation current_display_rotation_
;
409 // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
411 bool pinch_zoom_enabled_
;
416 gfx::Rect current_display_area_
;
418 uint32 renderer_frame_number_
;
420 base::OneShotTimer
<RenderWidgetHostViewBase
> flush_input_timer_
;
422 base::WeakPtrFactory
<RenderWidgetHostViewBase
> weak_factory_
;
424 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase
);
427 } // namespace content
429 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_