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. 4 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_ 7 8 #if defined(OS_MACOSX) 9 #include <OpenGL/OpenGL.h> 10 #endif 11 12 #include <string> 13 #include <vector> 14 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" 35 36 class SkBitmap; 37 38 struct AccessibilityHostMsg_EventParams; 39 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params; 40 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params; 41 struct ViewHostMsg_SelectionBounds_Params; 42 43 namespace media { 44 class VideoFrame; 45 } 46 47 namespace blink { 48 struct WebScreenInfo; 49 } 50 51 namespace content { 52 class BrowserAccessibilityDelegate; 53 class BrowserAccessibilityManager; 54 class SyntheticGesture; 55 class SyntheticGestureTarget; 56 class WebCursor; 57 struct DidOverscrollParams; 58 struct NativeWebKeyboardEvent; 59 struct WebPluginGeometry; 60 61 // TODO(Sikugu): Though we have the return status of the result here, 62 // we should add the reason for failure as a new parameter to handle cases 63 // efficiently. 64 typedef const base::Callback<void(bool, const SkBitmap&)> 65 CopyFromCompositingSurfaceCallback; 66 67 // Basic implementation shared by concrete RenderWidgetHostView subclasses. 68 class CONTENT_EXPORT RenderWidgetHostViewBase : public RenderWidgetHostView, 69 public IPC::Listener { 70 public: 71 virtual ~RenderWidgetHostViewBase(); 72 73 // RenderWidgetHostView implementation. 74 virtual void SetBackgroundOpaque(bool opaque) OVERRIDE; 75 virtual bool GetBackgroundOpaque() OVERRIDE; 76 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; 77 virtual bool IsShowingContextMenu() const OVERRIDE; 78 virtual void SetShowingContextMenu(bool showing_menu) OVERRIDE; 79 virtual base::string16 GetSelectedText() const OVERRIDE; 80 virtual bool IsMouseLocked() OVERRIDE; 81 virtual gfx::Size GetVisibleViewportSize() const OVERRIDE; 82 virtual void SetInsets(const gfx::Insets& insets) OVERRIDE; 83 virtual void BeginFrameSubscription( 84 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) OVERRIDE; 85 virtual void EndFrameSubscription() OVERRIDE; 86 87 // IPC::Listener implementation: 88 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; 89 90 // Called by the host when the input flush has completed. 91 void OnDidFlushInput(); 92 93 void SetPopupType(blink::WebPopupType popup_type); 94 95 blink::WebPopupType GetPopupType(); 96 97 // Return a value that is incremented each time the renderer swaps a new frame 98 // to the view. 99 uint32 RendererFrameNumber(); 100 101 // Called each time the RenderWidgetHost receives a new frame for display from 102 // the renderer. 103 void DidReceiveRendererFrame(); 104 105 // Notification that a resize or move session ended on the native widget. 106 void UpdateScreenInfo(gfx::NativeView view); 107 108 // Tells if the display property (work area/scale factor) has 109 // changed since the last time. 110 bool HasDisplayPropertyChanged(gfx::NativeView view); 111 112 base::WeakPtr<RenderWidgetHostViewBase> GetWeakPtr(); 113 114 //---------------------------------------------------------------------------- 115 // The following methods can be overridden by derived classes. 116 117 // Notifies the View that the renderer text selection has changed. 118 virtual void SelectionChanged(const base::string16& text, 119 size_t offset, 120 const gfx::Range& range); 121 122 // The requested size of the renderer. May differ from GetViewBounds().size() 123 // when the view requires additional throttling. 124 virtual gfx::Size GetRequestedRendererSize() const; 125 126 // The size of the view's backing surface in non-DPI-adjusted pixels. 127 virtual gfx::Size GetPhysicalBackingSize() const; 128 129 // The amount that the viewport size given to Blink is shrunk by the URL-bar. 130 virtual float GetTopControlsLayoutHeight() const; 131 132 // Called prior to forwarding input event messages to the renderer, giving 133 // the view a chance to perform in-process event filtering or processing. 134 // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event| 135 // being forwarded. 136 virtual InputEventAckState FilterInputEvent( 137 const blink::WebInputEvent& input_event); 138 139 // Called by the host when it requires an input flush; the flush call should 140 // by synchronized with BeginFrame. 141 virtual void OnSetNeedsFlushInput(); 142 143 virtual void WheelEventAck(const blink::WebMouseWheelEvent& event, 144 InputEventAckState ack_result); 145 146 virtual void GestureEventAck(const blink::WebGestureEvent& event, 147 InputEventAckState ack_result); 148 149 // Create a platform specific SyntheticGestureTarget implementation that will 150 // be used to inject synthetic input events. 151 virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget(); 152 153 // Return true if frame subscription is supported on this platform. 154 virtual bool CanSubscribeFrame() const; 155 156 // Create a BrowserAccessibilityManager for this view. 157 virtual BrowserAccessibilityManager* CreateBrowserAccessibilityManager( 158 BrowserAccessibilityDelegate* delegate); 159 160 virtual void AccessibilityShowMenu(const gfx::Point& point); 161 virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds); 162 virtual gfx::AcceleratedWidget AccessibilityGetAcceleratedWidget(); 163 virtual gfx::NativeViewAccessible AccessibilityGetNativeViewAccessible(); 164 165 virtual SkColorType PreferredReadbackFormat(); 166 167 // Informs that the focused DOM node has changed. FocusedNodeChanged(bool is_editable_node)168 virtual void FocusedNodeChanged(bool is_editable_node) {} 169 OnSwapCompositorFrame(uint32 output_surface_id,scoped_ptr<cc::CompositorFrame> frame)170 virtual void OnSwapCompositorFrame(uint32 output_surface_id, 171 scoped_ptr<cc::CompositorFrame> frame) {} 172 173 // Because the associated remote WebKit instance can asynchronously 174 // prevent-default on a dispatched touch event, the touch events are queued in 175 // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases 176 // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS) 177 // or ignored (when |ack_result| is CONSUMED). ProcessAckedTouchEvent(const TouchEventWithLatencyInfo & touch,InputEventAckState ack_result)178 virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch, 179 InputEventAckState ack_result) {} 180 DidOverscroll(const DidOverscrollParams & params)181 virtual void DidOverscroll(const DidOverscrollParams& params) {} 182 DidStopFlinging()183 virtual void DidStopFlinging() {} 184 185 //---------------------------------------------------------------------------- 186 // The following static methods are implemented by each platform. 187 188 static void GetDefaultScreenInfo(blink::WebScreenInfo* results); 189 190 //---------------------------------------------------------------------------- 191 // The following pure virtual methods are implemented by derived classes. 192 193 // Perform all the initialization steps necessary for this object to represent 194 // a popup (such as a <select> dropdown), then shows the popup at |pos|. 195 virtual void InitAsPopup(RenderWidgetHostView* parent_host_view, 196 const gfx::Rect& pos) = 0; 197 198 // Perform all the initialization steps necessary for this object to represent 199 // a full screen window. 200 // |reference_host_view| is the view associated with the creating page that 201 // helps to position the full screen widget on the correct monitor. 202 virtual void InitAsFullscreen(RenderWidgetHostView* reference_host_view) = 0; 203 204 // Notifies the View that it has become visible. 205 virtual void WasShown() = 0; 206 207 // Notifies the View that it has been hidden. 208 virtual void WasHidden() = 0; 209 210 // Moves all plugin windows as described in the given list. 211 // |scroll_offset| is the scroll offset of the render view. 212 virtual void MovePluginWindows( 213 const std::vector<WebPluginGeometry>& moves) = 0; 214 215 // Take focus from the associated View component. 216 virtual void Blur() = 0; 217 218 // Sets the cursor to the one associated with the specified cursor_type 219 virtual void UpdateCursor(const WebCursor& cursor) = 0; 220 221 // Indicates whether the page has finished loading. 222 virtual void SetIsLoading(bool is_loading) = 0; 223 224 // Updates the type of the input method attached to the view. 225 virtual void TextInputTypeChanged(ui::TextInputType type, 226 ui::TextInputMode mode, 227 bool can_compose_inline) = 0; 228 229 // Cancel the ongoing composition of the input method attached to the view. 230 virtual void ImeCancelComposition() = 0; 231 232 // Notifies the View that the renderer has ceased to exist. 233 virtual void RenderProcessGone(base::TerminationStatus status, 234 int error_code) = 0; 235 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 RenderWidgetHostGone()241 virtual void RenderWidgetHostGone() {} 242 243 // Tells the View to destroy itself. 244 virtual void Destroy() = 0; 245 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; 249 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; 256 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; 269 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; 283 284 // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to 285 // succeed. 286 // 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; 291 292 // Called when an accelerated compositing surface is initialized. 293 virtual void AcceleratedSurfaceInitialized(int host_id, int route_id) = 0; 294 // |params.window| and |params.surface_id| indicate which accelerated 295 // surface's buffers swapped. |params.renderer_id| and |params.route_id| 296 // are used to formulate a reply to the GPU process to prevent it from getting 297 // too far ahead. They may all be zero, in which case no flow control is 298 // enforced; this case is currently used for accelerated plugins. 299 virtual void AcceleratedSurfaceBuffersSwapped( 300 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params_in_pixel, 301 int gpu_host_id) = 0; 302 // Similar to above, except |params.(x|y|width|height)| define the region 303 // of the surface that changed. 304 virtual void AcceleratedSurfacePostSubBuffer( 305 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params_in_pixel, 306 int gpu_host_id) = 0; 307 308 // Release the accelerated surface temporarily. It will be recreated on the 309 // next swap buffers or post sub buffer. 310 virtual void AcceleratedSurfaceSuspend() = 0; 311 312 virtual void AcceleratedSurfaceRelease() = 0; 313 314 // Return true if the view has an accelerated surface that contains the last 315 // presented frame for the view. If |desired_size| is non-empty, true is 316 // returned only if the accelerated surface size matches. 317 virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) = 0; 318 319 // Compute the orientation type of the display assuming it is a mobile device. 320 static blink::WebScreenOrientationType GetOrientationTypeForMobile( 321 const gfx::Display& display); 322 323 // Compute the orientation type of the display assuming it is a desktop. 324 static blink::WebScreenOrientationType GetOrientationTypeForDesktop( 325 const gfx::Display& display); 326 327 virtual void GetScreenInfo(blink::WebScreenInfo* results) = 0; 328 329 // Gets the bounds of the window, in screen coordinates. 330 virtual gfx::Rect GetBoundsInRootWindow() = 0; 331 332 virtual gfx::GLSurfaceHandle GetCompositingSurface() = 0; 333 OnTextSurroundingSelectionResponse(const base::string16 & content,size_t start_offset,size_t end_offset)334 virtual void OnTextSurroundingSelectionResponse(const base::string16& content, 335 size_t start_offset, 336 size_t end_offset) {}; 337 338 #if defined(OS_ANDROID) || defined(TOOLKIT_VIEWS) || defined(USE_AURA) 339 virtual void ShowDisambiguationPopup(const gfx::Rect& rect_pixels, 340 const SkBitmap& zoomed_bitmap) = 0; 341 #endif 342 343 #if defined(OS_ANDROID) 344 // Instructs the view to not drop the surface even when the view is hidden. 345 virtual void LockCompositingSurface() = 0; 346 virtual void UnlockCompositingSurface() = 0; 347 #endif 348 349 #if defined(OS_MACOSX) 350 // Does any event handling necessary for plugin IME; should be called after 351 // the plugin has already had a chance to process the event. If plugin IME is 352 // not enabled, this is a no-op, so it is always safe to call. 353 // Returns true if the event was handled by IME. 354 virtual bool PostProcessEventForPluginIme( 355 const NativeWebKeyboardEvent& event) = 0; 356 #endif 357 358 #if defined(OS_MACOSX) || defined(USE_AURA) 359 // Updates the range of the marked text in an IME composition. 360 virtual void ImeCompositionRangeChanged( 361 const gfx::Range& range, 362 const std::vector<gfx::Rect>& character_bounds) = 0; 363 #endif 364 365 #if defined(OS_WIN) 366 virtual void SetParentNativeViewAccessible( 367 gfx::NativeViewAccessible accessible_parent) = 0; 368 369 // Returns an HWND that's given as the parent window for windowless Flash to 370 // workaround crbug.com/301548. 371 virtual gfx::NativeViewId GetParentForWindowlessPlugin() const = 0; 372 373 // The callback that DetachPluginsHelper calls for each child window. Call 374 // this directly if you want to do custom filtering on plugin windows first. 375 static void DetachPluginWindowsCallback(HWND window); 376 #endif 377 378 protected: 379 // Interface class only, do not construct. 380 RenderWidgetHostViewBase(); 381 382 #if defined(OS_WIN) 383 // Shared implementation of MovePluginWindows for use by win and aura/wina. 384 static void MovePluginWindowsHelper( 385 HWND parent, 386 const std::vector<WebPluginGeometry>& moves); 387 388 static void PaintPluginWindowsHelper( 389 HWND parent, 390 const gfx::Rect& damaged_screen_rect); 391 392 // Needs to be called before the HWND backing the view goes away to avoid 393 // crashes in Windowed plugins. 394 static void DetachPluginsHelper(HWND parent); 395 #endif 396 397 // Whether this view is a popup and what kind of popup it is (select, 398 // autofill...). 399 blink::WebPopupType popup_type_; 400 401 // When false, the background of the web content is not fully opaque. 402 bool background_opaque_; 403 404 // While the mouse is locked, the cursor is hidden from the user. Mouse events 405 // are still generated. However, the position they report is the last known 406 // mouse position just as mouse lock was entered; the movement they report 407 // indicates what the change in position of the mouse would be had it not been 408 // locked. 409 bool mouse_locked_; 410 411 // Whether we are showing a context menu. 412 bool showing_context_menu_; 413 414 // A buffer containing the text inside and around the current selection range. 415 base::string16 selection_text_; 416 417 // The offset of the text stored in |selection_text_| relative to the start of 418 // the web page. 419 size_t selection_text_offset_; 420 421 // The current selection range relative to the start of the web page. 422 gfx::Range selection_range_; 423 424 protected: 425 // The scale factor of the display the renderer is currently on. 426 float current_device_scale_factor_; 427 428 // The orientation of the display the renderer is currently on. 429 gfx::Display::Rotation current_display_rotation_; 430 431 // Whether pinch-to-zoom should be enabled and pinch events forwarded to the 432 // renderer. 433 bool pinch_zoom_enabled_; 434 435 private: 436 void FlushInput(); 437 438 gfx::Rect current_display_area_; 439 440 uint32 renderer_frame_number_; 441 442 base::OneShotTimer<RenderWidgetHostViewBase> flush_input_timer_; 443 444 base::WeakPtrFactory<RenderWidgetHostViewBase> weak_factory_; 445 446 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase); 447 }; 448 449 } // namespace content 450 451 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_ 452