Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / plugin_service_impl.cc
blob4aab6e7e10580c77b224c09fcd38d5151efc3c99
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.
5 #include "content/browser/plugin_service_impl.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/files/file_path.h"
11 #include "base/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/metrics/histogram.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/threading/thread.h"
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"
22 #include "content/common/pepper_plugin_registry.h"
23 #include "content/common/view_messages.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/content_browser_client.h"
26 #include "content/public/browser/plugin_service_filter.h"
27 #include "content/public/browser/resource_context.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/common/process_type.h"
30 #include "webkit/plugins/npapi/plugin_list.h"
31 #include "webkit/plugins/npapi/plugin_utils.h"
32 #include "webkit/plugins/plugin_constants.h"
33 #include "webkit/plugins/webplugininfo.h"
35 #if defined(OS_WIN)
36 #include "webkit/plugins/npapi/plugin_constants_win.h"
37 #endif
39 #if defined(OS_POSIX)
40 #include "content/browser/plugin_loader_posix.h"
41 #endif
43 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
44 using ::base::FilePathWatcher;
45 #endif
47 namespace content {
48 namespace {
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
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.
69 return !webkit::npapi::NPAPIPluginsSupported();
70 #endif
73 // Callback set on the PluginList to assert that plugin loading happens on the
74 // correct thread.
75 void WillLoadPluginsCallback(
76 base::SequencedWorkerPool::SequenceToken token) {
77 if (LoadPluginListInProcess()) {
78 CHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
79 token));
80 } else {
81 CHECK(false) << "Plugin loading should happen out-of-process.";
85 #if defined(OS_MACOSX)
86 void NotifyPluginsOfActivation() {
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
89 for (PluginProcessHostIterator iter; !iter.Done(); ++iter)
90 iter->OnAppActivation();
92 #endif
94 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
95 void NotifyPluginDirChanged(const base::FilePath& path, bool error) {
96 if (error) {
97 // TODO(pastarmovj): Add some sensible error handling. Maybe silently
98 // stopping the watcher would be enough. Or possibly restart it.
99 NOTREACHED();
100 return;
102 VLOG(1) << "Watched path changed: " << path.value();
103 // Make the plugin list update itself
104 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
105 BrowserThread::PostTask(
106 BrowserThread::UI, FROM_HERE,
107 base::Bind(&PluginService::PurgePluginListCache,
108 static_cast<BrowserContext*>(NULL), false));
110 #endif
112 } // namespace
114 // static
115 PluginService* PluginService::GetInstance() {
116 return PluginServiceImpl::GetInstance();
119 void PluginService::PurgePluginListCache(BrowserContext* browser_context,
120 bool reload_pages) {
121 for (RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator();
122 !it.IsAtEnd(); it.Advance()) {
123 RenderProcessHost* host = it.GetCurrentValue();
124 if (!browser_context || host->GetBrowserContext() == browser_context)
125 host->Send(new ViewMsg_PurgePluginListCache(reload_pages));
129 // static
130 PluginServiceImpl* PluginServiceImpl::GetInstance() {
131 return Singleton<PluginServiceImpl>::get();
134 PluginServiceImpl::PluginServiceImpl()
135 : plugin_list_(NULL), filter_(NULL) {
136 // Collect the total number of browser processes (which create
137 // PluginServiceImpl objects, to be precise). The number is used to normalize
138 // the number of processes which start at least one NPAPI/PPAPI Flash process.
139 static bool counted = false;
140 if (!counted) {
141 counted = true;
142 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage", TOTAL_BROWSER_PROCESSES,
143 FLASH_USAGE_ENUM_COUNT);
147 PluginServiceImpl::~PluginServiceImpl() {
148 #if defined(OS_WIN)
149 // Release the events since they're owned by RegKey, not WaitableEvent.
150 hkcu_watcher_.StopWatching();
151 hklm_watcher_.StopWatching();
152 if (hkcu_event_)
153 hkcu_event_->Release();
154 if (hklm_event_)
155 hklm_event_->Release();
156 #endif
157 // Make sure no plugin channel requests have been leaked.
158 DCHECK(pending_plugin_clients_.empty());
161 void PluginServiceImpl::Init() {
162 if (!plugin_list_)
163 plugin_list_ = webkit::npapi::PluginList::Singleton();
165 plugin_list_token_ = BrowserThread::GetBlockingPool()->GetSequenceToken();
166 plugin_list_->set_will_load_plugins_callback(
167 base::Bind(&WillLoadPluginsCallback, plugin_list_token_));
169 RegisterPepperPlugins();
171 // The --site-per-process flag enables an out-of-process iframes
172 // prototype, which uses WebView for rendering. We need to register the MIME
173 // type we use with the plugin, so the renderer can instantiate it.
174 const CommandLine* command_line = CommandLine::ForCurrentProcess();
175 if (command_line->HasSwitch(switches::kSitePerProcess)) {
176 webkit::WebPluginInfo webview_plugin(
177 ASCIIToUTF16("WebView Tag"),
178 base::FilePath(),
179 ASCIIToUTF16("1.2.3.4"),
180 ASCIIToUTF16("Browser Plugin."));
181 webview_plugin.type = webkit::WebPluginInfo::PLUGIN_TYPE_NPAPI;
182 webkit::WebPluginMimeType webview_plugin_mime_type;
183 webview_plugin_mime_type.mime_type = "application/browser-plugin";
184 webview_plugin_mime_type.file_extensions.push_back("*");
185 webview_plugin.mime_types.push_back(webview_plugin_mime_type);
186 RegisterInternalPlugin(webview_plugin, true);
189 GetContentClient()->AddNPAPIPlugins(plugin_list_);
191 // Load any specified on the command line as well.
192 base::FilePath path =
193 command_line->GetSwitchValuePath(switches::kLoadPlugin);
194 if (!path.empty())
195 AddExtraPluginPath(path);
196 path = command_line->GetSwitchValuePath(switches::kExtraPluginDir);
197 if (!path.empty())
198 plugin_list_->AddExtraPluginDir(path);
200 if (command_line->HasSwitch(switches::kDisablePluginsDiscovery))
201 plugin_list_->DisablePluginsDiscovery();
204 void PluginServiceImpl::StartWatchingPlugins() {
205 // Start watching for changes in the plugin list. This means watching
206 // for changes in the Windows registry keys and on both Windows and POSIX
207 // watch for changes in the paths that are expected to contain plugins.
208 #if defined(OS_WIN)
209 if (hkcu_key_.Create(HKEY_CURRENT_USER,
210 webkit::npapi::kRegistryMozillaPlugins,
211 KEY_NOTIFY) == ERROR_SUCCESS) {
212 if (hkcu_key_.StartWatching() == ERROR_SUCCESS) {
213 hkcu_event_.reset(new base::WaitableEvent(hkcu_key_.watch_event()));
214 base::WaitableEventWatcher::EventCallback callback =
215 base::Bind(&PluginServiceImpl::OnWaitableEventSignaled,
216 base::Unretained(this));
217 hkcu_watcher_.StartWatching(hkcu_event_.get(), callback);
220 if (hklm_key_.Create(HKEY_LOCAL_MACHINE,
221 webkit::npapi::kRegistryMozillaPlugins,
222 KEY_NOTIFY) == ERROR_SUCCESS) {
223 if (hklm_key_.StartWatching() == ERROR_SUCCESS) {
224 hklm_event_.reset(new base::WaitableEvent(hklm_key_.watch_event()));
225 base::WaitableEventWatcher::EventCallback callback =
226 base::Bind(&PluginServiceImpl::OnWaitableEventSignaled,
227 base::Unretained(this));
228 hklm_watcher_.StartWatching(hklm_event_.get(), callback);
231 #endif
232 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
233 // On ChromeOS the user can't install plugins anyway and on Windows all
234 // important plugins register themselves in the registry so no need to do that.
236 // Get the list of all paths for registering the FilePathWatchers
237 // that will track and if needed reload the list of plugins on runtime.
238 std::vector<base::FilePath> plugin_dirs;
239 plugin_list_->GetPluginDirectories(&plugin_dirs);
241 for (size_t i = 0; i < plugin_dirs.size(); ++i) {
242 // FilePathWatcher can not handle non-absolute paths under windows.
243 // We don't watch for file changes in windows now but if this should ever
244 // be extended to Windows these lines might save some time of debugging.
245 #if defined(OS_WIN)
246 if (!plugin_dirs[i].IsAbsolute())
247 continue;
248 #endif
249 FilePathWatcher* watcher = new FilePathWatcher();
250 VLOG(1) << "Watching for changes in: " << plugin_dirs[i].value();
251 BrowserThread::PostTask(
252 BrowserThread::FILE, FROM_HERE,
253 base::Bind(&PluginServiceImpl::RegisterFilePathWatcher, watcher,
254 plugin_dirs[i]));
255 file_watchers_.push_back(watcher);
257 #endif
260 PluginProcessHost* PluginServiceImpl::FindNpapiPluginProcess(
261 const base::FilePath& plugin_path) {
262 for (PluginProcessHostIterator iter; !iter.Done(); ++iter) {
263 if (iter->info().path == plugin_path)
264 return *iter;
267 return NULL;
270 PpapiPluginProcessHost* PluginServiceImpl::FindPpapiPluginProcess(
271 const base::FilePath& plugin_path,
272 const base::FilePath& profile_data_directory) {
273 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
274 if (iter->plugin_path() == plugin_path &&
275 iter->profile_data_directory() == profile_data_directory) {
276 return *iter;
279 return NULL;
282 PpapiPluginProcessHost* PluginServiceImpl::FindPpapiBrokerProcess(
283 const base::FilePath& broker_path) {
284 for (PpapiBrokerProcessHostIterator iter; !iter.Done(); ++iter) {
285 if (iter->plugin_path() == broker_path)
286 return *iter;
289 return NULL;
292 PluginProcessHost* PluginServiceImpl::FindOrStartNpapiPluginProcess(
293 int render_process_id,
294 const base::FilePath& plugin_path) {
295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
297 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path))
298 return NULL;
300 PluginProcessHost* plugin_host = FindNpapiPluginProcess(plugin_path);
301 if (plugin_host)
302 return plugin_host;
304 webkit::WebPluginInfo info;
305 if (!GetPluginInfoByPath(plugin_path, &info)) {
306 return NULL;
309 // Record when NPAPI Flash process is started for the first time.
310 static bool counted = false;
311 if (!counted && UTF16ToUTF8(info.name) == kFlashPluginName) {
312 counted = true;
313 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage",
314 START_NPAPI_FLASH_AT_LEAST_ONCE,
315 FLASH_USAGE_ENUM_COUNT);
318 // This plugin isn't loaded by any plugin process, so create a new process.
319 scoped_ptr<PluginProcessHost> new_host(new PluginProcessHost());
320 if (!new_host->Init(info)) {
321 NOTREACHED(); // Init is not expected to fail.
322 return NULL;
324 return new_host.release();
327 PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiPluginProcess(
328 int render_process_id,
329 const base::FilePath& plugin_path,
330 const base::FilePath& profile_data_directory,
331 PpapiPluginProcessHost::PluginClient* client) {
332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
334 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path))
335 return NULL;
337 PpapiPluginProcessHost* plugin_host =
338 FindPpapiPluginProcess(plugin_path, profile_data_directory);
339 if (plugin_host)
340 return plugin_host;
342 // Validate that the plugin is actually registered.
343 PepperPluginInfo* info = GetRegisteredPpapiPluginInfo(plugin_path);
344 if (!info)
345 return NULL;
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);
356 // This plugin isn't loaded by any plugin process, so create a new process.
357 return PpapiPluginProcessHost::CreatePluginHost(
358 *info, profile_data_directory,
359 client->GetResourceContext()->GetHostResolver());
362 PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiBrokerProcess(
363 int render_process_id,
364 const base::FilePath& plugin_path) {
365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
367 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path))
368 return NULL;
370 PpapiPluginProcessHost* plugin_host = FindPpapiBrokerProcess(plugin_path);
371 if (plugin_host)
372 return plugin_host;
374 // Validate that the plugin is actually registered.
375 PepperPluginInfo* info = GetRegisteredPpapiPluginInfo(plugin_path);
376 if (!info)
377 return NULL;
379 // TODO(ddorwin): Uncomment once out of process is supported.
380 // DCHECK(info->is_out_of_process);
382 // This broker isn't loaded by any broker process, so create a new process.
383 return PpapiPluginProcessHost::CreateBrokerHost(*info);
386 void PluginServiceImpl::OpenChannelToNpapiPlugin(
387 int render_process_id,
388 int render_view_id,
389 const GURL& url,
390 const GURL& page_url,
391 const std::string& mime_type,
392 PluginProcessHost::Client* client) {
393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
394 DCHECK(!ContainsKey(pending_plugin_clients_, client));
395 pending_plugin_clients_.insert(client);
397 // Make sure plugins are loaded if necessary.
398 PluginServiceFilterParams params = {
399 render_process_id,
400 render_view_id,
401 page_url,
402 client->GetResourceContext()
404 GetPlugins(base::Bind(
405 &PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin,
406 base::Unretained(this), params, url, mime_type, client));
409 void PluginServiceImpl::OpenChannelToPpapiPlugin(
410 int render_process_id,
411 const base::FilePath& plugin_path,
412 const base::FilePath& profile_data_directory,
413 PpapiPluginProcessHost::PluginClient* client) {
414 PpapiPluginProcessHost* plugin_host = FindOrStartPpapiPluginProcess(
415 render_process_id, plugin_path, profile_data_directory, client);
416 if (plugin_host) {
417 plugin_host->OpenChannelToPlugin(client);
418 } else {
419 // Send error.
420 client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0);
424 void PluginServiceImpl::OpenChannelToPpapiBroker(
425 int render_process_id,
426 const base::FilePath& path,
427 PpapiPluginProcessHost::BrokerClient* client) {
428 PpapiPluginProcessHost* plugin_host = FindOrStartPpapiBrokerProcess(
429 render_process_id, path);
430 if (plugin_host) {
431 plugin_host->OpenChannelToPlugin(client);
432 } else {
433 // Send error.
434 client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0);
438 void PluginServiceImpl::CancelOpenChannelToNpapiPlugin(
439 PluginProcessHost::Client* client) {
440 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
441 DCHECK(ContainsKey(pending_plugin_clients_, client));
442 pending_plugin_clients_.erase(client);
445 void PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin(
446 const PluginServiceFilterParams& params,
447 const GURL& url,
448 const std::string& mime_type,
449 PluginProcessHost::Client* client,
450 const std::vector<webkit::WebPluginInfo>&) {
451 GetAllowedPluginForOpenChannelToPlugin(params.render_process_id,
452 params.render_view_id, url, params.page_url, mime_type, client,
453 params.resource_context);
456 void PluginServiceImpl::GetAllowedPluginForOpenChannelToPlugin(
457 int render_process_id,
458 int render_view_id,
459 const GURL& url,
460 const GURL& page_url,
461 const std::string& mime_type,
462 PluginProcessHost::Client* client,
463 ResourceContext* resource_context) {
464 webkit::WebPluginInfo info;
465 bool allow_wildcard = true;
466 bool found = GetPluginInfo(
467 render_process_id, render_view_id, resource_context,
468 url, page_url, mime_type, allow_wildcard,
469 NULL, &info, NULL);
470 base::FilePath plugin_path;
471 if (found)
472 plugin_path = info.path;
474 // Now we jump back to the IO thread to finish opening the channel.
475 BrowserThread::PostTask(
476 BrowserThread::IO, FROM_HERE,
477 base::Bind(&PluginServiceImpl::FinishOpenChannelToPlugin,
478 base::Unretained(this),
479 render_process_id,
480 plugin_path,
481 client));
484 void PluginServiceImpl::FinishOpenChannelToPlugin(
485 int render_process_id,
486 const base::FilePath& plugin_path,
487 PluginProcessHost::Client* client) {
488 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
490 // Make sure it hasn't been canceled yet.
491 if (!ContainsKey(pending_plugin_clients_, client))
492 return;
493 pending_plugin_clients_.erase(client);
495 PluginProcessHost* plugin_host = FindOrStartNpapiPluginProcess(
496 render_process_id, plugin_path);
497 if (plugin_host) {
498 client->OnFoundPluginProcessHost(plugin_host);
499 plugin_host->OpenChannelToPlugin(client);
500 } else {
501 client->OnError();
505 bool PluginServiceImpl::GetPluginInfoArray(
506 const GURL& url,
507 const std::string& mime_type,
508 bool allow_wildcard,
509 std::vector<webkit::WebPluginInfo>* plugins,
510 std::vector<std::string>* actual_mime_types) {
511 bool use_stale = false;
512 plugin_list_->GetPluginInfoArray(url, mime_type, allow_wildcard,
513 &use_stale, plugins, actual_mime_types);
514 return use_stale;
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,
525 webkit::WebPluginInfo* info,
526 std::string* actual_mime_type) {
527 std::vector<webkit::WebPluginInfo> plugins;
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;
534 for (size_t i = 0; i < plugins.size(); ++i) {
535 if (!filter_ || filter_->IsPluginAvailable(render_process_id,
536 render_view_id,
537 context,
538 url,
539 page_url,
540 &plugins[i])) {
541 *info = plugins[i];
542 if (actual_mime_type)
543 *actual_mime_type = mime_types[i];
544 return true;
547 return false;
550 bool PluginServiceImpl::GetPluginInfoByPath(const base::FilePath& plugin_path,
551 webkit::WebPluginInfo* info) {
552 std::vector<webkit::WebPluginInfo> plugins;
553 plugin_list_->GetPluginsNoRefresh(&plugins);
555 for (std::vector<webkit::WebPluginInfo>::iterator it = plugins.begin();
556 it != plugins.end();
557 ++it) {
558 if (it->path == plugin_path) {
559 *info = *it;
560 return true;
564 return false;
567 string16 PluginServiceImpl::GetPluginDisplayNameByPath(
568 const base::FilePath& path) {
569 string16 plugin_name = path.LossyDisplayName();
570 webkit::WebPluginInfo info;
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
582 return plugin_name;
585 void PluginServiceImpl::GetPlugins(const GetPluginsCallback& callback) {
586 scoped_refptr<base::MessageLoopProxy> target_loop(
587 base::MessageLoop::current()->message_loop_proxy());
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;
600 #if defined(OS_POSIX)
601 std::vector<webkit::WebPluginInfo> cached_plugins;
602 if (plugin_list_->GetPluginsNoRefresh(&cached_plugins)) {
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.
609 if (!plugin_loader_.get())
610 plugin_loader_ = new PluginLoaderPosix;
611 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
612 base::Bind(&PluginLoaderPosix::LoadPlugins, plugin_loader_,
613 target_loop, callback));
615 #else
616 NOTREACHED();
617 #endif
620 void PluginServiceImpl::GetPluginsInternal(
621 base::MessageLoopProxy* target_loop,
622 const PluginService::GetPluginsCallback& callback) {
623 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
624 plugin_list_token_));
626 std::vector<webkit::WebPluginInfo> plugins;
627 plugin_list_->GetPlugins(&plugins);
629 target_loop->PostTask(FROM_HERE,
630 base::Bind(callback, plugins));
633 void PluginServiceImpl::OnWaitableEventSignaled(
634 base::WaitableEvent* waitable_event) {
635 #if defined(OS_WIN)
636 if (waitable_event == hkcu_event_) {
637 hkcu_key_.StartWatching();
638 } else {
639 hklm_key_.StartWatching();
642 plugin_list_->RefreshPlugins();
643 PurgePluginListCache(NULL, false);
644 #else
645 // This event should only get signaled on a Windows machine.
646 NOTREACHED();
647 #endif // defined(OS_WIN)
650 void PluginServiceImpl::RegisterPepperPlugins() {
651 // TODO(abarth): It seems like the PepperPluginRegistry should do this work.
652 PepperPluginRegistry::ComputeList(&ppapi_plugins_);
653 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
654 RegisterInternalPlugin(ppapi_plugins_[i].ToWebPluginInfo(), true);
658 // There should generally be very few plugins so a brute-force search is fine.
659 PepperPluginInfo* PluginServiceImpl::GetRegisteredPpapiPluginInfo(
660 const base::FilePath& plugin_path) {
661 PepperPluginInfo* info = NULL;
662 for (size_t i = 0; i < ppapi_plugins_.size(); i++) {
663 if (ppapi_plugins_[i].path == plugin_path) {
664 info = &ppapi_plugins_[i];
665 break;
668 if (info)
669 return info;
670 // We did not find the plugin in our list. But wait! the plugin can also
671 // be a latecomer, as it happens with pepper flash. This information
672 // can be obtained from the PluginList singleton and we can use it to
673 // construct it and add it to the list. This same deal needs to be done
674 // in the renderer side in PepperPluginRegistry.
675 webkit::WebPluginInfo webplugin_info;
676 if (!GetPluginInfoByPath(plugin_path, &webplugin_info))
677 return NULL;
678 PepperPluginInfo new_pepper_info;
679 if (!MakePepperPluginInfo(webplugin_info, &new_pepper_info))
680 return NULL;
681 ppapi_plugins_.push_back(new_pepper_info);
682 return &ppapi_plugins_[ppapi_plugins_.size() - 1];
685 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
686 // static
687 void PluginServiceImpl::RegisterFilePathWatcher(FilePathWatcher* watcher,
688 const base::FilePath& path) {
689 bool result = watcher->Watch(path, false,
690 base::Bind(&NotifyPluginDirChanged));
691 DCHECK(result);
693 #endif
695 void PluginServiceImpl::SetFilter(PluginServiceFilter* filter) {
696 filter_ = filter;
699 PluginServiceFilter* PluginServiceImpl::GetFilter() {
700 return filter_;
703 void PluginServiceImpl::ForcePluginShutdown(const base::FilePath& plugin_path) {
704 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
705 BrowserThread::PostTask(
706 BrowserThread::IO, FROM_HERE,
707 base::Bind(&PluginServiceImpl::ForcePluginShutdown,
708 base::Unretained(this), plugin_path));
709 return;
712 PluginProcessHost* plugin = FindNpapiPluginProcess(plugin_path);
713 if (plugin)
714 plugin->ForceShutdown();
717 static const unsigned int kMaxCrashesPerInterval = 3;
718 static const unsigned int kCrashesInterval = 120;
720 void PluginServiceImpl::RegisterPluginCrash(const base::FilePath& path) {
721 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
722 std::map<base::FilePath, std::vector<base::Time> >::iterator i =
723 crash_times_.find(path);
724 if (i == crash_times_.end()) {
725 crash_times_[path] = std::vector<base::Time>();
726 i = crash_times_.find(path);
728 if (i->second.size() == kMaxCrashesPerInterval) {
729 i->second.erase(i->second.begin());
731 base::Time time = base::Time::Now();
732 i->second.push_back(time);
735 bool PluginServiceImpl::IsPluginUnstable(const base::FilePath& path) {
736 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
737 std::map<base::FilePath, std::vector<base::Time> >::const_iterator i =
738 crash_times_.find(path);
739 if (i == crash_times_.end()) {
740 return false;
742 if (i->second.size() != kMaxCrashesPerInterval) {
743 return false;
745 base::TimeDelta delta = base::Time::Now() - i->second[0];
746 return delta.InSeconds() <= kCrashesInterval;
749 void PluginServiceImpl::RefreshPlugins() {
750 plugin_list_->RefreshPlugins();
753 void PluginServiceImpl::AddExtraPluginPath(const base::FilePath& path) {
754 plugin_list_->AddExtraPluginPath(path);
757 void PluginServiceImpl::AddExtraPluginDir(const base::FilePath& path) {
758 plugin_list_->AddExtraPluginDir(path);
761 void PluginServiceImpl::RemoveExtraPluginPath(const base::FilePath& path) {
762 plugin_list_->RemoveExtraPluginPath(path);
765 void PluginServiceImpl::UnregisterInternalPlugin(const base::FilePath& path) {
766 plugin_list_->UnregisterInternalPlugin(path);
769 void PluginServiceImpl::SetPluginListForTesting(
770 webkit::npapi::PluginList* plugin_list) {
771 plugin_list_ = plugin_list;
774 #if defined(OS_MACOSX)
775 void PluginServiceImpl::AppActivated() {
776 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
777 base::Bind(&NotifyPluginsOfActivation));
779 #endif
781 void PluginServiceImpl::RegisterInternalPlugin(
782 const webkit::WebPluginInfo& info,
783 bool add_at_beginning) {
784 plugin_list_->RegisterInternalPlugin(info, add_at_beginning);
787 void PluginServiceImpl::GetInternalPlugins(
788 std::vector<webkit::WebPluginInfo>* plugins) {
789 plugin_list_->GetInternalPlugins(plugins);
792 webkit::npapi::PluginList* PluginServiceImpl::GetPluginList() {
793 return plugin_list_;
796 } // namespace content