Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / plugins / chrome_plugin_service_filter.h
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 namespace content {
27 class WebContents;
28 }
29
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 {
34  public:
35   static ChromePluginServiceFilter* GetInstance();
36
37   // This method should be called on the UI thread.
38   void RegisterResourceContext(PluginPrefs* plugin_prefs, const void* context);
39
40   void UnregisterResourceContext(const void* context);
41
42   // Overrides the plugin lookup mechanism for a given tab and object URL to use
43   // a specifc plugin.
44   void OverridePluginForFrame(int render_process_id,
45                               int render_frame_id,
46                               const GURL& url,
47                               const content::WebPluginInfo& plugin);
48
49   // Restricts the given plugin to the given profile and origin of the given
50   // URL.
51   void RestrictPluginToProfileAndOrigin(const base::FilePath& plugin_path,
52                                         Profile* profile,
53                                         const GURL& url);
54
55   // Lifts a restriction on a plug-in.
56   void UnrestrictPlugin(const base::FilePath& plugin_path);
57
58   // Authorizes a given plug-in for a given process.
59   void AuthorizePlugin(int render_process_id,
60                        const base::FilePath& plugin_path);
61
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,
67                            bool load_blocked,
68                            const std::string& identifier);
69
70   // Returns whether the plugin is found in restricted_plugins_.
71   bool IsPluginRestricted(const base::FilePath& plugin_path);
72
73   // PluginServiceFilter implementation:
74   bool IsPluginAvailable(int render_process_id,
75                          int render_frame_id,
76                          const void* context,
77                          const GURL& url,
78                          const GURL& policy_url,
79                          content::WebPluginInfo* plugin) override;
80
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;
85
86  private:
87   friend struct DefaultSingletonTraits<ChromePluginServiceFilter>;
88
89   struct OverriddenPlugin {
90     OverriddenPlugin();
91     ~OverriddenPlugin();
92
93     int render_frame_id;
94     GURL url;  // If empty, the override applies to all urls in render_frame.
95     content::WebPluginInfo plugin;
96   };
97
98   struct ProcessDetails {
99     ProcessDetails();
100     ~ProcessDetails();
101
102     std::vector<OverriddenPlugin> overridden_plugins;
103     std::set<base::FilePath> authorized_plugins;
104   };
105
106   ChromePluginServiceFilter();
107   ~ChromePluginServiceFilter() override;
108
109   // content::NotificationObserver implementation:
110   void Observe(int type,
111                const content::NotificationSource& source,
112                const content::NotificationDetails& details) override;
113
114   ProcessDetails* GetOrRegisterProcess(int render_process_id);
115   const ProcessDetails* GetProcess(int render_process_id) const;
116
117   content::NotificationRegistrar registrar_;
118
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_;
127
128   std::map<int, ProcessDetails> plugin_details_;
129 };
130
131 #endif  // CHROME_BROWSER_PLUGINS_CHROME_PLUGIN_SERVICE_FILTER_H_