blob: 1691c0aa9334a1bbeb81de6965b60e21eda86b5f [file] [log] [blame]
[email protected]a0421732011-02-23 03:55:401// Copyright (c) 2011 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_H_
9#define CONTENT_BROWSER_PLUGIN_SERVICE_H_
10#pragma once
11
12#include <string>
[email protected]6b14feb2011-08-16 11:22:5613#include <vector>
[email protected]a0421732011-02-23 03:55:4014
15#include "base/basictypes.h"
[email protected]d33e7cc2011-09-23 01:43:5616#include "base/callback.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/scoped_vector.h"
18#include "base/memory/singleton.h"
[email protected]a0421732011-02-23 03:55:4019#include "base/synchronization/waitable_event_watcher.h"
20#include "build/build_config.h"
[email protected]a0421732011-02-23 03:55:4021#include "content/browser/plugin_process_host.h"
22#include "content/browser/ppapi_plugin_process_host.h"
[email protected]8d128d62011-09-13 22:11:5723#include "content/common/content_export.h"
[email protected]7f070d42011-03-09 20:25:3224#include "content/common/notification_observer.h"
25#include "content/common/notification_registrar.h"
[email protected]a0421732011-02-23 03:55:4026#include "googleurl/src/gurl.h"
27#include "ipc/ipc_channel_handle.h"
[email protected]a0421732011-02-23 03:55:4028
29#if defined(OS_WIN)
[email protected]3b63f8f42011-03-28 01:54:1530#include "base/memory/scoped_ptr.h"
[email protected]a0421732011-02-23 03:55:4031#include "base/win/registry.h"
32#endif
33
[email protected]e63c4d72011-05-31 22:38:2934#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]493c8002011-04-14 16:56:0135#include "base/files/file_path_watcher.h"
[email protected]a0421732011-02-23 03:55:4036#endif
37
[email protected]a0421732011-02-23 03:55:4038struct PepperPluginInfo;
39class PluginDirWatcherDelegate;
[email protected]a0421732011-02-23 03:55:4040
[email protected]d33e7cc2011-09-23 01:43:5641namespace base {
42class MessageLoopProxy;
43}
44
[email protected]dfba8762011-09-02 12:49:5445namespace content {
[email protected]45a22e62011-10-12 09:48:0246class BrowserContext;
[email protected]dfba8762011-09-02 12:49:5447class ResourceContext;
48class PluginServiceFilter;
49}
50
[email protected]d33e7cc2011-09-23 01:43:5651namespace webkit {
52namespace npapi {
53class PluginGroup;
54class PluginList;
55}
56}
57
[email protected]a0421732011-02-23 03:55:4058// This must be created on the main thread but it's only called on the IO/file
[email protected]d33e7cc2011-09-23 01:43:5659// thread. This is an asynchronous wrapper around the PluginList interface for
60// querying plugin information. This must be used instead of that to avoid
61// doing expensive disk operations on the IO/UI threads.
[email protected]8d128d62011-09-13 22:11:5762class CONTENT_EXPORT PluginService
[email protected]a0421732011-02-23 03:55:4063 : public base::WaitableEventWatcher::Delegate,
64 public NotificationObserver {
65 public:
66 struct OverriddenPlugin {
67 int render_process_id;
68 int render_view_id;
[email protected]6b14feb2011-08-16 11:22:5669 GURL url; // If empty, the override applies to all urls in render_view.
[email protected]91d9f3d2011-08-14 05:24:4470 webkit::WebPluginInfo plugin;
[email protected]a0421732011-02-23 03:55:4071 };
72
[email protected]d33e7cc2011-09-23 01:43:5673 typedef base::Callback<void(const std::vector<webkit::WebPluginInfo>&)>
74 GetPluginsCallback;
75 typedef base::Callback<void(const std::vector<webkit::npapi::PluginGroup>&)>
76 GetPluginGroupsCallback;
77
[email protected]a0421732011-02-23 03:55:4078 // Returns the PluginService singleton.
79 static PluginService* GetInstance();
80
[email protected]dfba8762011-09-02 12:49:5481 // Starts watching for changes in the list of installed plug-ins.
82 void StartWatchingPlugins();
83
[email protected]a0421732011-02-23 03:55:4084 // Gets the browser's UI locale.
85 const std::string& GetUILocale();
86
87 // Returns the plugin process host corresponding to the plugin process that
88 // has been started by this service. Returns NULL if no process has been
89 // started.
90 PluginProcessHost* FindNpapiPluginProcess(const FilePath& plugin_path);
91 PpapiPluginProcessHost* FindPpapiPluginProcess(const FilePath& plugin_path);
[email protected]a50432d2011-09-30 16:32:1492 PpapiPluginProcessHost* FindPpapiBrokerProcess(const FilePath& broker_path);
[email protected]a0421732011-02-23 03:55:4093
94 // Returns the plugin process host corresponding to the plugin process that
95 // has been started by this service. This will start a process to host the
96 // 'plugin_path' if needed. If the process fails to start, the return value
97 // is NULL. Must be called on the IO thread.
98 PluginProcessHost* FindOrStartNpapiPluginProcess(
99 const FilePath& plugin_path);
100 PpapiPluginProcessHost* FindOrStartPpapiPluginProcess(
[email protected]d259a8e2011-05-18 22:31:09101 const FilePath& plugin_path,
[email protected]a50432d2011-09-30 16:32:14102 PpapiPluginProcessHost::PluginClient* client);
103 PpapiPluginProcessHost* FindOrStartPpapiBrokerProcess(
[email protected]eb415bf0e2011-04-14 02:45:42104 const FilePath& plugin_path);
[email protected]a0421732011-02-23 03:55:40105
106 // Opens a channel to a plugin process for the given mime type, starting
107 // a new plugin process if necessary. This must be called on the IO thread
108 // or else a deadlock can occur.
109 void OpenChannelToNpapiPlugin(int render_process_id,
110 int render_view_id,
111 const GURL& url,
[email protected]dfba8762011-09-02 12:49:54112 const GURL& page_url,
[email protected]a0421732011-02-23 03:55:40113 const std::string& mime_type,
114 PluginProcessHost::Client* client);
115 void OpenChannelToPpapiPlugin(const FilePath& path,
[email protected]a50432d2011-09-30 16:32:14116 PpapiPluginProcessHost::PluginClient* client);
[email protected]eb415bf0e2011-04-14 02:45:42117 void OpenChannelToPpapiBroker(const FilePath& path,
[email protected]a50432d2011-09-30 16:32:14118 PpapiPluginProcessHost::BrokerClient* client);
[email protected]a0421732011-02-23 03:55:40119
[email protected]4befe7592011-09-14 22:49:09120 // Cancels opening a channel to a NPAPI plugin.
121 void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client* client);
122
[email protected]11e1c182011-05-17 20:26:27123 // Gets the plugin in the list of plugins that matches the given url and mime
[email protected]51b63f62011-10-05 18:55:42124 // type. Returns true if the data is frome a stale plugin list, false if it
125 // is up to date. This can be called from any thread.
126 bool GetPluginInfoArray(const GURL& url,
127 const std::string& mime_type,
128 bool allow_wildcard,
129 std::vector<webkit::WebPluginInfo>* info,
130 std::vector<std::string>* actual_mime_types);
131
132 // Gets plugin info for an individual plugin and filters the plugins using
133 // the |context| and renderer IDs. This will report whether the data is stale
134 // via |is_stale| and returns whether or not the plugin can be found.
[email protected]11e1c182011-05-17 20:26:27135 bool GetPluginInfo(int render_process_id,
136 int render_view_id,
[email protected]dfba8762011-09-02 12:49:54137 const content::ResourceContext& context,
[email protected]11e1c182011-05-17 20:26:27138 const GURL& url,
[email protected]dfba8762011-09-02 12:49:54139 const GURL& page_url,
[email protected]11e1c182011-05-17 20:26:27140 const std::string& mime_type,
[email protected]dfba8762011-09-02 12:49:54141 bool allow_wildcard,
142 bool* use_stale,
[email protected]91d9f3d2011-08-14 05:24:44143 webkit::WebPluginInfo* info,
[email protected]11e1c182011-05-17 20:26:27144 std::string* actual_mime_type);
[email protected]a0421732011-02-23 03:55:40145
[email protected]d33e7cc2011-09-23 01:43:56146 // Marks the plugin list as dirty and will cause the plugins to be reloaded
147 // on the next access through GetPlugins() or GetPluginGroups().
148 void RefreshPluginList();
149
150 // Asynchronously loads plugins if necessary and then calls back to the
151 // provided function on the calling MessageLoop on completion.
152 void GetPlugins(const GetPluginsCallback& callback);
153
154 // Asynchronously loads the list of plugin groups if necessary and then calls
155 // back to the provided function on the calling MessageLoop on completion.
156 void GetPluginGroups(const GetPluginGroupsCallback& callback);
[email protected]2de307592011-04-05 21:16:58157
[email protected]45a22e62011-10-12 09:48:02158 // Tells all the renderer processes associated with the given browser context
159 // to throw away their cache of the plugin list, and optionally also reload
160 // all the pages with plugins. If |browser_context| is NULL, purges the cache
161 // in all renderers.
[email protected]2de307592011-04-05 21:16:58162 // NOTE: can only be called on the UI thread.
[email protected]45a22e62011-10-12 09:48:02163 static void PurgePluginListCache(content::BrowserContext* browser_context,
164 bool reload_pages);
[email protected]2de307592011-04-05 21:16:58165
[email protected]dfba8762011-09-02 12:49:54166 void set_filter(content::PluginServiceFilter* filter) {
167 filter_ = filter;
168 }
[email protected]d33e7cc2011-09-23 01:43:56169 content::PluginServiceFilter* filter() { return filter_; }
[email protected]dfba8762011-09-02 12:49:54170
[email protected]a0421732011-02-23 03:55:40171 private:
172 friend struct DefaultSingletonTraits<PluginService>;
173
174 // Creates the PluginService object, but doesn't actually build the plugin
175 // list yet. It's generated lazily.
176 PluginService();
[email protected]3690ebe02011-05-25 09:08:19177 virtual ~PluginService();
[email protected]a0421732011-02-23 03:55:40178
179 // base::WaitableEventWatcher::Delegate implementation.
180 virtual void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
181
182 // NotificationObserver implementation
[email protected]432115822011-07-10 15:52:27183 virtual void Observe(int type, const NotificationSource& source,
[email protected]a0421732011-02-23 03:55:40184 const NotificationDetails& details);
185
186 void RegisterPepperPlugins();
187
[email protected]eb415bf0e2011-04-14 02:45:42188 PepperPluginInfo* GetRegisteredPpapiPluginInfo(const FilePath& plugin_path);
189
[email protected]d33e7cc2011-09-23 01:43:56190 // Function that is run on the FILE thread to load the plugins synchronously.
191 void GetPluginsInternal(base::MessageLoopProxy* target_loop,
192 const GetPluginsCallback& callback);
193
[email protected]a0421732011-02-23 03:55:40194 // Helper so we can do the plugin lookup on the FILE thread.
195 void GetAllowedPluginForOpenChannelToPlugin(
196 int render_process_id,
197 int render_view_id,
198 const GURL& url,
[email protected]dfba8762011-09-02 12:49:54199 const GURL& page_url,
[email protected]a0421732011-02-23 03:55:40200 const std::string& mime_type,
[email protected]87c4be42011-09-16 01:10:59201 PluginProcessHost::Client* client,
202 const content::ResourceContext* resource_context);
[email protected]a0421732011-02-23 03:55:40203
204 // Helper so we can finish opening the channel after looking up the
205 // plugin.
206 void FinishOpenChannelToPlugin(
207 const FilePath& plugin_path,
208 PluginProcessHost::Client* client);
209
[email protected]e63c4d72011-05-31 22:38:29210#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]a0421732011-02-23 03:55:40211 // Registers a new FilePathWatcher for a given path.
212 static void RegisterFilePathWatcher(
[email protected]493c8002011-04-14 16:56:01213 base::files::FilePathWatcher* watcher,
[email protected]a0421732011-02-23 03:55:40214 const FilePath& path,
[email protected]493c8002011-04-14 16:56:01215 base::files::FilePathWatcher::Delegate* delegate);
[email protected]a0421732011-02-23 03:55:40216#endif
217
[email protected]a0421732011-02-23 03:55:40218 // The browser's UI locale.
219 const std::string ui_locale_;
220
[email protected]a0421732011-02-23 03:55:40221 NotificationRegistrar registrar_;
222
[email protected]a0421732011-02-23 03:55:40223#if defined(OS_WIN)
224 // Registry keys for getting notifications when new plugins are installed.
225 base::win::RegKey hkcu_key_;
226 base::win::RegKey hklm_key_;
227 scoped_ptr<base::WaitableEvent> hkcu_event_;
228 scoped_ptr<base::WaitableEvent> hklm_event_;
229 base::WaitableEventWatcher hkcu_watcher_;
230 base::WaitableEventWatcher hklm_watcher_;
231#endif
232
[email protected]e63c4d72011-05-31 22:38:29233#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]493c8002011-04-14 16:56:01234 ScopedVector<base::files::FilePathWatcher> file_watchers_;
[email protected]a0421732011-02-23 03:55:40235 scoped_refptr<PluginDirWatcherDelegate> file_watcher_delegate_;
236#endif
237
238 std::vector<PepperPluginInfo> ppapi_plugins_;
239
[email protected]dfba8762011-09-02 12:49:54240 // Weak pointer; outlives us.
241 content::PluginServiceFilter* filter_;
[email protected]a0421732011-02-23 03:55:40242
[email protected]4befe7592011-09-14 22:49:09243 std::set<PluginProcessHost::Client*> pending_plugin_clients_;
244
[email protected]a0421732011-02-23 03:55:40245 DISALLOW_COPY_AND_ASSIGN(PluginService);
246};
247
248DISABLE_RUNNABLE_METHOD_REFCOUNT(PluginService);
249
250#endif // CONTENT_BROWSER_PLUGIN_SERVICE_H_