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.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 "googleurl/src/gurl.h"
28 #include "ipc/ipc_channel_handle.h"
31 #include "base/memory/scoped_ptr.h"
32 #include "base/win/registry.h"
35 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
36 #include "base/files/file_path_watcher.h"
40 class MessageLoopProxy
;
51 class PluginDirWatcherDelegate
;
52 class PluginLoaderPosix
;
53 class PluginServiceFilter
;
54 class ResourceContext
;
55 struct PepperPluginInfo
;
57 // base::Bind() has limited arity, and the filter-related methods tend to
58 // surpass that limit.
59 struct PluginServiceFilterParams
{
60 int render_process_id
;
63 ResourceContext
* resource_context
;
66 class CONTENT_EXPORT PluginServiceImpl
67 : NON_EXPORTED_BASE(public PluginService
),
68 public base::WaitableEventWatcher::Delegate
{
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
<webkit::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
,
90 webkit::WebPluginInfo
* info
,
91 std::string
* actual_mime_type
) OVERRIDE
;
92 virtual bool GetPluginInfoByPath(const FilePath
& plugin_path
,
93 webkit::WebPluginInfo
* info
) OVERRIDE
;
94 virtual string16
GetPluginDisplayNameByPath(const FilePath
& path
) OVERRIDE
;
95 virtual void GetPlugins(const GetPluginsCallback
& callback
) OVERRIDE
;
96 virtual PepperPluginInfo
* GetRegisteredPpapiPluginInfo(
97 const FilePath
& plugin_path
) OVERRIDE
;
98 virtual void SetFilter(PluginServiceFilter
* filter
) OVERRIDE
;
99 virtual PluginServiceFilter
* GetFilter() OVERRIDE
;
100 virtual void ForcePluginShutdown(const FilePath
& plugin_path
) OVERRIDE
;
101 virtual bool IsPluginUnstable(const FilePath
& plugin_path
) OVERRIDE
;
102 virtual void RefreshPlugins() OVERRIDE
;
103 virtual void AddExtraPluginPath(const FilePath
& path
) OVERRIDE
;
104 virtual void AddExtraPluginDir(const FilePath
& path
) OVERRIDE
;
105 virtual void RemoveExtraPluginPath(const FilePath
& path
) OVERRIDE
;
106 virtual void UnregisterInternalPlugin(const FilePath
& path
) OVERRIDE
;
107 virtual void RegisterInternalPlugin(
108 const webkit::WebPluginInfo
& info
, bool add_at_beginning
) OVERRIDE
;
109 virtual void GetInternalPlugins(
110 std::vector
<webkit::WebPluginInfo
>* plugins
) OVERRIDE
;
111 virtual webkit::npapi::PluginList
* GetPluginList() OVERRIDE
;
112 virtual void SetPluginListForTesting(
113 webkit::npapi::PluginList
* plugin_list
) OVERRIDE
;
114 #if defined(OS_MACOSX)
115 virtual void AppActivated() OVERRIDE
;
118 // 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(
125 const FilePath
& plugin_path
,
126 const FilePath
& profile_data_directory
,
127 PpapiPluginProcessHost::PluginClient
* client
);
128 PpapiPluginProcessHost
* FindOrStartPpapiBrokerProcess(
129 const FilePath
& plugin_path
);
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
,
137 const GURL
& page_url
,
138 const std::string
& mime_type
,
139 PluginProcessHost::Client
* client
);
140 void OpenChannelToPpapiPlugin(const FilePath
& plugin_path
,
141 const FilePath
& profile_data_directory
,
142 PpapiPluginProcessHost::PluginClient
* client
);
143 void OpenChannelToPpapiBroker(const FilePath
& path
,
144 PpapiPluginProcessHost::BrokerClient
* client
);
146 // Cancels opening a channel to a NPAPI plugin.
147 void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client
* client
);
149 // Used to monitor plug-in stability.
150 void RegisterPluginCrash(const FilePath
& plugin_path
);
153 friend struct DefaultSingletonTraits
<PluginServiceImpl
>;
155 // Creates the PluginServiceImpl object, but doesn't actually build the plugin
156 // list yet. It's generated lazily.
158 virtual ~PluginServiceImpl();
160 // base::WaitableEventWatcher::Delegate implementation.
161 virtual void OnWaitableEventSignaled(
162 base::WaitableEvent
* waitable_event
) OVERRIDE
;
164 // 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
167 PluginProcessHost
* FindNpapiPluginProcess(const FilePath
& plugin_path
);
168 PpapiPluginProcessHost
* FindPpapiPluginProcess(
169 const FilePath
& plugin_path
,
170 const FilePath
& profile_data_directory
);
171 PpapiPluginProcessHost
* FindPpapiBrokerProcess(const FilePath
& broker_path
);
173 void RegisterPepperPlugins();
175 // Run on the blocking pool to load the plugins synchronously.
176 void GetPluginsInternal(base::MessageLoopProxy
* target_loop
,
177 const GetPluginsCallback
& callback
);
179 // Binding directly to GetAllowedPluginForOpenChannelToPlugin() isn't possible
180 // because more arity is needed <http://crbug.com/98542>. This just forwards.
181 void ForwardGetAllowedPluginForOpenChannelToPlugin(
182 const PluginServiceFilterParams
& params
,
184 const std::string
& mime_type
,
185 PluginProcessHost::Client
* client
,
186 const std::vector
<webkit::WebPluginInfo
>&);
187 // Helper so we can do the plugin lookup on the FILE thread.
188 void GetAllowedPluginForOpenChannelToPlugin(
189 int render_process_id
,
192 const GURL
& page_url
,
193 const std::string
& mime_type
,
194 PluginProcessHost::Client
* client
,
195 ResourceContext
* resource_context
);
197 // Helper so we can finish opening the channel after looking up the
199 void FinishOpenChannelToPlugin(const FilePath
& plugin_path
,
200 PluginProcessHost::Client
* client
);
202 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
203 // Registers a new FilePathWatcher for a given path.
204 static void RegisterFilePathWatcher(base::FilePathWatcher
* watcher
,
205 const FilePath
& path
);
208 // The plugin list instance.
209 webkit::npapi::PluginList
* plugin_list_
;
212 // Registry keys for getting notifications when new plugins are installed.
213 base::win::RegKey hkcu_key_
;
214 base::win::RegKey hklm_key_
;
215 scoped_ptr
<base::WaitableEvent
> hkcu_event_
;
216 scoped_ptr
<base::WaitableEvent
> hklm_event_
;
217 base::WaitableEventWatcher hkcu_watcher_
;
218 base::WaitableEventWatcher hklm_watcher_
;
221 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
222 ScopedVector
<base::FilePathWatcher
> file_watchers_
;
225 std::vector
<PepperPluginInfo
> ppapi_plugins_
;
227 // Weak pointer; outlives us.
228 PluginServiceFilter
* filter_
;
230 std::set
<PluginProcessHost::Client
*> pending_plugin_clients_
;
232 // Used to sequentialize loading plug-ins from disk.
233 base::SequencedWorkerPool::SequenceToken plugin_list_token_
;
235 #if defined(OS_POSIX)
236 scoped_refptr
<PluginLoaderPosix
> plugin_loader_
;
239 // Used to detect if a given plug-in is crashing over and over.
240 std::map
<FilePath
, std::vector
<base::Time
> > crash_times_
;
242 DISALLOW_COPY_AND_ASSIGN(PluginServiceImpl
);
245 } // namespace content
247 #endif // CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_