[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" |
| 17 | #include "base/scoped_vector.h" |
| 18 | #include "base/singleton.h" |
| 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] | 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" |
| 28 | #include "webkit/plugins/npapi/webplugininfo.h" |
| 29 | |
| 30 | #if defined(OS_WIN) |
| 31 | #include "base/scoped_ptr.h" |
| 32 | #include "base/win/registry.h" |
| 33 | #endif |
| 34 | |
| 35 | #if defined(OS_LINUX) |
| 36 | #include "chrome/browser/file_path_watcher/file_path_watcher.h" |
| 37 | #endif |
| 38 | |
| 39 | #if defined(OS_CHROMEOS) |
| 40 | namespace chromeos { |
| 41 | class PluginSelectionPolicy; |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | namespace IPC { |
| 46 | class Message; |
| 47 | } |
| 48 | |
| 49 | class MessageLoop; |
| 50 | struct PepperPluginInfo; |
| 51 | class PluginDirWatcherDelegate; |
| 52 | class Profile; |
| 53 | class ResourceDispatcherHost; |
| 54 | |
| 55 | namespace net { |
| 56 | class URLRequestContext; |
| 57 | } // namespace net |
| 58 | |
| 59 | // This must be created on the main thread but it's only called on the IO/file |
| 60 | // thread. |
| 61 | class PluginService |
| 62 | : public base::WaitableEventWatcher::Delegate, |
| 63 | public NotificationObserver { |
| 64 | public: |
| 65 | struct OverriddenPlugin { |
| 66 | int render_process_id; |
| 67 | int render_view_id; |
| 68 | GURL url; |
| 69 | webkit::npapi::WebPluginInfo plugin; |
| 70 | }; |
| 71 | |
| 72 | // Initializes the global instance; should be called on startup from the main |
| 73 | // thread. |
| 74 | static void InitGlobalInstance(Profile* profile); |
| 75 | |
| 76 | // Returns the PluginService singleton. |
| 77 | static PluginService* GetInstance(); |
| 78 | |
| 79 | // Load all the plugins that should be loaded for the lifetime of the browser |
| 80 | // (ie, with the LoadOnStartup flag set). |
| 81 | void LoadChromePlugins(ResourceDispatcherHost* resource_dispatcher_host); |
| 82 | |
| 83 | // Sets/gets the data directory that Chrome plugins should use to store |
| 84 | // persistent data. |
| 85 | void SetChromePluginDataDir(const FilePath& data_dir); |
| 86 | const FilePath& GetChromePluginDataDir(); |
| 87 | |
| 88 | // Gets the browser's UI locale. |
| 89 | const std::string& GetUILocale(); |
| 90 | |
| 91 | // Returns the plugin process host corresponding to the plugin process that |
| 92 | // has been started by this service. Returns NULL if no process has been |
| 93 | // started. |
| 94 | PluginProcessHost* FindNpapiPluginProcess(const FilePath& plugin_path); |
| 95 | PpapiPluginProcessHost* FindPpapiPluginProcess(const FilePath& plugin_path); |
| 96 | |
| 97 | // Returns the plugin process host corresponding to the plugin process that |
| 98 | // has been started by this service. This will start a process to host the |
| 99 | // 'plugin_path' if needed. If the process fails to start, the return value |
| 100 | // is NULL. Must be called on the IO thread. |
| 101 | PluginProcessHost* FindOrStartNpapiPluginProcess( |
| 102 | const FilePath& plugin_path); |
| 103 | PpapiPluginProcessHost* FindOrStartPpapiPluginProcess( |
| 104 | const FilePath& plugin_path); |
| 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, |
| 112 | const std::string& mime_type, |
| 113 | PluginProcessHost::Client* client); |
| 114 | void OpenChannelToPpapiPlugin(const FilePath& path, |
| 115 | PpapiPluginProcessHost::Client* client); |
| 116 | |
| 117 | // Gets the first allowed plugin in the list of plugins that matches |
| 118 | // the given url and mime type. Must be called on the FILE thread. |
| 119 | bool GetFirstAllowedPluginInfo(int render_process_id, |
| 120 | int render_view_id, |
| 121 | const GURL& url, |
| 122 | const std::string& mime_type, |
| 123 | webkit::npapi::WebPluginInfo* info, |
| 124 | std::string* actual_mime_type); |
| 125 | |
| 126 | // Returns true if the given plugin is allowed to be used by a page with |
| 127 | // the given URL. |
| 128 | bool PrivatePluginAllowedForURL(const FilePath& plugin_path, const GURL& url); |
| 129 | |
| 130 | // Safe to be called from any thread. |
| 131 | void OverridePluginForTab(OverriddenPlugin plugin); |
| 132 | |
| 133 | // The UI thread's message loop |
| 134 | MessageLoop* main_message_loop() { return main_message_loop_; } |
| 135 | |
| 136 | ResourceDispatcherHost* resource_dispatcher_host() const { |
| 137 | return resource_dispatcher_host_; |
| 138 | } |
| 139 | |
| 140 | static void EnableChromePlugins(bool enable); |
| 141 | |
| 142 | private: |
| 143 | friend struct DefaultSingletonTraits<PluginService>; |
| 144 | |
| 145 | // Creates the PluginService object, but doesn't actually build the plugin |
| 146 | // list yet. It's generated lazily. |
| 147 | PluginService(); |
| 148 | ~PluginService(); |
| 149 | |
| 150 | // base::WaitableEventWatcher::Delegate implementation. |
| 151 | virtual void OnWaitableEventSignaled(base::WaitableEvent* waitable_event); |
| 152 | |
| 153 | // NotificationObserver implementation |
| 154 | virtual void Observe(NotificationType type, const NotificationSource& source, |
| 155 | const NotificationDetails& details); |
| 156 | |
| 157 | void RegisterPepperPlugins(); |
| 158 | |
| 159 | // Helper so we can do the plugin lookup on the FILE thread. |
| 160 | void GetAllowedPluginForOpenChannelToPlugin( |
| 161 | int render_process_id, |
| 162 | int render_view_id, |
| 163 | const GURL& url, |
| 164 | const std::string& mime_type, |
| 165 | PluginProcessHost::Client* client); |
| 166 | |
| 167 | // Helper so we can finish opening the channel after looking up the |
| 168 | // plugin. |
| 169 | void FinishOpenChannelToPlugin( |
| 170 | const FilePath& plugin_path, |
| 171 | PluginProcessHost::Client* client); |
| 172 | |
| 173 | #if defined(OS_LINUX) |
| 174 | // Registers a new FilePathWatcher for a given path. |
| 175 | static void RegisterFilePathWatcher( |
| 176 | FilePathWatcher* watcher, |
| 177 | const FilePath& path, |
| 178 | FilePathWatcher::Delegate* delegate); |
| 179 | #endif |
| 180 | |
| 181 | // The main thread's message loop. |
| 182 | MessageLoop* main_message_loop_; |
| 183 | |
| 184 | // The IO thread's resource dispatcher host. |
| 185 | ResourceDispatcherHost* resource_dispatcher_host_; |
| 186 | |
| 187 | // The data directory that Chrome plugins should use to store persistent data. |
| 188 | FilePath chrome_plugin_data_dir_; |
| 189 | |
| 190 | // The browser's UI locale. |
| 191 | const std::string ui_locale_; |
| 192 | |
| 193 | // Map of plugin paths to the origin they are restricted to. Used for |
| 194 | // extension-only plugins. |
| 195 | typedef base::hash_map<FilePath, GURL> PrivatePluginMap; |
| 196 | PrivatePluginMap private_plugins_; |
| 197 | |
| 198 | NotificationRegistrar registrar_; |
| 199 | |
| 200 | #if defined(OS_CHROMEOS) |
| 201 | scoped_refptr<chromeos::PluginSelectionPolicy> plugin_selection_policy_; |
| 202 | #endif |
| 203 | |
| 204 | #if defined(OS_WIN) |
| 205 | // Registry keys for getting notifications when new plugins are installed. |
| 206 | base::win::RegKey hkcu_key_; |
| 207 | base::win::RegKey hklm_key_; |
| 208 | scoped_ptr<base::WaitableEvent> hkcu_event_; |
| 209 | scoped_ptr<base::WaitableEvent> hklm_event_; |
| 210 | base::WaitableEventWatcher hkcu_watcher_; |
| 211 | base::WaitableEventWatcher hklm_watcher_; |
| 212 | #endif |
| 213 | |
| 214 | #if defined(OS_LINUX) |
| 215 | ScopedVector<FilePathWatcher> file_watchers_; |
| 216 | scoped_refptr<PluginDirWatcherDelegate> file_watcher_delegate_; |
| 217 | #endif |
| 218 | |
| 219 | std::vector<PepperPluginInfo> ppapi_plugins_; |
| 220 | |
| 221 | // Set to true if chrome plugins are enabled. Defaults to true. |
| 222 | static bool enable_chrome_plugins_; |
| 223 | |
| 224 | std::vector<OverriddenPlugin> overridden_plugins_; |
| 225 | base::Lock overridden_plugins_lock_; |
| 226 | |
| 227 | DISALLOW_COPY_AND_ASSIGN(PluginService); |
| 228 | }; |
| 229 | |
| 230 | DISABLE_RUNNABLE_METHOD_REFCOUNT(PluginService); |
| 231 | |
| 232 | #endif // CONTENT_BROWSER_PLUGIN_SERVICE_H_ |