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
;
46 class PluginDirWatcherDelegate
;
47 class PluginLoaderPosix
;
48 class PluginServiceFilter
;
49 class ResourceContext
;
50 struct PepperPluginInfo
;
52 // base::Bind() has limited arity, and the filter-related methods tend to
53 // surpass that limit.
54 struct PluginServiceFilterParams
{
55 int render_process_id
;
58 ResourceContext
* resource_context
;
61 class CONTENT_EXPORT PluginServiceImpl
62 : NON_EXPORTED_BASE(public PluginService
) {
64 // Returns the PluginServiceImpl singleton.
65 static PluginServiceImpl
* GetInstance();
67 // PluginService implementation:
68 virtual void Init() OVERRIDE
;
69 virtual void StartWatchingPlugins() OVERRIDE
;
70 virtual bool GetPluginInfoArray(
72 const std::string
& mime_type
,
74 std::vector
<WebPluginInfo
>* info
,
75 std::vector
<std::string
>* actual_mime_types
) OVERRIDE
;
76 virtual bool GetPluginInfo(int render_process_id
,
78 ResourceContext
* context
,
81 const std::string
& mime_type
,
85 std::string
* actual_mime_type
) OVERRIDE
;
86 virtual bool GetPluginInfoByPath(const base::FilePath
& plugin_path
,
87 WebPluginInfo
* info
) OVERRIDE
;
88 virtual base::string16
GetPluginDisplayNameByPath(
89 const base::FilePath
& path
) OVERRIDE
;
90 virtual void GetPlugins(const GetPluginsCallback
& callback
) OVERRIDE
;
91 virtual PepperPluginInfo
* GetRegisteredPpapiPluginInfo(
92 const base::FilePath
& plugin_path
) OVERRIDE
;
93 virtual void SetFilter(PluginServiceFilter
* filter
) OVERRIDE
;
94 virtual PluginServiceFilter
* GetFilter() OVERRIDE
;
95 virtual void ForcePluginShutdown(const base::FilePath
& plugin_path
) OVERRIDE
;
96 virtual bool IsPluginUnstable(const base::FilePath
& plugin_path
) OVERRIDE
;
97 virtual void RefreshPlugins() OVERRIDE
;
98 virtual void AddExtraPluginPath(const base::FilePath
& path
) OVERRIDE
;
99 virtual void RemoveExtraPluginPath(const base::FilePath
& path
) OVERRIDE
;
100 virtual void AddExtraPluginDir(const base::FilePath
& path
) OVERRIDE
;
101 virtual void RegisterInternalPlugin(
102 const WebPluginInfo
& info
, bool add_at_beginning
) OVERRIDE
;
103 virtual void UnregisterInternalPlugin(const base::FilePath
& path
) OVERRIDE
;
104 virtual void GetInternalPlugins(
105 std::vector
<WebPluginInfo
>* plugins
) OVERRIDE
;
106 virtual bool NPAPIPluginsSupported() OVERRIDE
;
107 virtual void DisablePluginsDiscoveryForTesting() OVERRIDE
;
108 #if defined(OS_MACOSX)
109 virtual void AppActivated() OVERRIDE
;
110 #elif defined(OS_WIN)
111 virtual bool GetPluginInfoFromWindow(HWND window
,
112 base::string16
* plugin_name
,
113 base::string16
* plugin_version
) OVERRIDE
;
115 // Returns true iff the given HWND is a plugin.
116 bool IsPluginWindow(HWND window
);
118 virtual bool PpapiDevChannelSupported() OVERRIDE
;
120 // Returns the plugin process host corresponding to the plugin process that
121 // has been started by this service. This will start a process to host the
122 // 'plugin_path' if needed. If the process fails to start, the return value
123 // is NULL. Must be called on the IO thread.
124 PluginProcessHost
* FindOrStartNpapiPluginProcess(
125 int render_process_id
, const base::FilePath
& plugin_path
);
126 PpapiPluginProcessHost
* FindOrStartPpapiPluginProcess(
127 int render_process_id
,
128 const base::FilePath
& plugin_path
,
129 const base::FilePath
& profile_data_directory
);
130 PpapiPluginProcessHost
* FindOrStartPpapiBrokerProcess(
131 int render_process_id
, const base::FilePath
& plugin_path
);
133 // Opens a channel to a plugin process for the given mime type, starting
134 // a new plugin process if necessary. This must be called on the IO thread
135 // or else a deadlock can occur.
136 void OpenChannelToNpapiPlugin(int render_process_id
,
139 const GURL
& page_url
,
140 const std::string
& mime_type
,
141 PluginProcessHost::Client
* client
);
142 void OpenChannelToPpapiPlugin(int render_process_id
,
143 const base::FilePath
& plugin_path
,
144 const base::FilePath
& profile_data_directory
,
145 PpapiPluginProcessHost::PluginClient
* client
);
146 void OpenChannelToPpapiBroker(int render_process_id
,
147 const base::FilePath
& path
,
148 PpapiPluginProcessHost::BrokerClient
* client
);
150 // Cancels opening a channel to a NPAPI plugin.
151 void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client
* client
);
153 // Used to monitor plug-in stability.
154 void RegisterPluginCrash(const base::FilePath
& plugin_path
);
157 friend struct DefaultSingletonTraits
<PluginServiceImpl
>;
159 // Creates the PluginServiceImpl object, but doesn't actually build the plugin
160 // list yet. It's generated lazily.
162 virtual ~PluginServiceImpl();
164 void OnWaitableEventSignaled(base::WaitableEvent
* waitable_event
);
166 // Returns the plugin process host corresponding to the plugin process that
167 // has been started by this service. Returns NULL if no process has been
169 PluginProcessHost
* FindNpapiPluginProcess(const base::FilePath
& plugin_path
);
170 PpapiPluginProcessHost
* FindPpapiPluginProcess(
171 const base::FilePath
& plugin_path
,
172 const base::FilePath
& profile_data_directory
);
173 PpapiPluginProcessHost
* FindPpapiBrokerProcess(
174 const base::FilePath
& broker_path
);
176 void RegisterPepperPlugins();
178 // Run on the blocking pool to load the plugins synchronously.
179 void GetPluginsInternal(base::MessageLoopProxy
* target_loop
,
180 const GetPluginsCallback
& callback
);
182 #if defined(OS_POSIX)
183 void GetPluginsOnIOThread(
184 base::MessageLoopProxy
* target_loop
,
185 const GetPluginsCallback
& callback
);
188 // Binding directly to GetAllowedPluginForOpenChannelToPlugin() isn't possible
189 // because more arity is needed <http://crbug.com/98542>. This just forwards.
190 void ForwardGetAllowedPluginForOpenChannelToPlugin(
191 const PluginServiceFilterParams
& params
,
193 const std::string
& mime_type
,
194 PluginProcessHost::Client
* client
,
195 const std::vector
<WebPluginInfo
>&);
196 // Helper so we can do the plugin lookup on the FILE thread.
197 void GetAllowedPluginForOpenChannelToPlugin(
198 int render_process_id
,
201 const GURL
& page_url
,
202 const std::string
& mime_type
,
203 PluginProcessHost::Client
* client
,
204 ResourceContext
* resource_context
);
206 // Helper so we can finish opening the channel after looking up the
208 void FinishOpenChannelToPlugin(int render_process_id
,
209 const base::FilePath
& plugin_path
,
210 PluginProcessHost::Client
* client
);
212 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
213 // Registers a new FilePathWatcher for a given path.
214 static void RegisterFilePathWatcher(base::FilePathWatcher
* watcher
,
215 const base::FilePath
& path
);
219 // Registry keys for getting notifications when new plugins are installed.
220 base::win::RegKey hkcu_key_
;
221 base::win::RegKey hklm_key_
;
222 scoped_ptr
<base::WaitableEvent
> hkcu_event_
;
223 scoped_ptr
<base::WaitableEvent
> hklm_event_
;
224 base::WaitableEventWatcher hkcu_watcher_
;
225 base::WaitableEventWatcher hklm_watcher_
;
228 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
229 ScopedVector
<base::FilePathWatcher
> file_watchers_
;
232 std::vector
<PepperPluginInfo
> ppapi_plugins_
;
234 // Weak pointer; outlives us.
235 PluginServiceFilter
* filter_
;
237 std::set
<PluginProcessHost::Client
*> pending_plugin_clients_
;
239 // Used to sequentialize loading plug-ins from disk.
240 base::SequencedWorkerPool::SequenceToken plugin_list_token_
;
242 #if defined(OS_POSIX)
243 scoped_refptr
<PluginLoaderPosix
> plugin_loader_
;
246 // Used to detect if a given plug-in is crashing over and over.
247 std::map
<base::FilePath
, std::vector
<base::Time
> > crash_times_
;
249 DISALLOW_COPY_AND_ASSIGN(PluginServiceImpl
);
252 } // namespace content
254 #endif // CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_