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
) {
69 // Returns the PluginServiceImpl singleton.
70 static PluginServiceImpl
* GetInstance();
72 // PluginService implementation:
73 virtual void Init() OVERRIDE
;
74 virtual void StartWatchingPlugins() OVERRIDE
;
75 virtual bool GetPluginInfoArray(
77 const std::string
& mime_type
,
79 std::vector
<webkit::WebPluginInfo
>* info
,
80 std::vector
<std::string
>* actual_mime_types
) OVERRIDE
;
81 virtual bool GetPluginInfo(int render_process_id
,
83 ResourceContext
* context
,
86 const std::string
& mime_type
,
89 webkit::WebPluginInfo
* info
,
90 std::string
* actual_mime_type
) OVERRIDE
;
91 virtual bool GetPluginInfoByPath(const base::FilePath
& plugin_path
,
92 webkit::WebPluginInfo
* info
) OVERRIDE
;
93 virtual string16
GetPluginDisplayNameByPath(
94 const base::FilePath
& path
) OVERRIDE
;
95 virtual void GetPlugins(const GetPluginsCallback
& callback
) OVERRIDE
;
96 virtual PepperPluginInfo
* GetRegisteredPpapiPluginInfo(
97 const base::FilePath
& plugin_path
) OVERRIDE
;
98 virtual void SetFilter(PluginServiceFilter
* filter
) OVERRIDE
;
99 virtual PluginServiceFilter
* GetFilter() OVERRIDE
;
100 virtual void ForcePluginShutdown(const base::FilePath
& plugin_path
) OVERRIDE
;
101 virtual bool IsPluginUnstable(const base::FilePath
& plugin_path
) OVERRIDE
;
102 virtual void RefreshPlugins() OVERRIDE
;
103 virtual void AddExtraPluginPath(const base::FilePath
& path
) OVERRIDE
;
104 virtual void AddExtraPluginDir(const base::FilePath
& path
) OVERRIDE
;
105 virtual void RemoveExtraPluginPath(const base::FilePath
& path
) OVERRIDE
;
106 virtual void UnregisterInternalPlugin(const base::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 int render_process_id
, const base::FilePath
& plugin_path
);
124 PpapiPluginProcessHost
* FindOrStartPpapiPluginProcess(
125 int render_process_id
,
126 const base::FilePath
& plugin_path
,
127 const base::FilePath
& profile_data_directory
,
128 PpapiPluginProcessHost::PluginClient
* client
);
129 PpapiPluginProcessHost
* FindOrStartPpapiBrokerProcess(
130 int render_process_id
, const base::FilePath
& plugin_path
);
132 // Opens a channel to a plugin process for the given mime type, starting
133 // a new plugin process if necessary. This must be called on the IO thread
134 // or else a deadlock can occur.
135 void OpenChannelToNpapiPlugin(int render_process_id
,
138 const GURL
& page_url
,
139 const std::string
& mime_type
,
140 PluginProcessHost::Client
* client
);
141 void OpenChannelToPpapiPlugin(int render_process_id
,
142 const base::FilePath
& plugin_path
,
143 const base::FilePath
& profile_data_directory
,
144 PpapiPluginProcessHost::PluginClient
* client
);
145 void OpenChannelToPpapiBroker(int render_process_id
,
146 const base::FilePath
& path
,
147 PpapiPluginProcessHost::BrokerClient
* client
);
149 // Cancels opening a channel to a NPAPI plugin.
150 void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client
* client
);
152 // Used to monitor plug-in stability.
153 void RegisterPluginCrash(const base::FilePath
& plugin_path
);
156 friend struct DefaultSingletonTraits
<PluginServiceImpl
>;
158 // Creates the PluginServiceImpl object, but doesn't actually build the plugin
159 // list yet. It's generated lazily.
161 virtual ~PluginServiceImpl();
163 void OnWaitableEventSignaled(base::WaitableEvent
* waitable_event
);
165 // Returns the plugin process host corresponding to the plugin process that
166 // has been started by this service. Returns NULL if no process has been
168 PluginProcessHost
* FindNpapiPluginProcess(const base::FilePath
& plugin_path
);
169 PpapiPluginProcessHost
* FindPpapiPluginProcess(
170 const base::FilePath
& plugin_path
,
171 const base::FilePath
& profile_data_directory
);
172 PpapiPluginProcessHost
* FindPpapiBrokerProcess(
173 const base::FilePath
& broker_path
);
175 void RegisterPepperPlugins();
177 // Run on the blocking pool to load the plugins synchronously.
178 void GetPluginsInternal(base::MessageLoopProxy
* target_loop
,
179 const GetPluginsCallback
& callback
);
181 // Binding directly to GetAllowedPluginForOpenChannelToPlugin() isn't possible
182 // because more arity is needed <http://crbug.com/98542>. This just forwards.
183 void ForwardGetAllowedPluginForOpenChannelToPlugin(
184 const PluginServiceFilterParams
& params
,
186 const std::string
& mime_type
,
187 PluginProcessHost::Client
* client
,
188 const std::vector
<webkit::WebPluginInfo
>&);
189 // Helper so we can do the plugin lookup on the FILE thread.
190 void GetAllowedPluginForOpenChannelToPlugin(
191 int render_process_id
,
194 const GURL
& page_url
,
195 const std::string
& mime_type
,
196 PluginProcessHost::Client
* client
,
197 ResourceContext
* resource_context
);
199 // Helper so we can finish opening the channel after looking up the
201 void FinishOpenChannelToPlugin(int render_process_id
,
202 const base::FilePath
& plugin_path
,
203 PluginProcessHost::Client
* client
);
205 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
206 // Registers a new FilePathWatcher for a given path.
207 static void RegisterFilePathWatcher(base::FilePathWatcher
* watcher
,
208 const base::FilePath
& path
);
211 // The plugin list instance.
212 webkit::npapi::PluginList
* plugin_list_
;
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_
;
224 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
225 ScopedVector
<base::FilePathWatcher
> file_watchers_
;
228 std::vector
<PepperPluginInfo
> ppapi_plugins_
;
230 // Weak pointer; outlives us.
231 PluginServiceFilter
* filter_
;
233 std::set
<PluginProcessHost::Client
*> pending_plugin_clients_
;
235 // Used to sequentialize loading plug-ins from disk.
236 base::SequencedWorkerPool::SequenceToken plugin_list_token_
;
238 #if defined(OS_POSIX)
239 scoped_refptr
<PluginLoaderPosix
> plugin_loader_
;
242 // Used to detect if a given plug-in is crashing over and over.
243 std::map
<base::FilePath
, std::vector
<base::Time
> > crash_times_
;
245 DISALLOW_COPY_AND_ASSIGN(PluginServiceImpl
);
248 } // namespace content
250 #endif // CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_