Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / renderer_host / render_widget_host_view_base.h
blob62daf7f3d3a9242f2b6d3d2d4803cbd092536783
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 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
40 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
41 struct ViewHostMsg_SelectionBounds_Params;
42 struct ViewHostMsg_TextInputState_Params;
44 namespace media {
45 class VideoFrame;
48 namespace blink {
49 struct WebScreenInfo;
52 namespace content {
53 class BrowserAccessibilityDelegate;
54 class BrowserAccessibilityManager;
55 class SyntheticGesture;
56 class SyntheticGestureTarget;
57 class WebCursor;
58 struct DidOverscrollParams;
59 struct NativeWebKeyboardEvent;
60 struct WebPluginGeometry;
62 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
63 class CONTENT_EXPORT RenderWidgetHostViewBase : public RenderWidgetHostView,
64 public IPC::Listener {
65 public:
66 virtual ~RenderWidgetHostViewBase();
68 // RenderWidgetHostView implementation.
69 virtual void SetBackgroundOpaque(bool opaque) OVERRIDE;
70 virtual bool GetBackgroundOpaque() OVERRIDE;
71 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE;
72 virtual bool IsShowingContextMenu() const OVERRIDE;
73 virtual void SetShowingContextMenu(bool showing_menu) OVERRIDE;
74 virtual base::string16 GetSelectedText() const OVERRIDE;
75 virtual bool IsMouseLocked() OVERRIDE;
76 virtual gfx::Size GetVisibleViewportSize() const OVERRIDE;
77 virtual void SetInsets(const gfx::Insets& insets) OVERRIDE;
78 virtual void BeginFrameSubscription(
79 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) OVERRIDE;
80 virtual void EndFrameSubscription() OVERRIDE;
82 // IPC::Listener implementation:
83 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
85 // Called by the host when the input flush has completed.
86 void OnDidFlushInput();
88 void SetPopupType(blink::WebPopupType popup_type);
90 blink::WebPopupType GetPopupType();
92 // Return a value that is incremented each time the renderer swaps a new frame
93 // to the view.
94 uint32 RendererFrameNumber();
96 // Called each time the RenderWidgetHost receives a new frame for display from
97 // the renderer.
98 void DidReceiveRendererFrame();
100 // Notification that a resize or move session ended on the native widget.
101 void UpdateScreenInfo(gfx::NativeView view);
103 // Tells if the display property (work area/scale factor) has
104 // changed since the last time.
105 bool HasDisplayPropertyChanged(gfx::NativeView view);
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,
112 size_t offset,
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 // The amount that the viewport size given to Blink is shrunk by the URL-bar.
123 virtual float GetTopControlsLayoutHeight() const;
125 // Called prior to forwarding input event messages to the renderer, giving
126 // the view a chance to perform in-process event filtering or processing.
127 // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event|
128 // being forwarded.
129 virtual InputEventAckState FilterInputEvent(
130 const blink::WebInputEvent& input_event);
132 // Called by the host when it requires an input flush; the flush call should
133 // by synchronized with BeginFrame.
134 virtual void OnSetNeedsFlushInput();
136 virtual void WheelEventAck(const blink::WebMouseWheelEvent& event,
137 InputEventAckState ack_result);
139 virtual void GestureEventAck(const blink::WebGestureEvent& event,
140 InputEventAckState ack_result);
142 // Create a platform specific SyntheticGestureTarget implementation that will
143 // be used to inject synthetic input events.
144 virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget();
146 // Return true if frame subscription is supported on this platform.
147 virtual bool CanSubscribeFrame() const;
149 // Create a BrowserAccessibilityManager for this view.
150 virtual BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
151 BrowserAccessibilityDelegate* delegate);
153 virtual void AccessibilityShowMenu(const gfx::Point& point);
154 virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds);
155 virtual gfx::AcceleratedWidget AccessibilityGetAcceleratedWidget();
156 virtual gfx::NativeViewAccessible AccessibilityGetNativeViewAccessible();
158 virtual SkColorType PreferredReadbackFormat();
160 // Informs that the focused DOM node has changed.
161 virtual void FocusedNodeChanged(bool is_editable_node) {}
163 virtual void OnSwapCompositorFrame(uint32 output_surface_id,
164 scoped_ptr<cc::CompositorFrame> frame) {}
166 // Because the associated remote WebKit instance can asynchronously
167 // prevent-default on a dispatched touch event, the touch events are queued in
168 // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
169 // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
170 // or ignored (when |ack_result| is CONSUMED).
171 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
172 InputEventAckState ack_result) {}
174 virtual void DidOverscroll(const DidOverscrollParams& params) {}
176 virtual void DidStopFlinging() {}
178 //----------------------------------------------------------------------------
179 // The following static methods are implemented by each platform.
181 static void GetDefaultScreenInfo(blink::WebScreenInfo* results);
183 //----------------------------------------------------------------------------
184 // The following pure virtual methods are implemented by derived classes.
186 // Perform all the initialization steps necessary for this object to represent
187 // a popup (such as a <select> dropdown), then shows the popup at |pos|.
188 virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
189 const gfx::Rect& pos) = 0;
191 // Perform all the initialization steps necessary for this object to represent
192 // a full screen window.
193 // |reference_host_view| is the view associated with the creating page that
194 // helps to position the full screen widget on the correct monitor.
195 virtual void InitAsFullscreen(RenderWidgetHostView* reference_host_view) = 0;
197 // Notifies the View that it has become visible.
198 virtual void WasShown() = 0;
200 // Notifies the View that it has been hidden.
201 virtual void WasHidden() = 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 // Take focus from the associated View component.
209 virtual void Blur() = 0;
211 // Sets the cursor to the one associated with the specified cursor_type
212 virtual void UpdateCursor(const WebCursor& cursor) = 0;
214 // Indicates whether the page has finished loading.
215 virtual void SetIsLoading(bool is_loading) = 0;
217 // Updates the type of the input method attached to the view.
218 virtual void TextInputStateChanged(
219 const ViewHostMsg_TextInputState_Params& params) = 0;
221 // Cancel the ongoing composition of the input method attached to the view.
222 virtual void ImeCancelComposition() = 0;
224 // Notifies the View that the renderer has ceased to exist.
225 virtual void RenderProcessGone(base::TerminationStatus status,
226 int error_code) = 0;
228 // Tells the View to destroy itself.
229 virtual void Destroy() = 0;
231 // Tells the View that the tooltip text for the current mouse position over
232 // the page has changed.
233 virtual void SetTooltipText(const base::string16& tooltip_text) = 0;
235 // Notifies the View that the renderer selection bounds has changed.
236 // |start_rect| and |end_rect| are the bounds end of the selection in the
237 // coordinate system of the render view. |start_direction| and |end_direction|
238 // indicates the direction at which the selection was made on touch devices.
239 virtual void SelectionBoundsChanged(
240 const ViewHostMsg_SelectionBounds_Params& params) = 0;
242 // Copies the contents of the compositing surface, providing a new SkBitmap
243 // result via an asynchronously-run |callback|. |src_subrect| is specified in
244 // layer space coordinates for the current platform (e.g., DIP for Aura/Mac,
245 // physical for Android), and is the region to be copied from this view. The
246 // copy is then scaled to a SkBitmap of size |dst_size|. |callback| is run
247 // with true on success, false otherwise. A smaller region than |src_subrect|
248 // may be copied if the underlying surface is smaller than |src_subrect|.
249 virtual void CopyFromCompositingSurface(
250 const gfx::Rect& src_subrect,
251 const gfx::Size& dst_size,
252 const base::Callback<void(bool, const SkBitmap&)>& callback,
253 const SkColorType color_type) = 0;
255 // Copies the contents of the compositing surface, populating the given
256 // |target| with YV12 image data. |src_subrect| is specified in layer space
257 // coordinates for the current platform (e.g., DIP for Aura/Mac, physical for
258 // Android), and is the region to be copied from this view. The copy is then
259 // scaled and letterboxed with black borders to fit |target|. Finally,
260 // |callback| is asynchronously run with true/false for
261 // success/failure. |target| must point to an allocated, YV12 video frame of
262 // the intended size. This operation will fail if there is no available
263 // compositing surface.
264 virtual void CopyFromCompositingSurfaceToVideoFrame(
265 const gfx::Rect& src_subrect,
266 const scoped_refptr<media::VideoFrame>& target,
267 const base::Callback<void(bool)>& callback) = 0;
269 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
270 // succeed.
272 // TODO(nick): When VideoFrame copies are broadly implemented, this method
273 // should be renamed to HasCompositingSurface(), or unified with
274 // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
275 virtual bool CanCopyToVideoFrame() const = 0;
277 // Called when an accelerated compositing surface is initialized.
278 virtual void AcceleratedSurfaceInitialized(int host_id, int route_id) = 0;
279 // |params.window| and |params.surface_id| indicate which accelerated
280 // surface's buffers swapped. |params.renderer_id| and |params.route_id|
281 // are used to formulate a reply to the GPU process to prevent it from getting
282 // too far ahead. They may all be zero, in which case no flow control is
283 // enforced; this case is currently used for accelerated plugins.
284 virtual void AcceleratedSurfaceBuffersSwapped(
285 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params_in_pixel,
286 int gpu_host_id) = 0;
287 // Similar to above, except |params.(x|y|width|height)| define the region
288 // of the surface that changed.
289 virtual void AcceleratedSurfacePostSubBuffer(
290 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params_in_pixel,
291 int gpu_host_id) = 0;
293 // Release the accelerated surface temporarily. It will be recreated on the
294 // next swap buffers or post sub buffer.
295 virtual void AcceleratedSurfaceSuspend() = 0;
297 virtual void AcceleratedSurfaceRelease() = 0;
299 // Return true if the view has an accelerated surface that contains the last
300 // presented frame for the view. If |desired_size| is non-empty, true is
301 // returned only if the accelerated surface size matches.
302 virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) = 0;
304 // Compute the orientation type of the display assuming it is a mobile device.
305 static blink::WebScreenOrientationType GetOrientationTypeForMobile(
306 const gfx::Display& display);
308 // Compute the orientation type of the display assuming it is a desktop.
309 static blink::WebScreenOrientationType GetOrientationTypeForDesktop(
310 const gfx::Display& display);
312 virtual void GetScreenInfo(blink::WebScreenInfo* results) = 0;
314 // Gets the bounds of the window, in screen coordinates.
315 virtual gfx::Rect GetBoundsInRootWindow() = 0;
317 virtual gfx::GLSurfaceHandle GetCompositingSurface() = 0;
319 virtual void OnTextSurroundingSelectionResponse(const base::string16& content,
320 size_t start_offset,
321 size_t end_offset) {};
323 #if defined(OS_ANDROID)
324 virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
325 const SkBitmap& zoomed_bitmap) = 0;
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)
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 // When false, the background of the web content is not fully opaque.
385 bool background_opaque_;
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 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase);
430 } // namespace content
432 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_