Home | History | Annotate | Download | only in renderer_host
      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/web/WebPopupType.h"
     26 #include "third_party/WebKit/public/web/WebTextDirection.h"
     27 #include "ui/base/ime/text_input_mode.h"
     28 #include "ui/base/ime/text_input_type.h"
     29 #include "ui/gfx/display.h"
     30 #include "ui/gfx/native_widget_types.h"
     31 #include "ui/gfx/range/range.h"
     32 #include "ui/gfx/rect.h"
     33 #include "ui/surface/transport_dib.h"
     34 
     35 class SkBitmap;
     36 
     37 struct AccessibilityHostMsg_EventParams;
     38 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
     39 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
     40 struct ViewHostMsg_SelectionBounds_Params;
     41 struct ViewHostMsg_TextInputState_Params;
     42 
     43 namespace media {
     44 class VideoFrame;
     45 }
     46 
     47 namespace blink {
     48 struct WebScreenInfo;
     49 }
     50 
     51 namespace content {
     52 class BrowserAccessibilityManager;
     53 class SyntheticGesture;
     54 class SyntheticGestureTarget;
     55 class WebCursor;
     56 struct DidOverscrollParams;
     57 struct NativeWebKeyboardEvent;
     58 struct WebPluginGeometry;
     59 
     60 // Basic implementation shared by concrete RenderWidgetHostView subclasses.
     61 class CONTENT_EXPORT RenderWidgetHostViewBase : public RenderWidgetHostView,
     62                                                 public IPC::Listener {
     63  public:
     64   virtual ~RenderWidgetHostViewBase();
     65 
     66   // RenderWidgetHostView implementation.
     67   virtual void SetBackgroundOpaque(bool opaque) OVERRIDE;
     68   virtual bool GetBackgroundOpaque() OVERRIDE;
     69   virtual ui::TextInputClient* GetTextInputClient() OVERRIDE;
     70   virtual bool IsShowingContextMenu() const OVERRIDE;
     71   virtual void SetShowingContextMenu(bool showing_menu) OVERRIDE;
     72   virtual base::string16 GetSelectedText() const OVERRIDE;
     73   virtual bool IsMouseLocked() OVERRIDE;
     74   virtual gfx::Size GetVisibleViewportSize() const OVERRIDE;
     75   virtual void SetInsets(const gfx::Insets& insets) OVERRIDE;
     76   virtual void BeginFrameSubscription(
     77       scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) OVERRIDE;
     78   virtual void EndFrameSubscription() OVERRIDE;
     79 
     80   // IPC::Listener implementation:
     81   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
     82 
     83   // Called by the host when the input flush has completed.
     84   void OnDidFlushInput();
     85 
     86   void SetPopupType(blink::WebPopupType popup_type);
     87 
     88   blink::WebPopupType GetPopupType();
     89 
     90   // Get the BrowserAccessibilityManager if it exists, may return NULL.
     91   BrowserAccessibilityManager* GetBrowserAccessibilityManager() const;
     92 
     93   void SetBrowserAccessibilityManager(BrowserAccessibilityManager* manager);
     94 
     95   // Return a value that is incremented each time the renderer swaps a new frame
     96   // to the view.
     97   uint32 RendererFrameNumber();
     98 
     99   // Called each time the RenderWidgetHost receives a new frame for display from
    100   // the renderer.
    101   void DidReceiveRendererFrame();
    102 
    103   // Notification that a resize or move session ended on the native widget.
    104   void UpdateScreenInfo(gfx::NativeView view);
    105 
    106   // Tells if the display property (work area/scale factor) has
    107   // changed since the last time.
    108   bool HasDisplayPropertyChanged(gfx::NativeView view);
    109 
    110   //----------------------------------------------------------------------------
    111   // The following methods can be overridden by derived classes.
    112 
    113   // Notifies the View that the renderer text selection has changed.
    114   virtual void SelectionChanged(const base::string16& text,
    115                                 size_t offset,
    116                                 const gfx::Range& range);
    117 
    118   // The requested size of the renderer. May differ from GetViewBounds().size()
    119   // when the view requires additional throttling.
    120   virtual gfx::Size GetRequestedRendererSize() const;
    121 
    122   // The size of the view's backing surface in non-DPI-adjusted pixels.
    123   virtual gfx::Size GetPhysicalBackingSize() const;
    124 
    125   // The height of the physical backing surface that is overdrawn opaquely in
    126   // the browser, for example by an on-screen-keyboard (in DPI-adjusted pixels).
    127   virtual float GetOverdrawBottomHeight() const;
    128 
    129   // Called prior to forwarding input event messages to the renderer, giving
    130   // the view a chance to perform in-process event filtering or processing.
    131   // Return values of |NOT_CONSUMED| or |UNKNOWN| will result in |input_event|
    132   // being forwarded.
    133   virtual InputEventAckState FilterInputEvent(
    134       const blink::WebInputEvent& input_event);
    135 
    136   // Called by the host when it requires an input flush; the flush call should
    137   // by synchronized with BeginFrame.
    138   virtual void OnSetNeedsFlushInput();
    139 
    140   virtual void WheelEventAck(const blink::WebMouseWheelEvent& event,
    141                              InputEventAckState ack_result);
    142 
    143   virtual void GestureEventAck(const blink::WebGestureEvent& event,
    144                                InputEventAckState ack_result);
    145 
    146   // Create a platform specific SyntheticGestureTarget implementation that will
    147   // be used to inject synthetic input events.
    148   virtual scoped_ptr<SyntheticGestureTarget> CreateSyntheticGestureTarget();
    149 
    150   // Return true if frame subscription is supported on this platform.
    151   virtual bool CanSubscribeFrame() const;
    152 
    153   // Create a BrowserAccessibilityManager for this view if it's possible to
    154   // create one and if one doesn't exist already. Some ports may not create
    155   // one depending on the current state.
    156   virtual void CreateBrowserAccessibilityManagerIfNeeded();
    157 
    158   virtual void OnAccessibilitySetFocus(int acc_obj_id);
    159   virtual void AccessibilityShowMenu(int acc_obj_id);
    160   virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds);
    161 
    162   virtual SkBitmap::Config PreferredReadbackFormat();
    163 
    164   // Informs that the focused DOM node has changed.
    165   virtual void FocusedNodeChanged(bool is_editable_node) {}
    166 
    167   virtual void OnSwapCompositorFrame(uint32 output_surface_id,
    168                                      scoped_ptr<cc::CompositorFrame> frame) {}
    169 
    170   // Because the associated remote WebKit instance can asynchronously
    171   // prevent-default on a dispatched touch event, the touch events are queued in
    172   // the GestureRecognizer until invocation of ProcessAckedTouchEvent releases
    173   // it to be consumed (when |ack_result| is NOT_CONSUMED OR NO_CONSUMER_EXISTS)
    174   // or ignored (when |ack_result| is CONSUMED).
    175   virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
    176                                       InputEventAckState ack_result) {}
    177 
    178   virtual void DidOverscroll(const DidOverscrollParams& params) {}
    179 
    180   virtual void DidStopFlinging() {}
    181 
    182   //----------------------------------------------------------------------------
    183   // The following static methods are implemented by each platform.
    184 
    185   static void GetDefaultScreenInfo(blink::WebScreenInfo* results);
    186 
    187   //----------------------------------------------------------------------------
    188   // The following pure virtual methods are implemented by derived classes.
    189 
    190   // Perform all the initialization steps necessary for this object to represent
    191   // a popup (such as a <select> dropdown), then shows the popup at |pos|.
    192   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
    193                            const gfx::Rect& pos) = 0;
    194 
    195   // Perform all the initialization steps necessary for this object to represent
    196   // a full screen window.
    197   // |reference_host_view| is the view associated with the creating page that
    198   // helps to position the full screen widget on the correct monitor.
    199   virtual void InitAsFullscreen(RenderWidgetHostView* reference_host_view) = 0;
    200 
    201   // Notifies the View that it has become visible.
    202   virtual void WasShown() = 0;
    203 
    204   // Notifies the View that it has been hidden.
    205   virtual void WasHidden() = 0;
    206 
    207   // Moves all plugin windows as described in the given list.
    208   // |scroll_offset| is the scroll offset of the render view.
    209   virtual void MovePluginWindows(
    210       const std::vector<WebPluginGeometry>& moves) = 0;
    211 
    212   // Take focus from the associated View component.
    213   virtual void Blur() = 0;
    214 
    215   // Sets the cursor to the one associated with the specified cursor_type
    216   virtual void UpdateCursor(const WebCursor& cursor) = 0;
    217 
    218   // Indicates whether the page has finished loading.
    219   virtual void SetIsLoading(bool is_loading) = 0;
    220 
    221   // Updates the type of the input method attached to the view.
    222   virtual void TextInputStateChanged(
    223       const ViewHostMsg_TextInputState_Params& params) = 0;
    224 
    225   // Cancel the ongoing composition of the input method attached to the view.
    226   virtual void ImeCancelComposition() = 0;
    227 
    228   // Notifies the View that the renderer has ceased to exist.
    229   virtual void RenderProcessGone(base::TerminationStatus status,
    230                                  int error_code) = 0;
    231 
    232   // Tells the View to destroy itself.
    233   virtual void Destroy() = 0;
    234 
    235   // Tells the View that the tooltip text for the current mouse position over
    236   // the page has changed.
    237   virtual void SetTooltipText(const base::string16& tooltip_text) = 0;
    238 
    239   // Notifies the View that the renderer selection bounds has changed.
    240   // |start_rect| and |end_rect| are the bounds end of the selection in the
    241   // coordinate system of the render view. |start_direction| and |end_direction|
    242   // indicates the direction at which the selection was made on touch devices.
    243   virtual void SelectionBoundsChanged(
    244       const ViewHostMsg_SelectionBounds_Params& params) = 0;
    245 
    246   // Notifies the view that the scroll offset has changed.
    247   virtual void ScrollOffsetChanged() = 0;
    248 
    249   // Copies the contents of the compositing surface into the given
    250   // (uninitialized) PlatformCanvas if any.
    251   // The rectangle region specified with |src_subrect| is copied from the
    252   // contents, scaled to |dst_size|, and written to |output|.
    253   // |callback| is invoked with true on success, false otherwise. |output| can
    254   // be initialized even on failure.
    255   // A smaller region than |src_subrect| may be copied if the underlying surface
    256   // is smaller than |src_subrect|.
    257   // NOTE: |callback| is called asynchronously.
    258   virtual void CopyFromCompositingSurface(
    259       const gfx::Rect& src_subrect,
    260       const gfx::Size& dst_size,
    261       const base::Callback<void(bool, const SkBitmap&)>& callback,
    262       const SkBitmap::Config config) = 0;
    263 
    264   // Copies a given subset of the compositing surface's content into a YV12
    265   // VideoFrame, and invokes a callback with a success/fail parameter. |target|
    266   // must contain an allocated, YV12 video frame of the intended size. If the
    267   // copy rectangle does not match |target|'s size, the copied content will be
    268   // scaled and letterboxed with black borders. The copy will happen
    269   // asynchronously. This operation will fail if there is no available
    270   // compositing surface.
    271   virtual void CopyFromCompositingSurfaceToVideoFrame(
    272       const gfx::Rect& src_subrect,
    273       const scoped_refptr<media::VideoFrame>& target,
    274       const base::Callback<void(bool)>& callback) = 0;
    275 
    276   // Returns true if CopyFromCompositingSurfaceToVideoFrame() is likely to
    277   // succeed.
    278   //
    279   // TODO(nick): When VideoFrame copies are broadly implemented, this method
    280   // should be renamed to HasCompositingSurface(), or unified with
    281   // IsSurfaceAvailableForCopy() and HasAcceleratedSurface().
    282   virtual bool CanCopyToVideoFrame() const = 0;
    283 
    284   // Called when an accelerated compositing surface is initialized.
    285   virtual void AcceleratedSurfaceInitialized(int host_id, int route_id) = 0;
    286   // |params.window| and |params.surface_id| indicate which accelerated
    287   // surface's buffers swapped. |params.renderer_id| and |params.route_id|
    288   // are used to formulate a reply to the GPU process to prevent it from getting
    289   // too far ahead. They may all be zero, in which case no flow control is
    290   // enforced; this case is currently used for accelerated plugins.
    291   virtual void AcceleratedSurfaceBuffersSwapped(
    292       const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params_in_pixel,
    293       int gpu_host_id) = 0;
    294   // Similar to above, except |params.(x|y|width|height)| define the region
    295   // of the surface that changed.
    296   virtual void AcceleratedSurfacePostSubBuffer(
    297       const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params_in_pixel,
    298       int gpu_host_id) = 0;
    299 
    300   // Release the accelerated surface temporarily. It will be recreated on the
    301   // next swap buffers or post sub buffer.
    302   virtual void AcceleratedSurfaceSuspend() = 0;
    303 
    304   virtual void AcceleratedSurfaceRelease() = 0;
    305 
    306   // Return true if the view has an accelerated surface that contains the last
    307   // presented frame for the view. If |desired_size| is non-empty, true is
    308   // returned only if the accelerated surface size matches.
    309   virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) = 0;
    310 
    311   virtual void GetScreenInfo(blink::WebScreenInfo* results) = 0;
    312 
    313   // Gets the bounds of the window, in screen coordinates.
    314   virtual gfx::Rect GetBoundsInRootWindow() = 0;
    315 
    316   virtual gfx::GLSurfaceHandle GetCompositingSurface() = 0;
    317 
    318   virtual void OnTextSurroundingSelectionResponse(const base::string16& content,
    319                                                   size_t start_offset,
    320                                                   size_t end_offset) {};
    321 
    322 #if defined(OS_ANDROID)
    323   virtual void ShowDisambiguationPopup(const gfx::Rect& target_rect,
    324                                        const SkBitmap& zoomed_bitmap) = 0;
    325 
    326   // Instructs the view to not drop the surface even when the view is hidden.
    327   virtual void LockCompositingSurface() = 0;
    328   virtual void UnlockCompositingSurface() = 0;
    329 #endif
    330 
    331 #if defined(OS_MACOSX)
    332   // Does any event handling necessary for plugin IME; should be called after
    333   // the plugin has already had a chance to process the event. If plugin IME is
    334   // not enabled, this is a no-op, so it is always safe to call.
    335   // Returns true if the event was handled by IME.
    336   virtual bool PostProcessEventForPluginIme(
    337       const NativeWebKeyboardEvent& event) = 0;
    338 #endif
    339 
    340 #if defined(OS_MACOSX) || defined(USE_AURA)
    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;
    345 #endif
    346 
    347 #if defined(OS_WIN)
    348   virtual void SetParentNativeViewAccessible(
    349       gfx::NativeViewAccessible accessible_parent) = 0;
    350 
    351   // Returns an HWND that's given as the parent window for windowless Flash to
    352   // workaround crbug.com/301548.
    353   virtual gfx::NativeViewId GetParentForWindowlessPlugin() const = 0;
    354 
    355   // The callback that DetachPluginsHelper calls for each child window. Call
    356   // this directly if you want to do custom filtering on plugin windows first.
    357   static void DetachPluginWindowsCallback(HWND window);
    358 #endif
    359 
    360  protected:
    361   // Interface class only, do not construct.
    362   RenderWidgetHostViewBase();
    363 
    364 #if defined(OS_WIN)
    365   // Shared implementation of MovePluginWindows for use by win and aura/wina.
    366   static void MovePluginWindowsHelper(
    367       HWND parent,
    368       const std::vector<WebPluginGeometry>& moves);
    369 
    370   static void PaintPluginWindowsHelper(
    371       HWND parent,
    372       const gfx::Rect& damaged_screen_rect);
    373 
    374   // Needs to be called before the HWND backing the view goes away to avoid
    375   // crashes in Windowed plugins.
    376   static void DetachPluginsHelper(HWND parent);
    377 #endif
    378 
    379   // Whether this view is a popup and what kind of popup it is (select,
    380   // autofill...).
    381   blink::WebPopupType popup_type_;
    382 
    383   // When false, the background of the web content is not fully opaque.
    384   bool background_opaque_;
    385 
    386   // While the mouse is locked, the cursor is hidden from the user. Mouse events
    387   // are still generated. However, the position they report is the last known
    388   // mouse position just as mouse lock was entered; the movement they report
    389   // indicates what the change in position of the mouse would be had it not been
    390   // locked.
    391   bool mouse_locked_;
    392 
    393   // Whether we are showing a context menu.
    394   bool showing_context_menu_;
    395 
    396   // A buffer containing the text inside and around the current selection range.
    397   base::string16 selection_text_;
    398 
    399   // The offset of the text stored in |selection_text_| relative to the start of
    400   // the web page.
    401   size_t selection_text_offset_;
    402 
    403   // The current selection range relative to the start of the web page.
    404   gfx::Range selection_range_;
    405 
    406 protected:
    407   // The scale factor of the display the renderer is currently on.
    408   float current_device_scale_factor_;
    409 
    410   // The orientation of the display the renderer is currently on.
    411   gfx::Display::Rotation current_display_rotation_;
    412 
    413   // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
    414   // renderer.
    415   bool pinch_zoom_enabled_;
    416 
    417  private:
    418   void FlushInput();
    419 
    420   // Manager of the tree representation of the WebKit render tree.
    421   scoped_ptr<BrowserAccessibilityManager> browser_accessibility_manager_;
    422 
    423   gfx::Rect current_display_area_;
    424 
    425   uint32 renderer_frame_number_;
    426 
    427   base::OneShotTimer<RenderWidgetHostViewBase> flush_input_timer_;
    428 
    429   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBase);
    430 };
    431 
    432 }  // namespace content
    433 
    434 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_BASE_H_
    435