- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / plugin_service_impl.h
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.
4
5 // This class responds to requests from renderers for the list of plugins, and
6 // also a proxy object for plugin instances.
7
8 #ifndef CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_
9 #define CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_
10
11 #include <map>
12 #include <set>
13 #include <vector>
14
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"
29 #include "url/gurl.h"
30
31 #if defined(OS_WIN)
32 #include "base/memory/scoped_ptr.h"
33 #include "base/win/registry.h"
34 #endif
35
36 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
37 #include "base/files/file_path_watcher.h"
38 #endif
39
40 namespace base {
41 class MessageLoopProxy;
42 }
43
44 namespace webkit {
45 namespace npapi {
46 class PluginList;
47 }
48 }
49
50 namespace content {
51 class BrowserContext;
52 class PluginDirWatcherDelegate;
53 class PluginLoaderPosix;
54 class PluginServiceFilter;
55 class ResourceContext;
56 struct PepperPluginInfo;
57
58 // base::Bind() has limited arity, and the filter-related methods tend to
59 // surpass that limit.
60 struct PluginServiceFilterParams {
61   int render_process_id;
62   int render_view_id;
63   GURL page_url;
64   ResourceContext* resource_context;
65 };
66
67 class CONTENT_EXPORT PluginServiceImpl
68     : NON_EXPORTED_BASE(public PluginService) {
69  public:
70   // Returns the PluginServiceImpl singleton.
71   static PluginServiceImpl* GetInstance();
72
73   // PluginService implementation:
74   virtual void Init() OVERRIDE;
75   virtual void StartWatchingPlugins() OVERRIDE;
76   virtual bool GetPluginInfoArray(
77       const GURL& url,
78       const std::string& mime_type,
79       bool allow_wildcard,
80       std::vector<WebPluginInfo>* info,
81       std::vector<std::string>* actual_mime_types) OVERRIDE;
82   virtual bool GetPluginInfo(int render_process_id,
83                              int render_view_id,
84                              ResourceContext* context,
85                              const GURL& url,
86                              const GURL& page_url,
87                              const std::string& mime_type,
88                              bool allow_wildcard,
89                              bool* is_stale,
90                              WebPluginInfo* info,
91                              std::string* actual_mime_type) OVERRIDE;
92   virtual bool GetPluginInfoByPath(const base::FilePath& plugin_path,
93                                    WebPluginInfo* info) OVERRIDE;
94   virtual string16 GetPluginDisplayNameByPath(
95       const base::FilePath& path) OVERRIDE;
96   virtual void GetPlugins(const GetPluginsCallback& callback) OVERRIDE;
97   virtual PepperPluginInfo* GetRegisteredPpapiPluginInfo(
98       const base::FilePath& plugin_path) OVERRIDE;
99   virtual void SetFilter(PluginServiceFilter* filter) OVERRIDE;
100   virtual PluginServiceFilter* GetFilter() OVERRIDE;
101   virtual void ForcePluginShutdown(const base::FilePath& plugin_path) OVERRIDE;
102   virtual bool IsPluginUnstable(const base::FilePath& plugin_path) OVERRIDE;
103   virtual void RefreshPlugins() OVERRIDE;
104   virtual void AddExtraPluginPath(const base::FilePath& path) OVERRIDE;
105   virtual void RemoveExtraPluginPath(const base::FilePath& path) OVERRIDE;
106   virtual void AddExtraPluginDir(const base::FilePath& path) OVERRIDE;
107   virtual void RegisterInternalPlugin(
108       const WebPluginInfo& info, bool add_at_beginning) OVERRIDE;
109   virtual void UnregisterInternalPlugin(const base::FilePath& path) OVERRIDE;
110   virtual void GetInternalPlugins(
111       std::vector<WebPluginInfo>* plugins) OVERRIDE;
112   virtual bool NPAPIPluginsSupported() OVERRIDE;
113   virtual void DisablePluginsDiscoveryForTesting() OVERRIDE;
114 #if defined(OS_MACOSX)
115   virtual void AppActivated() OVERRIDE;
116 #elif defined(OS_WIN)
117   virtual bool GetPluginInfoFromWindow(HWND window,
118                                        base::string16* plugin_name,
119                                        base::string16* plugin_version) OVERRIDE;
120
121   // Returns true iff the given HWND is a plugin.
122   bool IsPluginWindow(HWND window);
123 #endif
124
125   // Returns the plugin process host corresponding to the plugin process that
126   // has been started by this service. This will start a process to host the
127   // 'plugin_path' if needed. If the process fails to start, the return value
128   // is NULL. Must be called on the IO thread.
129   PluginProcessHost* FindOrStartNpapiPluginProcess(
130       int render_process_id, const base::FilePath& plugin_path);
131   PpapiPluginProcessHost* FindOrStartPpapiPluginProcess(
132       int render_process_id,
133       const base::FilePath& plugin_path,
134       const base::FilePath& profile_data_directory);
135   PpapiPluginProcessHost* FindOrStartPpapiBrokerProcess(
136       int render_process_id, const base::FilePath& plugin_path);
137
138   // Opens a channel to a plugin process for the given mime type, starting
139   // a new plugin process if necessary.  This must be called on the IO thread
140   // or else a deadlock can occur.
141   void OpenChannelToNpapiPlugin(int render_process_id,
142                                 int render_view_id,
143                                 const GURL& url,
144                                 const GURL& page_url,
145                                 const std::string& mime_type,
146                                 PluginProcessHost::Client* client);
147   void OpenChannelToPpapiPlugin(int render_process_id,
148                                 const base::FilePath& plugin_path,
149                                 const base::FilePath& profile_data_directory,
150                                 PpapiPluginProcessHost::PluginClient* client);
151   void OpenChannelToPpapiBroker(int render_process_id,
152                                 const base::FilePath& path,
153                                 PpapiPluginProcessHost::BrokerClient* client);
154
155   // Cancels opening a channel to a NPAPI plugin.
156   void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client* client);
157
158   // Used to monitor plug-in stability.
159   void RegisterPluginCrash(const base::FilePath& plugin_path);
160
161  private:
162   friend struct DefaultSingletonTraits<PluginServiceImpl>;
163
164   // Creates the PluginServiceImpl object, but doesn't actually build the plugin
165   // list yet.  It's generated lazily.
166   PluginServiceImpl();
167   virtual ~PluginServiceImpl();
168
169   void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
170
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);
180
181   void RegisterPepperPlugins();
182
183   // Run on the blocking pool to load the plugins synchronously.
184   void GetPluginsInternal(base::MessageLoopProxy* target_loop,
185                           const GetPluginsCallback& callback);
186
187   // Binding directly to GetAllowedPluginForOpenChannelToPlugin() isn't possible
188   // because more arity is needed <http://crbug.com/98542>. This just forwards.
189   void ForwardGetAllowedPluginForOpenChannelToPlugin(
190       const PluginServiceFilterParams& params,
191       const GURL& url,
192       const std::string& mime_type,
193       PluginProcessHost::Client* client,
194       const std::vector<WebPluginInfo>&);
195   // Helper so we can do the plugin lookup on the FILE thread.
196   void GetAllowedPluginForOpenChannelToPlugin(
197       int render_process_id,
198       int render_view_id,
199       const GURL& url,
200       const GURL& page_url,
201       const std::string& mime_type,
202       PluginProcessHost::Client* client,
203       ResourceContext* resource_context);
204
205   // Helper so we can finish opening the channel after looking up the
206   // plugin.
207   void FinishOpenChannelToPlugin(int render_process_id,
208                                  const base::FilePath& plugin_path,
209                                  PluginProcessHost::Client* client);
210
211 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
212   // Registers a new FilePathWatcher for a given path.
213   static void RegisterFilePathWatcher(base::FilePathWatcher* watcher,
214                                       const base::FilePath& path);
215 #endif
216
217 #if defined(OS_WIN)
218   // Registry keys for getting notifications when new plugins are installed.
219   base::win::RegKey hkcu_key_;
220   base::win::RegKey hklm_key_;
221   scoped_ptr<base::WaitableEvent> hkcu_event_;
222   scoped_ptr<base::WaitableEvent> hklm_event_;
223   base::WaitableEventWatcher hkcu_watcher_;
224   base::WaitableEventWatcher hklm_watcher_;
225 #endif
226
227 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
228   ScopedVector<base::FilePathWatcher> file_watchers_;
229 #endif
230
231   std::vector<PepperPluginInfo> ppapi_plugins_;
232
233   // Weak pointer; outlives us.
234   PluginServiceFilter* filter_;
235
236   std::set<PluginProcessHost::Client*> pending_plugin_clients_;
237
238   // Used to sequentialize loading plug-ins from disk.
239   base::SequencedWorkerPool::SequenceToken plugin_list_token_;
240
241 #if defined(OS_POSIX)
242   scoped_refptr<PluginLoaderPosix> plugin_loader_;
243 #endif
244
245   // Used to detect if a given plug-in is crashing over and over.
246   std::map<base::FilePath, std::vector<base::Time> > crash_times_;
247
248   DISALLOW_COPY_AND_ASSIGN(PluginServiceImpl);
249 };
250
251 }  // namespace content
252
253 #endif  // CONTENT_BROWSER_PLUGIN_SERVICE_IMPL_H_