cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / browser / renderer_host / render_widget_host_view_base.h
blobe6e3ec69b9e0cdabd3c8fa91519526638b823518
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 #if defined(OS_MACOSX)
9 #include <OpenGL/OpenGL.h>
10 #endif
12 #include <string>
13 #include <vector>
15 #include "base/memory/scoped_ptr.h"
16 #include "base/callback_forward.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/render_widget_host_view.h"
24 #include "ipc/ipc_listener.h"
25 #include "third_party/WebKit/public/platform/WebScreenOrientationType.h"
26 #include "third_party/WebKit/public/web/WebPopupType.h"
27 #include "third_party/WebKit/public/web/WebTextDirection.h"
28 #include "ui/base/ime/text_input_mode.h"
29 #include "ui/base/ime/text_input_type.h"
30 #include "ui/gfx/display.h"
31 #include "ui/gfx/native_widget_types.h"
32 #include "ui/gfx/range/range.h"
33 #include "ui/gfx/rect.h"
34 #include "ui/surface/transport_dib.h"
36 class SkBitmap;
38 struct AccessibilityHostMsg_EventParams;
39 struct ViewHostMsg_SelectionBounds_Params;
41 namespace media {
42 class VideoFrame;
45 namespace blink {
46 struct WebScreenInfo;
49 namespace content {
50 class BrowserAccessibilityDelegate;
51 class BrowserAccessibilityManager;
52 class SyntheticGesture;
53 class SyntheticGestureTarget;
54 class WebCursor;
55 struct DidOverscrollParams;
56 struct NativeWebKeyboardEvent;
57 struct WebPluginGeometry;
59 // TODO(Sikugu): Though we have the return status of the result here,
60 // we should add the reason for failure as a new parameter to handle cases
61 // efficiently.
62 typedef const base::Callback<void(bool, const SkBitmap&)>
63 CopyFromCompositingSurfaceCallback;
65 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
66 class CONTENT_EXPORT RenderWidgetHostViewBase : public RenderWidgetHostView,
67 public IPC::Listener {
68 public:
69 ~RenderWidgetHostViewBase() override;
71 // RenderWidgetHostView implementation.
72 void SetBackgroundColor(SkColor color) override;
73 void SetBackgroundColorToDefault() final override;
74 bool GetBackgroundOpaque() override;
75 ui::TextInputClient* GetTextInputClient() override;
76 bool IsShowingContextMenu() const override;
77 void SetShowingContextMenu(bool showing_menu) override;
78 base::string16 GetSelectedText() const override;
79 bool IsMouseLocked() override;
80 gfx::Size GetVisibleViewportSize() const override;
81 void SetInsets(const gfx::Insets& insets) override;
82 void BeginFrameSubscription(
83 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) override;
84 void EndFrameSubscription() override;
86 // IPC::Listener implementation:
87 bool OnMessageReceived(const IPC::Message& msg) override;
89 // Called by the host when the input flush has completed.
90 void OnDidFlushInput();
92 void SetPopupType(blink::WebPopupType popup_type);
94 blink::WebPopupType GetPopupType();
96 // Return a value that is incremented each time the renderer swaps a new frame
97 // to the view.
98 uint32 RendererFrameNumber();
100 // Called each time the RenderWidgetHost receives a new frame for display from
101 // the renderer.
102 void DidReceiveRendererFrame();
104 // Notification that a resize or move session ended on the native widget.
105 void UpdateScreenInfo(gfx::NativeView view);
107 // Tells if the display property (work area/scale factor) has
108 // changed since the last time.
109 bool HasDisplayPropertyChanged(gfx::NativeView view);
111 base::WeakPtr<RenderWidgetHostViewBase> GetWeakPtr();
113 //----------------------------------------------------------------------------
114 // The following methods can be overridden by derived classes.
116 // Notifies the View that the renderer text selection has changed.
117 virtual void SelectionChanged(const base::string16& text,
118 size_t offset,
119 const gfx::Range& range);
121 // The requested size of the renderer. May differ from GetViewBounds().size()
122 // when the view requires additional throttling.
123 virtual gfx::Size GetRequestedRendererSize() const;
125 // The size of the view's backing surface in non-DPI-adjusted pixels.
126 virtual gfx::Size GetPhysicalBackingSize() const;
128 // The amount that the viewport size given to Blink is shrunk by the URL-bar.
129 virtual float GetTopControlsLayoutHeight() const;
131 // Called prior to forwarding input event messages to the renderer, giving
132 // the view a chance to perform in-process event filtering or processing.
133 // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event|
134 // being forwarded.
135 virtual InputEventAckState FilterInputEvent(
136 const blink::WebInputEvent& input_event);
138 // Called by the host when it requires an input flush; the flush call should
139 // by synchronized with BeginFrame.
140 virtual void OnSetNeedsFlushInput();
142 virtual void WheelEventAck(const blink::WebMouseWheelEvent& event,
143 InputEventAckState ack_result);
145 virtual void GestureEventAck(const blink::WebGestureEvent& event,
146 InputEventAckState ack_result);
148 // Create a platform specific SyntheticGestureTarget implementation that will
149 // be used to inject synthetic input events.
150 virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget();
152 // Return true if frame subscription is supported on this platform.
153 virtual bool CanSubscribeFrame() const;
155 // Create a BrowserAccessibilityManager for this view.
156 virtual BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
157 BrowserAccessibilityDelegate* delegate);
159 virtual void AccessibilityShowMenu(const gfx::Point& point);
160 virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds);
161 virtual gfx::AcceleratedWidget AccessibilityGetAcceleratedWidget();
162 virtual gfx::NativeViewAccessible AccessibilityGetNativeViewAccessible();
164 virtual SkColorType PreferredReadbackFormat();
166 // Informs that the focused DOM node has changed.
167 virtual void FocusedNodeChanged(bool is_editable_node) {}
169 virtual void OnSwapCompositorFrame(uint32 output_surface_id,
170 scoped_ptr<cc::CompositorFrame> frame) {}
172 // Because the associated remote WebKit instance can asynchronously
173 // prevent-default on a dispatched touch event, the touch events are queued in
174 // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
175 // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
176 // or ignored (when |ack_result| is CONSUMED).
177 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
178 InputEventAckState ack_result) {}
180 virtual void DidOverscroll(const DidOverscrollParams& params) {}
182 virtual void DidStopFlinging() {}
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& pos) = 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 // Notifies the View that it has become visible.
204 virtual void WasShown() = 0;
206 // Notifies the View that it has been hidden.
207 virtual void WasHidden() = 0;
209 // Moves all plugin windows as described in the given list.
210 // |scroll_offset| is the scroll offset of the render view.
211 virtual void MovePluginWindows(
212 const std::vector<WebPluginGeometry>& moves) = 0;
214 // Take focus from the associated View component.
215 virtual void Blur() = 0;
217 // Sets the cursor to the one associated with the specified cursor_type
218 virtual void UpdateCursor(const WebCursor& cursor) = 0;
220 // Indicates whether the page has finished loading.
221 virtual void SetIsLoading(bool is_loading) = 0;
223 // Updates the type of the input method attached to the view.
224 virtual void TextInputTypeChanged(ui::TextInputType type,
225 ui::TextInputMode mode,
226 bool can_compose_inline,
227 int flags) = 0;
229 // Cancel the ongoing composition of the input method attached to the view.
230 virtual void ImeCancelComposition() = 0;
232 // Notifies the View that the renderer has ceased to exist.
233 virtual void RenderProcessGone(base::TerminationStatus status,
234 int error_code) = 0;
236 // Notifies the View that the renderer's host has ceased to exist.
237 // The default implementation of this is a no-op. This hack exists to fix
238 // a crash on the branch.
239 // TODO(ccameron): Clean this up.
240 // http://crbug.com/404828
241 virtual void RenderWidgetHostGone() {}
243 // Tells the View to destroy itself.
244 virtual void Destroy() = 0;
246 // Tells the View that the tooltip text for the current mouse position over
247 // the page has changed.
248 virtual void SetTooltipText(const base::string16& tooltip_text) = 0;
250 // Notifies the View that the renderer selection bounds has changed.
251 // |start_rect| and |end_rect| are the bounds end of the selection in the
252 // coordinate system of the render view. |start_direction| and |end_direction|
253 // indicates the direction at which the selection was made on touch devices.
254 virtual void SelectionBoundsChanged(
255 const ViewHostMsg_SelectionBounds_Params& params) = 0;
257 // Copies the contents of the compositing surface, providing a new SkBitmap
258 // result via an asynchronously-run |callback|. |src_subrect| is specified in
259 // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
260 // physical for Android), and is the region to be copied from this view. The
261 // copy is then scaled to a SkBitmap of size |dst_size|. |callback| is run
262 // with true on success, false otherwise. A smaller region than |src_subrect|
263 // may be copied if the underlying surface is smaller than |src_subrect|.
264 virtual void CopyFromCompositingSurface(
265 const gfx::Rect& src_subrect,
266 const gfx::Size& dst_size,
267 CopyFromCompositingSurfaceCallback& callback,
268 const SkColorType color_type) = 0;
270 // Copies the contents of the compositing surface, populating the given
271 // |target| with YV12 image data. |src_subrect| is specified in layer space
272 // coordinates for the current platform (e.g., DIP for Aura/Mac, physical for
273 // Android), and is the region to be copied from this view. The copy is then
274 // scaled and letterboxed with black borders to fit |target|. Finally,
275 // |callback| is asynchronously run with true/false for
276 // success/failure. |target| must point to an allocated, YV12 video frame of
277 // the intended size. This operation will fail if there is no available
278 // compositing surface.
279 virtual void CopyFromCompositingSurfaceToVideoFrame(
280 const gfx::Rect& src_subrect,
281 const scoped_refptr<media::VideoFrame>& target,
282 const base::Callback<void(bool)>& callback) = 0;
284 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
285 // succeed.
287 // TODO(nick): When VideoFrame copies are broadly implemented, this method
288 // should be renamed to HasCompositingSurface(), or unified with
289 // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
290 virtual bool CanCopyToVideoFrame() const = 0;
292 // DEPRECATED. Called when an accelerated compositing surface is initialized.
293 virtual void AcceleratedSurfaceInitialized(int route_id) {}
295 // Return true if the view has an accelerated surface that contains the last
296 // presented frame for the view. If |desired_size| is non-empty, true is
297 // returned only if the accelerated surface size matches.
298 virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) = 0;
300 // Compute the orientation type of the display assuming it is a mobile device.
301 static blink::WebScreenOrientationType GetOrientationTypeForMobile(
302 const gfx::Display& display);
304 // Compute the orientation type of the display assuming it is a desktop.
305 static blink::WebScreenOrientationType GetOrientationTypeForDesktop(
306 const gfx::Display& display);
308 virtual void GetScreenInfo(blink::WebScreenInfo* results) = 0;
310 // Gets the bounds of the window, in screen coordinates.
311 virtual gfx::Rect GetBoundsInRootWindow() = 0;
313 virtual gfx::GLSurfaceHandle GetCompositingSurface() = 0;
315 // Called by the RenderFrameHost when it receives an IPC response to a
316 // TextSurroundingSelectionRequest.
317 virtual void OnTextSurroundingSelectionResponse(const base::string16& content,
318 size_t start_offset,
319 size_t end_offset);
321 // Called by the RenderWidgetHost when an ambiguous gesture is detected to
322 // show the disambiguation popup bubble.
323 virtual void ShowDisambiguationPopup(const gfx::Rect& rect_pixels,
324 const SkBitmap& zoomed_bitmap);
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 #if defined(OS_MACOSX) || defined(USE_AURA) || defined(OS_ANDROID)
342 // Updates the range of the marked text in an IME composition.
343 virtual void ImeCompositionRangeChanged(
344 const gfx::Range& range,
345 const std::vector<gfx::Rect>& character_bounds) = 0;
346 #endif
348 #if defined(OS_WIN)
349 virtual void SetParentNativeViewAccessible(
350 gfx::NativeViewAccessible accessible_parent) = 0;
352 // Returns an HWND that's given as the parent window for windowless Flash to
353 // workaround crbug.com/301548.
354 virtual gfx::NativeViewId GetParentForWindowlessPlugin() const = 0;
356 // The callback that DetachPluginsHelper calls for each child window. Call
357 // this directly if you want to do custom filtering on plugin windows first.
358 static void DetachPluginWindowsCallback(HWND window);
359 #endif
361 protected:
362 // Interface class only, do not construct.
363 RenderWidgetHostViewBase();
365 #if defined(OS_WIN)
366 // Shared implementation of MovePluginWindows for use by win and aura/wina.
367 static void MovePluginWindowsHelper(
368 HWND parent,
369 const std::vector<WebPluginGeometry>& moves);
371 static void PaintPluginWindowsHelper(
372 HWND parent,
373 const gfx::Rect& damaged_screen_rect);
375 // Needs to be called before the HWND backing the view goes away to avoid
376 // crashes in Windowed plugins.
377 static void DetachPluginsHelper(HWND parent);
378 #endif
380 // Whether this view is a popup and what kind of popup it is (select,
381 // autofill...).
382 blink::WebPopupType popup_type_;
384 // The background color of the web content.
385 SkColor background_color_;
387 // While the mouse is locked, the cursor is hidden from the user. Mouse events
388 // are still generated. However, the position they report is the last known
389 // mouse position just as mouse lock was entered; the movement they report
390 // indicates what the change in position of the mouse would be had it not been
391 // locked.
392 bool mouse_locked_;
394 // Whether we are showing a context menu.
395 bool showing_context_menu_;
397 // A buffer containing the text inside and around the current selection range.
398 base::string16 selection_text_;
400 // The offset of the text stored in |selection_text_| relative to the start of
401 // the web page.
402 size_t selection_text_offset_;
404 // The current selection range relative to the start of the web page.
405 gfx::Range selection_range_;
407 protected:
408 // The scale factor of the display the renderer is currently on.
409 float current_device_scale_factor_;
411 // The orientation of the display the renderer is currently on.
412 gfx::Display::Rotation current_display_rotation_;
414 // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
415 // renderer.
416 bool pinch_zoom_enabled_;
418 private:
419 void FlushInput();
421 gfx::Rect current_display_area_;
423 uint32 renderer_frame_number_;
425 base::OneShotTimer<RenderWidgetHostViewBase> flush_input_timer_;
427 base::WeakPtrFactory<RenderWidgetHostViewBase> weak_factory_;
429 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase);
432 } // namespace content
434 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_