blob: 1b7edfaa20bf9adab5a8f6477ee4b6c06c847788 [file] [log] [blame]
[email protected]3b48dbc2012-01-06 16:34:171// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a0421732011-02-23 03:55:402// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This class responds to requests from renderers for the list of plugins, and
6// also a proxy object for plugin instances.
7
[email protected]e67385f2011-12-21 06:00:568#ifndef CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_
9#define CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_
[email protected]a0421732011-02-23 03:55:4010
[email protected]47214d882012-02-29 06:28:4811#include <map>
[email protected]7dba43dc2011-11-10 01:57:5812#include <set>
[email protected]47214d882012-02-29 06:28:4813#include <vector>
[email protected]a0421732011-02-23 03:55:4014
15#include "base/basictypes.h"
[email protected]bdd6deb2011-12-21 03:03:2416#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/scoped_vector.h"
18#include "base/memory/singleton.h"
[email protected]a0421732011-02-23 03:55:4019#include "base/synchronization/waitable_event_watcher.h"
[email protected]a33fa9d2012-05-16 14:47:4920#include "base/threading/sequenced_worker_pool.h"
[email protected]47214d882012-02-29 06:28:4821#include "base/time.h"
[email protected]a0421732011-02-23 03:55:4022#include "build/build_config.h"
[email protected]a0421732011-02-23 03:55:4023#include "content/browser/plugin_process_host.h"
24#include "content/browser/ppapi_plugin_process_host.h"
[email protected]8d128d62011-09-13 22:11:5725#include "content/common/content_export.h"
[email protected]3a5180ae2011-12-21 02:39:3826#include "content/public/browser/plugin_service.h"
[email protected]a0421732011-02-23 03:55:4027#include "googleurl/src/gurl.h"
28#include "ipc/ipc_channel_handle.h"
[email protected]a0421732011-02-23 03:55:4029
30#if defined(OS_WIN)
[email protected]3b63f8f42011-03-28 01:54:1531#include "base/memory/scoped_ptr.h"
[email protected]a0421732011-02-23 03:55:4032#include "base/win/registry.h"
33#endif
34
[email protected]52348b22012-11-07 10:19:3435#if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
[email protected]493c8002011-04-14 16:56:0136#include "base/files/file_path_watcher.h"
[email protected]a0421732011-02-23 03:55:4037#endif
38
[email protected]d33e7cc2011-09-23 01:43:5639namespace base {
40class MessageLoopProxy;
41}
42
[email protected]d33e7cc2011-09-23 01:43:5643namespace webkit {
44namespace npapi {
[email protected]d33e7cc2011-09-23 01:43:5645class PluginList;
46}
47}
48
[email protected]130757672012-10-24 00:26:1949namespace content {
50class BrowserContext;
51class PluginDirWatcherDelegate;
52class PluginLoaderPosix;
53class PluginServiceFilter;
54class ResourceContext;
55struct PepperPluginInfo;
56
[email protected]209f2ae2012-03-13 01:28:0857// base::Bind() has limited arity, and the filter-related methods tend to
58// surpass that limit.
59struct PluginServiceFilterParams {
60 int render_process_id;
61 int render_view_id;
62 GURL page_url;
[email protected]130757672012-10-24 00:26:1963 ResourceContext* resource_context;
[email protected]209f2ae2012-03-13 01:28:0864};
65
[email protected]e67385f2011-12-21 06:00:5666class CONTENT_EXPORT PluginServiceImpl
[email protected]130757672012-10-24 00:26:1967 : NON_EXPORTED_BASE(public PluginService),
[email protected]5904cb42012-09-24 15:05:2068 public base::WaitableEventWatcher::Delegate {
[email protected]a0421732011-02-23 03:55:4069 public:
[email protected]e67385f2011-12-21 06:00:5670 // Returns the PluginServiceImpl singleton.
71 static PluginServiceImpl* GetInstance();
[email protected]a0421732011-02-23 03:55:4072
[email protected]130757672012-10-24 00:26:1973 // PluginService implementation:
[email protected]3a5180ae2011-12-21 02:39:3874 virtual void Init() OVERRIDE;
75 virtual void StartWatchingPlugins() OVERRIDE;
[email protected]3a5180ae2011-12-21 02:39:3876 virtual bool GetPluginInfoArray(
77 const GURL& url,
78 const std::string& mime_type,
79 bool allow_wildcard,
80 std::vector<webkit::WebPluginInfo>* info,
81 std::vector<std::string>* actual_mime_types) OVERRIDE;
82 virtual bool GetPluginInfo(int render_process_id,
83 int render_view_id,
[email protected]130757672012-10-24 00:26:1984 ResourceContext* context,
[email protected]3a5180ae2011-12-21 02:39:3885 const GURL& url,
86 const GURL& page_url,
87 const std::string& mime_type,
88 bool allow_wildcard,
89 bool* is_stale,
90 webkit::WebPluginInfo* info,
91 std::string* actual_mime_type) OVERRIDE;
92 virtual bool GetPluginInfoByPath(const FilePath& plugin_path,
93 webkit::WebPluginInfo* info) OVERRIDE;
[email protected]8be45842012-04-13 19:49:2994 virtual string16 GetPluginDisplayNameByPath(const FilePath& path) OVERRIDE;
[email protected]3a5180ae2011-12-21 02:39:3895 virtual void GetPlugins(const GetPluginsCallback& callback) OVERRIDE;
[email protected]130757672012-10-24 00:26:1996 virtual PepperPluginInfo* GetRegisteredPpapiPluginInfo(
[email protected]3a5180ae2011-12-21 02:39:3897 const FilePath& plugin_path) OVERRIDE;
[email protected]130757672012-10-24 00:26:1998 virtual void SetFilter(PluginServiceFilter* filter) OVERRIDE;
99 virtual PluginServiceFilter* GetFilter() OVERRIDE;
[email protected]b6a2f8de2012-01-31 17:28:49100 virtual void ForcePluginShutdown(const FilePath& plugin_path) OVERRIDE;
[email protected]47214d882012-02-29 06:28:48101 virtual bool IsPluginUnstable(const FilePath& plugin_path) OVERRIDE;
[email protected]3a5180ae2011-12-21 02:39:38102 virtual void RefreshPlugins() OVERRIDE;
103 virtual void AddExtraPluginPath(const FilePath& path) OVERRIDE;
[email protected]c6f3dea2012-01-14 02:23:11104 virtual void AddExtraPluginDir(const FilePath& path) OVERRIDE;
[email protected]3a5180ae2011-12-21 02:39:38105 virtual void RemoveExtraPluginPath(const FilePath& path) OVERRIDE;
106 virtual void UnregisterInternalPlugin(const FilePath& path) OVERRIDE;
107 virtual void RegisterInternalPlugin(
[email protected]c6f3dea2012-01-14 02:23:11108 const webkit::WebPluginInfo& info, bool add_at_beginning) OVERRIDE;
[email protected]57aece22012-10-10 22:10:27109 virtual void GetInternalPlugins(
110 std::vector<webkit::WebPluginInfo>* plugins) OVERRIDE;
[email protected]3a5180ae2011-12-21 02:39:38111 virtual webkit::npapi::PluginList* GetPluginList() OVERRIDE;
[email protected]e67385f2011-12-21 06:00:56112 virtual void SetPluginListForTesting(
113 webkit::npapi::PluginList* plugin_list) OVERRIDE;
[email protected]5904cb42012-09-24 15:05:20114#if defined(OS_MACOSX)
115 virtual void AppActivated() OVERRIDE;
116#endif
[email protected]dfba8762011-09-02 12:49:54117
[email protected]a0421732011-02-23 03:55:40118 // Returns the plugin process host corresponding to the plugin process that
119 // has been started by this service. This will start a process to host the
120 // 'plugin_path' if needed. If the process fails to start, the return value
121 // is NULL. Must be called on the IO thread.
122 PluginProcessHost* FindOrStartNpapiPluginProcess(
123 const FilePath& plugin_path);
124 PpapiPluginProcessHost* FindOrStartPpapiPluginProcess(
[email protected]d259a8e2011-05-18 22:31:09125 const FilePath& plugin_path,
[email protected]dd9a0952012-05-31 20:11:31126 const FilePath& profile_data_directory,
[email protected]a50432d2011-09-30 16:32:14127 PpapiPluginProcessHost::PluginClient* client);
128 PpapiPluginProcessHost* FindOrStartPpapiBrokerProcess(
[email protected]eb415bf0e2011-04-14 02:45:42129 const FilePath& plugin_path);
[email protected]a0421732011-02-23 03:55:40130
131 // Opens a channel to a plugin process for the given mime type, starting
132 // a new plugin process if necessary. This must be called on the IO thread
133 // or else a deadlock can occur.
134 void OpenChannelToNpapiPlugin(int render_process_id,
135 int render_view_id,
136 const GURL& url,
[email protected]dfba8762011-09-02 12:49:54137 const GURL& page_url,
[email protected]a0421732011-02-23 03:55:40138 const std::string& mime_type,
139 PluginProcessHost::Client* client);
[email protected]dd9a0952012-05-31 20:11:31140 void OpenChannelToPpapiPlugin(const FilePath& plugin_path,
141 const FilePath& profile_data_directory,
[email protected]a50432d2011-09-30 16:32:14142 PpapiPluginProcessHost::PluginClient* client);
[email protected]eb415bf0e2011-04-14 02:45:42143 void OpenChannelToPpapiBroker(const FilePath& path,
[email protected]a50432d2011-09-30 16:32:14144 PpapiPluginProcessHost::BrokerClient* client);
[email protected]a0421732011-02-23 03:55:40145
[email protected]4befe7592011-09-14 22:49:09146 // Cancels opening a channel to a NPAPI plugin.
147 void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client* client);
148
[email protected]47214d882012-02-29 06:28:48149 // Used to monitor plug-in stability.
150 void RegisterPluginCrash(const FilePath& plugin_path);
151
[email protected]a0421732011-02-23 03:55:40152 private:
[email protected]e67385f2011-12-21 06:00:56153 friend struct DefaultSingletonTraits<PluginServiceImpl>;
[email protected]a0421732011-02-23 03:55:40154
[email protected]e67385f2011-12-21 06:00:56155 // Creates the PluginServiceImpl object, but doesn't actually build the plugin
[email protected]a0421732011-02-23 03:55:40156 // list yet. It's generated lazily.
[email protected]e67385f2011-12-21 06:00:56157 PluginServiceImpl();
158 virtual ~PluginServiceImpl();
[email protected]a0421732011-02-23 03:55:40159
160 // base::WaitableEventWatcher::Delegate implementation.
[email protected]edc64de2011-11-17 20:07:38161 virtual void OnWaitableEventSignaled(
162 base::WaitableEvent* waitable_event) OVERRIDE;
[email protected]a0421732011-02-23 03:55:40163
[email protected]b6a2f8de2012-01-31 17:28:49164 // Returns the plugin process host corresponding to the plugin process that
165 // has been started by this service. Returns NULL if no process has been
166 // started.
167 PluginProcessHost* FindNpapiPluginProcess(const FilePath& plugin_path);
[email protected]dd9a0952012-05-31 20:11:31168 PpapiPluginProcessHost* FindPpapiPluginProcess(
169 const FilePath& plugin_path,
170 const FilePath& profile_data_directory);
[email protected]b6a2f8de2012-01-31 17:28:49171 PpapiPluginProcessHost* FindPpapiBrokerProcess(const FilePath& broker_path);
172
[email protected]a0421732011-02-23 03:55:40173 void RegisterPepperPlugins();
174
[email protected]a33fa9d2012-05-16 14:47:49175 // Run on the blocking pool to load the plugins synchronously.
[email protected]d33e7cc2011-09-23 01:43:56176 void GetPluginsInternal(base::MessageLoopProxy* target_loop,
177 const GetPluginsCallback& callback);
178
[email protected]88ca4912011-10-12 14:00:43179 // Binding directly to GetAllowedPluginForOpenChannelToPlugin() isn't possible
180 // because more arity is needed <http://crbug.com/98542>. This just forwards.
181 void ForwardGetAllowedPluginForOpenChannelToPlugin(
[email protected]209f2ae2012-03-13 01:28:08182 const PluginServiceFilterParams& params,
[email protected]88ca4912011-10-12 14:00:43183 const GURL& url,
184 const std::string& mime_type,
185 PluginProcessHost::Client* client,
186 const std::vector<webkit::WebPluginInfo>&);
[email protected]a0421732011-02-23 03:55:40187 // Helper so we can do the plugin lookup on the FILE thread.
188 void GetAllowedPluginForOpenChannelToPlugin(
189 int render_process_id,
190 int render_view_id,
191 const GURL& url,
[email protected]dfba8762011-09-02 12:49:54192 const GURL& page_url,
[email protected]a0421732011-02-23 03:55:40193 const std::string& mime_type,
[email protected]87c4be42011-09-16 01:10:59194 PluginProcessHost::Client* client,
[email protected]130757672012-10-24 00:26:19195 ResourceContext* resource_context);
[email protected]a0421732011-02-23 03:55:40196
197 // Helper so we can finish opening the channel after looking up the
198 // plugin.
199 void FinishOpenChannelToPlugin(
200 const FilePath& plugin_path,
201 PluginProcessHost::Client* client);
202
[email protected]52348b22012-11-07 10:19:34203#if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
[email protected]a0421732011-02-23 03:55:40204 // Registers a new FilePathWatcher for a given path.
205 static void RegisterFilePathWatcher(
[email protected]493c8002011-04-14 16:56:01206 base::files::FilePathWatcher* watcher,
[email protected]a0421732011-02-23 03:55:40207 const FilePath& path,
[email protected]493c8002011-04-14 16:56:01208 base::files::FilePathWatcher::Delegate* delegate);
[email protected]a0421732011-02-23 03:55:40209#endif
210
[email protected]ee066172011-11-10 23:20:05211 // The plugin list instance.
212 webkit::npapi::PluginList* plugin_list_;
213
[email protected]a0421732011-02-23 03:55:40214#if defined(OS_WIN)
215 // Registry keys for getting notifications when new plugins are installed.
216 base::win::RegKey hkcu_key_;
217 base::win::RegKey hklm_key_;
218 scoped_ptr<base::WaitableEvent> hkcu_event_;
219 scoped_ptr<base::WaitableEvent> hklm_event_;
220 base::WaitableEventWatcher hkcu_watcher_;
221 base::WaitableEventWatcher hklm_watcher_;
222#endif
223
[email protected]52348b22012-11-07 10:19:34224#if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
[email protected]493c8002011-04-14 16:56:01225 ScopedVector<base::files::FilePathWatcher> file_watchers_;
[email protected]a0421732011-02-23 03:55:40226 scoped_refptr<PluginDirWatcherDelegate> file_watcher_delegate_;
227#endif
228
[email protected]130757672012-10-24 00:26:19229 std::vector<PepperPluginInfo> ppapi_plugins_;
[email protected]a0421732011-02-23 03:55:40230
[email protected]dfba8762011-09-02 12:49:54231 // Weak pointer; outlives us.
[email protected]130757672012-10-24 00:26:19232 PluginServiceFilter* filter_;
[email protected]a0421732011-02-23 03:55:40233
[email protected]4befe7592011-09-14 22:49:09234 std::set<PluginProcessHost::Client*> pending_plugin_clients_;
235
[email protected]a33fa9d2012-05-16 14:47:49236 // Used to sequentialize loading plug-ins from disk.
237 base::SequencedWorkerPool::SequenceToken plugin_list_token_;
[email protected]1b517202012-12-19 17:16:10238
[email protected]d4af1e72011-10-21 17:45:43239#if defined(OS_POSIX)
240 scoped_refptr<PluginLoaderPosix> plugin_loader_;
241#endif
242
[email protected]47214d882012-02-29 06:28:48243 // Used to detect if a given plug-in is crashing over and over.
244 std::map<FilePath, std::vector<base::Time> > crash_times_;
245
[email protected]e67385f2011-12-21 06:00:56246 DISALLOW_COPY_AND_ASSIGN(PluginServiceImpl);
[email protected]a0421732011-02-23 03:55:40247};
248
[email protected]130757672012-10-24 00:26:19249} // namespace content
250
[email protected]e67385f2011-12-21 06:00:56251#endif // CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_