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.
5 #ifndef CHROME_BROWSER_PLUGINS_CHROME_PLUGIN_SERVICE_FILTER_H_
6 #define CHROME_BROWSER_PLUGINS_CHROME_PLUGIN_SERVICE_FILTER_H_
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"
30 // This class must be created (by calling the |GetInstance| method) on the UI
31 // thread, but is safe to use on any thread after that.
32 class ChromePluginServiceFilter : public content::PluginServiceFilter,
33 public content::NotificationObserver {
35 static ChromePluginServiceFilter* GetInstance();
37 // This method should be called on the UI thread.
38 void RegisterResourceContext(PluginPrefs* plugin_prefs, const void* context);
40 void UnregisterResourceContext(const void* context);
42 // Overrides the plugin lookup mechanism for a given tab and object URL to use
44 void OverridePluginForFrame(int render_process_id,
47 const content::WebPluginInfo& plugin);
49 // Restricts the given plugin to the given profile and origin of the given
51 void RestrictPluginToProfileAndOrigin(const base::FilePath& plugin_path,
55 // Lifts a restriction on a plug-in.
56 void UnrestrictPlugin(const base::FilePath& plugin_path);
58 // Authorizes a given plug-in for a given process.
59 void AuthorizePlugin(int render_process_id,
60 const base::FilePath& plugin_path);
62 // Authorizes all plug-ins for a given WebContents. If |load_blocked| is true,
63 // then the renderer is told to load the plugin with given |identifier| (or
64 // pllugins if |identifier| is empty).
65 // This method can only be called on the UI thread.
66 void AuthorizeAllPlugins(content::WebContents* web_contents,
68 const std::string& identifier);
70 // Returns whether the plugin is found in restricted_plugins_.
71 bool IsPluginRestricted(const base::FilePath& plugin_path);
73 // PluginServiceFilter implementation:
74 bool IsPluginAvailable(int render_process_id,
78 const GURL& policy_url,
79 content::WebPluginInfo* plugin) override;
81 // CanLoadPlugin always grants permission to the browser
82 // (render_process_id == 0)
83 bool CanLoadPlugin(int render_process_id,
84 const base::FilePath& path) override;
87 friend struct DefaultSingletonTraits<ChromePluginServiceFilter>;
89 struct OverriddenPlugin {
94 GURL url; // If empty, the override applies to all urls in render_frame.
95 content::WebPluginInfo plugin;
98 struct ProcessDetails {
102 std::vector<OverriddenPlugin> overridden_plugins;
103 std::set<base::FilePath> authorized_plugins;
106 ChromePluginServiceFilter();
107 ~ChromePluginServiceFilter() override;
109 // content::NotificationObserver implementation:
110 void Observe(int type,
111 const content::NotificationSource& source,
112 const content::NotificationDetails& details) override;
114 ProcessDetails* GetOrRegisterProcess(int render_process_id);
115 const ProcessDetails* GetProcess(int render_process_id) const;
117 content::NotificationRegistrar registrar_;
119 base::Lock lock_; // Guards access to member variables.
120 // Map of plugin paths to the origin they are restricted to.
121 typedef std::pair<const void*, GURL> RestrictedPluginPair;
122 typedef base::hash_map<base::FilePath,
123 RestrictedPluginPair> RestrictedPluginMap;
124 RestrictedPluginMap restricted_plugins_;
125 typedef std::map<const void*, scoped_refptr<PluginPrefs> > ResourceContextMap;
126 ResourceContextMap resource_context_map_;
128 std::map<int, ProcessDetails> plugin_details_;
131 #endif // CHROME_BROWSER_PLUGINS_CHROME_PLUGIN_SERVICE_FILTER_H_