Home | History | Annotate | Download | only in browser_plugin
      1 // Copyright 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_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
      6 #define  CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
      7 
      8 #include "third_party/WebKit/public/web/WebPlugin.h"
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/sequenced_task_runner_helpers.h"
     13 #include "content/renderer/browser_plugin/browser_plugin_bindings.h"
     14 #include "content/renderer/mouse_lock_dispatcher.h"
     15 #include "content/renderer/render_view_impl.h"
     16 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
     17 #include "third_party/WebKit/public/web/WebDragStatus.h"
     18 #include "third_party/WebKit/public/web/WebWidget.h"
     19 
     20 struct BrowserPluginHostMsg_AutoSize_Params;
     21 struct BrowserPluginHostMsg_ResizeGuest_Params;
     22 struct BrowserPluginMsg_UpdateRect_Params;
     23 struct FrameMsg_BuffersSwapped_Params;
     24 
     25 namespace content {
     26 
     27 class ChildFrameCompositingHelper;
     28 class BrowserPluginManager;
     29 class MockBrowserPlugin;
     30 
     31 class CONTENT_EXPORT BrowserPlugin :
     32     NON_EXPORTED_BASE(public blink::WebPlugin),
     33     public MouseLockDispatcher::LockTarget {
     34  public:
     35   RenderViewImpl* render_view() const { return render_view_.get(); }
     36   int render_view_routing_id() const { return render_view_routing_id_; }
     37   int guest_instance_id() const { return guest_instance_id_; }
     38   bool attached() const { return attached_; }
     39   BrowserPluginManager* browser_plugin_manager() const {
     40     return browser_plugin_manager_.get();
     41   }
     42 
     43   bool OnMessageReceived(const IPC::Message& msg);
     44 
     45   // Update Browser Plugin's DOM Node attribute |attribute_name| with the value
     46   // |attribute_value|.
     47   void UpdateDOMAttribute(const std::string& attribute_name,
     48                           const std::string& attribute_value);
     49   // Remove the DOM Node attribute with the name |attribute_name|.
     50   void RemoveDOMAttribute(const std::string& attribute_name);
     51   // Get Browser Plugin's DOM Node attribute |attribute_name|'s value.
     52   std::string GetDOMAttributeValue(const std::string& attribute_name) const;
     53   // Checks if the attribute |attribute_name| exists in the DOM.
     54   bool HasDOMAttribute(const std::string& attribute_name) const;
     55 
     56   // Get the allowtransparency attribute value.
     57   bool GetAllowTransparencyAttribute() const;
     58   // Parse the allowtransparency attribute and adjust transparency of
     59   // BrowserPlugin accordingly.
     60   void ParseAllowTransparencyAttribute();
     61   // Get the autosize attribute value.
     62   bool GetAutoSizeAttribute() const;
     63   // Parses the autosize attribute value.
     64   void ParseAutoSizeAttribute();
     65   // Get the maxheight attribute value.
     66   int GetMaxHeightAttribute() const;
     67   // Get the maxwidth attribute value.
     68   int GetMaxWidthAttribute() const;
     69   // Get the minheight attribute value.
     70   int GetMinHeightAttribute() const;
     71   // Get the minwidth attribute value.
     72   int GetMinWidthAttribute() const;
     73   // Parse the minwidth, maxwidth, minheight, and maxheight attribute values.
     74   void ParseSizeContraintsChanged();
     75 
     76   bool InAutoSizeBounds(const gfx::Size& size) const;
     77 
     78   // Get the guest's DOMWindow proxy.
     79   NPObject* GetContentWindow() const;
     80 
     81   // Returns whether the guest process has crashed.
     82   bool guest_crashed() const { return guest_crashed_; }
     83   // Returns whether this BrowserPlugin has allocated an instance ID.
     84   bool HasGuestInstanceID() const;
     85 
     86   // Informs the guest of an updated focus state.
     87   void UpdateGuestFocusState();
     88   // Indicates whether the guest should be focused.
     89   bool ShouldGuestBeFocused() const;
     90 
     91   // Embedder's device scale factor changed, we need to update the guest
     92   // renderer.
     93   void UpdateDeviceScaleFactor(float device_scale_factor);
     94 
     95   // A request to enable hardware compositing.
     96   void EnableCompositing(bool enable);
     97 
     98   // Provided that a guest instance ID has been allocated, this method attaches
     99   // this BrowserPlugin instance to that guest. |extra_params| are parameters
    100   // passed in by the content embedder to the browser process.
    101   void Attach(int guest_instance_id,
    102               scoped_ptr<base::DictionaryValue> extra_params);
    103 
    104   // Notify the plugin about a compositor commit so that frame ACKs could be
    105   // sent, if needed.
    106   void DidCommitCompositorFrame();
    107 
    108   // Returns whether a message should be forwarded to BrowserPlugin.
    109   static bool ShouldForwardToBrowserPlugin(const IPC::Message& message);
    110 
    111   // blink::WebPlugin implementation.
    112   virtual blink::WebPluginContainer* container() const OVERRIDE;
    113   virtual bool initialize(blink::WebPluginContainer* container) OVERRIDE;
    114   virtual void destroy() OVERRIDE;
    115   virtual NPObject* scriptableObject() OVERRIDE;
    116   virtual struct _NPP* pluginNPP() OVERRIDE;
    117   virtual bool supportsKeyboardFocus() const OVERRIDE;
    118   virtual bool supportsEditCommands() const OVERRIDE;
    119   virtual bool supportsInputMethod() const OVERRIDE;
    120   virtual bool canProcessDrag() const OVERRIDE;
    121   virtual void paint(
    122       blink::WebCanvas* canvas,
    123       const blink::WebRect& rect) OVERRIDE;
    124   virtual void updateGeometry(
    125       const blink::WebRect& frame_rect,
    126       const blink::WebRect& clip_rect,
    127       const blink::WebVector<blink::WebRect>& cut_outs_rects,
    128       bool is_visible) OVERRIDE;
    129   virtual void updateFocus(bool focused) OVERRIDE;
    130   virtual void updateVisibility(bool visible) OVERRIDE;
    131   virtual bool acceptsInputEvents() OVERRIDE;
    132   virtual bool handleInputEvent(
    133       const blink::WebInputEvent& event,
    134       blink::WebCursorInfo& cursor_info) OVERRIDE;
    135   virtual bool handleDragStatusUpdate(blink::WebDragStatus drag_status,
    136                                       const blink::WebDragData& drag_data,
    137                                       blink::WebDragOperationsMask mask,
    138                                       const blink::WebPoint& position,
    139                                       const blink::WebPoint& screen) OVERRIDE;
    140   virtual void didReceiveResponse(
    141       const blink::WebURLResponse& response) OVERRIDE;
    142   virtual void didReceiveData(const char* data, int data_length) OVERRIDE;
    143   virtual void didFinishLoading() OVERRIDE;
    144   virtual void didFailLoading(const blink::WebURLError& error) OVERRIDE;
    145   virtual void didFinishLoadingFrameRequest(
    146       const blink::WebURL& url,
    147       void* notify_data) OVERRIDE;
    148   virtual void didFailLoadingFrameRequest(
    149       const blink::WebURL& url,
    150       void* notify_data,
    151       const blink::WebURLError& error) OVERRIDE;
    152   virtual bool executeEditCommand(const blink::WebString& name) OVERRIDE;
    153   virtual bool executeEditCommand(const blink::WebString& name,
    154                                   const blink::WebString& value) OVERRIDE;
    155   virtual bool setComposition(
    156       const blink::WebString& text,
    157       const blink::WebVector<blink::WebCompositionUnderline>& underlines,
    158       int selectionStart,
    159       int selectionEnd) OVERRIDE;
    160   virtual bool confirmComposition(
    161       const blink::WebString& text,
    162       blink::WebWidget::ConfirmCompositionBehavior selectionBehavior) OVERRIDE;
    163   virtual void extendSelectionAndDelete(int before, int after) OVERRIDE;
    164 
    165   // MouseLockDispatcher::LockTarget implementation.
    166   virtual void OnLockMouseACK(bool succeeded) OVERRIDE;
    167   virtual void OnMouseLockLost() OVERRIDE;
    168   virtual bool HandleMouseLockedInputEvent(
    169           const blink::WebMouseEvent& event) OVERRIDE;
    170 
    171  private:
    172   friend class base::DeleteHelper<BrowserPlugin>;
    173   // Only the manager is allowed to create a BrowserPlugin.
    174   friend class BrowserPluginManagerImpl;
    175   friend class MockBrowserPluginManager;
    176 
    177   // For unit/integration tests.
    178   friend class MockBrowserPlugin;
    179 
    180   // A BrowserPlugin object is a controller that represents an instance of a
    181   // browser plugin within the embedder renderer process. Once a BrowserPlugin
    182   // does an initial navigation or is attached to a newly created guest, it
    183   // acquires a guest_instance_id as well. The guest instance ID uniquely
    184   // identifies a guest WebContents that's hosted by this BrowserPlugin.
    185   BrowserPlugin(RenderViewImpl* render_view,
    186                 blink::WebFrame* frame,
    187                 bool auto_navigate);
    188 
    189   virtual ~BrowserPlugin();
    190 
    191   int width() const { return plugin_rect_.width(); }
    192   int height() const { return plugin_rect_.height(); }
    193   gfx::Size plugin_size() const { return plugin_rect_.size(); }
    194   gfx::Rect plugin_rect() const { return plugin_rect_; }
    195   // Gets the Max Height value used for auto size.
    196   int GetAdjustedMaxHeight() const;
    197   // Gets the Max Width value used for auto size.
    198   int GetAdjustedMaxWidth() const;
    199   // Gets the Min Height value used for auto size.
    200   int GetAdjustedMinHeight() const;
    201   // Gets the Min Width value used for auto size.
    202   int GetAdjustedMinWidth() const;
    203 
    204   // Virtual to allow for mocking in tests.
    205   virtual float GetDeviceScaleFactor() const;
    206 
    207   void ShowSadGraphic();
    208 
    209   // Triggers the event-listeners for |event_name|. Note that the function
    210   // frees all the values in |props|.
    211   void TriggerEvent(const std::string& event_name,
    212                     std::map<std::string, base::Value*>* props);
    213 
    214   // Populates BrowserPluginHostMsg_ResizeGuest_Params with resize state.
    215   void PopulateResizeGuestParameters(
    216       BrowserPluginHostMsg_ResizeGuest_Params* params,
    217       const gfx::Size& view_size,
    218       bool needs_repaint);
    219 
    220   // Populates BrowserPluginHostMsg_AutoSize_Params object with autosize state.
    221   void PopulateAutoSizeParameters(
    222       BrowserPluginHostMsg_AutoSize_Params* params, bool auto_size_enabled);
    223 
    224   // Populates both AutoSize and ResizeGuest parameters based on the current
    225   // autosize state.
    226   void GetSizeParams(
    227       BrowserPluginHostMsg_AutoSize_Params* auto_size_params,
    228       BrowserPluginHostMsg_ResizeGuest_Params* resize_guest_params,
    229       bool needs_repaint);
    230 
    231   // Informs the guest of an updated autosize state.
    232   void UpdateGuestAutoSizeState(bool auto_size_enabled);
    233 
    234 
    235   // IPC message handlers.
    236   // Please keep in alphabetical order.
    237   void OnAdvanceFocus(int instance_id, bool reverse);
    238   void OnAttachACK(int instance_id);
    239   void OnBuffersSwapped(int instance_id,
    240                         const FrameMsg_BuffersSwapped_Params& params);
    241   void OnCompositorFrameSwapped(const IPC::Message& message);
    242   void OnCopyFromCompositingSurface(int instance_id,
    243                                     int request_id,
    244                                     gfx::Rect source_rect,
    245                                     gfx::Size dest_size);
    246   void OnGuestContentWindowReady(int instance_id,
    247                                  int content_window_routing_id);
    248   void OnGuestGone(int instance_id);
    249   void OnSetCursor(int instance_id, const WebCursor& cursor);
    250   void OnSetMouseLock(int instance_id, bool enable);
    251   void OnShouldAcceptTouchEvents(int instance_id, bool accept);
    252   void OnUpdateRect(int instance_id,
    253                     const BrowserPluginMsg_UpdateRect_Params& params);
    254 
    255   // This is the browser-process-allocated instance ID that uniquely identifies
    256   // a guest WebContents.
    257   int guest_instance_id_;
    258   // This indicates whether this BrowserPlugin has been attached to a
    259   // WebContents.
    260   bool attached_;
    261   const base::WeakPtr<RenderViewImpl> render_view_;
    262   // We cache the |render_view_|'s routing ID because we need it on destruction.
    263   // If the |render_view_| is destroyed before the BrowserPlugin is destroyed
    264   // then we will attempt to access a NULL pointer.
    265   const int render_view_routing_id_;
    266   blink::WebPluginContainer* container_;
    267   scoped_ptr<BrowserPluginBindings> bindings_;
    268   bool paint_ack_received_;
    269   gfx::Rect plugin_rect_;
    270   float last_device_scale_factor_;
    271   // Bitmap for crashed plugin. Lazily initialized, non-owning pointer.
    272   SkBitmap* sad_guest_;
    273   bool guest_crashed_;
    274   bool is_auto_size_state_dirty_;
    275   // Maximum size constraint for autosize.
    276   gfx::Size max_auto_size_;
    277   int content_window_routing_id_;
    278   bool plugin_focused_;
    279   // Tracks the visibility of the browser plugin regardless of the whole
    280   // embedder RenderView's visibility.
    281   bool visible_;
    282 
    283   const bool auto_navigate_;
    284   std::string html_string_;
    285 
    286   WebCursor cursor_;
    287 
    288   gfx::Size last_view_size_;
    289   bool mouse_locked_;
    290 
    291   // BrowserPlugin outlives RenderViewImpl in Chrome Apps and so we need to
    292   // store the BrowserPlugin's BrowserPluginManager in a member variable to
    293   // avoid accessing the RenderViewImpl.
    294   const scoped_refptr<BrowserPluginManager> browser_plugin_manager_;
    295 
    296   // Used for HW compositing.
    297   scoped_refptr<ChildFrameCompositingHelper> compositing_helper_;
    298 
    299   // Used to identify the plugin to WebBindings.
    300   scoped_ptr<struct _NPP> npp_;
    301 
    302   // URL for the embedder frame.
    303   const GURL embedder_frame_url_;
    304 
    305   std::vector<EditCommand> edit_commands_;
    306 
    307   // Weak factory used in v8 |MakeWeak| callback, since the v8 callback might
    308   // get called after BrowserPlugin has been destroyed.
    309   base::WeakPtrFactory<BrowserPlugin> weak_ptr_factory_;
    310 
    311   DISALLOW_COPY_AND_ASSIGN(BrowserPlugin);
    312 };
    313 
    314 }  // namespace content
    315 
    316 #endif  // CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
    317