[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 1 | // 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] | 6b14feb | 2011-08-16 11:22:56 | [diff] [blame] | 13 | #include <vector> |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 14 | |
| 15 | #include "base/basictypes.h" |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 16 | #include "base/callback.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/scoped_vector.h" |
| 18 | #include "base/memory/singleton.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 19 | #include "base/synchronization/waitable_event_watcher.h" |
| 20 | #include "build/build_config.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 21 | #include "content/browser/plugin_process_host.h" |
| 22 | #include "content/browser/ppapi_plugin_process_host.h" |
[email protected] | 8d128d6 | 2011-09-13 22:11:57 | [diff] [blame] | 23 | #include "content/common/content_export.h" |
[email protected] | 7f070d4 | 2011-03-09 20:25:32 | [diff] [blame] | 24 | #include "content/common/notification_observer.h" |
| 25 | #include "content/common/notification_registrar.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 26 | #include "googleurl/src/gurl.h" |
| 27 | #include "ipc/ipc_channel_handle.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 28 | |
| 29 | #if defined(OS_WIN) |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 30 | #include "base/memory/scoped_ptr.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 31 | #include "base/win/registry.h" |
| 32 | #endif |
| 33 | |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 34 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 35 | #include "base/files/file_path_watcher.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 36 | #endif |
| 37 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 38 | struct PepperPluginInfo; |
| 39 | class PluginDirWatcherDelegate; |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 40 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 41 | namespace base { |
| 42 | class MessageLoopProxy; |
| 43 | } |
| 44 | |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 45 | namespace content { |
[email protected] | 45a22e6 | 2011-10-12 09:48:02 | [diff] [blame^] | 46 | class BrowserContext; |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 47 | class ResourceContext; |
| 48 | class PluginServiceFilter; |
| 49 | } |
| 50 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 51 | namespace webkit { |
| 52 | namespace npapi { |
| 53 | class PluginGroup; |
| 54 | class PluginList; |
| 55 | } |
| 56 | } |
| 57 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 58 | // This must be created on the main thread but it's only called on the IO/file |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 59 | // 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] | 8d128d6 | 2011-09-13 22:11:57 | [diff] [blame] | 62 | class CONTENT_EXPORT PluginService |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 63 | : public base::WaitableEventWatcher::Delegate, |
| 64 | public NotificationObserver { |
| 65 | public: |
| 66 | struct OverriddenPlugin { |
| 67 | int render_process_id; |
| 68 | int render_view_id; |
[email protected] | 6b14feb | 2011-08-16 11:22:56 | [diff] [blame] | 69 | GURL url; // If empty, the override applies to all urls in render_view. |
[email protected] | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame] | 70 | webkit::WebPluginInfo plugin; |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 71 | }; |
| 72 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 73 | 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] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 78 | // Returns the PluginService singleton. |
| 79 | static PluginService* GetInstance(); |
| 80 | |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 81 | // Starts watching for changes in the list of installed plug-ins. |
| 82 | void StartWatchingPlugins(); |
| 83 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 84 | // 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] | a50432d | 2011-09-30 16:32:14 | [diff] [blame] | 92 | PpapiPluginProcessHost* FindPpapiBrokerProcess(const FilePath& broker_path); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 93 | |
| 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] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 101 | const FilePath& plugin_path, |
[email protected] | a50432d | 2011-09-30 16:32:14 | [diff] [blame] | 102 | PpapiPluginProcessHost::PluginClient* client); |
| 103 | PpapiPluginProcessHost* FindOrStartPpapiBrokerProcess( |
[email protected] | eb415bf0e | 2011-04-14 02:45:42 | [diff] [blame] | 104 | const FilePath& plugin_path); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 105 | |
| 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] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 112 | const GURL& page_url, |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 113 | const std::string& mime_type, |
| 114 | PluginProcessHost::Client* client); |
| 115 | void OpenChannelToPpapiPlugin(const FilePath& path, |
[email protected] | a50432d | 2011-09-30 16:32:14 | [diff] [blame] | 116 | PpapiPluginProcessHost::PluginClient* client); |
[email protected] | eb415bf0e | 2011-04-14 02:45:42 | [diff] [blame] | 117 | void OpenChannelToPpapiBroker(const FilePath& path, |
[email protected] | a50432d | 2011-09-30 16:32:14 | [diff] [blame] | 118 | PpapiPluginProcessHost::BrokerClient* client); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 119 | |
[email protected] | 4befe759 | 2011-09-14 22:49:09 | [diff] [blame] | 120 | // Cancels opening a channel to a NPAPI plugin. |
| 121 | void CancelOpenChannelToNpapiPlugin(PluginProcessHost::Client* client); |
| 122 | |
[email protected] | 11e1c18 | 2011-05-17 20:26:27 | [diff] [blame] | 123 | // Gets the plugin in the list of plugins that matches the given url and mime |
[email protected] | 51b63f6 | 2011-10-05 18:55:42 | [diff] [blame] | 124 | // 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] | 11e1c18 | 2011-05-17 20:26:27 | [diff] [blame] | 135 | bool GetPluginInfo(int render_process_id, |
| 136 | int render_view_id, |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 137 | const content::ResourceContext& context, |
[email protected] | 11e1c18 | 2011-05-17 20:26:27 | [diff] [blame] | 138 | const GURL& url, |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 139 | const GURL& page_url, |
[email protected] | 11e1c18 | 2011-05-17 20:26:27 | [diff] [blame] | 140 | const std::string& mime_type, |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 141 | bool allow_wildcard, |
| 142 | bool* use_stale, |
[email protected] | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame] | 143 | webkit::WebPluginInfo* info, |
[email protected] | 11e1c18 | 2011-05-17 20:26:27 | [diff] [blame] | 144 | std::string* actual_mime_type); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 145 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 146 | // 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] | 2de30759 | 2011-04-05 21:16:58 | [diff] [blame] | 157 | |
[email protected] | 45a22e6 | 2011-10-12 09:48:02 | [diff] [blame^] | 158 | // 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] | 2de30759 | 2011-04-05 21:16:58 | [diff] [blame] | 162 | // NOTE: can only be called on the UI thread. |
[email protected] | 45a22e6 | 2011-10-12 09:48:02 | [diff] [blame^] | 163 | static void PurgePluginListCache(content::BrowserContext* browser_context, |
| 164 | bool reload_pages); |
[email protected] | 2de30759 | 2011-04-05 21:16:58 | [diff] [blame] | 165 | |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 166 | void set_filter(content::PluginServiceFilter* filter) { |
| 167 | filter_ = filter; |
| 168 | } |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 169 | content::PluginServiceFilter* filter() { return filter_; } |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 170 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 171 | 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] | 3690ebe0 | 2011-05-25 09:08:19 | [diff] [blame] | 177 | virtual ~PluginService(); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 178 | |
| 179 | // base::WaitableEventWatcher::Delegate implementation. |
| 180 | virtual void OnWaitableEventSignaled(base::WaitableEvent* waitable_event); |
| 181 | |
| 182 | // NotificationObserver implementation |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 183 | virtual void Observe(int type, const NotificationSource& source, |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 184 | const NotificationDetails& details); |
| 185 | |
| 186 | void RegisterPepperPlugins(); |
| 187 | |
[email protected] | eb415bf0e | 2011-04-14 02:45:42 | [diff] [blame] | 188 | PepperPluginInfo* GetRegisteredPpapiPluginInfo(const FilePath& plugin_path); |
| 189 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 190 | // 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] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 194 | // 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] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 199 | const GURL& page_url, |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 200 | const std::string& mime_type, |
[email protected] | 87c4be4 | 2011-09-16 01:10:59 | [diff] [blame] | 201 | PluginProcessHost::Client* client, |
| 202 | const content::ResourceContext* resource_context); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 203 | |
| 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] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 210 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 211 | // Registers a new FilePathWatcher for a given path. |
| 212 | static void RegisterFilePathWatcher( |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 213 | base::files::FilePathWatcher* watcher, |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 214 | const FilePath& path, |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 215 | base::files::FilePathWatcher::Delegate* delegate); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 216 | #endif |
| 217 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 218 | // The browser's UI locale. |
| 219 | const std::string ui_locale_; |
| 220 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 221 | NotificationRegistrar registrar_; |
| 222 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 223 | #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] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 233 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 234 | ScopedVector<base::files::FilePathWatcher> file_watchers_; |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 235 | scoped_refptr<PluginDirWatcherDelegate> file_watcher_delegate_; |
| 236 | #endif |
| 237 | |
| 238 | std::vector<PepperPluginInfo> ppapi_plugins_; |
| 239 | |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 240 | // Weak pointer; outlives us. |
| 241 | content::PluginServiceFilter* filter_; |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 242 | |
[email protected] | 4befe759 | 2011-09-14 22:49:09 | [diff] [blame] | 243 | std::set<PluginProcessHost::Client*> pending_plugin_clients_; |
| 244 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 245 | DISALLOW_COPY_AND_ASSIGN(PluginService); |
| 246 | }; |
| 247 | |
| 248 | DISABLE_RUNNABLE_METHOD_REFCOUNT(PluginService); |
| 249 | |
| 250 | #endif // CONTENT_BROWSER_PLUGIN_SERVICE_H_ |