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"
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"
36 #include "base/memory/scoped_ptr.h"
37 #include "base/win/registry.h"
40 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
41 #include "base/files/file_path_watcher.h"
45 class MessageLoopProxy
;
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
;
62 ResourceContext
* resource_context
;
65 class CONTENT_EXPORT PluginServiceImpl
66 : NON_EXPORTED_BASE(public PluginService
) {
68 // Returns the PluginServiceImpl singleton.
69 static PluginServiceImpl
* GetInstance();
71 // PluginService implementation:
73 void StartWatchingPlugins() override
;
74 bool GetPluginInfoArray(const GURL
& url
,
75 const std::string
& mime_type
,
77 std::vector
<WebPluginInfo
>* info
,
78 std::vector
<std::string
>* actual_mime_types
) override
;
79 bool GetPluginInfo(int render_process_id
,
81 ResourceContext
* context
,
84 const std::string
& mime_type
,
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
);
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
,
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
);
161 friend struct DefaultSingletonTraits
<PluginServiceImpl
>;
163 // Creates the PluginServiceImpl object, but doesn't actually build the plugin
164 // list yet. It's generated lazily.
166 ~PluginServiceImpl() override
;
169 void OnKeyChanged(base::win::RegKey
* key
);
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
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
);
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
,
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
,
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
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
);
225 // Registry keys for getting notifications when new plugins are installed.
226 base::win::RegKey hkcu_key_
;
227 base::win::RegKey hklm_key_
;
230 bool npapi_plugins_enabled_
;
232 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
233 ScopedVector
<base::FilePathWatcher
> file_watchers_
;
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_
;
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_