Home | History | Annotate | Download | only in plugins
      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 CHROME_BROWSER_PLUGINS_CHROME_PLUGIN_SERVICE_FILTER_H_
      6 #define CHROME_BROWSER_PLUGINS_CHROME_PLUGIN_SERVICE_FILTER_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <vector>
     11 
     12 #include "base/containers/hash_tables.h"
     13 #include "base/files/file_path.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/singleton.h"
     16 #include "base/synchronization/lock.h"
     17 #include "content/public/browser/notification_observer.h"
     18 #include "content/public/browser/notification_registrar.h"
     19 #include "content/public/browser/plugin_service_filter.h"
     20 #include "content/public/common/webplugininfo.h"
     21 #include "url/gurl.h"
     22 
     23 class PluginPrefs;
     24 class Profile;
     25 
     26 // This class must be created (by calling the |GetInstance| method) on the UI
     27 // thread, but is safe to use on any thread after that.
     28 class ChromePluginServiceFilter : public content::PluginServiceFilter,
     29                                   public content::NotificationObserver {
     30  public:
     31   static ChromePluginServiceFilter* GetInstance();
     32 
     33   // This method should be called on the UI thread.
     34   void RegisterResourceContext(PluginPrefs* plugin_prefs, const void* context);
     35 
     36   void UnregisterResourceContext(const void* context);
     37 
     38   // Overrides the plugin lookup mechanism for a given tab and object URL to use
     39   // a specifc plugin.
     40   void OverridePluginForFrame(int render_process_id,
     41                               int render_frame_id,
     42                               const GURL& url,
     43                               const content::WebPluginInfo& plugin);
     44 
     45   // Restricts the given plugin to the given profile and origin of the given
     46   // URL.
     47   void RestrictPluginToProfileAndOrigin(const base::FilePath& plugin_path,
     48                                         Profile* profile,
     49                                         const GURL& url);
     50 
     51   // Lifts a restriction on a plug-in.
     52   void UnrestrictPlugin(const base::FilePath& plugin_path);
     53 
     54   // Authorizes a given plug-in for a given process.
     55   void AuthorizePlugin(int render_process_id,
     56                        const base::FilePath& plugin_path);
     57 
     58   // Authorizes all plug-ins for a given process.
     59   void AuthorizeAllPlugins(int render_process_id);
     60 
     61   // Returns whether the plugin is found in restricted_plugins_.
     62   bool IsPluginRestricted(const base::FilePath& plugin_path);
     63 
     64   // PluginServiceFilter implementation:
     65   virtual bool IsPluginAvailable(
     66       int render_process_id,
     67       int render_frame_id,
     68       const void* context,
     69       const GURL& url,
     70       const GURL& policy_url,
     71       content::WebPluginInfo* plugin) OVERRIDE;
     72 
     73   // CanLoadPlugin always grants permission to the browser
     74   // (render_process_id == 0)
     75   virtual bool CanLoadPlugin(
     76       int render_process_id,
     77       const base::FilePath& path) OVERRIDE;
     78 
     79  private:
     80   friend struct DefaultSingletonTraits<ChromePluginServiceFilter>;
     81 
     82   struct OverriddenPlugin {
     83     OverriddenPlugin();
     84     ~OverriddenPlugin();
     85 
     86     int render_frame_id;
     87     GURL url;  // If empty, the override applies to all urls in render_frame.
     88     content::WebPluginInfo plugin;
     89   };
     90 
     91   struct ProcessDetails {
     92     ProcessDetails();
     93     ~ProcessDetails();
     94 
     95     std::vector<OverriddenPlugin> overridden_plugins;
     96     std::set<base::FilePath> authorized_plugins;
     97   };
     98 
     99   ChromePluginServiceFilter();
    100   virtual ~ChromePluginServiceFilter();
    101 
    102   // content::NotificationObserver implementation:
    103   virtual void Observe(int type,
    104                        const content::NotificationSource& source,
    105                        const content::NotificationDetails& details) OVERRIDE;
    106 
    107   ProcessDetails* GetOrRegisterProcess(int render_process_id);
    108   const ProcessDetails* GetProcess(int render_process_id) const;
    109 
    110   content::NotificationRegistrar registrar_;
    111 
    112   base::Lock lock_;  // Guards access to member variables.
    113   // Map of plugin paths to the origin they are restricted to.
    114   typedef std::pair<const void*, GURL> RestrictedPluginPair;
    115   typedef base::hash_map<base::FilePath,
    116                          RestrictedPluginPair> RestrictedPluginMap;
    117   RestrictedPluginMap restricted_plugins_;
    118   typedef std::map<const void*, scoped_refptr<PluginPrefs> > ResourceContextMap;
    119   ResourceContextMap resource_context_map_;
    120 
    121   std::map<int, ProcessDetails> plugin_details_;
    122 };
    123 
    124 #endif  // CHROME_BROWSER_PLUGINS_CHROME_PLUGIN_SERVICE_FILTER_H_
    125