Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / plugin_service_impl.cc
blob02c6ff9f8fc0b93f084fadcf0e5359ce649d98c4
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/location.h"
12 #include "base/metrics/histogram.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/thread.h"
18 #include "content/browser/ppapi_plugin_process_host.h"
19 #include "content/browser/renderer_host/render_process_host_impl.h"
20 #include "content/browser/renderer_host/render_view_host_impl.h"
21 #include "content/common/content_switches_internal.h"
22 #include "content/common/pepper_plugin_list.h"
23 #include "content/common/plugin_list.h"
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"
29 #include "content/public/common/content_constants.h"
30 #include "content/public/common/content_switches.h"
31 #include "content/public/common/process_type.h"
32 #include "content/public/common/webplugininfo.h"
34 #if defined(OS_WIN)
35 #include "content/common/plugin_constants_win.h"
36 #include "ui/gfx/win/hwnd_util.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 enum NPAPIPluginStatus {
64 // Platform does not support NPAPI.
65 NPAPI_STATUS_UNSUPPORTED,
66 // Platform supports NPAPI and NPAPI is disabled.
67 NPAPI_STATUS_DISABLED,
68 // Platform supports NPAPI and NPAPI is enabled.
69 NPAPI_STATUS_ENABLED,
70 NPAPI_STATUS_ENUM_COUNT
73 bool LoadPluginListInProcess() {
74 #if defined(OS_WIN)
75 return true;
76 #else
77 // If on POSIX, we don't want to load the list of NPAPI plugins in-process as
78 // that causes instability.
80 // Can't load the plugins on the utility thread when in single process mode
81 // since that requires GTK which can only be used on the main thread.
82 if (RenderProcessHost::run_renderer_in_process())
83 return true;
85 return !PluginService::GetInstance()->NPAPIPluginsSupported();
86 #endif
89 // Callback set on the PluginList to assert that plugin loading happens on the
90 // correct thread.
91 void WillLoadPluginsCallback(
92 base::SequencedWorkerPool::SequenceToken token) {
93 if (LoadPluginListInProcess()) {
94 CHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
95 token));
96 } else {
97 CHECK(false) << "Plugin loading should happen out-of-process.";
101 #if defined(OS_MACOSX)
102 void NotifyPluginsOfActivation() {
103 DCHECK_CURRENTLY_ON(BrowserThread::IO);
105 for (PluginProcessHostIterator iter; !iter.Done(); ++iter)
106 iter->OnAppActivation();
108 #endif
110 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
111 void NotifyPluginDirChanged(const base::FilePath& path, bool error) {
112 if (error) {
113 // TODO(pastarmovj): Add some sensible error handling. Maybe silently
114 // stopping the watcher would be enough. Or possibly restart it.
115 NOTREACHED();
116 return;
118 VLOG(1) << "Watched path changed: " << path.value();
119 // Make the plugin list update itself
120 PluginList::Singleton()->RefreshPlugins();
121 BrowserThread::PostTask(
122 BrowserThread::UI, FROM_HERE,
123 base::Bind(&PluginService::PurgePluginListCache,
124 static_cast<BrowserContext*>(NULL), false));
126 #endif
128 void ForwardCallback(base::SingleThreadTaskRunner* target_task_runner,
129 const PluginService::GetPluginsCallback& callback,
130 const std::vector<WebPluginInfo>& plugins) {
131 target_task_runner->PostTask(FROM_HERE, base::Bind(callback, plugins));
134 } // namespace
136 // static
137 PluginService* PluginService::GetInstance() {
138 return PluginServiceImpl::GetInstance();
141 void PluginService::PurgePluginListCache(BrowserContext* browser_context,
142 bool reload_pages) {
143 for (RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator();
144 !it.IsAtEnd(); it.Advance()) {
145 RenderProcessHost* host = it.GetCurrentValue();
146 if (!browser_context || host->GetBrowserContext() == browser_context)
147 host->Send(new ViewMsg_PurgePluginListCache(reload_pages));
151 // static
152 PluginServiceImpl* PluginServiceImpl::GetInstance() {
153 return Singleton<PluginServiceImpl>::get();
156 PluginServiceImpl::PluginServiceImpl()
157 : npapi_plugins_enabled_(false), filter_(NULL) {
158 // Collect the total number of browser processes (which create
159 // PluginServiceImpl objects, to be precise). The number is used to normalize
160 // the number of processes which start at least one NPAPI/PPAPI Flash process.
161 static bool counted = false;
162 if (!counted) {
163 counted = true;
164 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage", TOTAL_BROWSER_PROCESSES,
165 FLASH_USAGE_ENUM_COUNT);
169 PluginServiceImpl::~PluginServiceImpl() {
170 // Make sure no plugin channel requests have been leaked.
171 DCHECK(pending_plugin_clients_.empty());
174 void PluginServiceImpl::Init() {
175 plugin_list_token_ = BrowserThread::GetBlockingPool()->GetSequenceToken();
176 PluginList::Singleton()->set_will_load_plugins_callback(
177 base::Bind(&WillLoadPluginsCallback, plugin_list_token_));
179 RegisterPepperPlugins();
181 // Load any specified on the command line as well.
182 const base::CommandLine* command_line =
183 base::CommandLine::ForCurrentProcess();
184 base::FilePath path =
185 command_line->GetSwitchValuePath(switches::kLoadPlugin);
186 if (!path.empty())
187 AddExtraPluginPath(path);
188 path = command_line->GetSwitchValuePath(switches::kExtraPluginDir);
189 if (!path.empty())
190 PluginList::Singleton()->AddExtraPluginDir(path);
192 if (command_line->HasSwitch(switches::kDisablePluginsDiscovery))
193 PluginList::Singleton()->DisablePluginsDiscovery();
196 void PluginServiceImpl::StartWatchingPlugins() {
197 // Start watching for changes in the plugin list. This means watching
198 // for changes in the Windows registry keys and on both Windows and POSIX
199 // watch for changes in the paths that are expected to contain plugins.
200 #if defined(OS_WIN)
201 if (hkcu_key_.Create(HKEY_CURRENT_USER,
202 kRegistryMozillaPlugins,
203 KEY_NOTIFY) == ERROR_SUCCESS) {
204 base::win::RegKey::ChangeCallback callback =
205 base::Bind(&PluginServiceImpl::OnKeyChanged, base::Unretained(this),
206 base::Unretained(&hkcu_key_));
207 hkcu_key_.StartWatching(callback);
209 if (hklm_key_.Create(HKEY_LOCAL_MACHINE,
210 kRegistryMozillaPlugins,
211 KEY_NOTIFY) == ERROR_SUCCESS) {
212 base::win::RegKey::ChangeCallback callback =
213 base::Bind(&PluginServiceImpl::OnKeyChanged, base::Unretained(this),
214 base::Unretained(&hklm_key_));
215 hklm_key_.StartWatching(callback);
217 #endif
218 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
219 // On ChromeOS the user can't install plugins anyway and on Windows all
220 // important plugins register themselves in the registry so no need to do that.
222 // Get the list of all paths for registering the FilePathWatchers
223 // that will track and if needed reload the list of plugins on runtime.
224 std::vector<base::FilePath> plugin_dirs;
225 PluginList::Singleton()->GetPluginDirectories(&plugin_dirs);
227 for (size_t i = 0; i < plugin_dirs.size(); ++i) {
228 // FilePathWatcher can not handle non-absolute paths under windows.
229 // We don't watch for file changes in windows now but if this should ever
230 // be extended to Windows these lines might save some time of debugging.
231 #if defined(OS_WIN)
232 if (!plugin_dirs[i].IsAbsolute())
233 continue;
234 #endif
235 FilePathWatcher* watcher = new FilePathWatcher();
236 VLOG(1) << "Watching for changes in: " << plugin_dirs[i].value();
237 BrowserThread::PostTask(
238 BrowserThread::FILE, FROM_HERE,
239 base::Bind(&PluginServiceImpl::RegisterFilePathWatcher, watcher,
240 plugin_dirs[i]));
241 file_watchers_.push_back(watcher);
243 #endif
246 PluginProcessHost* PluginServiceImpl::FindNpapiPluginProcess(
247 const base::FilePath& plugin_path) {
248 for (PluginProcessHostIterator iter; !iter.Done(); ++iter) {
249 if (iter->info().path == plugin_path)
250 return *iter;
253 return NULL;
256 PpapiPluginProcessHost* PluginServiceImpl::FindPpapiPluginProcess(
257 const base::FilePath& plugin_path,
258 const base::FilePath& profile_data_directory) {
259 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
260 if (iter->plugin_path() == plugin_path &&
261 iter->profile_data_directory() == profile_data_directory) {
262 return *iter;
265 return NULL;
268 PpapiPluginProcessHost* PluginServiceImpl::FindPpapiBrokerProcess(
269 const base::FilePath& broker_path) {
270 for (PpapiBrokerProcessHostIterator iter; !iter.Done(); ++iter) {
271 if (iter->plugin_path() == broker_path)
272 return *iter;
275 return NULL;
278 PluginProcessHost* PluginServiceImpl::FindOrStartNpapiPluginProcess(
279 int render_process_id,
280 const base::FilePath& plugin_path) {
281 DCHECK_CURRENTLY_ON(BrowserThread::IO);
283 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path))
284 return NULL;
286 PluginProcessHost* plugin_host = FindNpapiPluginProcess(plugin_path);
287 if (plugin_host)
288 return plugin_host;
290 WebPluginInfo info;
291 if (!GetPluginInfoByPath(plugin_path, &info)) {
292 return NULL;
295 // Record when NPAPI Flash process is started for the first time.
296 static bool counted = false;
297 if (!counted && base::UTF16ToUTF8(info.name) == kFlashPluginName) {
298 counted = true;
299 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage",
300 START_NPAPI_FLASH_AT_LEAST_ONCE,
301 FLASH_USAGE_ENUM_COUNT);
303 #if defined(OS_CHROMEOS)
304 // TODO(ihf): Move to an earlier place once crbug.com/314301 is fixed. For now
305 // we still want Plugin.FlashUsage recorded if we end up here.
306 LOG(WARNING) << "Refusing to start npapi plugin on ChromeOS.";
307 return NULL;
308 #endif
309 // This plugin isn't loaded by any plugin process, so create a new process.
310 scoped_ptr<PluginProcessHost> new_host(new PluginProcessHost());
311 if (!new_host->Init(info)) {
312 NOTREACHED(); // Init is not expected to fail.
313 return NULL;
315 return new_host.release();
318 PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiPluginProcess(
319 int render_process_id,
320 const base::FilePath& plugin_path,
321 const base::FilePath& profile_data_directory) {
322 DCHECK_CURRENTLY_ON(BrowserThread::IO);
324 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path)) {
325 VLOG(1) << "Unable to load ppapi plugin: " << plugin_path.MaybeAsASCII();
326 return NULL;
329 PpapiPluginProcessHost* plugin_host =
330 FindPpapiPluginProcess(plugin_path, profile_data_directory);
331 if (plugin_host)
332 return plugin_host;
334 // Validate that the plugin is actually registered.
335 PepperPluginInfo* info = GetRegisteredPpapiPluginInfo(plugin_path);
336 if (!info) {
337 VLOG(1) << "Unable to find ppapi plugin registration for: "
338 << plugin_path.MaybeAsASCII();
339 return NULL;
342 // Record when PPAPI Flash process is started for the first time.
343 static bool counted = false;
344 if (!counted && info->name == kFlashPluginName) {
345 counted = true;
346 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage",
347 START_PPAPI_FLASH_AT_LEAST_ONCE,
348 FLASH_USAGE_ENUM_COUNT);
351 // This plugin isn't loaded by any plugin process, so create a new process.
352 plugin_host = PpapiPluginProcessHost::CreatePluginHost(
353 *info, profile_data_directory);
354 if (!plugin_host) {
355 VLOG(1) << "Unable to create ppapi plugin process for: "
356 << plugin_path.MaybeAsASCII();
359 return plugin_host;
362 PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiBrokerProcess(
363 int render_process_id,
364 const base::FilePath& plugin_path) {
365 DCHECK_CURRENTLY_ON(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_frame_id,
389 const GURL& url,
390 const GURL& page_url,
391 const std::string& mime_type,
392 PluginProcessHost::Client* client) {
393 DCHECK_CURRENTLY_ON(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_frame_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);
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_CURRENTLY_ON(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<WebPluginInfo>&) {
451 GetAllowedPluginForOpenChannelToPlugin(
452 params.render_process_id, params.render_frame_id, url, params.page_url,
453 mime_type, client, params.resource_context);
456 void PluginServiceImpl::GetAllowedPluginForOpenChannelToPlugin(
457 int render_process_id,
458 int render_frame_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 WebPluginInfo info;
465 bool allow_wildcard = true;
466 bool found = GetPluginInfo(
467 render_process_id, render_frame_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));
482 if (filter_) {
483 DCHECK_EQ(WebPluginInfo::PLUGIN_TYPE_NPAPI, info.type);
484 filter_->NPAPIPluginLoaded(render_process_id, render_frame_id, mime_type,
485 info);
489 void PluginServiceImpl::FinishOpenChannelToPlugin(
490 int render_process_id,
491 const base::FilePath& plugin_path,
492 PluginProcessHost::Client* client) {
493 DCHECK_CURRENTLY_ON(BrowserThread::IO);
495 // Make sure it hasn't been canceled yet.
496 if (!ContainsKey(pending_plugin_clients_, client))
497 return;
498 pending_plugin_clients_.erase(client);
500 PluginProcessHost* plugin_host = FindOrStartNpapiPluginProcess(
501 render_process_id, plugin_path);
502 if (plugin_host) {
503 client->OnFoundPluginProcessHost(plugin_host);
504 plugin_host->OpenChannelToPlugin(client);
505 } else {
506 client->OnError();
510 bool PluginServiceImpl::GetPluginInfoArray(
511 const GURL& url,
512 const std::string& mime_type,
513 bool allow_wildcard,
514 std::vector<WebPluginInfo>* plugins,
515 std::vector<std::string>* actual_mime_types) {
516 bool use_stale = false;
517 PluginList::Singleton()->GetPluginInfoArray(
518 url, mime_type, allow_wildcard, &use_stale, NPAPIPluginsSupported(),
519 plugins, actual_mime_types);
520 return use_stale;
523 bool PluginServiceImpl::GetPluginInfo(int render_process_id,
524 int render_frame_id,
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,
531 WebPluginInfo* info,
532 std::string* actual_mime_type) {
533 std::vector<WebPluginInfo> plugins;
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;
540 for (size_t i = 0; i < plugins.size(); ++i) {
541 if (!filter_ || filter_->IsPluginAvailable(render_process_id,
542 render_frame_id,
543 context,
544 url,
545 page_url,
546 &plugins[i])) {
547 *info = plugins[i];
548 if (actual_mime_type)
549 *actual_mime_type = mime_types[i];
550 return true;
553 return false;
556 bool PluginServiceImpl::GetPluginInfoByPath(const base::FilePath& plugin_path,
557 WebPluginInfo* info) {
558 std::vector<WebPluginInfo> plugins;
559 PluginList::Singleton()->GetPluginsNoRefresh(&plugins);
561 for (std::vector<WebPluginInfo>::iterator it = plugins.begin();
562 it != plugins.end();
563 ++it) {
564 if (it->path == plugin_path) {
565 *info = *it;
566 return true;
570 return false;
573 base::string16 PluginServiceImpl::GetPluginDisplayNameByPath(
574 const base::FilePath& path) {
575 base::string16 plugin_name = path.LossyDisplayName();
576 WebPluginInfo info;
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";
584 if (base::EndsWith(plugin_name, base::ASCIIToUTF16(kPluginExtension),
585 base::CompareCase::SENSITIVE))
586 plugin_name.erase(plugin_name.length() - kPluginExtension.length());
587 #endif // OS_MACOSX
589 return plugin_name;
592 void PluginServiceImpl::GetPlugins(const GetPluginsCallback& callback) {
593 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner(
594 base::ThreadTaskRunnerHandle::Get());
596 if (LoadPluginListInProcess()) {
597 BrowserThread::GetBlockingPool()
598 ->PostSequencedWorkerTaskWithShutdownBehavior(
599 plugin_list_token_, FROM_HERE,
600 base::Bind(&PluginServiceImpl::GetPluginsInternal,
601 base::Unretained(this), target_task_runner, callback),
602 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
603 return;
605 #if defined(OS_POSIX)
606 BrowserThread::PostTask(
607 BrowserThread::IO, FROM_HERE,
608 base::Bind(&PluginServiceImpl::GetPluginsOnIOThread,
609 base::Unretained(this), target_task_runner, callback));
610 #else
611 NOTREACHED();
612 #endif
615 void PluginServiceImpl::GetPluginsInternal(
616 base::SingleThreadTaskRunner* target_task_runner,
617 const PluginService::GetPluginsCallback& callback) {
618 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
619 plugin_list_token_));
621 std::vector<WebPluginInfo> plugins;
622 PluginList::Singleton()->GetPlugins(&plugins, NPAPIPluginsSupported());
624 target_task_runner->PostTask(FROM_HERE, base::Bind(callback, plugins));
627 #if defined(OS_POSIX)
628 void PluginServiceImpl::GetPluginsOnIOThread(
629 base::SingleThreadTaskRunner* target_task_runner,
630 const GetPluginsCallback& callback) {
631 DCHECK_CURRENTLY_ON(BrowserThread::IO);
633 // If we switch back to loading plugins in process, then we need to make
634 // sure g_thread_init() gets called since plugins may call glib at load.
636 if (!plugin_loader_.get())
637 plugin_loader_ = new PluginLoaderPosix;
639 plugin_loader_->GetPlugins(base::Bind(
640 &ForwardCallback, make_scoped_refptr(target_task_runner), callback));
642 #endif
644 #if defined(OS_WIN)
645 void PluginServiceImpl::OnKeyChanged(base::win::RegKey* key) {
646 key->StartWatching(base::Bind(&PluginServiceImpl::OnKeyChanged,
647 base::Unretained(this),
648 base::Unretained(key)));
650 PluginList::Singleton()->RefreshPlugins();
651 PurgePluginListCache(NULL, false);
653 #endif // defined(OS_WIN)
655 void PluginServiceImpl::RegisterPepperPlugins() {
656 ComputePepperPluginList(&ppapi_plugins_);
657 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
658 RegisterInternalPlugin(ppapi_plugins_[i].ToWebPluginInfo(), true);
662 // There should generally be very few plugins so a brute-force search is fine.
663 PepperPluginInfo* PluginServiceImpl::GetRegisteredPpapiPluginInfo(
664 const base::FilePath& plugin_path) {
665 PepperPluginInfo* info = NULL;
666 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
667 if (ppapi_plugins_[i].path == plugin_path) {
668 info = &ppapi_plugins_[i];
669 break;
672 if (info)
673 return info;
674 // We did not find the plugin in our list. But wait! the plugin can also
675 // be a latecomer, as it happens with pepper flash. This information
676 // can be obtained from the PluginList singleton and we can use it to
677 // construct it and add it to the list. This same deal needs to be done
678 // in the renderer side in PepperPluginRegistry.
679 WebPluginInfo webplugin_info;
680 if (!GetPluginInfoByPath(plugin_path, &webplugin_info))
681 return NULL;
682 PepperPluginInfo new_pepper_info;
683 if (!MakePepperPluginInfo(webplugin_info, &new_pepper_info))
684 return NULL;
685 ppapi_plugins_.push_back(new_pepper_info);
686 return &ppapi_plugins_[ppapi_plugins_.size() - 1];
689 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
690 // static
691 void PluginServiceImpl::RegisterFilePathWatcher(FilePathWatcher* watcher,
692 const base::FilePath& path) {
693 bool result = watcher->Watch(path, false,
694 base::Bind(&NotifyPluginDirChanged));
695 DCHECK(result);
697 #endif
699 void PluginServiceImpl::SetFilter(PluginServiceFilter* filter) {
700 filter_ = filter;
703 PluginServiceFilter* PluginServiceImpl::GetFilter() {
704 return filter_;
707 void PluginServiceImpl::ForcePluginShutdown(const base::FilePath& plugin_path) {
708 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
709 BrowserThread::PostTask(
710 BrowserThread::IO, FROM_HERE,
711 base::Bind(&PluginServiceImpl::ForcePluginShutdown,
712 base::Unretained(this), plugin_path));
713 return;
716 PluginProcessHost* plugin = FindNpapiPluginProcess(plugin_path);
717 if (plugin)
718 plugin->ForceShutdown();
721 static const unsigned int kMaxCrashesPerInterval = 3;
722 static const unsigned int kCrashesInterval = 120;
724 void PluginServiceImpl::RegisterPluginCrash(const base::FilePath& path) {
725 DCHECK_CURRENTLY_ON(BrowserThread::IO);
726 std::map<base::FilePath, std::vector<base::Time> >::iterator i =
727 crash_times_.find(path);
728 if (i == crash_times_.end()) {
729 crash_times_[path] = std::vector<base::Time>();
730 i = crash_times_.find(path);
732 if (i->second.size() == kMaxCrashesPerInterval) {
733 i->second.erase(i->second.begin());
735 base::Time time = base::Time::Now();
736 i->second.push_back(time);
739 bool PluginServiceImpl::IsPluginUnstable(const base::FilePath& path) {
740 DCHECK_CURRENTLY_ON(BrowserThread::IO);
741 std::map<base::FilePath, std::vector<base::Time> >::const_iterator i =
742 crash_times_.find(path);
743 if (i == crash_times_.end()) {
744 return false;
746 if (i->second.size() != kMaxCrashesPerInterval) {
747 return false;
749 base::TimeDelta delta = base::Time::Now() - i->second[0];
750 return delta.InSeconds() <= kCrashesInterval;
753 void PluginServiceImpl::RefreshPlugins() {
754 PluginList::Singleton()->RefreshPlugins();
757 void PluginServiceImpl::AddExtraPluginPath(const base::FilePath& path) {
758 if (!NPAPIPluginsSupported()) {
759 // TODO(jam): remove and just have CHECK once we're sure this doesn't get
760 // triggered.
761 DVLOG(0) << "NPAPI plugins not supported";
762 return;
764 PluginList::Singleton()->AddExtraPluginPath(path);
767 void PluginServiceImpl::RemoveExtraPluginPath(const base::FilePath& path) {
768 PluginList::Singleton()->RemoveExtraPluginPath(path);
771 void PluginServiceImpl::AddExtraPluginDir(const base::FilePath& path) {
772 PluginList::Singleton()->AddExtraPluginDir(path);
775 void PluginServiceImpl::RegisterInternalPlugin(
776 const WebPluginInfo& info,
777 bool add_at_beginning) {
778 // Internal plugins should never be NPAPI.
779 CHECK_NE(info.type, WebPluginInfo::PLUGIN_TYPE_NPAPI);
780 if (info.type == WebPluginInfo::PLUGIN_TYPE_NPAPI) {
781 DVLOG(0) << "Don't register NPAPI plugins when they're not supported";
782 return;
784 PluginList::Singleton()->RegisterInternalPlugin(info, add_at_beginning);
787 void PluginServiceImpl::UnregisterInternalPlugin(const base::FilePath& path) {
788 PluginList::Singleton()->UnregisterInternalPlugin(path);
791 void PluginServiceImpl::GetInternalPlugins(
792 std::vector<WebPluginInfo>* plugins) {
793 PluginList::Singleton()->GetInternalPlugins(plugins);
796 bool PluginServiceImpl::NPAPIPluginsSupported() {
797 #if defined(OS_WIN) || defined(OS_MACOSX)
798 npapi_plugins_enabled_ = GetContentClient()->browser()->IsNPAPIEnabled();
799 #if defined(OS_WIN)
800 // NPAPI plugins don't play well with Win32k renderer lockdown.
801 if (npapi_plugins_enabled_)
802 DisableWin32kRendererLockdown();
803 #endif
804 NPAPIPluginStatus status =
805 npapi_plugins_enabled_ ? NPAPI_STATUS_ENABLED : NPAPI_STATUS_DISABLED;
806 #else
807 NPAPIPluginStatus status = NPAPI_STATUS_UNSUPPORTED;
808 #endif
809 UMA_HISTOGRAM_ENUMERATION("Plugin.NPAPIStatus", status,
810 NPAPI_STATUS_ENUM_COUNT);
812 return npapi_plugins_enabled_;
815 void PluginServiceImpl::DisablePluginsDiscoveryForTesting() {
816 PluginList::Singleton()->DisablePluginsDiscovery();
819 #if defined(OS_MACOSX)
820 void PluginServiceImpl::AppActivated() {
821 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
822 base::Bind(&NotifyPluginsOfActivation));
824 #elif defined(OS_WIN)
826 bool GetPluginPropertyFromWindow(
827 HWND window, const wchar_t* plugin_atom_property,
828 base::string16* plugin_property) {
829 ATOM plugin_atom = static_cast<ATOM>(
830 reinterpret_cast<uintptr_t>(GetPropW(window, plugin_atom_property)));
831 if (plugin_atom != 0) {
832 WCHAR plugin_property_local[MAX_PATH] = {0};
833 GlobalGetAtomNameW(plugin_atom,
834 plugin_property_local,
835 ARRAYSIZE(plugin_property_local));
836 *plugin_property = plugin_property_local;
837 return true;
839 return false;
842 bool PluginServiceImpl::GetPluginInfoFromWindow(
843 HWND window,
844 base::string16* plugin_name,
845 base::string16* plugin_version) {
846 if (!IsPluginWindow(window))
847 return false;
850 DWORD process_id = 0;
851 GetWindowThreadProcessId(window, &process_id);
852 WebPluginInfo info;
853 if (!PluginProcessHost::GetWebPluginInfoFromPluginPid(process_id, &info))
854 return false;
856 *plugin_name = info.name;
857 *plugin_version = info.version;
858 return true;
861 bool PluginServiceImpl::IsPluginWindow(HWND window) {
862 return gfx::GetClassName(window) == base::string16(kNativeWindowClassName);
864 #endif
866 bool PluginServiceImpl::PpapiDevChannelSupported(
867 BrowserContext* browser_context,
868 const GURL& document_url) {
869 return GetContentClient()->browser()->IsPluginAllowedToUseDevChannelAPIs(
870 browser_context, document_url);
873 } // namespace content