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