Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / plugin_service_impl.h
blob1d419e09dead02283c91cd174de0daffcf7bb9d0
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 // This class responds to requests from renderers for the list of plugins, and
6 // also a proxy object for plugin instances.
8 #ifndef CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_
9 #define CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_
11 #if !defined(ENABLE_PLUGINS)
12 #error "Plugins should be enabled"
13 #endif
15 #include <map>
16 #include <set>
17 #include <vector>
19 #include "base/basictypes.h"
20 #include "base/compiler_specific.h"
21 #include "base/memory/scoped_vector.h"
22 #include "base/memory/singleton.h"
23 #include "base/synchronization/waitable_event_watcher.h"
24 #include "base/threading/sequenced_worker_pool.h"
25 #include "base/time/time.h"
26 #include "build/build_config.h"
27 #include "content/browser/plugin_process_host.h"
28 #include "content/browser/ppapi_plugin_process_host.h"
29 #include "content/common/content_export.h"
30 #include "content/public/browser/plugin_service.h"
31 #include "content/public/common/pepper_plugin_info.h"
32 #include "ipc/ipc_channel_handle.h"
33 #include "url/gurl.h"
35 #if defined(OS_WIN)
36 #include "base/memory/scoped_ptr.h"
37 #include "base/win/registry.h"
38 #endif
40 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
41 #include "base/files/file_path_watcher.h"
42 #endif
44 namespace base {
45 class MessageLoopProxy;
48 namespace content {
49 class BrowserContext;
50 class PluginDirWatcherDelegate;
51 class PluginLoaderPosix;
52 class PluginServiceFilter;
53 class ResourceContext;
54 struct PepperPluginInfo;
56 // base::Bind() has limited arity, and the filter-related methods tend to
57 // surpass that limit.
58 struct PluginServiceFilterParams {
59 int render_process_id;
60 int render_frame_id;
61 GURL page_url;
62 ResourceContext* resource_context;
65 class CONTENT_EXPORT PluginServiceImpl
66 : NON_EXPORTED_BASE(public PluginService) {
67 public:
68 // Returns the PluginServiceImpl singleton.
69 static PluginServiceImpl* GetInstance();
71 // PluginService implementation:
72 void Init() override;
73 void StartWatchingPlugins() override;
74 bool GetPluginInfoArray(const GURL& url,
75 const std::string& mime_type,
76 bool allow_wildcard,
77 std::vector<WebPluginInfo>* info,
78 std::vector<std::string>* actual_mime_types) override;
79 bool GetPluginInfo(int render_process_id,
80 int render_frame_id,
81 ResourceContext* context,
82 const GURL& url,
83 const GURL& page_url,
84 const std::string& mime_type,
85 bool allow_wildcard,
86 bool* is_stale,
87 WebPluginInfo* info,
88 std::string* actual_mime_type) override;
89 bool GetPluginInfoByPath(const base::FilePath& plugin_path,
90 WebPluginInfo* info) override;
91 base::string16 GetPluginDisplayNameByPath(
92 const base::FilePath& path) override;
93 void GetPlugins(const GetPluginsCallback& callback) override;
94 PepperPluginInfo* GetRegisteredPpapiPluginInfo(
95 const base::FilePath& plugin_path) override;
96 void SetFilter(PluginServiceFilter* filter) override;
97 PluginServiceFilter* GetFilter() override;
98 void ForcePluginShutdown(const base::FilePath& plugin_path) override;
99 bool IsPluginUnstable(const base::FilePath& plugin_path) override;
100 void RefreshPlugins() override;
101 void AddExtraPluginPath(const base::FilePath& path) override;
102 void RemoveExtraPluginPath(const base::FilePath& path) override;
103 void AddExtraPluginDir(const base::FilePath& path) override;
104 void RegisterInternalPlugin(const WebPluginInfo& info,
105 bool add_at_beginning) override;
106 void UnregisterInternalPlugin(const base::FilePath& path) override;
107 void GetInternalPlugins(std::vector<WebPluginInfo>* plugins) override;
108 bool NPAPIPluginsSupported() override;
109 void EnableNpapiPlugins() override;
110 void DisablePluginsDiscoveryForTesting() override;
111 #if defined(OS_MACOSX)
112 void AppActivated() override;
113 #elif defined(OS_WIN)
114 bool GetPluginInfoFromWindow(HWND window,
115 base::string16* plugin_name,
116 base::string16* plugin_version) override;
118 // Returns true iff the given HWND is a plugin.
119 bool IsPluginWindow(HWND window);
120 #endif
121 bool PpapiDevChannelSupported(BrowserContext* browser_context,
122 const GURL& document_url) override;
124 // Returns the plugin process host corresponding to the plugin process that
125 // has been started by this service. This will start a process to host the
126 // 'plugin_path' if needed. If the process fails to start, the return value
127 // is NULL. Must be called on the IO thread.
128 PluginProcessHost* FindOrStartNpapiPluginProcess(
129 int render_process_id, const base::FilePath& plugin_path);
130 PpapiPluginProcessHost* FindOrStartPpapiPluginProcess(
131 int render_process_id,
132 const base::FilePath& plugin_path,
133 const base::FilePath& profile_data_directory);
134 PpapiPluginProcessHost* FindOrStartPpapiBrokerProcess(
135 int render_process_id, const base::FilePath& plugin_path);
137 // Opens a channel to a plugin process for the given mime type, starting
138 // a new plugin process if necessary. This must be called on the IO thread
139 // or else a deadlock can occur.
140 void OpenChannelToNpapiPlugin(int render_process_id,
141 int render_frame_id,
142 const GURL& url,
143 const GURL& page_url,
144 const std::string& mime_type,
145 PluginProcessHost::Client* client);
146 void OpenChannelToPpapiPlugin(int render_process_id,
147 const base::FilePath& plugin_path,
148 const base::FilePath& profile_data_directory,
149 PpapiPluginProcessHost::PluginClient* client);
150 void OpenChannelToPpapiBroker(int render_process_id,
151 const base::FilePath& path,
152 PpapiPluginProcessHost::BrokerClient* client);
154 // Cancels opening a channel to a NPAPI plugin.
155 void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client* client);
157 // Used to monitor plugin stability.
158 void RegisterPluginCrash(const base::FilePath& plugin_path);
160 private:
161 friend struct DefaultSingletonTraits<PluginServiceImpl>;
163 // Creates the PluginServiceImpl object, but doesn't actually build the plugin
164 // list yet. It's generated lazily.
165 PluginServiceImpl();
166 ~PluginServiceImpl() override;
168 #if defined(OS_WIN)
169 void OnKeyChanged(base::win::RegKey* key);
170 #endif
172 // Returns the plugin process host corresponding to the plugin process that
173 // has been started by this service. Returns NULL if no process has been
174 // started.
175 PluginProcessHost* FindNpapiPluginProcess(const base::FilePath& plugin_path);
176 PpapiPluginProcessHost* FindPpapiPluginProcess(
177 const base::FilePath& plugin_path,
178 const base::FilePath& profile_data_directory);
179 PpapiPluginProcessHost* FindPpapiBrokerProcess(
180 const base::FilePath& broker_path);
182 void RegisterPepperPlugins();
184 // Run on the blocking pool to load the plugins synchronously.
185 void GetPluginsInternal(base::MessageLoopProxy* target_loop,
186 const GetPluginsCallback& callback);
188 #if defined(OS_POSIX)
189 void GetPluginsOnIOThread(
190 base::MessageLoopProxy* target_loop,
191 const GetPluginsCallback& callback);
192 #endif
194 // Binding directly to GetAllowedPluginForOpenChannelToPlugin() isn't possible
195 // because more arity is needed <http://crbug.com/98542>. This just forwards.
196 void ForwardGetAllowedPluginForOpenChannelToPlugin(
197 const PluginServiceFilterParams& params,
198 const GURL& url,
199 const std::string& mime_type,
200 PluginProcessHost::Client* client,
201 const std::vector<WebPluginInfo>&);
202 // Helper so we can do the plugin lookup on the FILE thread.
203 void GetAllowedPluginForOpenChannelToPlugin(
204 int render_process_id,
205 int render_frame_id,
206 const GURL& url,
207 const GURL& page_url,
208 const std::string& mime_type,
209 PluginProcessHost::Client* client,
210 ResourceContext* resource_context);
212 // Helper so we can finish opening the channel after looking up the
213 // plugin.
214 void FinishOpenChannelToPlugin(int render_process_id,
215 const base::FilePath& plugin_path,
216 PluginProcessHost::Client* client);
218 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
219 // Registers a new FilePathWatcher for a given path.
220 static void RegisterFilePathWatcher(base::FilePathWatcher* watcher,
221 const base::FilePath& path);
222 #endif
224 #if defined(OS_WIN)
225 // Registry keys for getting notifications when new plugins are installed.
226 base::win::RegKey hkcu_key_;
227 base::win::RegKey hklm_key_;
228 #endif
230 bool npapi_plugins_enabled_;
232 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
233 ScopedVector<base::FilePathWatcher> file_watchers_;
234 #endif
236 std::vector<PepperPluginInfo> ppapi_plugins_;
238 // Weak pointer; outlives us.
239 PluginServiceFilter* filter_;
241 std::set<PluginProcessHost::Client*> pending_plugin_clients_;
243 // Used to sequentialize loading plugins from disk.
244 base::SequencedWorkerPool::SequenceToken plugin_list_token_;
246 #if defined(OS_POSIX)
247 scoped_refptr<PluginLoaderPosix> plugin_loader_;
248 #endif
250 // Used to detect if a given plugin is crashing over and over.
251 std::map<base::FilePath, std::vector<base::Time> > crash_times_;
253 DISALLOW_COPY_AND_ASSIGN(PluginServiceImpl);
256 } // namespace content
258 #endif // CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_