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_
15 #include "base/basictypes.h"
16 #include "base/compiler_specific.h"
17 #include "base/memory/scoped_vector.h"
18 #include "base/memory/singleton.h"
19 #include "base/synchronization/waitable_event_watcher.h"
20 #include "base/threading/sequenced_worker_pool.h"
21 #include "base/time/time.h"
22 #include "build/build_config.h"
23 #include "content/browser/plugin_process_host.h"
24 #include "content/browser/ppapi_plugin_process_host.h"
25 #include "content/common/content_export.h"
26 #include "content/public/browser/plugin_service.h"
27 #include "content/public/common/pepper_plugin_info.h"
28 #include "ipc/ipc_channel_handle.h"
32 #include "base/memory/scoped_ptr.h"
33 #include "base/win/registry.h"
36 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
37 #include "base/files/file_path_watcher.h"
41 class MessageLoopProxy;
52 class PluginDirWatcherDelegate;
53 class PluginLoaderPosix;
54 class PluginServiceFilter;
55 class ResourceContext;
56 struct PepperPluginInfo;
58 // base::Bind() has limited arity, and the filter-related methods tend to
59 // surpass that limit.
60 struct PluginServiceFilterParams {
61 int render_process_id;
64 ResourceContext* resource_context;
67 class CONTENT_EXPORT PluginServiceImpl
68 : NON_EXPORTED_BASE(public PluginService) {
70 // Returns the PluginServiceImpl singleton.
71 static PluginServiceImpl* GetInstance();
73 // PluginService implementation:
74 virtual void Init() OVERRIDE;
75 virtual void StartWatchingPlugins() OVERRIDE;
76 virtual bool GetPluginInfoArray(
78 const std::string& mime_type,
80 std::vector<WebPluginInfo>* info,
81 std::vector<std::string>* actual_mime_types) OVERRIDE;
82 virtual bool GetPluginInfo(int render_process_id,
84 ResourceContext* context,
87 const std::string& mime_type,
91 std::string* actual_mime_type) OVERRIDE;
92 virtual bool GetPluginInfoByPath(const base::FilePath& plugin_path,
93 WebPluginInfo* info) OVERRIDE;
94 virtual string16 GetPluginDisplayNameByPath(
95 const base::FilePath& path) OVERRIDE;
96 virtual void GetPlugins(const GetPluginsCallback& callback) OVERRIDE;
97 virtual PepperPluginInfo* GetRegisteredPpapiPluginInfo(
98 const base::FilePath& plugin_path) OVERRIDE;
99 virtual void SetFilter(PluginServiceFilter* filter) OVERRIDE;
100 virtual PluginServiceFilter* GetFilter() OVERRIDE;
101 virtual void ForcePluginShutdown(const base::FilePath& plugin_path) OVERRIDE;
102 virtual bool IsPluginUnstable(const base::FilePath& plugin_path) OVERRIDE;
103 virtual void RefreshPlugins() OVERRIDE;
104 virtual void AddExtraPluginPath(const base::FilePath& path) OVERRIDE;
105 virtual void RemoveExtraPluginPath(const base::FilePath& path) OVERRIDE;
106 virtual void AddExtraPluginDir(const base::FilePath& path) OVERRIDE;
107 virtual void RegisterInternalPlugin(
108 const WebPluginInfo& info, bool add_at_beginning) OVERRIDE;
109 virtual void UnregisterInternalPlugin(const base::FilePath& path) OVERRIDE;
110 virtual void GetInternalPlugins(
111 std::vector<WebPluginInfo>* plugins) OVERRIDE;
112 virtual bool NPAPIPluginsSupported() OVERRIDE;
113 virtual void DisablePluginsDiscoveryForTesting() OVERRIDE;
114 #if defined(OS_MACOSX)
115 virtual void AppActivated() OVERRIDE;
116 #elif defined(OS_WIN)
117 virtual bool GetPluginInfoFromWindow(HWND window,
118 base::string16* plugin_name,
119 base::string16* plugin_version) OVERRIDE;
121 // Returns true iff the given HWND is a plugin.
122 bool IsPluginWindow(HWND window);
125 // Returns the plugin process host corresponding to the plugin process that
126 // has been started by this service. This will start a process to host the
127 // 'plugin_path' if needed. If the process fails to start, the return value
128 // is NULL. Must be called on the IO thread.
129 PluginProcessHost* FindOrStartNpapiPluginProcess(
130 int render_process_id, const base::FilePath& plugin_path);
131 PpapiPluginProcessHost* FindOrStartPpapiPluginProcess(
132 int render_process_id,
133 const base::FilePath& plugin_path,
134 const base::FilePath& profile_data_directory);
135 PpapiPluginProcessHost* FindOrStartPpapiBrokerProcess(
136 int render_process_id, const base::FilePath& plugin_path);
138 // Opens a channel to a plugin process for the given mime type, starting
139 // a new plugin process if necessary. This must be called on the IO thread
140 // or else a deadlock can occur.
141 void OpenChannelToNpapiPlugin(int render_process_id,
144 const GURL& page_url,
145 const std::string& mime_type,
146 PluginProcessHost::Client* client);
147 void OpenChannelToPpapiPlugin(int render_process_id,
148 const base::FilePath& plugin_path,
149 const base::FilePath& profile_data_directory,
150 PpapiPluginProcessHost::PluginClient* client);
151 void OpenChannelToPpapiBroker(int render_process_id,
152 const base::FilePath& path,
153 PpapiPluginProcessHost::BrokerClient* client);
155 // Cancels opening a channel to a NPAPI plugin.
156 void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client* client);
158 // Used to monitor plug-in stability.
159 void RegisterPluginCrash(const base::FilePath& plugin_path);
162 friend struct DefaultSingletonTraits<PluginServiceImpl>;
164 // Creates the PluginServiceImpl object, but doesn't actually build the plugin
165 // list yet. It's generated lazily.
167 virtual ~PluginServiceImpl();
169 void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
171 // Returns the plugin process host corresponding to the plugin process that
172 // has been started by this service. Returns NULL if no process has been
174 PluginProcessHost* FindNpapiPluginProcess(const base::FilePath& plugin_path);
175 PpapiPluginProcessHost* FindPpapiPluginProcess(
176 const base::FilePath& plugin_path,
177 const base::FilePath& profile_data_directory);
178 PpapiPluginProcessHost* FindPpapiBrokerProcess(
179 const base::FilePath& broker_path);
181 void RegisterPepperPlugins();
183 // Run on the blocking pool to load the plugins synchronously.
184 void GetPluginsInternal(base::MessageLoopProxy* target_loop,
185 const GetPluginsCallback& callback);
187 // Binding directly to GetAllowedPluginForOpenChannelToPlugin() isn't possible
188 // because more arity is needed <http://crbug.com/98542>. This just forwards.
189 void ForwardGetAllowedPluginForOpenChannelToPlugin(
190 const PluginServiceFilterParams& params,
192 const std::string& mime_type,
193 PluginProcessHost::Client* client,
194 const std::vector<WebPluginInfo>&);
195 // Helper so we can do the plugin lookup on the FILE thread.
196 void GetAllowedPluginForOpenChannelToPlugin(
197 int render_process_id,
200 const GURL& page_url,
201 const std::string& mime_type,
202 PluginProcessHost::Client* client,
203 ResourceContext* resource_context);
205 // Helper so we can finish opening the channel after looking up the
207 void FinishOpenChannelToPlugin(int render_process_id,
208 const base::FilePath& plugin_path,
209 PluginProcessHost::Client* client);
211 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
212 // Registers a new FilePathWatcher for a given path.
213 static void RegisterFilePathWatcher(base::FilePathWatcher* watcher,
214 const base::FilePath& path);
218 // Registry keys for getting notifications when new plugins are installed.
219 base::win::RegKey hkcu_key_;
220 base::win::RegKey hklm_key_;
221 scoped_ptr<base::WaitableEvent> hkcu_event_;
222 scoped_ptr<base::WaitableEvent> hklm_event_;
223 base::WaitableEventWatcher hkcu_watcher_;
224 base::WaitableEventWatcher hklm_watcher_;
227 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
228 ScopedVector<base::FilePathWatcher> file_watchers_;
231 std::vector<PepperPluginInfo> ppapi_plugins_;
233 // Weak pointer; outlives us.
234 PluginServiceFilter* filter_;
236 std::set<PluginProcessHost::Client*> pending_plugin_clients_;
238 // Used to sequentialize loading plug-ins from disk.
239 base::SequencedWorkerPool::SequenceToken plugin_list_token_;
241 #if defined(OS_POSIX)
242 scoped_refptr<PluginLoaderPosix> plugin_loader_;
245 // Used to detect if a given plug-in is crashing over and over.
246 std::map<base::FilePath, std::vector<base::Time> > crash_times_;
248 DISALLOW_COPY_AND_ASSIGN(PluginServiceImpl);
251 } // namespace content
253 #endif // CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_