Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | #include "content/browser/plugin_service_impl.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/command_line.h" |
| 9 | #include "base/compiler_specific.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 12 | #include "base/message_loop/message_loop_proxy.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 13 | #include "base/metrics/histogram.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 14 | #include "base/path_service.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
| 16 | #include "base/strings/utf_string_conversions.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 17 | #include "base/synchronization/waitable_event.h" |
| 18 | #include "base/threading/thread.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 19 | #include "content/browser/ppapi_plugin_process_host.h" |
| 20 | #include "content/browser/renderer_host/render_process_host_impl.h" |
| 21 | #include "content/browser/renderer_host/render_view_host_impl.h" |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 22 | #include "content/common/pepper_plugin_list.h" |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 23 | #include "content/common/plugin_list.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 24 | #include "content/common/view_messages.h" |
| 25 | #include "content/public/browser/browser_thread.h" |
| 26 | #include "content/public/browser/content_browser_client.h" |
| 27 | #include "content/public/browser/plugin_service_filter.h" |
| 28 | #include "content/public/browser/resource_context.h" |
Ben Murdoch | ba5b9a6 | 2013-08-12 14:20:17 +0100 | [diff] [blame] | 29 | #include "content/public/common/content_constants.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 30 | #include "content/public/common/content_switches.h" |
| 31 | #include "content/public/common/process_type.h" |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 32 | #include "content/public/common/webplugininfo.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 33 | |
| 34 | #if defined(OS_WIN) |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 35 | #include "content/common/plugin_constants_win.h" |
| 36 | #include "ui/base/win/hwnd_util.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | #if defined(OS_POSIX) |
| 40 | #include "content/browser/plugin_loader_posix.h" |
| 41 | #endif |
| 42 | |
| 43 | #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 44 | using ::base::FilePathWatcher; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | namespace content { |
| 48 | namespace { |
| 49 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 50 | // This enum is used to collect Flash usage data. |
| 51 | enum FlashUsage { |
| 52 | // Number of browser processes that have started at least one NPAPI Flash |
| 53 | // process during their lifetime. |
| 54 | START_NPAPI_FLASH_AT_LEAST_ONCE, |
| 55 | // Number of browser processes that have started at least one PPAPI Flash |
| 56 | // process during their lifetime. |
| 57 | START_PPAPI_FLASH_AT_LEAST_ONCE, |
| 58 | // Total number of browser processes. |
| 59 | TOTAL_BROWSER_PROCESSES, |
| 60 | FLASH_USAGE_ENUM_COUNT |
| 61 | }; |
| 62 | |
| 63 | bool LoadPluginListInProcess() { |
| 64 | #if defined(OS_WIN) |
| 65 | return true; |
| 66 | #else |
| 67 | // If on POSIX, we don't want to load the list of NPAPI plugins in-process as |
| 68 | // that causes instability. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 69 | |
| 70 | // Can't load the plugins on the utility thread when in single process mode |
| 71 | // since that requires GTK which can only be used on the main thread. |
| 72 | if (RenderProcessHost::run_renderer_in_process()) |
| 73 | return true; |
| 74 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 75 | return !PluginService::GetInstance()->NPAPIPluginsSupported(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 76 | #endif |
| 77 | } |
| 78 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 79 | // Callback set on the PluginList to assert that plugin loading happens on the |
| 80 | // correct thread. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 81 | void WillLoadPluginsCallback( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 82 | base::SequencedWorkerPool::SequenceToken token) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 83 | if (LoadPluginListInProcess()) { |
| 84 | CHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
| 85 | token)); |
| 86 | } else { |
| 87 | CHECK(false) << "Plugin loading should happen out-of-process."; |
| 88 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 89 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 90 | |
| 91 | #if defined(OS_MACOSX) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 92 | void NotifyPluginsOfActivation() { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 93 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 94 | |
| 95 | for (PluginProcessHostIterator iter; !iter.Done(); ++iter) |
| 96 | iter->OnAppActivation(); |
| 97 | } |
| 98 | #endif |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 99 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 100 | #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID) |
| 101 | void NotifyPluginDirChanged(const base::FilePath& path, bool error) { |
| 102 | if (error) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 103 | // TODO(pastarmovj): Add some sensible error handling. Maybe silently |
| 104 | // stopping the watcher would be enough. Or possibly restart it. |
| 105 | NOTREACHED(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 106 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 107 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 108 | VLOG(1) << "Watched path changed: " << path.value(); |
| 109 | // Make the plugin list update itself |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 110 | PluginList::Singleton()->RefreshPlugins(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 111 | BrowserThread::PostTask( |
| 112 | BrowserThread::UI, FROM_HERE, |
| 113 | base::Bind(&PluginService::PurgePluginListCache, |
| 114 | static_cast<BrowserContext*>(NULL), false)); |
| 115 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 116 | #endif |
| 117 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 118 | } // namespace |
| 119 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 120 | // static |
| 121 | PluginService* PluginService::GetInstance() { |
| 122 | return PluginServiceImpl::GetInstance(); |
| 123 | } |
| 124 | |
| 125 | void PluginService::PurgePluginListCache(BrowserContext* browser_context, |
| 126 | bool reload_pages) { |
| 127 | for (RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator(); |
| 128 | !it.IsAtEnd(); it.Advance()) { |
| 129 | RenderProcessHost* host = it.GetCurrentValue(); |
| 130 | if (!browser_context || host->GetBrowserContext() == browser_context) |
| 131 | host->Send(new ViewMsg_PurgePluginListCache(reload_pages)); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // static |
| 136 | PluginServiceImpl* PluginServiceImpl::GetInstance() { |
| 137 | return Singleton<PluginServiceImpl>::get(); |
| 138 | } |
| 139 | |
| 140 | PluginServiceImpl::PluginServiceImpl() |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 141 | : filter_(NULL) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 142 | // Collect the total number of browser processes (which create |
| 143 | // PluginServiceImpl objects, to be precise). The number is used to normalize |
| 144 | // the number of processes which start at least one NPAPI/PPAPI Flash process. |
| 145 | static bool counted = false; |
| 146 | if (!counted) { |
| 147 | counted = true; |
| 148 | UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage", TOTAL_BROWSER_PROCESSES, |
| 149 | FLASH_USAGE_ENUM_COUNT); |
| 150 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | PluginServiceImpl::~PluginServiceImpl() { |
| 154 | #if defined(OS_WIN) |
| 155 | // Release the events since they're owned by RegKey, not WaitableEvent. |
| 156 | hkcu_watcher_.StopWatching(); |
| 157 | hklm_watcher_.StopWatching(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 158 | if (hkcu_event_) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 159 | hkcu_event_->Release(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 160 | if (hklm_event_) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 161 | hklm_event_->Release(); |
| 162 | #endif |
| 163 | // Make sure no plugin channel requests have been leaked. |
| 164 | DCHECK(pending_plugin_clients_.empty()); |
| 165 | } |
| 166 | |
| 167 | void PluginServiceImpl::Init() { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 168 | plugin_list_token_ = BrowserThread::GetBlockingPool()->GetSequenceToken(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 169 | PluginList::Singleton()->set_will_load_plugins_callback( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 170 | base::Bind(&WillLoadPluginsCallback, plugin_list_token_)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 171 | |
| 172 | RegisterPepperPlugins(); |
| 173 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 174 | // The --site-per-process flag enables an out-of-process iframes |
| 175 | // prototype, which uses WebView for rendering. We need to register the MIME |
| 176 | // type we use with the plugin, so the renderer can instantiate it. |
| 177 | const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 178 | if (command_line->HasSwitch(switches::kSitePerProcess)) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 179 | WebPluginInfo webview_plugin( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 180 | ASCIIToUTF16("WebView Tag"), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 181 | base::FilePath(), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 182 | ASCIIToUTF16("1.2.3.4"), |
| 183 | ASCIIToUTF16("Browser Plugin.")); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 184 | webview_plugin.type = WebPluginInfo::PLUGIN_TYPE_NPAPI; |
| 185 | WebPluginMimeType webview_plugin_mime_type; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 186 | webview_plugin_mime_type.mime_type = "application/browser-plugin"; |
| 187 | webview_plugin_mime_type.file_extensions.push_back("*"); |
| 188 | webview_plugin.mime_types.push_back(webview_plugin_mime_type); |
| 189 | RegisterInternalPlugin(webview_plugin, true); |
| 190 | } |
| 191 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 192 | // Load any specified on the command line as well. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 193 | base::FilePath path = |
| 194 | command_line->GetSwitchValuePath(switches::kLoadPlugin); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 195 | if (!path.empty()) |
| 196 | AddExtraPluginPath(path); |
| 197 | path = command_line->GetSwitchValuePath(switches::kExtraPluginDir); |
| 198 | if (!path.empty()) |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 199 | PluginList::Singleton()->AddExtraPluginDir(path); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 200 | |
| 201 | if (command_line->HasSwitch(switches::kDisablePluginsDiscovery)) |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 202 | PluginList::Singleton()->DisablePluginsDiscovery(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void PluginServiceImpl::StartWatchingPlugins() { |
| 206 | // Start watching for changes in the plugin list. This means watching |
| 207 | // for changes in the Windows registry keys and on both Windows and POSIX |
| 208 | // watch for changes in the paths that are expected to contain plugins. |
| 209 | #if defined(OS_WIN) |
| 210 | if (hkcu_key_.Create(HKEY_CURRENT_USER, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 211 | kRegistryMozillaPlugins, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 212 | KEY_NOTIFY) == ERROR_SUCCESS) { |
| 213 | if (hkcu_key_.StartWatching() == ERROR_SUCCESS) { |
| 214 | hkcu_event_.reset(new base::WaitableEvent(hkcu_key_.watch_event())); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 215 | base::WaitableEventWatcher::EventCallback callback = |
| 216 | base::Bind(&PluginServiceImpl::OnWaitableEventSignaled, |
| 217 | base::Unretained(this)); |
| 218 | hkcu_watcher_.StartWatching(hkcu_event_.get(), callback); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | if (hklm_key_.Create(HKEY_LOCAL_MACHINE, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 222 | kRegistryMozillaPlugins, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 223 | KEY_NOTIFY) == ERROR_SUCCESS) { |
| 224 | if (hklm_key_.StartWatching() == ERROR_SUCCESS) { |
| 225 | hklm_event_.reset(new base::WaitableEvent(hklm_key_.watch_event())); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 226 | base::WaitableEventWatcher::EventCallback callback = |
| 227 | base::Bind(&PluginServiceImpl::OnWaitableEventSignaled, |
| 228 | base::Unretained(this)); |
| 229 | hklm_watcher_.StartWatching(hklm_event_.get(), callback); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | #endif |
| 233 | #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID) |
| 234 | // On ChromeOS the user can't install plugins anyway and on Windows all |
| 235 | // important plugins register themselves in the registry so no need to do that. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 236 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 237 | // Get the list of all paths for registering the FilePathWatchers |
| 238 | // that will track and if needed reload the list of plugins on runtime. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 239 | std::vector<base::FilePath> plugin_dirs; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 240 | PluginList::Singleton()->GetPluginDirectories(&plugin_dirs); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 241 | |
| 242 | for (size_t i = 0; i < plugin_dirs.size(); ++i) { |
| 243 | // FilePathWatcher can not handle non-absolute paths under windows. |
| 244 | // We don't watch for file changes in windows now but if this should ever |
| 245 | // be extended to Windows these lines might save some time of debugging. |
| 246 | #if defined(OS_WIN) |
| 247 | if (!plugin_dirs[i].IsAbsolute()) |
| 248 | continue; |
| 249 | #endif |
| 250 | FilePathWatcher* watcher = new FilePathWatcher(); |
| 251 | VLOG(1) << "Watching for changes in: " << plugin_dirs[i].value(); |
| 252 | BrowserThread::PostTask( |
| 253 | BrowserThread::FILE, FROM_HERE, |
| 254 | base::Bind(&PluginServiceImpl::RegisterFilePathWatcher, watcher, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 255 | plugin_dirs[i])); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 256 | file_watchers_.push_back(watcher); |
| 257 | } |
| 258 | #endif |
| 259 | } |
| 260 | |
| 261 | PluginProcessHost* PluginServiceImpl::FindNpapiPluginProcess( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 262 | const base::FilePath& plugin_path) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 263 | for (PluginProcessHostIterator iter; !iter.Done(); ++iter) { |
| 264 | if (iter->info().path == plugin_path) |
| 265 | return *iter; |
| 266 | } |
| 267 | |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | PpapiPluginProcessHost* PluginServiceImpl::FindPpapiPluginProcess( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 272 | const base::FilePath& plugin_path, |
| 273 | const base::FilePath& profile_data_directory) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 274 | for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) { |
| 275 | if (iter->plugin_path() == plugin_path && |
| 276 | iter->profile_data_directory() == profile_data_directory) { |
| 277 | return *iter; |
| 278 | } |
| 279 | } |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | PpapiPluginProcessHost* PluginServiceImpl::FindPpapiBrokerProcess( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 284 | const base::FilePath& broker_path) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 285 | for (PpapiBrokerProcessHostIterator iter; !iter.Done(); ++iter) { |
| 286 | if (iter->plugin_path() == broker_path) |
| 287 | return *iter; |
| 288 | } |
| 289 | |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | PluginProcessHost* PluginServiceImpl::FindOrStartNpapiPluginProcess( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 294 | int render_process_id, |
| 295 | const base::FilePath& plugin_path) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 296 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 297 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 298 | if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path)) |
| 299 | return NULL; |
| 300 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 301 | PluginProcessHost* plugin_host = FindNpapiPluginProcess(plugin_path); |
| 302 | if (plugin_host) |
| 303 | return plugin_host; |
| 304 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 305 | WebPluginInfo info; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 306 | if (!GetPluginInfoByPath(plugin_path, &info)) { |
| 307 | return NULL; |
| 308 | } |
| 309 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 310 | // Record when NPAPI Flash process is started for the first time. |
| 311 | static bool counted = false; |
| 312 | if (!counted && UTF16ToUTF8(info.name) == kFlashPluginName) { |
| 313 | counted = true; |
| 314 | UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage", |
| 315 | START_NPAPI_FLASH_AT_LEAST_ONCE, |
| 316 | FLASH_USAGE_ENUM_COUNT); |
| 317 | } |
| 318 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 319 | // This plugin isn't loaded by any plugin process, so create a new process. |
| 320 | scoped_ptr<PluginProcessHost> new_host(new PluginProcessHost()); |
| 321 | if (!new_host->Init(info)) { |
| 322 | NOTREACHED(); // Init is not expected to fail. |
| 323 | return NULL; |
| 324 | } |
| 325 | return new_host.release(); |
| 326 | } |
| 327 | |
| 328 | PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiPluginProcess( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 329 | int render_process_id, |
| 330 | const base::FilePath& plugin_path, |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame^] | 331 | const base::FilePath& profile_data_directory) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 332 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 333 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 334 | if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path)) |
| 335 | return NULL; |
| 336 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 337 | PpapiPluginProcessHost* plugin_host = |
| 338 | FindPpapiPluginProcess(plugin_path, profile_data_directory); |
| 339 | if (plugin_host) |
| 340 | return plugin_host; |
| 341 | |
| 342 | // Validate that the plugin is actually registered. |
| 343 | PepperPluginInfo* info = GetRegisteredPpapiPluginInfo(plugin_path); |
| 344 | if (!info) |
| 345 | return NULL; |
| 346 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 347 | // Record when PPAPI Flash process is started for the first time. |
| 348 | static bool counted = false; |
| 349 | if (!counted && info->name == kFlashPluginName) { |
| 350 | counted = true; |
| 351 | UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage", |
| 352 | START_PPAPI_FLASH_AT_LEAST_ONCE, |
| 353 | FLASH_USAGE_ENUM_COUNT); |
| 354 | } |
| 355 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 356 | // This plugin isn't loaded by any plugin process, so create a new process. |
| 357 | return PpapiPluginProcessHost::CreatePluginHost( |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame^] | 358 | *info, profile_data_directory); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiBrokerProcess( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 362 | int render_process_id, |
| 363 | const base::FilePath& plugin_path) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 364 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 365 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 366 | if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path)) |
| 367 | return NULL; |
| 368 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 369 | PpapiPluginProcessHost* plugin_host = FindPpapiBrokerProcess(plugin_path); |
| 370 | if (plugin_host) |
| 371 | return plugin_host; |
| 372 | |
| 373 | // Validate that the plugin is actually registered. |
| 374 | PepperPluginInfo* info = GetRegisteredPpapiPluginInfo(plugin_path); |
| 375 | if (!info) |
| 376 | return NULL; |
| 377 | |
| 378 | // TODO(ddorwin): Uncomment once out of process is supported. |
| 379 | // DCHECK(info->is_out_of_process); |
| 380 | |
| 381 | // This broker isn't loaded by any broker process, so create a new process. |
| 382 | return PpapiPluginProcessHost::CreateBrokerHost(*info); |
| 383 | } |
| 384 | |
| 385 | void PluginServiceImpl::OpenChannelToNpapiPlugin( |
| 386 | int render_process_id, |
| 387 | int render_view_id, |
| 388 | const GURL& url, |
| 389 | const GURL& page_url, |
| 390 | const std::string& mime_type, |
| 391 | PluginProcessHost::Client* client) { |
| 392 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 393 | DCHECK(!ContainsKey(pending_plugin_clients_, client)); |
| 394 | pending_plugin_clients_.insert(client); |
| 395 | |
| 396 | // Make sure plugins are loaded if necessary. |
| 397 | PluginServiceFilterParams params = { |
| 398 | render_process_id, |
| 399 | render_view_id, |
| 400 | page_url, |
| 401 | client->GetResourceContext() |
| 402 | }; |
| 403 | GetPlugins(base::Bind( |
| 404 | &PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin, |
| 405 | base::Unretained(this), params, url, mime_type, client)); |
| 406 | } |
| 407 | |
| 408 | void PluginServiceImpl::OpenChannelToPpapiPlugin( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 409 | int render_process_id, |
| 410 | const base::FilePath& plugin_path, |
| 411 | const base::FilePath& profile_data_directory, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 412 | PpapiPluginProcessHost::PluginClient* client) { |
| 413 | PpapiPluginProcessHost* plugin_host = FindOrStartPpapiPluginProcess( |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame^] | 414 | render_process_id, plugin_path, profile_data_directory); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 415 | if (plugin_host) { |
| 416 | plugin_host->OpenChannelToPlugin(client); |
| 417 | } else { |
| 418 | // Send error. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 419 | client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | void PluginServiceImpl::OpenChannelToPpapiBroker( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 424 | int render_process_id, |
| 425 | const base::FilePath& path, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 426 | PpapiPluginProcessHost::BrokerClient* client) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 427 | PpapiPluginProcessHost* plugin_host = FindOrStartPpapiBrokerProcess( |
| 428 | render_process_id, path); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 429 | if (plugin_host) { |
| 430 | plugin_host->OpenChannelToPlugin(client); |
| 431 | } else { |
| 432 | // Send error. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 433 | client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | void PluginServiceImpl::CancelOpenChannelToNpapiPlugin( |
| 438 | PluginProcessHost::Client* client) { |
| 439 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 440 | DCHECK(ContainsKey(pending_plugin_clients_, client)); |
| 441 | pending_plugin_clients_.erase(client); |
| 442 | } |
| 443 | |
| 444 | void PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin( |
| 445 | const PluginServiceFilterParams& params, |
| 446 | const GURL& url, |
| 447 | const std::string& mime_type, |
| 448 | PluginProcessHost::Client* client, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 449 | const std::vector<WebPluginInfo>&) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 450 | GetAllowedPluginForOpenChannelToPlugin(params.render_process_id, |
| 451 | params.render_view_id, url, params.page_url, mime_type, client, |
| 452 | params.resource_context); |
| 453 | } |
| 454 | |
| 455 | void PluginServiceImpl::GetAllowedPluginForOpenChannelToPlugin( |
| 456 | int render_process_id, |
| 457 | int render_view_id, |
| 458 | const GURL& url, |
| 459 | const GURL& page_url, |
| 460 | const std::string& mime_type, |
| 461 | PluginProcessHost::Client* client, |
| 462 | ResourceContext* resource_context) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 463 | WebPluginInfo info; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 464 | bool allow_wildcard = true; |
| 465 | bool found = GetPluginInfo( |
| 466 | render_process_id, render_view_id, resource_context, |
| 467 | url, page_url, mime_type, allow_wildcard, |
| 468 | NULL, &info, NULL); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 469 | base::FilePath plugin_path; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 470 | if (found) |
| 471 | plugin_path = info.path; |
| 472 | |
| 473 | // Now we jump back to the IO thread to finish opening the channel. |
| 474 | BrowserThread::PostTask( |
| 475 | BrowserThread::IO, FROM_HERE, |
| 476 | base::Bind(&PluginServiceImpl::FinishOpenChannelToPlugin, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 477 | base::Unretained(this), |
| 478 | render_process_id, |
| 479 | plugin_path, |
| 480 | client)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void PluginServiceImpl::FinishOpenChannelToPlugin( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 484 | int render_process_id, |
| 485 | const base::FilePath& plugin_path, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 486 | PluginProcessHost::Client* client) { |
| 487 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 488 | |
| 489 | // Make sure it hasn't been canceled yet. |
| 490 | if (!ContainsKey(pending_plugin_clients_, client)) |
| 491 | return; |
| 492 | pending_plugin_clients_.erase(client); |
| 493 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 494 | PluginProcessHost* plugin_host = FindOrStartNpapiPluginProcess( |
| 495 | render_process_id, plugin_path); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 496 | if (plugin_host) { |
| 497 | client->OnFoundPluginProcessHost(plugin_host); |
| 498 | plugin_host->OpenChannelToPlugin(client); |
| 499 | } else { |
| 500 | client->OnError(); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | bool PluginServiceImpl::GetPluginInfoArray( |
| 505 | const GURL& url, |
| 506 | const std::string& mime_type, |
| 507 | bool allow_wildcard, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 508 | std::vector<WebPluginInfo>* plugins, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 509 | std::vector<std::string>* actual_mime_types) { |
| 510 | bool use_stale = false; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 511 | PluginList::Singleton()->GetPluginInfoArray( |
| 512 | url, mime_type, allow_wildcard, &use_stale, NPAPIPluginsSupported(), |
| 513 | plugins, actual_mime_types); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 514 | return use_stale; |
| 515 | } |
| 516 | |
| 517 | bool PluginServiceImpl::GetPluginInfo(int render_process_id, |
| 518 | int render_view_id, |
| 519 | ResourceContext* context, |
| 520 | const GURL& url, |
| 521 | const GURL& page_url, |
| 522 | const std::string& mime_type, |
| 523 | bool allow_wildcard, |
| 524 | bool* is_stale, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 525 | WebPluginInfo* info, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 526 | std::string* actual_mime_type) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 527 | std::vector<WebPluginInfo> plugins; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 528 | std::vector<std::string> mime_types; |
| 529 | bool stale = GetPluginInfoArray( |
| 530 | url, mime_type, allow_wildcard, &plugins, &mime_types); |
| 531 | if (is_stale) |
| 532 | *is_stale = stale; |
| 533 | |
| 534 | for (size_t i = 0; i < plugins.size(); ++i) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 535 | if (!filter_ || filter_->IsPluginAvailable(render_process_id, |
| 536 | render_view_id, |
| 537 | context, |
| 538 | url, |
| 539 | page_url, |
| 540 | &plugins[i])) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 541 | *info = plugins[i]; |
| 542 | if (actual_mime_type) |
| 543 | *actual_mime_type = mime_types[i]; |
| 544 | return true; |
| 545 | } |
| 546 | } |
| 547 | return false; |
| 548 | } |
| 549 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 550 | bool PluginServiceImpl::GetPluginInfoByPath(const base::FilePath& plugin_path, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 551 | WebPluginInfo* info) { |
| 552 | std::vector<WebPluginInfo> plugins; |
| 553 | PluginList::Singleton()->GetPluginsNoRefresh(&plugins); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 554 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 555 | for (std::vector<WebPluginInfo>::iterator it = plugins.begin(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 556 | it != plugins.end(); |
| 557 | ++it) { |
| 558 | if (it->path == plugin_path) { |
| 559 | *info = *it; |
| 560 | return true; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return false; |
| 565 | } |
| 566 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 567 | string16 PluginServiceImpl::GetPluginDisplayNameByPath( |
| 568 | const base::FilePath& path) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 569 | string16 plugin_name = path.LossyDisplayName(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 570 | WebPluginInfo info; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 571 | if (PluginService::GetInstance()->GetPluginInfoByPath(path, &info) && |
| 572 | !info.name.empty()) { |
| 573 | plugin_name = info.name; |
| 574 | #if defined(OS_MACOSX) |
| 575 | // Many plugins on the Mac have .plugin in the actual name, which looks |
| 576 | // terrible, so look for that and strip it off if present. |
| 577 | const std::string kPluginExtension = ".plugin"; |
| 578 | if (EndsWith(plugin_name, ASCIIToUTF16(kPluginExtension), true)) |
| 579 | plugin_name.erase(plugin_name.length() - kPluginExtension.length()); |
| 580 | #endif // OS_MACOSX |
| 581 | } |
| 582 | return plugin_name; |
| 583 | } |
| 584 | |
| 585 | void PluginServiceImpl::GetPlugins(const GetPluginsCallback& callback) { |
| 586 | scoped_refptr<base::MessageLoopProxy> target_loop( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 587 | base::MessageLoop::current()->message_loop_proxy()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 588 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 589 | if (LoadPluginListInProcess()) { |
| 590 | BrowserThread::GetBlockingPool()-> |
| 591 | PostSequencedWorkerTaskWithShutdownBehavior( |
| 592 | plugin_list_token_, |
| 593 | FROM_HERE, |
| 594 | base::Bind(&PluginServiceImpl::GetPluginsInternal, |
| 595 | base::Unretained(this), |
| 596 | target_loop, callback), |
| 597 | base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 598 | return; |
| 599 | } |
| 600 | #if defined(OS_POSIX) |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 601 | std::vector<WebPluginInfo> cached_plugins; |
| 602 | if (PluginList::Singleton()->GetPluginsNoRefresh(&cached_plugins)) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 603 | // Can't assume the caller is reentrant. |
| 604 | target_loop->PostTask(FROM_HERE, |
| 605 | base::Bind(callback, cached_plugins)); |
| 606 | } else { |
| 607 | // If we switch back to loading plugins in process, then we need to make |
| 608 | // sure g_thread_init() gets called since plugins may call glib at load. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 609 | if (!plugin_loader_.get()) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 610 | plugin_loader_ = new PluginLoaderPosix; |
| 611 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 612 | base::Bind(&PluginLoaderPosix::LoadPlugins, plugin_loader_, |
| 613 | target_loop, callback)); |
| 614 | } |
| 615 | #else |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 616 | NOTREACHED(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 617 | #endif |
| 618 | } |
| 619 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 620 | void PluginServiceImpl::GetPluginsInternal( |
| 621 | base::MessageLoopProxy* target_loop, |
| 622 | const PluginService::GetPluginsCallback& callback) { |
| 623 | DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
| 624 | plugin_list_token_)); |
| 625 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 626 | std::vector<WebPluginInfo> plugins; |
| 627 | PluginList::Singleton()->GetPlugins(&plugins, NPAPIPluginsSupported()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 628 | |
| 629 | target_loop->PostTask(FROM_HERE, |
| 630 | base::Bind(callback, plugins)); |
| 631 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 632 | |
| 633 | void PluginServiceImpl::OnWaitableEventSignaled( |
| 634 | base::WaitableEvent* waitable_event) { |
| 635 | #if defined(OS_WIN) |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 636 | if (waitable_event == hkcu_event_) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 637 | hkcu_key_.StartWatching(); |
| 638 | } else { |
| 639 | hklm_key_.StartWatching(); |
| 640 | } |
| 641 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 642 | PluginList::Singleton()->RefreshPlugins(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 643 | PurgePluginListCache(NULL, false); |
| 644 | #else |
| 645 | // This event should only get signaled on a Windows machine. |
| 646 | NOTREACHED(); |
| 647 | #endif // defined(OS_WIN) |
| 648 | } |
| 649 | |
| 650 | void PluginServiceImpl::RegisterPepperPlugins() { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 651 | ComputePepperPluginList(&ppapi_plugins_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 652 | for (size_t i = 0; i < ppapi_plugins_.size(); ++i) { |
| 653 | RegisterInternalPlugin(ppapi_plugins_[i].ToWebPluginInfo(), true); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // There should generally be very few plugins so a brute-force search is fine. |
| 658 | PepperPluginInfo* PluginServiceImpl::GetRegisteredPpapiPluginInfo( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 659 | const base::FilePath& plugin_path) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 660 | PepperPluginInfo* info = NULL; |
| 661 | for (size_t i = 0; i < ppapi_plugins_.size(); i++) { |
| 662 | if (ppapi_plugins_[i].path == plugin_path) { |
| 663 | info = &ppapi_plugins_[i]; |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | if (info) |
| 668 | return info; |
| 669 | // We did not find the plugin in our list. But wait! the plugin can also |
| 670 | // be a latecomer, as it happens with pepper flash. This information |
| 671 | // can be obtained from the PluginList singleton and we can use it to |
| 672 | // construct it and add it to the list. This same deal needs to be done |
| 673 | // in the renderer side in PepperPluginRegistry. |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 674 | WebPluginInfo webplugin_info; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 675 | if (!GetPluginInfoByPath(plugin_path, &webplugin_info)) |
| 676 | return NULL; |
| 677 | PepperPluginInfo new_pepper_info; |
| 678 | if (!MakePepperPluginInfo(webplugin_info, &new_pepper_info)) |
| 679 | return NULL; |
| 680 | ppapi_plugins_.push_back(new_pepper_info); |
| 681 | return &ppapi_plugins_[ppapi_plugins_.size() - 1]; |
| 682 | } |
| 683 | |
| 684 | #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID) |
| 685 | // static |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 686 | void PluginServiceImpl::RegisterFilePathWatcher(FilePathWatcher* watcher, |
| 687 | const base::FilePath& path) { |
| 688 | bool result = watcher->Watch(path, false, |
| 689 | base::Bind(&NotifyPluginDirChanged)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 690 | DCHECK(result); |
| 691 | } |
| 692 | #endif |
| 693 | |
| 694 | void PluginServiceImpl::SetFilter(PluginServiceFilter* filter) { |
| 695 | filter_ = filter; |
| 696 | } |
| 697 | |
| 698 | PluginServiceFilter* PluginServiceImpl::GetFilter() { |
| 699 | return filter_; |
| 700 | } |
| 701 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 702 | void PluginServiceImpl::ForcePluginShutdown(const base::FilePath& plugin_path) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 703 | if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 704 | BrowserThread::PostTask( |
| 705 | BrowserThread::IO, FROM_HERE, |
| 706 | base::Bind(&PluginServiceImpl::ForcePluginShutdown, |
| 707 | base::Unretained(this), plugin_path)); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | PluginProcessHost* plugin = FindNpapiPluginProcess(plugin_path); |
| 712 | if (plugin) |
| 713 | plugin->ForceShutdown(); |
| 714 | } |
| 715 | |
| 716 | static const unsigned int kMaxCrashesPerInterval = 3; |
| 717 | static const unsigned int kCrashesInterval = 120; |
| 718 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 719 | void PluginServiceImpl::RegisterPluginCrash(const base::FilePath& path) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 720 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 721 | std::map<base::FilePath, std::vector<base::Time> >::iterator i = |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 722 | crash_times_.find(path); |
| 723 | if (i == crash_times_.end()) { |
| 724 | crash_times_[path] = std::vector<base::Time>(); |
| 725 | i = crash_times_.find(path); |
| 726 | } |
| 727 | if (i->second.size() == kMaxCrashesPerInterval) { |
| 728 | i->second.erase(i->second.begin()); |
| 729 | } |
| 730 | base::Time time = base::Time::Now(); |
| 731 | i->second.push_back(time); |
| 732 | } |
| 733 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 734 | bool PluginServiceImpl::IsPluginUnstable(const base::FilePath& path) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 735 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 736 | std::map<base::FilePath, std::vector<base::Time> >::const_iterator i = |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 737 | crash_times_.find(path); |
| 738 | if (i == crash_times_.end()) { |
| 739 | return false; |
| 740 | } |
| 741 | if (i->second.size() != kMaxCrashesPerInterval) { |
| 742 | return false; |
| 743 | } |
| 744 | base::TimeDelta delta = base::Time::Now() - i->second[0]; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 745 | return delta.InSeconds() <= kCrashesInterval; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | void PluginServiceImpl::RefreshPlugins() { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 749 | PluginList::Singleton()->RefreshPlugins(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 752 | void PluginServiceImpl::AddExtraPluginPath(const base::FilePath& path) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 753 | if (!NPAPIPluginsSupported()) { |
| 754 | // TODO(jam): remove and just have CHECK once we're sure this doesn't get |
| 755 | // triggered. |
| 756 | DLOG(INFO) << "NPAPI plugins not supported"; |
| 757 | return; |
| 758 | } |
| 759 | PluginList::Singleton()->AddExtraPluginPath(path); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 762 | void PluginServiceImpl::RemoveExtraPluginPath(const base::FilePath& path) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 763 | PluginList::Singleton()->RemoveExtraPluginPath(path); |
| 764 | } |
| 765 | |
| 766 | void PluginServiceImpl::AddExtraPluginDir(const base::FilePath& path) { |
| 767 | PluginList::Singleton()->AddExtraPluginDir(path); |
| 768 | } |
| 769 | |
| 770 | void PluginServiceImpl::RegisterInternalPlugin( |
| 771 | const WebPluginInfo& info, |
| 772 | bool add_at_beginning) { |
| 773 | if (!NPAPIPluginsSupported() && |
| 774 | info.type == WebPluginInfo::PLUGIN_TYPE_NPAPI) { |
| 775 | DLOG(INFO) << "Don't register NPAPI plugins when they're not supported"; |
| 776 | return; |
| 777 | } |
| 778 | PluginList::Singleton()->RegisterInternalPlugin(info, add_at_beginning); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 781 | void PluginServiceImpl::UnregisterInternalPlugin(const base::FilePath& path) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 782 | PluginList::Singleton()->UnregisterInternalPlugin(path); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 785 | void PluginServiceImpl::GetInternalPlugins( |
| 786 | std::vector<WebPluginInfo>* plugins) { |
| 787 | PluginList::Singleton()->GetInternalPlugins(plugins); |
| 788 | } |
| 789 | |
| 790 | bool PluginServiceImpl::NPAPIPluginsSupported() { |
| 791 | #if defined(OS_WIN) || defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(USE_AURA)) |
| 792 | return true; |
| 793 | #else |
| 794 | return false; |
| 795 | #endif |
| 796 | } |
| 797 | |
| 798 | void PluginServiceImpl::DisablePluginsDiscoveryForTesting() { |
| 799 | PluginList::Singleton()->DisablePluginsDiscovery(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | #if defined(OS_MACOSX) |
| 803 | void PluginServiceImpl::AppActivated() { |
| 804 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 805 | base::Bind(&NotifyPluginsOfActivation)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 806 | } |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 807 | #elif defined(OS_WIN) |
| 808 | |
| 809 | bool GetPluginPropertyFromWindow( |
| 810 | HWND window, const wchar_t* plugin_atom_property, |
| 811 | base::string16* plugin_property) { |
| 812 | ATOM plugin_atom = reinterpret_cast<ATOM>( |
| 813 | GetPropW(window, plugin_atom_property)); |
| 814 | if (plugin_atom != 0) { |
| 815 | WCHAR plugin_property_local[MAX_PATH] = {0}; |
| 816 | GlobalGetAtomNameW(plugin_atom, |
| 817 | plugin_property_local, |
| 818 | ARRAYSIZE(plugin_property_local)); |
| 819 | *plugin_property = plugin_property_local; |
| 820 | return true; |
| 821 | } |
| 822 | return false; |
| 823 | } |
| 824 | |
| 825 | bool PluginServiceImpl::GetPluginInfoFromWindow( |
| 826 | HWND window, |
| 827 | base::string16* plugin_name, |
| 828 | base::string16* plugin_version) { |
| 829 | if (!IsPluginWindow(window)) |
| 830 | return false; |
| 831 | |
| 832 | GetPluginPropertyFromWindow( |
| 833 | window, kPluginNameAtomProperty, plugin_name); |
| 834 | GetPluginPropertyFromWindow( |
| 835 | window, kPluginVersionAtomProperty, plugin_version); |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | bool PluginServiceImpl::IsPluginWindow(HWND window) { |
| 840 | return ui::GetClassName(window) == base::string16(kNativeWindowClassName); |
| 841 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 842 | #endif |
| 843 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 844 | } // namespace content |