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 #if defined(OS_WIN)
     14 #include "base/memory/shared_memory.h"
     15 #endif
     16 #include "base/values.h"
     17 #include "content/renderer/browser_plugin/browser_plugin_backing_store.h"
     18 #include "content/renderer/browser_plugin/browser_plugin_bindings.h"
     19 #include "content/renderer/mouse_lock_dispatcher.h"
     20 #include "content/renderer/render_view_impl.h"
     21 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
     22 #include "third_party/WebKit/public/web/WebDragStatus.h"
     23 #include "third_party/WebKit/public/web/WebWidget.h"
     24 
     25 struct BrowserPluginHostMsg_AutoSize_Params;
     26 struct BrowserPluginHostMsg_ResizeGuest_Params;
     27 struct BrowserPluginMsg_Attach_ACK_Params;
     28 struct BrowserPluginMsg_BuffersSwapped_Params;
     29 struct BrowserPluginMsg_UpdateRect_Params;
     30 
     31 namespace content {
     32 
     33 class BrowserPluginCompositingHelper;
     34 class BrowserPluginManager;
     35 class MockBrowserPlugin;
     36 
     37 class CONTENT_EXPORT BrowserPlugin :
     38     NON_EXPORTED_BASE(public blink::WebPlugin),
     39     public MouseLockDispatcher::LockTarget {
     40  public:
     41   RenderViewImpl* render_view() const { return render_view_.get(); }
     42   int render_view_routing_id() const { return render_view_routing_id_; }
     43   int guest_instance_id() const { return guest_instance_id_; }
     44   bool attached() const { return attached_; }
     45 
     46   static BrowserPlugin* FromContainer(blink::WebPluginContainer* container);
     47 
     48   bool OnMessageReceived(const IPC::Message& msg);
     49 
     50   // Update Browser Plugin's DOM Node attribute |attribute_name| with the value
     51   // |attribute_value|.
     52   void UpdateDOMAttribute(const std::string& attribute_name,
     53                           const std::string& attribute_value);
     54   // Remove the DOM Node attribute with the name |attribute_name|.
     55   void RemoveDOMAttribute(const std::string& attribute_name);
     56   // Get Browser Plugin's DOM Node attribute |attribute_name|'s value.
     57   std::string GetDOMAttributeValue(const std::string& attribute_name) const;
     58   // Checks if the attribute |attribute_name| exists in the DOM.
     59   bool HasDOMAttribute(const std::string& attribute_name) const;
     60 
     61   // Get the name attribute value.
     62   std::string GetNameAttribute() const;
     63   // Parse the name attribute value.
     64   void ParseNameAttribute();
     65   // Get the allowtransparency attribute value.
     66   bool GetAllowTransparencyAttribute() const;
     67   // Parse the allowtransparency attribute and adjust transparency of
     68   // BrowserPlugin accordingly.
     69   void ParseAllowTransparencyAttribute();
     70   // Get the src attribute value of the BrowserPlugin instance.
     71   std::string GetSrcAttribute() const;
     72   // Parse the src attribute value of the BrowserPlugin instance.
     73   bool ParseSrcAttribute(std::string* error_message);
     74   // Get the autosize attribute value.
     75   bool GetAutoSizeAttribute() const;
     76   // Parses the autosize attribute value.
     77   void ParseAutoSizeAttribute();
     78   // Get the maxheight attribute value.
     79   int GetMaxHeightAttribute() const;
     80   // Get the maxwidth attribute value.
     81   int GetMaxWidthAttribute() const;
     82   // Get the minheight attribute value.
     83   int GetMinHeightAttribute() const;
     84   // Get the minwidth attribute value.
     85   int GetMinWidthAttribute() const;
     86   // Parse the minwidth, maxwidth, minheight, and maxheight attribute values.
     87   void ParseSizeContraintsChanged();
     88   // The partition identifier string is stored as UTF-8.
     89   std::string GetPartitionAttribute() const;
     90   // This method can be successfully called only before the first navigation for
     91   // this instance of BrowserPlugin. If an error occurs, the |error_message| is
     92   // set appropriately to indicate the failure reason.
     93   bool ParsePartitionAttribute(std::string* error_message);
     94   // True if the partition attribute can be removed.
     95   bool CanRemovePartitionAttribute(std::string* error_message);
     96 
     97   bool InAutoSizeBounds(const gfx::Size& size) const;
     98 
     99   // Get the guest's DOMWindow proxy.
    100   NPObject* GetContentWindow() const;
    101 
    102   // Returns whether the guest process has crashed.
    103   bool guest_crashed() const { return guest_crashed_; }
    104   // Returns whether this BrowserPlugin has requested an instance ID.
    105   bool HasNavigated() const;
    106   // Returns whether this BrowserPlugin has allocated an instance ID.
    107   bool HasGuestInstanceID() const;
    108 
    109   // Attaches the window identified by |window_id| to the the given node
    110   // encapsulating a BrowserPlugin.
    111   static bool AttachWindowTo(const blink::WebNode& node,
    112                              int window_id);
    113 
    114   // Informs the guest of an updated focus state.
    115   void UpdateGuestFocusState();
    116   // Indicates whether the guest should be focused.
    117   bool ShouldGuestBeFocused() const;
    118 
    119   // Embedder's device scale factor changed, we need to update the guest
    120   // renderer.
    121   void UpdateDeviceScaleFactor(float device_scale_factor);
    122 
    123   // A request to enable hardware compositing.
    124   void EnableCompositing(bool enable);
    125 
    126   // Returns true if |point| lies within the bounds of the plugin rectangle.
    127   // Not OK to use this function for making security-sensitive decision since it
    128   // can return false positives when the plugin has rotation transformation
    129   // applied.
    130   bool InBounds(const gfx::Point& point) const;
    131 
    132   gfx::Point ToLocalCoordinates(const gfx::Point& point) const;
    133 
    134   // Called when a guest instance ID has been allocated by the browser process.
    135   void OnInstanceIDAllocated(int guest_instance_id);
    136   // Provided that a guest instance ID has been allocated, this method attaches
    137   // this BrowserPlugin instance to that guest. |extra_params| are parameters
    138   // passed in by the content embedder to the browser process.
    139   void Attach(scoped_ptr<base::DictionaryValue> extra_params);
    140 
    141   // Notify the plugin about a compositor commit so that frame ACKs could be
    142   // sent, if needed.
    143   void DidCommitCompositorFrame();
    144 
    145   // Returns whether a message should be forwarded to BrowserPlugin.
    146   static bool ShouldForwardToBrowserPlugin(const IPC::Message& message);
    147 
    148   // blink::WebPlugin implementation.
    149   virtual blink::WebPluginContainer* container() const OVERRIDE;
    150   virtual bool initialize(blink::WebPluginContainer* container) OVERRIDE;
    151   virtual void destroy() OVERRIDE;
    152   virtual NPObject* scriptableObject() OVERRIDE;
    153   virtual struct _NPP* pluginNPP() OVERRIDE;
    154   virtual bool supportsKeyboardFocus() const OVERRIDE;
    155   virtual bool supportsEditCommands() const OVERRIDE;
    156   virtual bool supportsInputMethod() const OVERRIDE;
    157   virtual bool canProcessDrag() const OVERRIDE;
    158   virtual void paint(
    159       blink::WebCanvas* canvas,
    160       const blink::WebRect& rect) OVERRIDE;
    161   virtual void updateGeometry(
    162       const blink::WebRect& frame_rect,
    163       const blink::WebRect& clip_rect,
    164       const blink::WebVector<blink::WebRect>& cut_outs_rects,
    165       bool is_visible) OVERRIDE;
    166   virtual void updateFocus(bool focused) OVERRIDE;
    167   virtual void updateVisibility(bool visible) OVERRIDE;
    168   virtual bool acceptsInputEvents() OVERRIDE;
    169   virtual bool handleInputEvent(
    170       const blink::WebInputEvent& event,
    171       blink::WebCursorInfo& cursor_info) OVERRIDE;
    172   virtual bool handleDragStatusUpdate(blink::WebDragStatus drag_status,
    173                                       const blink::WebDragData& drag_data,
    174                                       blink::WebDragOperationsMask mask,
    175                                       const blink::WebPoint& position,
    176                                       const blink::WebPoint& screen) OVERRIDE;
    177   virtual void didReceiveResponse(
    178       const blink::WebURLResponse& response) OVERRIDE;
    179   virtual void didReceiveData(const char* data, int data_length) OVERRIDE;
    180   virtual void didFinishLoading() OVERRIDE;
    181   virtual void didFailLoading(const blink::WebURLError& error) OVERRIDE;
    182   virtual void didFinishLoadingFrameRequest(
    183       const blink::WebURL& url,
    184       void* notify_data) OVERRIDE;
    185   virtual void didFailLoadingFrameRequest(
    186       const blink::WebURL& url,
    187       void* notify_data,
    188       const blink::WebURLError& error) OVERRIDE;
    189   virtual bool executeEditCommand(const blink::WebString& name) OVERRIDE;
    190   virtual bool executeEditCommand(const blink::WebString& name,
    191                                   const blink::WebString& value) OVERRIDE;
    192   virtual bool setComposition(
    193       const blink::WebString& text,
    194       const blink::WebVector<blink::WebCompositionUnderline>& underlines,
    195       int selectionStart,
    196       int selectionEnd) OVERRIDE;
    197   virtual bool confirmComposition(
    198       const blink::WebString& text,
    199       blink::WebWidget::ConfirmCompositionBehavior selectionBehavior) OVERRIDE;
    200   virtual void extendSelectionAndDelete(int before, int after) OVERRIDE;
    201 
    202   // MouseLockDispatcher::LockTarget implementation.
    203   virtual void OnLockMouseACK(bool succeeded) OVERRIDE;
    204   virtual void OnMouseLockLost() OVERRIDE;
    205   virtual bool HandleMouseLockedInputEvent(
    206           const blink::WebMouseEvent& event) OVERRIDE;
    207 
    208  private:
    209   friend class base::DeleteHelper<BrowserPlugin>;
    210   // Only the manager is allowed to create a BrowserPlugin.
    211   friend class BrowserPluginManagerImpl;
    212   friend class MockBrowserPluginManager;
    213 
    214   // For unit/integration tests.
    215   friend class MockBrowserPlugin;
    216 
    217   // A BrowserPlugin object is a controller that represents an instance of a
    218   // browser plugin within the embedder renderer process. Once a BrowserPlugin
    219   // does an initial navigation or is attached to a newly created guest, it
    220   // acquires a guest_instance_id as well. The guest instance ID uniquely
    221   // identifies a guest WebContents that's hosted by this BrowserPlugin.
    222   BrowserPlugin(RenderViewImpl* render_view, blink::WebFrame* frame);
    223 
    224   virtual ~BrowserPlugin();
    225 
    226   int width() const { return plugin_rect_.width(); }
    227   int height() const { return plugin_rect_.height(); }
    228   gfx::Rect plugin_rect() { return plugin_rect_; }
    229   // Gets the Max Height value used for auto size.
    230   int GetAdjustedMaxHeight() const;
    231   // Gets the Max Width value used for auto size.
    232   int GetAdjustedMaxWidth() const;
    233   // Gets the Min Height value used for auto size.
    234   int GetAdjustedMinHeight() const;
    235   // Gets the Min Width value used for auto size.
    236   int GetAdjustedMinWidth() const;
    237   BrowserPluginManager* browser_plugin_manager() const {
    238     return browser_plugin_manager_.get();
    239   }
    240 
    241   // Virtual to allow for mocking in tests.
    242   virtual float GetDeviceScaleFactor() const;
    243 
    244   void ShowSadGraphic();
    245 
    246   // Parses the attributes of the browser plugin from the element's attributes
    247   // and sets them appropriately.
    248   void ParseAttributes();
    249 
    250   // Triggers the event-listeners for |event_name|. Note that the function
    251   // frees all the values in |props|.
    252   void TriggerEvent(const std::string& event_name,
    253                     std::map<std::string, base::Value*>* props);
    254 
    255   // Creates and maps a shared damage buffer.
    256   virtual base::SharedMemory* CreateDamageBuffer(
    257       const size_t size,
    258       base::SharedMemoryHandle* shared_memory_handle);
    259   // Swaps out the |current_damage_buffer_| with the |pending_damage_buffer_|.
    260   void SwapDamageBuffers();
    261 
    262   // Populates BrowserPluginHostMsg_ResizeGuest_Params with resize state and
    263   // allocates a new |pending_damage_buffer_| if in software rendering mode.
    264   void PopulateResizeGuestParameters(
    265       BrowserPluginHostMsg_ResizeGuest_Params* params,
    266       const gfx::Rect& view_size,
    267       bool needs_repaint);
    268 
    269   // Populates BrowserPluginHostMsg_AutoSize_Params object with autosize state.
    270   void PopulateAutoSizeParameters(
    271       BrowserPluginHostMsg_AutoSize_Params* params, bool auto_size_enabled);
    272 
    273   // Populates both AutoSize and ResizeGuest parameters based on the current
    274   // autosize state.
    275   void GetDamageBufferWithSizeParams(
    276       BrowserPluginHostMsg_AutoSize_Params* auto_size_params,
    277       BrowserPluginHostMsg_ResizeGuest_Params* resize_guest_params,
    278       bool needs_repaint);
    279 
    280   // Informs the guest of an updated autosize state.
    281   void UpdateGuestAutoSizeState(bool auto_size_enabled);
    282 
    283   // Indicates whether a damage buffer was used by the guest process for the
    284   // provided |params|.
    285   static bool UsesDamageBuffer(
    286       const BrowserPluginMsg_UpdateRect_Params& params);
    287 
    288   // Indicates whether the |pending_damage_buffer_| was used to copy over pixels
    289   // given the provided |params|.
    290   bool UsesPendingDamageBuffer(
    291       const BrowserPluginMsg_UpdateRect_Params& params);
    292 
    293   // IPC message handlers.
    294   // Please keep in alphabetical order.
    295   void OnAdvanceFocus(int instance_id, bool reverse);
    296   void OnAttachACK(int instance_id,
    297                    const BrowserPluginMsg_Attach_ACK_Params& ack_params);
    298   void OnBuffersSwapped(int instance_id,
    299                         const BrowserPluginMsg_BuffersSwapped_Params& params);
    300   void OnCompositorFrameSwapped(const IPC::Message& message);
    301   void OnCopyFromCompositingSurface(int instance_id,
    302                                     int request_id,
    303                                     gfx::Rect source_rect,
    304                                     gfx::Size dest_size);
    305   void OnGuestContentWindowReady(int instance_id,
    306                                  int content_window_routing_id);
    307   void OnGuestGone(int instance_id);
    308   void OnSetCursor(int instance_id, const WebCursor& cursor);
    309   void OnSetMouseLock(int instance_id, bool enable);
    310   void OnShouldAcceptTouchEvents(int instance_id, bool accept);
    311   void OnUpdatedName(int instance_id, const std::string& name);
    312   void OnUpdateRect(int instance_id,
    313                     const BrowserPluginMsg_UpdateRect_Params& params);
    314 
    315   // This is the browser-process-allocated instance ID that uniquely identifies
    316   // a guest WebContents.
    317   int guest_instance_id_;
    318   // This indicates whether this BrowserPlugin has been attached to a
    319   // WebContents.
    320   bool attached_;
    321   base::WeakPtr<RenderViewImpl> render_view_;
    322   // We cache the |render_view_|'s routing ID because we need it on destruction.
    323   // If the |render_view_| is destroyed before the BrowserPlugin is destroyed
    324   // then we will attempt to access a NULL pointer.
    325   int render_view_routing_id_;
    326   blink::WebPluginContainer* container_;
    327   scoped_ptr<BrowserPluginBindings> bindings_;
    328   scoped_ptr<BrowserPluginBackingStore> backing_store_;
    329   scoped_ptr<base::SharedMemory> current_damage_buffer_;
    330   scoped_ptr<base::SharedMemory> pending_damage_buffer_;
    331   uint32 damage_buffer_sequence_id_;
    332   bool paint_ack_received_;
    333   gfx::Rect plugin_rect_;
    334   float last_device_scale_factor_;
    335   // Bitmap for crashed plugin. Lazily initialized, non-owning pointer.
    336   SkBitmap* sad_guest_;
    337   bool guest_crashed_;
    338   scoped_ptr<BrowserPluginHostMsg_ResizeGuest_Params> pending_resize_params_;
    339   bool is_auto_size_state_dirty_;
    340   // Maximum size constraint for autosize.
    341   gfx::Size max_auto_size_;
    342   std::string storage_partition_id_;
    343   bool persist_storage_;
    344   bool valid_partition_id_;
    345   int content_window_routing_id_;
    346   bool plugin_focused_;
    347   // Tracks the visibility of the browser plugin regardless of the whole
    348   // embedder RenderView's visibility.
    349   bool visible_;
    350 
    351   WebCursor cursor_;
    352 
    353   gfx::Size last_view_size_;
    354   bool before_first_navigation_;
    355   bool mouse_locked_;
    356 
    357   // BrowserPlugin outlives RenderViewImpl in Chrome Apps and so we need to
    358   // store the BrowserPlugin's BrowserPluginManager in a member variable to
    359   // avoid accessing the RenderViewImpl.
    360   scoped_refptr<BrowserPluginManager> browser_plugin_manager_;
    361 
    362   // Used for HW compositing.
    363   bool compositing_enabled_;
    364   scoped_refptr<BrowserPluginCompositingHelper> compositing_helper_;
    365 
    366   // Used to identify the plugin to WebBindings.
    367   scoped_ptr<struct _NPP> npp_;
    368 
    369   // URL for the embedder frame.
    370   GURL embedder_frame_url_;
    371 
    372   // Weak factory used in v8 |MakeWeak| callback, since the v8 callback might
    373   // get called after BrowserPlugin has been destroyed.
    374   base::WeakPtrFactory<BrowserPlugin> weak_ptr_factory_;
    375 
    376   std::vector<EditCommand> edit_commands_;
    377 
    378   DISALLOW_COPY_AND_ASSIGN(BrowserPlugin);
    379 };
    380 
    381 }  // namespace content
    382 
    383 #endif  // CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_H_
    384