[Android] A prototype of the interactive media notification.
[chromium-blink-merge.git] / content / browser / plugin_service_impl.h
blob19fefba66415ddb996a0b5a299deea107c6a104d
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"
13 #endif
15 #include <map>
16 #include <set>
17 #include <vector>
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"
33 #include "url/gurl.h"
35 #if defined(OS_WIN)
36 #include "base/memory/scoped_ptr.h"
37 #include "base/win/registry.h"
38 #endif
40 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
41 #include "base/files/file_path_watcher.h"
42 #endif
44 namespace base {
45 class SingleThreadTaskRunner;
48 namespace content {
49 class BrowserContext;
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;
60 int render_frame_id;
61 GURL page_url;
62 ResourceContext* resource_context;
65 class CONTENT_EXPORT PluginServiceImpl
66 : NON_EXPORTED_BASE(public PluginService) {
67 public:
68 // Returns the PluginServiceImpl singleton.
69 static PluginServiceImpl* GetInstance();
71 // PluginService implementation:
72 void Init() override;
73 void StartWatchingPlugins() override;
74 bool GetPluginInfoArray(const GURL& url,
75 const std::string& mime_type,
76 bool allow_wildcard,
77 std::vector<WebPluginInfo>* info,
78 std::vector<std::string>* actual_mime_types) override;
79 bool GetPluginInfo(int render_process_id,
80 int render_frame_id,
81 ResourceContext* context,
82 const GURL& url,
83 const GURL& page_url,
84 const std::string& mime_type,
85 bool allow_wildcard,
86 bool* is_stale,
87 WebPluginInfo* info,
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 DisablePluginsDiscoveryForTesting() override;
110 #if defined(OS_MACOSX)
111 void AppActivated() override;
112 #elif defined(OS_WIN)
113 bool GetPluginInfoFromWindow(HWND window,
114 base::string16* plugin_name,
115 base::string16* plugin_version) override;
117 // Returns true iff the given HWND is a plugin.
118 bool IsPluginWindow(HWND window);
119 #endif
120 bool PpapiDevChannelSupported(BrowserContext* browser_context,
121 const GURL& document_url) override;
123 // Returns the plugin process host corresponding to the plugin process that
124 // has been started by this service. This will start a process to host the
125 // 'plugin_path' if needed. If the process fails to start, the return value
126 // is NULL. Must be called on the IO thread.
127 PluginProcessHost* FindOrStartNpapiPluginProcess(
128 int render_process_id, const base::FilePath& plugin_path);
129 PpapiPluginProcessHost* FindOrStartPpapiPluginProcess(
130 int render_process_id,
131 const base::FilePath& plugin_path,
132 const base::FilePath& profile_data_directory);
133 PpapiPluginProcessHost* FindOrStartPpapiBrokerProcess(
134 int render_process_id, const base::FilePath& plugin_path);
136 // Opens a channel to a plugin process for the given mime type, starting
137 // a new plugin process if necessary. This must be called on the IO thread
138 // or else a deadlock can occur.
139 void OpenChannelToNpapiPlugin(int render_process_id,
140 int render_frame_id,
141 const GURL& url,
142 const GURL& page_url,
143 const std::string& mime_type,
144 PluginProcessHost::Client* client);
145 void OpenChannelToPpapiPlugin(int render_process_id,
146 const base::FilePath& plugin_path,
147 const base::FilePath& profile_data_directory,
148 PpapiPluginProcessHost::PluginClient* client);
149 void OpenChannelToPpapiBroker(int render_process_id,
150 const base::FilePath& path,
151 PpapiPluginProcessHost::BrokerClient* client);
153 // Cancels opening a channel to a NPAPI plugin.
154 void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client* client);
156 // Used to monitor plugin stability.
157 void RegisterPluginCrash(const base::FilePath& plugin_path);
159 private:
160 friend struct DefaultSingletonTraits<PluginServiceImpl>;
162 // Creates the PluginServiceImpl object, but doesn't actually build the plugin
163 // list yet. It's generated lazily.
164 PluginServiceImpl();
165 ~PluginServiceImpl() override;
167 #if defined(OS_WIN)
168 void OnKeyChanged(base::win::RegKey* key);
169 #endif
171 // Returns the plugin process host corresponding to the plugin process that
172 // has been started by this service. Returns NULL if no process has been
173 // started.
174 PluginProcessHost* FindNpapiPluginProcess(const base::FilePath& plugin_path);
175 PpapiPluginProcessHost* FindPpapiPluginProcess(
176 const base::FilePath& plugin_path,
177 const base::FilePath& profile_data_directory);
178 PpapiPluginProcessHost* FindPpapiBrokerProcess(
179 const base::FilePath& broker_path);
181 void RegisterPepperPlugins();
183 // Run on the blocking pool to load the plugins synchronously.
184 void GetPluginsInternal(base::SingleThreadTaskRunner* target_task_runner,
185 const GetPluginsCallback& callback);
187 #if defined(OS_POSIX)
188 void GetPluginsOnIOThread(base::SingleThreadTaskRunner* target_task_runner,
189 const GetPluginsCallback& callback);
190 #endif
192 // Binding directly to GetAllowedPluginForOpenChannelToPlugin() isn't possible
193 // because more arity is needed <http://crbug.com/98542>. This just forwards.
194 void ForwardGetAllowedPluginForOpenChannelToPlugin(
195 const PluginServiceFilterParams& params,
196 const GURL& url,
197 const std::string& mime_type,
198 PluginProcessHost::Client* client,
199 const std::vector<WebPluginInfo>&);
200 // Helper so we can do the plugin lookup on the FILE thread.
201 void GetAllowedPluginForOpenChannelToPlugin(
202 int render_process_id,
203 int render_frame_id,
204 const GURL& url,
205 const GURL& page_url,
206 const std::string& mime_type,
207 PluginProcessHost::Client* client,
208 ResourceContext* resource_context);
210 // Helper so we can finish opening the channel after looking up the
211 // plugin.
212 void FinishOpenChannelToPlugin(int render_process_id,
213 const base::FilePath& plugin_path,
214 PluginProcessHost::Client* client);
216 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
217 // Registers a new FilePathWatcher for a given path.
218 static void RegisterFilePathWatcher(base::FilePathWatcher* watcher,
219 const base::FilePath& path);
220 #endif
222 #if defined(OS_WIN)
223 // Registry keys for getting notifications when new plugins are installed.
224 base::win::RegKey hkcu_key_;
225 base::win::RegKey hklm_key_;
226 #endif
228 bool npapi_plugins_enabled_;
230 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
231 ScopedVector<base::FilePathWatcher> file_watchers_;
232 #endif
234 std::vector<PepperPluginInfo> ppapi_plugins_;
236 // Weak pointer; outlives us.
237 PluginServiceFilter* filter_;
239 std::set<PluginProcessHost::Client*> pending_plugin_clients_;
241 // Used to sequentialize loading plugins from disk.
242 base::SequencedWorkerPool::SequenceToken plugin_list_token_;
244 #if defined(OS_POSIX)
245 scoped_refptr<PluginLoaderPosix> plugin_loader_;
246 #endif
248 // Used to detect if a given plugin is crashing over and over.
249 std::map<base::FilePath, std::vector<base::Time> > crash_times_;
251 DISALLOW_COPY_AND_ASSIGN(PluginServiceImpl);
254 } // namespace content
256 #endif // CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_