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_
15 #include "base/callback_forward.h"
16 #include "base/macros.h"
17 #include "base/observer_list.h"
18 #include "base/process/kill.h"
19 #include "base/strings/string16.h"
20 #include "base/timer/timer.h"
21 #include "build/build_config.h"
22 #include "cc/output/compositor_frame.h"
23 #include "cc/surfaces/surface_id.h"
24 #include "content/browser/renderer_host/event_with_latency_info.h"
25 #include "content/common/content_export.h"
26 #include "content/common/input/input_event_ack_state.h"
27 #include "content/public/browser/readback_types.h"
28 #include "content/public/browser/render_widget_host_view.h"
29 #include "content/public/common/screen_info.h"
30 #include "ipc/ipc_listener.h"
31 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScreenOrientationType.h"
32 #include "third_party/WebKit/public/web/WebPopupType.h"
33 #include "third_party/WebKit/public/web/WebTextDirection.h"
34 #include "third_party/skia/include/core/SkImageInfo.h"
35 #include "ui/base/ime/text_input_mode.h"
36 #include "ui/base/ime/text_input_type.h"
37 #include "ui/display/display.h"
38 #include "ui/gfx/geometry/rect.h"
39 #include "ui/gfx/native_widget_types.h"
40 #include "ui/gfx/range/range.h"
41 #include "ui/surface/transport_dib.h"
45 struct ViewHostMsg_SelectionBounds_Params;
53 class WebMouseWheelEvent;
57 class SurfaceHittestDelegate;
62 struct DidOverscrollParams;
66 class BrowserAccessibilityDelegate;
67 class BrowserAccessibilityManager;
68 class RenderWidgetHostImpl;
69 class RenderWidgetHostViewBaseObserver;
70 class SyntheticGesture;
71 class SyntheticGestureTarget;
72 class TextInputManager;
74 struct NativeWebKeyboardEvent;
75 struct TextInputState;
77 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
78 class CONTENT_EXPORT RenderWidgetHostViewBase : public RenderWidgetHostView,
79 public IPC::Listener {
81 ~RenderWidgetHostViewBase() override;
83 float current_device_scale_factor() const {
84 return current_device_scale_factor_;
87 // Returns the focused RenderWidgetHost inside this |view|'s RWH.
88 RenderWidgetHostImpl* GetFocusedWidget() const;
90 // RenderWidgetHostView implementation.
91 RenderWidgetHost* GetRenderWidgetHost() const override;
92 void SetBackgroundColor(SkColor color) override;
93 SkColor background_color() override;
94 void SetBackgroundColorToDefault() final;
95 bool GetBackgroundOpaque() override;
96 ui::TextInputClient* GetTextInputClient() override;
97 void WasUnOccluded() override {}
98 void WasOccluded() override {}
99 bool IsShowingContextMenu() const override;
100 void SetShowingContextMenu(bool showing_menu) override;
101 base::string16 GetSelectedText() override;
102 bool IsMouseLocked() override;
103 gfx::Size GetVisibleViewportSize() const override;
104 void SetInsets(const gfx::Insets& insets) override;
105 void BeginFrameSubscription(
106 std::unique_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) override;
107 void EndFrameSubscription() override;
108 void FocusedNodeTouched(const gfx::Point& location_dips_screen,
109 bool editable) override;
111 // This only needs to be overridden by RenderWidgetHostViewBase subclasses
112 // that handle content embedded within other RenderWidgetHostViews.
113 gfx::Point TransformPointToRootCoordSpace(const gfx::Point& point) override;
114 gfx::PointF TransformPointToRootCoordSpaceF(
115 const gfx::PointF& point) override;
117 // IPC::Listener implementation:
118 bool OnMessageReceived(const IPC::Message& msg) override;
120 void SetPopupType(blink::WebPopupType popup_type);
122 blink::WebPopupType GetPopupType();
124 // Return a value that is incremented each time the renderer swaps a new frame
126 uint32_t RendererFrameNumber();
128 // Called each time the RenderWidgetHost receives a new frame for display from
130 void DidReceiveRendererFrame();
132 // Notification that a resize or move session ended on the native widget.
133 void UpdateScreenInfo(gfx::NativeView view);
135 // Tells if the display property (work area/scale factor) has
136 // changed since the last time.
137 bool HasDisplayPropertyChanged(gfx::NativeView view);
139 // Called by the TextInputManager to notify the view about being removed from
140 // the list of registered views, i.e., TextInputManager is no longer tracking
141 // TextInputState from this view. The RWHV should reset |text_input_manager_|
143 void DidUnregisterFromTextInputManager(TextInputManager* text_input_manager);
145 base::WeakPtr<RenderWidgetHostViewBase> GetWeakPtr();
147 //----------------------------------------------------------------------------
148 // The following methods can be overridden by derived classes.
150 // Notifies the View that the renderer text selection has changed.
151 virtual void SelectionChanged(const base::string16& text,
153 const gfx::Range& range);
155 // The requested size of the renderer. May differ from GetViewBounds().size()
156 // when the view requires additional throttling.
157 virtual gfx::Size GetRequestedRendererSize() const;
159 // The size of the view's backing surface in non-DPI-adjusted pixels.
160 virtual gfx::Size GetPhysicalBackingSize() const;
162 // Whether or not Blink's viewport size should be shrunk by the height of the
164 virtual bool DoBrowserControlsShrinkBlinkSize() const;
166 // The height of the URL-bar browser controls.
167 virtual float GetTopControlsHeight() const;
169 // The height of the bottom bar.
170 virtual float GetBottomControlsHeight() const;
172 // Called prior to forwarding input event messages to the renderer, giving
173 // the view a chance to perform in-process event filtering or processing.
174 // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event|
176 virtual InputEventAckState FilterInputEvent(
177 const blink::WebInputEvent& input_event);
179 // Called by the host when it requires an input flush; the flush call should
180 // by synchronized with BeginFrame.
181 virtual void OnSetNeedsFlushInput();
183 virtual void WheelEventAck(const blink::WebMouseWheelEvent& event,
184 InputEventAckState ack_result);
186 virtual void GestureEventAck(const blink::WebGestureEvent& event,
187 InputEventAckState ack_result);
189 // Create a platform specific SyntheticGestureTarget implementation that will
190 // be used to inject synthetic input events.
191 virtual std::unique_ptr<SyntheticGestureTarget>
192 CreateSyntheticGestureTarget();
194 // Create a BrowserAccessibilityManager for a frame in this view.
195 // If |for_root_frame| is true, creates a BrowserAccessibilityManager
196 // suitable for the root frame, which may be linked to its native
198 virtual BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
199 BrowserAccessibilityDelegate* delegate, bool for_root_frame);
201 virtual void AccessibilityShowMenu(const gfx::Point& point);
202 virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds);
203 virtual gfx::AcceleratedWidget AccessibilityGetAcceleratedWidget();
204 virtual gfx::NativeViewAccessible AccessibilityGetNativeViewAccessible();
206 // Informs that the focused DOM node has changed.
207 #if defined(OS_ANDROID)
208 virtual void FocusedNodeChanged(bool is_editable_node,
209 const gfx::Rect& node_bounds_in_screen,
211 base::string16 node_id) {}
212 #elif defined(OS_TIZEN_TV_PRODUCT)
213 virtual void FocusedNodeChanged(bool is_editable_node,
214 const gfx::Rect& node_bounds_in_screen,
215 bool is_radio_or_checkbox) {}
217 virtual void FocusedNodeChanged(bool is_editable_node,
218 const gfx::Rect& node_bounds_in_screen) {}
221 virtual void OnSwapCompositorFrame(uint32_t compositor_frame_sink_id,
222 cc::CompositorFrame frame) {}
224 // This method exists to allow removing of displayed graphics, after a new
225 // page has been loaded, to prevent the displayed URL from being out of sync
226 // with what is visible on screen.
227 virtual void ClearCompositorFrame() = 0;
229 // Because the associated remote WebKit instance can asynchronously
230 // prevent-default on a dispatched touch event, the touch events are queued in
231 // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
232 // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
233 // or ignored (when |ack_result| is CONSUMED).
234 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
235 InputEventAckState ack_result) {}
237 virtual void DidOverscroll(const ui::DidOverscrollParams& params) {}
239 virtual void DidStopFlinging() {}
241 // Returns the compositing surface ID namespace, or 0 if Surfaces are not
243 virtual cc::FrameSinkId GetFrameSinkId();
245 // When there are multiple RenderWidgetHostViews for a single page, input
246 // events need to be targeted to the correct one for handling. The following
247 // methods are invoked on the RenderWidgetHostView that should be able to
248 // properly handle the event (i.e. it has focus for keyboard events, or has
249 // been identified by hit testing mouse, touch or gesture events).
250 virtual cc::FrameSinkId FrameSinkIdAtPoint(
251 cc::SurfaceHittestDelegate* delegate,
252 const gfx::Point& point,
253 gfx::Point* transformed_point);
254 virtual void ProcessKeyboardEvent(const NativeWebKeyboardEvent& event) {}
255 virtual void ProcessMouseEvent(const blink::WebMouseEvent& event,
256 const ui::LatencyInfo& latency) {}
257 virtual void ProcessMouseWheelEvent(const blink::WebMouseWheelEvent& event,
258 const ui::LatencyInfo& latency) {}
259 virtual void ProcessTouchEvent(const blink::WebTouchEvent& event,
260 const ui::LatencyInfo& latency) {}
261 virtual void ProcessGestureEvent(const blink::WebGestureEvent& event,
262 const ui::LatencyInfo& latency) {}
264 // Transform a point that is in the coordinate space of a Surface that is
265 // embedded within the RenderWidgetHostViewBase's Surface to the
266 // coordinate space of an embedding, or embedded, Surface. Typically this
267 // means that a point was received from an out-of-process iframe's
268 // RenderWidget and needs to be translated to viewport coordinates for the
269 // root RWHV, in which case this method is called on the root RWHV with the
270 // out-of-process iframe's SurfaceId.
271 // Returns false when this attempts to transform a point between coordinate
272 // spaces of surfaces where one does not contain the other. To transform
273 // between sibling surfaces, the point must be transformed to the root's
274 // coordinate space as an intermediate step.
275 virtual bool TransformPointToLocalCoordSpace(
276 const gfx::Point& point,
277 const cc::SurfaceId& original_surface,
278 gfx::Point* transformed_point);
280 // Transform a point that is in the coordinate space for the current
281 // RenderWidgetHostView to the coordinate space of the target_view.
282 virtual bool TransformPointToCoordSpaceForView(
283 const gfx::Point& point,
284 RenderWidgetHostViewBase* target_view,
285 gfx::Point* transformed_point);
287 // TODO(kenrb, wjmaclean): This is a temporary subclass identifier for
288 // RenderWidgetHostViewGuests that is needed for special treatment during
289 // input event routing. It can be removed either when RWHVGuests properly
290 // support direct mouse event routing, or when RWHVGuest is removed
291 // entirely, which comes first.
292 virtual bool IsRenderWidgetHostViewGuest();
294 // Subclass identifier for RenderWidgetHostViewChildFrames. This is useful
295 // to be able to know if this RWHV is embedded within another RWHV. If
296 // other kinds of embeddable RWHVs are created, this should be renamed to
297 // a more generic term -- in which case, static casts to RWHVChildFrame will
298 // need to also be resolved.
299 virtual bool IsRenderWidgetHostViewChildFrame();
301 //----------------------------------------------------------------------------
302 // The following methods are related to IME.
303 // TODO(ekaramad): Most of the IME methods should not stay virtual after IME
304 // is implemented for OOPIF. After fixing IME, mark the corresponding methods
305 // non-virtual (https://crbug.com/578168).
307 virtual void OnRecognizeArticleResult(
308 bool is_article, const base::string16& page_url) {}
310 // Updates the state of the input method attached to the view.
311 virtual void TextInputStateChanged(const TextInputState& text_input_state);
313 // Cancel the ongoing composition of the input method attached to the view.
314 virtual void ImeCancelComposition();
316 // Notifies the view that the renderer selection bounds has changed.
317 // Selection bounds are described as a focus bound which is the current
318 // position of caret on the screen, as well as the anchor bound which is the
319 // starting position of the selection. The coordinates are with respect to
320 // RenderWidget's window's origin. Focus and anchor bound are represented as
322 virtual void SelectionBoundsChanged(
323 const ViewHostMsg_SelectionBounds_Params& params);
325 // Updates the range of the marked text in an IME composition.
326 virtual void ImeCompositionRangeChanged(
327 const gfx::Range& range,
328 const std::vector<gfx::Rect>& character_bounds);
330 //----------------------------------------------------------------------------
331 // The following pure virtual methods are implemented by derived classes.
333 // Perform all the initialization steps necessary for this object to represent
334 // a popup (such as a <select> dropdown), then shows the popup at |pos|.
335 virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
336 const gfx::Rect& bounds) = 0;
338 // Perform all the initialization steps necessary for this object to represent
339 // a full screen window.
340 // |reference_host_view| is the view associated with the creating page that
341 // helps to position the full screen widget on the correct monitor.
342 virtual void InitAsFullscreen(RenderWidgetHostView* reference_host_view) = 0;
344 // Sets the cursor to the one associated with the specified cursor_type
345 virtual void UpdateCursor(const WebCursor& cursor) = 0;
347 // Indicates whether the page has finished loading.
348 virtual void SetIsLoading(bool is_loading) = 0;
350 // Notifies the View that the renderer has ceased to exist.
351 virtual void RenderProcessGone(base::TerminationStatus status,
354 // Tells the View to destroy itself.
355 virtual void Destroy() = 0;
357 // Tells the View that the tooltip text for the current mouse position over
358 // the page has changed.
359 virtual void SetTooltipText(const base::string16& tooltip_text) = 0;
361 // Copies the contents of the compositing surface, providing a new SkBitmap
362 // result via an asynchronously-run |callback|. |src_subrect| is specified in
363 // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
364 // physical for Android), and is the region to be copied from this view. When
365 // |src_subrect| is empty then the whole surface will be copied. The copy is
366 // then scaled to a SkBitmap of size |dst_size|. If |dst_size| is empty then
367 // output will be unscaled. |callback| is run with true on success,
368 // false otherwise. A smaller region than |src_subrect| may be copied
369 // if the underlying surface is smaller than |src_subrect|.
370 virtual void CopyFromCompositingSurface(
371 const gfx::Rect& src_subrect,
372 const gfx::Size& dst_size,
373 const ReadbackRequestCallback& callback,
374 const SkColorType preferred_color_type) = 0;
376 // Copies the contents of the compositing surface, populating the given
377 // |target| with YV12 image data. |src_subrect| is specified in layer space
378 // coordinates for the current platform (e.g., DIP for Aura/Mac, physical for
379 // Android), and is the region to be copied from this view. The copy is then
380 // scaled and letterboxed with black borders to fit |target|. Finally,
381 // |callback| is asynchronously run with true/false for
382 // success/failure. |target| must point to an allocated, YV12 video frame of
383 // the intended size. This operation will fail if there is no available
384 // compositing surface.
385 virtual void CopyFromCompositingSurfaceToVideoFrame(
386 const gfx::Rect& src_subrect,
387 const scoped_refptr<media::VideoFrame>& target,
388 const base::Callback<void(const gfx::Rect&, bool)>& callback) = 0;
390 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
393 // TODO(nick): When VideoFrame copies are broadly implemented, this method
394 // should be renamed to HasCompositingSurface(), or unified with
395 // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
396 virtual bool CanCopyToVideoFrame() const = 0;
398 // Return true if the view has an accelerated surface that contains the last
399 // presented frame for the view. If |desired_size| is non-empty, true is
400 // returned only if the accelerated surface size matches.
401 virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) = 0;
403 // Compute the orientation type of the display assuming it is a mobile device.
404 static ScreenOrientationValues GetOrientationTypeForMobile(
405 const display::Display& display);
407 // Compute the orientation type of the display assuming it is a desktop.
408 static ScreenOrientationValues GetOrientationTypeForDesktop(
409 const display::Display& display);
411 // Gets the bounds of the window, in screen coordinates.
412 virtual gfx::Rect GetBoundsInRootWindow() = 0;
414 // Called by the RenderWidgetHost when an ambiguous gesture is detected to
415 // show the disambiguation popup bubble.
416 virtual void ShowDisambiguationPopup(const gfx::Rect& rect_pixels,
417 const SkBitmap& zoomed_bitmap);
419 // Called by the WebContentsImpl when a user tries to navigate a new page on
421 virtual void OnDidNavigateMainFrameToNewPage();
423 // Instructs the view to not drop the surface even when the view is hidden.
424 virtual void LockCompositingSurface() = 0;
425 virtual void UnlockCompositingSurface() = 0;
426 virtual void UpdateFixedElementHeightAtPoint(int height) {};
428 // Add and remove observers for lifetime event notifications. The order in
429 // which notifications are sent to observers is undefined. Clients must be
430 // sure to remove the observer before they go away.
431 void AddObserver(RenderWidgetHostViewBaseObserver* observer);
432 void RemoveObserver(RenderWidgetHostViewBaseObserver* observer);
434 // Returns a reference to the current instance of TextInputManager. The
435 // reference is obtained from RenderWidgetHostDelegate. The first time a non-
436 // null reference is obtained, its value is cached in |text_input_manager_|
437 // and this view is registered with it. The RWHV will unregister from the
438 // TextInputManager if it is destroyed or if the TextInputManager itself is
439 // destroyed. The unregistration of the RWHV from TextInputManager is
440 // necessary and must be done by explicitly calling
441 // TextInputManager::Unregister.
442 // It is safer to use this method rather than directly dereferencing
443 // |text_input_manager_|.
444 TextInputManager* GetTextInputManager();
446 bool is_fullscreen() { return is_fullscreen_; }
448 // Exposed for testing.
449 virtual bool IsChildFrameForTesting() const;
450 virtual cc::SurfaceId SurfaceIdForTesting() const;
453 // Interface class only, do not construct.
454 RenderWidgetHostViewBase();
456 void NotifyObserversAboutShutdown();
458 // Is this a fullscreen view?
461 // Whether this view is a popup and what kind of popup it is (select,
463 blink::WebPopupType popup_type_;
465 // The background color of the web content.
466 SkColor background_color_;
468 // While the mouse is locked, the cursor is hidden from the user. Mouse events
469 // are still generated. However, the position they report is the last known
470 // mouse position just as mouse lock was entered; the movement they report
471 // indicates what the change in position of the mouse would be had it not been
475 // Whether we are showing a context menu.
476 bool showing_context_menu_;
478 // TODO(ekaramad): In aura, text selection tracking for IME is done through the
479 // TextInputManager. We still need the following variables for other platforms.
480 // Remove them when tracking is done by TextInputManager on all platforms
481 // (https://crbug.com/578168 and https://crbug.com/602427).
482 #if !defined(USE_AURA)
483 // A buffer containing the text inside and around the current selection range.
484 base::string16 selection_text_;
486 // The offset of the text stored in |selection_text_| relative to the start of
488 size_t selection_text_offset_;
490 // The current selection range relative to the start of the web page.
491 gfx::Range selection_range_;
494 // The scale factor of the display the renderer is currently on.
495 float current_device_scale_factor_;
497 // The orientation of the display the renderer is currently on.
498 display::Display::Rotation current_display_rotation_;
500 // A reference to current TextInputManager instance this RWHV is registered
501 // with. This is initially nullptr until the first time the view calls
502 // GetTextInputManager(). It also becomes nullptr when TextInputManager is
503 // destroyed before the RWHV is destroyed.
504 TextInputManager* text_input_manager_;
509 gfx::Rect current_display_area_;
511 uint32_t renderer_frame_number_;
513 base::OneShotTimer flush_input_timer_;
515 base::ObserverList<RenderWidgetHostViewBaseObserver> observers_;
517 base::WeakPtrFactory<RenderWidgetHostViewBase> weak_factory_;
519 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase);
522 } // namespace content
524 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_