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