Home | History | Annotate | Download | only in browser_plugin
      1 // Copyright 2013 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 // A BrowserPluginGuestManager serves as a message router to BrowserPluginGuests
      6 // for all guests within a given profile.
      7 // Messages are routed to a particular guest instance based on an instance_id.
      8 
      9 #ifndef CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_MANAGER_H_
     10 #define CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_MANAGER_H_
     11 
     12 #include "base/basictypes.h"
     13 #include "base/supports_user_data.h"
     14 #include "base/values.h"
     15 #include "content/common/content_export.h"
     16 #include "ipc/ipc_message.h"
     17 
     18 struct BrowserPluginHostMsg_Attach_Params;
     19 struct BrowserPluginHostMsg_ResizeGuest_Params;
     20 class GURL;
     21 
     22 namespace gfx {
     23 class Point;
     24 }
     25 
     26 namespace IPC {
     27 class Message;
     28 }  // namespace IPC
     29 
     30 namespace content {
     31 
     32 class BrowserPluginGuest;
     33 class BrowserPluginHostFactory;
     34 class RenderProcessHostImpl;
     35 class RenderWidgetHostImpl;
     36 class SiteInstance;
     37 class WebContents;
     38 class WebContentsImpl;
     39 struct NativeWebKeyboardEvent;
     40 
     41 class CONTENT_EXPORT BrowserPluginGuestManager :
     42     public base::SupportsUserData::Data {
     43  public:
     44   virtual ~BrowserPluginGuestManager();
     45 
     46   static BrowserPluginGuestManager* Create();
     47 
     48   // Overrides factory for testing. Default (NULL) value indicates regular
     49   // (non-test) environment.
     50   static void set_factory_for_testing(BrowserPluginHostFactory* factory) {
     51     content::BrowserPluginGuestManager::factory_ = factory;
     52   }
     53 
     54   // Gets the next available instance id.
     55   int get_next_instance_id() { return ++next_instance_id_; }
     56 
     57   // Creates a guest WebContents with the provided |instance_id| and |params|.
     58   // If params.src is present, the new guest will also be navigated to the
     59   // provided URL. Optionally, the new guest may be attached to a
     60   // |guest_opener|, and may be attached to a pre-selected |routing_id|.
     61   BrowserPluginGuest* CreateGuest(
     62       SiteInstance* embedder_site_instance,
     63       int instance_id,
     64       const BrowserPluginHostMsg_Attach_Params& params,
     65       scoped_ptr<base::DictionaryValue> extra_params);
     66 
     67   // Returns a BrowserPluginGuest given an |instance_id|. Returns NULL if the
     68   // guest wasn't found.  If the embedder is not permitted to access the given
     69   // |instance_id|, the embedder is killed, and NULL is returned.
     70   BrowserPluginGuest* GetGuestByInstanceID(
     71       int instance_id,
     72       int embedder_render_process_id) const;
     73 
     74   // Adds a new |guest_web_contents| to the embedder (overridable in test).
     75   virtual void AddGuest(int instance_id, WebContentsImpl* guest_web_contents);
     76 
     77   // Removes the guest with the given |instance_id| from this
     78   // BrowserPluginGuestManager.
     79   void RemoveGuest(int instance_id);
     80 
     81   // Returns whether the specified embedder is permitted to access the given
     82   // |instance_id|, and kills the embedder if not.
     83   bool CanEmbedderAccessInstanceIDMaybeKill(int embedder_render_process_id,
     84                                             int instance_id) const;
     85 
     86   void DidSendScreenRects(WebContentsImpl* embedder_web_contents);
     87 
     88   bool UnlockMouseIfNecessary(WebContentsImpl* embedder_web_contents_,
     89                               const NativeWebKeyboardEvent& event);
     90 
     91   void OnMessageReceived(const IPC::Message& message, int render_process_id);
     92 
     93  private:
     94   friend class TestBrowserPluginGuestManager;
     95 
     96   BrowserPluginGuestManager();
     97 
     98   // Returns whether the given embedder process is allowed to access the
     99   // provided |guest|.
    100   static bool CanEmbedderAccessGuest(int embedder_render_process_id,
    101                                      BrowserPluginGuest* guest);
    102 
    103   // Returns whether the given embedder process is allowed to use the provided
    104   // |instance_id| or access the guest associated with the |instance_id|. If the
    105   // embedder can, the method returns true. If the guest does not exist but the
    106   // embedder can use that |instance_id|, then it returns true. If the embedder
    107   // is not permitted to use that instance ID or access the associated guest,
    108   // then it returns false.
    109   bool CanEmbedderAccessInstanceID(int embedder_render_process_id,
    110                                    int instance_id) const;
    111 
    112   // Returns an existing SiteInstance if the current profile has a guest of the
    113   // given |guest_site|.
    114   SiteInstance* GetGuestSiteInstance(const GURL& guest_site);
    115 
    116   // Message handlers.
    117   void OnUnhandledSwapBuffersACK(int instance_id,
    118                                  int route_id,
    119                                  int gpu_host_id,
    120                                  const std::string& mailbox_name,
    121                                  uint32 sync_point);
    122 
    123   // Static factory instance (always NULL outside of tests).
    124   static BrowserPluginHostFactory* factory_;
    125 
    126   // Contains guests' WebContents, mapping from their instance ids.
    127   typedef std::map<int, WebContentsImpl*> GuestInstanceMap;
    128   GuestInstanceMap guest_web_contents_by_instance_id_;
    129   int next_instance_id_;
    130 
    131   DISALLOW_COPY_AND_ASSIGN(BrowserPluginGuestManager);
    132 };
    133 
    134 }  // namespace content
    135 
    136 #endif  // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_GUEST_MANAGER_H_
    137 
    138