Home | History | Annotate | Download | only in browser_plugin
      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_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_MANAGER_H_
      6 #define CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_MANAGER_H_
      7 
      8 #include "base/id_map.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/observer_list.h"
     12 #include "content/public/renderer/render_view_observer.h"
     13 #include "ipc/ipc_sender.h"
     14 
     15 namespace blink {
     16 class WebFrame;
     17 struct WebPluginParams;
     18 }
     19 
     20 namespace content {
     21 
     22 class BrowserPlugin;
     23 class BrowserPluginManagerFactory;
     24 class RenderViewImpl;
     25 
     26 // BrowserPluginManager manages the routing of messages to the appropriate
     27 // BrowserPlugin object based on its instance ID.
     28 class CONTENT_EXPORT BrowserPluginManager
     29     : public RenderViewObserver,
     30       public base::RefCounted<BrowserPluginManager> {
     31  public:
     32   // Returns the one BrowserPluginManager for this process.
     33   static BrowserPluginManager* Create(RenderViewImpl* render_view);
     34 
     35   // Overrides factory for testing. Default (NULL) value indicates regular
     36   // (non-test) environment.
     37   static void set_factory_for_testing(BrowserPluginManagerFactory* factory) {
     38     BrowserPluginManager::factory_ = factory;
     39   }
     40 
     41   explicit BrowserPluginManager(RenderViewImpl* render_view);
     42 
     43   // Creates a new BrowserPlugin object.
     44   // BrowserPlugin is responsible for associating itself with the
     45   // BrowserPluginManager via AddBrowserPlugin. When it is destroyed, it is
     46   // responsible for removing its association via RemoveBrowserPlugin.
     47   virtual BrowserPlugin* CreateBrowserPlugin(
     48       RenderViewImpl* render_view, blink::WebFrame* frame) = 0;
     49 
     50   // Asynchronously requests a new browser-process-allocated instance ID.
     51   // After the browser process allocates an ID, it calls back into the
     52   // |browser_plugin| if it's still alive.
     53   virtual void AllocateInstanceID(
     54       const base::WeakPtr<BrowserPlugin>& browser_plugin) = 0;
     55 
     56   void AddBrowserPlugin(int guest_instance_id, BrowserPlugin* browser_plugin);
     57   void RemoveBrowserPlugin(int guest_instance_id);
     58   BrowserPlugin* GetBrowserPlugin(int guest_instance_id) const;
     59   void UpdateDeviceScaleFactor(float device_scale_factor);
     60   void UpdateFocusState();
     61   RenderViewImpl* render_view() const { return render_view_.get(); }
     62 
     63   // RenderViewObserver implementation.
     64 
     65   // BrowserPluginManager must override the default Send behavior.
     66   virtual bool Send(IPC::Message* msg) OVERRIDE = 0;
     67 
     68   // Don't destroy the BrowserPluginManager when the RenderViewImpl goes away.
     69   // BrowserPluginManager's lifetime is managed by a reference count. Once
     70   // the host RenderViewImpl and all BrowserPlugins release their references,
     71   // then the BrowserPluginManager will be destroyed.
     72   virtual void OnDestruct() OVERRIDE {}
     73 
     74  protected:
     75   // Friend RefCounted so that the dtor can be non-public.
     76   friend class base::RefCounted<BrowserPluginManager>;
     77 
     78   // Static factory instance (always NULL for non-test).
     79   static BrowserPluginManagerFactory* factory_;
     80 
     81   virtual ~BrowserPluginManager();
     82   // This map is keyed by guest instance IDs.
     83   IDMap<BrowserPlugin> instances_;
     84   base::WeakPtr<RenderViewImpl> render_view_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(BrowserPluginManager);
     87 };
     88 
     89 }  // namespace content
     90 
     91 #endif //  CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_MANAGER_H_
     92