[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> |
| 13 | |
| 14 | #include "base/basictypes.h" |
| 15 | #include "base/file_path.h" |
| 16 | #include "base/hash_tables.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/lock.h" |
| 20 | #include "base/synchronization/waitable_event_watcher.h" |
| 21 | #include "build/build_config.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 22 | #include "content/browser/plugin_process_host.h" |
| 23 | #include "content/browser/ppapi_plugin_process_host.h" |
[email protected] | eb415bf0e | 2011-04-14 02:45:42 | [diff] [blame] | 24 | #include "content/browser/ppapi_broker_process_host.h" |
[email protected] | 7f070d4 | 2011-03-09 20:25:32 | [diff] [blame] | 25 | #include "content/common/notification_observer.h" |
| 26 | #include "content/common/notification_registrar.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 27 | #include "googleurl/src/gurl.h" |
| 28 | #include "ipc/ipc_channel_handle.h" |
[email protected] | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame^] | 29 | #include "webkit/plugins/webplugininfo.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 30 | |
| 31 | #if defined(OS_WIN) |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 32 | #include "base/memory/scoped_ptr.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 33 | #include "base/win/registry.h" |
| 34 | #endif |
| 35 | |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 36 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 37 | #include "base/files/file_path_watcher.h" |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 38 | #endif |
| 39 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 40 | struct PepperPluginInfo; |
| 41 | class PluginDirWatcherDelegate; |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 42 | |
| 43 | // This must be created on the main thread but it's only called on the IO/file |
| 44 | // thread. |
| 45 | class PluginService |
| 46 | : public base::WaitableEventWatcher::Delegate, |
| 47 | public NotificationObserver { |
| 48 | public: |
| 49 | struct OverriddenPlugin { |
| 50 | int render_process_id; |
| 51 | int render_view_id; |
| 52 | GURL url; |
[email protected] | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame^] | 53 | webkit::WebPluginInfo plugin; |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 54 | }; |
| 55 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 56 | // Returns the PluginService singleton. |
| 57 | static PluginService* GetInstance(); |
| 58 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 59 | // Gets the browser's UI locale. |
| 60 | const std::string& GetUILocale(); |
| 61 | |
| 62 | // Returns the plugin process host corresponding to the plugin process that |
| 63 | // has been started by this service. Returns NULL if no process has been |
| 64 | // started. |
| 65 | PluginProcessHost* FindNpapiPluginProcess(const FilePath& plugin_path); |
| 66 | PpapiPluginProcessHost* FindPpapiPluginProcess(const FilePath& plugin_path); |
[email protected] | eb415bf0e | 2011-04-14 02:45:42 | [diff] [blame] | 67 | PpapiBrokerProcessHost* FindPpapiBrokerProcess(const FilePath& broker_path); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 68 | |
| 69 | // Returns the plugin process host corresponding to the plugin process that |
| 70 | // has been started by this service. This will start a process to host the |
| 71 | // 'plugin_path' if needed. If the process fails to start, the return value |
| 72 | // is NULL. Must be called on the IO thread. |
| 73 | PluginProcessHost* FindOrStartNpapiPluginProcess( |
| 74 | const FilePath& plugin_path); |
| 75 | PpapiPluginProcessHost* FindOrStartPpapiPluginProcess( |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 76 | const FilePath& plugin_path, |
| 77 | PpapiPluginProcessHost::Client* client); |
[email protected] | eb415bf0e | 2011-04-14 02:45:42 | [diff] [blame] | 78 | PpapiBrokerProcessHost* FindOrStartPpapiBrokerProcess( |
| 79 | const FilePath& plugin_path); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 80 | |
| 81 | // Opens a channel to a plugin process for the given mime type, starting |
| 82 | // a new plugin process if necessary. This must be called on the IO thread |
| 83 | // or else a deadlock can occur. |
| 84 | void OpenChannelToNpapiPlugin(int render_process_id, |
| 85 | int render_view_id, |
| 86 | const GURL& url, |
| 87 | const std::string& mime_type, |
| 88 | PluginProcessHost::Client* client); |
| 89 | void OpenChannelToPpapiPlugin(const FilePath& path, |
| 90 | PpapiPluginProcessHost::Client* client); |
[email protected] | eb415bf0e | 2011-04-14 02:45:42 | [diff] [blame] | 91 | void OpenChannelToPpapiBroker(const FilePath& path, |
| 92 | PpapiBrokerProcessHost::Client* client); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 93 | |
[email protected] | 11e1c18 | 2011-05-17 20:26:27 | [diff] [blame] | 94 | // Gets the plugin in the list of plugins that matches the given url and mime |
| 95 | // type. Must be called on the FILE thread. |
| 96 | bool GetPluginInfo(int render_process_id, |
| 97 | int render_view_id, |
| 98 | const GURL& url, |
| 99 | const std::string& mime_type, |
[email protected] | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame^] | 100 | webkit::WebPluginInfo* info, |
[email protected] | 11e1c18 | 2011-05-17 20:26:27 | [diff] [blame] | 101 | std::string* actual_mime_type); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 102 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 103 | // Safe to be called from any thread. |
[email protected] | bd5174d | 2011-03-11 01:02:56 | [diff] [blame] | 104 | void OverridePluginForTab(const OverriddenPlugin& plugin); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 105 | |
[email protected] | 2de30759 | 2011-04-05 21:16:58 | [diff] [blame] | 106 | // Restricts the given plugin to the the scheme and host of the given url. |
| 107 | // Call with an empty url to reset this. |
| 108 | // Can be called on any thread. |
| 109 | void RestrictPluginToUrl(const FilePath& plugin_path, const GURL& url); |
| 110 | |
| 111 | // Returns true if the given plugin is allowed to be used by a page with |
| 112 | // the given URL. |
| 113 | // Can be called on any thread. |
| 114 | bool PluginAllowedForURL(const FilePath& plugin_path, const GURL& url); |
| 115 | |
| 116 | // Tells all the renderer processes to throw away their cache of the plugin |
| 117 | // list, and optionally also reload all the pages with plugins. |
| 118 | // NOTE: can only be called on the UI thread. |
| 119 | static void PurgePluginListCache(bool reload_pages); |
| 120 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 121 | private: |
| 122 | friend struct DefaultSingletonTraits<PluginService>; |
| 123 | |
| 124 | // Creates the PluginService object, but doesn't actually build the plugin |
| 125 | // list yet. It's generated lazily. |
| 126 | PluginService(); |
[email protected] | 3690ebe0 | 2011-05-25 09:08:19 | [diff] [blame] | 127 | virtual ~PluginService(); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 128 | |
| 129 | // base::WaitableEventWatcher::Delegate implementation. |
| 130 | virtual void OnWaitableEventSignaled(base::WaitableEvent* waitable_event); |
| 131 | |
| 132 | // NotificationObserver implementation |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 133 | virtual void Observe(int type, const NotificationSource& source, |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 134 | const NotificationDetails& details); |
| 135 | |
| 136 | void RegisterPepperPlugins(); |
| 137 | |
[email protected] | eb415bf0e | 2011-04-14 02:45:42 | [diff] [blame] | 138 | PepperPluginInfo* GetRegisteredPpapiPluginInfo(const FilePath& plugin_path); |
| 139 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 140 | // Helper so we can do the plugin lookup on the FILE thread. |
| 141 | void GetAllowedPluginForOpenChannelToPlugin( |
| 142 | int render_process_id, |
| 143 | int render_view_id, |
| 144 | const GURL& url, |
| 145 | const std::string& mime_type, |
| 146 | PluginProcessHost::Client* client); |
| 147 | |
| 148 | // Helper so we can finish opening the channel after looking up the |
| 149 | // plugin. |
| 150 | void FinishOpenChannelToPlugin( |
| 151 | const FilePath& plugin_path, |
| 152 | PluginProcessHost::Client* client); |
| 153 | |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 154 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 155 | // Registers a new FilePathWatcher for a given path. |
| 156 | static void RegisterFilePathWatcher( |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 157 | base::files::FilePathWatcher* watcher, |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 158 | const FilePath& path, |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 159 | base::files::FilePathWatcher::Delegate* delegate); |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 160 | #endif |
| 161 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 162 | // The browser's UI locale. |
| 163 | const std::string ui_locale_; |
| 164 | |
[email protected] | 2de30759 | 2011-04-05 21:16:58 | [diff] [blame] | 165 | // Map of plugin paths to the origin they are restricted to. |
| 166 | base::Lock restricted_plugin_lock_; // Guards access to restricted_plugin_. |
| 167 | typedef base::hash_map<FilePath, GURL> RestrictedPluginMap; |
| 168 | RestrictedPluginMap restricted_plugin_; |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 169 | |
| 170 | NotificationRegistrar registrar_; |
| 171 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 172 | #if defined(OS_WIN) |
| 173 | // Registry keys for getting notifications when new plugins are installed. |
| 174 | base::win::RegKey hkcu_key_; |
| 175 | base::win::RegKey hklm_key_; |
| 176 | scoped_ptr<base::WaitableEvent> hkcu_event_; |
| 177 | scoped_ptr<base::WaitableEvent> hklm_event_; |
| 178 | base::WaitableEventWatcher hkcu_watcher_; |
| 179 | base::WaitableEventWatcher hklm_watcher_; |
| 180 | #endif |
| 181 | |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 182 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 493c800 | 2011-04-14 16:56:01 | [diff] [blame] | 183 | ScopedVector<base::files::FilePathWatcher> file_watchers_; |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 184 | scoped_refptr<PluginDirWatcherDelegate> file_watcher_delegate_; |
| 185 | #endif |
| 186 | |
| 187 | std::vector<PepperPluginInfo> ppapi_plugins_; |
| 188 | |
[email protected] | a042173 | 2011-02-23 03:55:40 | [diff] [blame] | 189 | std::vector<OverriddenPlugin> overridden_plugins_; |
| 190 | base::Lock overridden_plugins_lock_; |
| 191 | |
| 192 | DISALLOW_COPY_AND_ASSIGN(PluginService); |
| 193 | }; |
| 194 | |
| 195 | DISABLE_RUNNABLE_METHOD_REFCOUNT(PluginService); |
| 196 | |
| 197 | #endif // CONTENT_BROWSER_PLUGIN_SERVICE_H_ |