Roll src/third_party/skia 21b998b:bda7da8
[chromium-blink-merge.git] / content / browser / renderer_host / render_widget_host_view_base.h
blob5b3070da26997d5d972ab4b78e4fd13280015272
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_
8 #include <string>
9 #include <vector>
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"
33 class SkBitmap;
35 struct AccessibilityHostMsg_EventParams;
36 struct ViewHostMsg_SelectionBounds_Params;
37 struct ViewHostMsg_TextInputState_Params;
39 namespace media {
40 class VideoFrame;
43 namespace blink {
44 struct WebScreenInfo;
47 namespace content {
48 class BrowserAccessibilityDelegate;
49 class BrowserAccessibilityManager;
50 class SyntheticGesture;
51 class SyntheticGestureTarget;
52 class WebCursor;
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 {
60 public:
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
88 // to the view.
89 uint32 RendererFrameNumber();
91 // Called each time the RenderWidgetHost receives a new frame for display from
92 // the renderer.
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,
109 size_t offset,
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
120 // URL-bar.
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|
129 // being forwarded.
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
178 // enabled.
179 virtual uint32_t GetSurfaceIdNamespace();
181 // When there are multiple RenderWidgetHostViews for a single page, input
182 // events need to be targeted to the correct one for handling. The following
183 // methods are invoked on the RenderWidgetHostView that should be able to
184 // properly handle the event (i.e. it has focus for keyboard events, or has
185 // been identified by hit testing mouse, touch or gesture events).
186 virtual uint32_t SurfaceIdNamespaceAtPoint(const gfx::Point& point,
187 gfx::Point* transformed_point);
189 //----------------------------------------------------------------------------
190 // The following static methods are implemented by each platform.
192 static void GetDefaultScreenInfo(blink::WebScreenInfo* results);
194 //----------------------------------------------------------------------------
195 // The following pure virtual methods are implemented by derived classes.
197 // Perform all the initialization steps necessary for this object to represent
198 // a popup (such as a <select> dropdown), then shows the popup at |pos|.
199 virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
200 const gfx::Rect& bounds) = 0;
202 // Perform all the initialization steps necessary for this object to represent
203 // a full screen window.
204 // |reference_host_view| is the view associated with the creating page that
205 // helps to position the full screen widget on the correct monitor.
206 virtual void InitAsFullscreen(RenderWidgetHostView* reference_host_view) = 0;
208 // Moves all plugin windows as described in the given list.
209 // |scroll_offset| is the scroll offset of the render view.
210 virtual void MovePluginWindows(
211 const std::vector<WebPluginGeometry>& moves) = 0;
213 // Sets the cursor to the one associated with the specified cursor_type
214 virtual void UpdateCursor(const WebCursor& cursor) = 0;
216 // Indicates whether the page has finished loading.
217 virtual void SetIsLoading(bool is_loading) = 0;
219 // Updates the state of the input method attached to the view.
220 virtual void TextInputStateChanged(
221 const ViewHostMsg_TextInputState_Params& params) = 0;
223 // Cancel the ongoing composition of the input method attached to the view.
224 virtual void ImeCancelComposition() = 0;
226 // Notifies the View that the renderer has ceased to exist.
227 virtual void RenderProcessGone(base::TerminationStatus status,
228 int error_code) = 0;
230 // Notifies the View that the renderer's host has ceased to exist.
231 // The default implementation of this is a no-op. This hack exists to fix
232 // a crash on the branch.
233 // TODO(ccameron): Clean this up.
234 // http://crbug.com/404828
235 virtual void RenderWidgetHostGone() {}
237 // Tells the View to destroy itself.
238 virtual void Destroy() = 0;
240 // Tells the View that the tooltip text for the current mouse position over
241 // the page has changed.
242 virtual void SetTooltipText(const base::string16& tooltip_text) = 0;
244 // Notifies the View that the renderer selection bounds has changed.
245 // |start_rect| and |end_rect| are the bounds end of the selection in the
246 // coordinate system of the render view. |start_direction| and |end_direction|
247 // indicates the direction at which the selection was made on touch devices.
248 virtual void SelectionBoundsChanged(
249 const ViewHostMsg_SelectionBounds_Params& params) = 0;
251 // Copies the contents of the compositing surface, providing a new SkBitmap
252 // result via an asynchronously-run |callback|. |src_subrect| is specified in
253 // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
254 // physical for Android), and is the region to be copied from this view. When
255 // |src_subrect| is empty then the whole surface will be copied. The copy is
256 // then scaled to a SkBitmap of size |dst_size|. If |dst_size| is empty then
257 // output will be unscaled. |callback| is run with true on success,
258 // false otherwise. A smaller region than |src_subrect| may be copied
259 // if the underlying surface is smaller than |src_subrect|.
260 virtual void CopyFromCompositingSurface(
261 const gfx::Rect& src_subrect,
262 const gfx::Size& dst_size,
263 ReadbackRequestCallback& callback,
264 const SkColorType preferred_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
281 // succeed.
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,
314 size_t start_offset,
315 size_t end_offset);
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
323 // main frame.
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;
330 #endif
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;
339 #endif
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;
346 #if defined(OS_WIN)
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);
357 #endif
359 protected:
360 // Interface class only, do not construct.
361 RenderWidgetHostViewBase();
363 #if defined(OS_WIN)
364 // Shared implementation of MovePluginWindows for use by win and aura/wina.
365 static void MovePluginWindowsHelper(
366 HWND parent,
367 const std::vector<WebPluginGeometry>& moves);
369 static void PaintPluginWindowsHelper(
370 HWND parent,
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);
376 #endif
378 // Whether this view is a popup and what kind of popup it is (select,
379 // autofill...).
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
389 // locked.
390 bool mouse_locked_;
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
399 // the web page.
400 size_t selection_text_offset_;
402 // The current selection range relative to the start of the web page.
403 gfx::Range selection_range_;
405 protected:
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
413 // renderer.
414 bool pinch_zoom_enabled_;
416 private:
417 void FlushInput();
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_