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"
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/files/file_path.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/metrics/histogram.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/pepper_plugin_list.h"
22 #include "content/common/plugin_list.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_constants.h"
29 #include "content/public/common/content_switches.h"
30 #include "content/public/common/process_type.h"
31 #include "content/public/common/webplugininfo.h"
34 #include "content/common/plugin_constants_win.h"
35 #include "ui/gfx/win/hwnd_util.h"
39 #include "content/browser/plugin_loader_posix.h"
42 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
43 using ::base::FilePathWatcher
;
49 // This enum is used to collect Flash usage data.
51 // Number of browser processes that have started at least one NPAPI Flash
52 // process during their lifetime.
53 START_NPAPI_FLASH_AT_LEAST_ONCE
,
54 // Number of browser processes that have started at least one PPAPI Flash
55 // process during their lifetime.
56 START_PPAPI_FLASH_AT_LEAST_ONCE
,
57 // Total number of browser processes.
58 TOTAL_BROWSER_PROCESSES
,
59 FLASH_USAGE_ENUM_COUNT
62 bool LoadPluginListInProcess() {
66 // If on POSIX, we don't want to load the list of NPAPI plugins in-process as
67 // that causes instability.
69 // Can't load the plugins on the utility thread when in single process mode
70 // since that requires GTK which can only be used on the main thread.
71 if (RenderProcessHost::run_renderer_in_process())
74 return !PluginService::GetInstance()->NPAPIPluginsSupported();
78 // Callback set on the PluginList to assert that plugin loading happens on the
80 void WillLoadPluginsCallback(
81 base::SequencedWorkerPool::SequenceToken token
) {
82 if (LoadPluginListInProcess()) {
83 CHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
86 CHECK(false) << "Plugin loading should happen out-of-process.";
90 #if defined(OS_MACOSX)
91 void NotifyPluginsOfActivation() {
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
94 for (PluginProcessHostIterator iter
; !iter
.Done(); ++iter
)
95 iter
->OnAppActivation();
99 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
100 void NotifyPluginDirChanged(const base::FilePath
& path
, bool error
) {
102 // TODO(pastarmovj): Add some sensible error handling. Maybe silently
103 // stopping the watcher would be enough. Or possibly restart it.
107 VLOG(1) << "Watched path changed: " << path
.value();
108 // Make the plugin list update itself
109 PluginList::Singleton()->RefreshPlugins();
110 BrowserThread::PostTask(
111 BrowserThread::UI
, FROM_HERE
,
112 base::Bind(&PluginService::PurgePluginListCache
,
113 static_cast<BrowserContext
*>(NULL
), false));
117 void ForwardCallback(base::MessageLoopProxy
* target_loop
,
118 const PluginService::GetPluginsCallback
& callback
,
119 const std::vector
<WebPluginInfo
>& plugins
) {
120 target_loop
->PostTask(FROM_HERE
, base::Bind(callback
, plugins
));
126 PluginService
* PluginService::GetInstance() {
127 return PluginServiceImpl::GetInstance();
130 void PluginService::PurgePluginListCache(BrowserContext
* browser_context
,
132 for (RenderProcessHost::iterator it
= RenderProcessHost::AllHostsIterator();
133 !it
.IsAtEnd(); it
.Advance()) {
134 RenderProcessHost
* host
= it
.GetCurrentValue();
135 if (!browser_context
|| host
->GetBrowserContext() == browser_context
)
136 host
->Send(new ViewMsg_PurgePluginListCache(reload_pages
));
141 PluginServiceImpl
* PluginServiceImpl::GetInstance() {
142 return Singleton
<PluginServiceImpl
>::get();
145 PluginServiceImpl::PluginServiceImpl()
147 // Collect the total number of browser processes (which create
148 // PluginServiceImpl objects, to be precise). The number is used to normalize
149 // the number of processes which start at least one NPAPI/PPAPI Flash process.
150 static bool counted
= false;
153 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage", TOTAL_BROWSER_PROCESSES
,
154 FLASH_USAGE_ENUM_COUNT
);
158 PluginServiceImpl::~PluginServiceImpl() {
159 // Make sure no plugin channel requests have been leaked.
160 DCHECK(pending_plugin_clients_
.empty());
163 void PluginServiceImpl::Init() {
164 plugin_list_token_
= BrowserThread::GetBlockingPool()->GetSequenceToken();
165 PluginList::Singleton()->set_will_load_plugins_callback(
166 base::Bind(&WillLoadPluginsCallback
, plugin_list_token_
));
168 RegisterPepperPlugins();
170 // Load any specified on the command line as well.
171 const base::CommandLine
* command_line
=
172 base::CommandLine::ForCurrentProcess();
173 base::FilePath path
=
174 command_line
->GetSwitchValuePath(switches::kLoadPlugin
);
176 AddExtraPluginPath(path
);
177 path
= command_line
->GetSwitchValuePath(switches::kExtraPluginDir
);
179 PluginList::Singleton()->AddExtraPluginDir(path
);
181 if (command_line
->HasSwitch(switches::kDisablePluginsDiscovery
))
182 PluginList::Singleton()->DisablePluginsDiscovery();
185 void PluginServiceImpl::StartWatchingPlugins() {
186 // Start watching for changes in the plugin list. This means watching
187 // for changes in the Windows registry keys and on both Windows and POSIX
188 // watch for changes in the paths that are expected to contain plugins.
190 if (hkcu_key_
.Create(HKEY_CURRENT_USER
,
191 kRegistryMozillaPlugins
,
192 KEY_NOTIFY
) == ERROR_SUCCESS
) {
193 base::win::RegKey::ChangeCallback callback
=
194 base::Bind(&PluginServiceImpl::OnKeyChanged
, base::Unretained(this),
195 base::Unretained(&hkcu_key_
));
196 hkcu_key_
.StartWatching(callback
);
198 if (hklm_key_
.Create(HKEY_LOCAL_MACHINE
,
199 kRegistryMozillaPlugins
,
200 KEY_NOTIFY
) == ERROR_SUCCESS
) {
201 base::win::RegKey::ChangeCallback callback
=
202 base::Bind(&PluginServiceImpl::OnKeyChanged
, base::Unretained(this),
203 base::Unretained(&hklm_key_
));
204 hklm_key_
.StartWatching(callback
);
207 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
208 // On ChromeOS the user can't install plugins anyway and on Windows all
209 // important plugins register themselves in the registry so no need to do that.
211 // Get the list of all paths for registering the FilePathWatchers
212 // that will track and if needed reload the list of plugins on runtime.
213 std::vector
<base::FilePath
> plugin_dirs
;
214 PluginList::Singleton()->GetPluginDirectories(&plugin_dirs
);
216 for (size_t i
= 0; i
< plugin_dirs
.size(); ++i
) {
217 // FilePathWatcher can not handle non-absolute paths under windows.
218 // We don't watch for file changes in windows now but if this should ever
219 // be extended to Windows these lines might save some time of debugging.
221 if (!plugin_dirs
[i
].IsAbsolute())
224 FilePathWatcher
* watcher
= new FilePathWatcher();
225 VLOG(1) << "Watching for changes in: " << plugin_dirs
[i
].value();
226 BrowserThread::PostTask(
227 BrowserThread::FILE, FROM_HERE
,
228 base::Bind(&PluginServiceImpl::RegisterFilePathWatcher
, watcher
,
230 file_watchers_
.push_back(watcher
);
235 PluginProcessHost
* PluginServiceImpl::FindNpapiPluginProcess(
236 const base::FilePath
& plugin_path
) {
237 for (PluginProcessHostIterator iter
; !iter
.Done(); ++iter
) {
238 if (iter
->info().path
== plugin_path
)
245 PpapiPluginProcessHost
* PluginServiceImpl::FindPpapiPluginProcess(
246 const base::FilePath
& plugin_path
,
247 const base::FilePath
& profile_data_directory
) {
248 for (PpapiPluginProcessHostIterator iter
; !iter
.Done(); ++iter
) {
249 if (iter
->plugin_path() == plugin_path
&&
250 iter
->profile_data_directory() == profile_data_directory
) {
257 PpapiPluginProcessHost
* PluginServiceImpl::FindPpapiBrokerProcess(
258 const base::FilePath
& broker_path
) {
259 for (PpapiBrokerProcessHostIterator iter
; !iter
.Done(); ++iter
) {
260 if (iter
->plugin_path() == broker_path
)
267 PluginProcessHost
* PluginServiceImpl::FindOrStartNpapiPluginProcess(
268 int render_process_id
,
269 const base::FilePath
& plugin_path
) {
270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
272 if (filter_
&& !filter_
->CanLoadPlugin(render_process_id
, plugin_path
))
275 PluginProcessHost
* plugin_host
= FindNpapiPluginProcess(plugin_path
);
280 if (!GetPluginInfoByPath(plugin_path
, &info
)) {
284 // Record when NPAPI Flash process is started for the first time.
285 static bool counted
= false;
286 if (!counted
&& base::UTF16ToUTF8(info
.name
) == kFlashPluginName
) {
288 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage",
289 START_NPAPI_FLASH_AT_LEAST_ONCE
,
290 FLASH_USAGE_ENUM_COUNT
);
292 #if defined(OS_CHROMEOS)
293 // TODO(ihf): Move to an earlier place once crbug.com/314301 is fixed. For now
294 // we still want Plugin.FlashUsage recorded if we end up here.
295 LOG(WARNING
) << "Refusing to start npapi plugin on ChromeOS.";
298 // This plugin isn't loaded by any plugin process, so create a new process.
299 scoped_ptr
<PluginProcessHost
> new_host(new PluginProcessHost());
300 if (!new_host
->Init(info
)) {
301 NOTREACHED(); // Init is not expected to fail.
304 return new_host
.release();
307 PpapiPluginProcessHost
* PluginServiceImpl::FindOrStartPpapiPluginProcess(
308 int render_process_id
,
309 const base::FilePath
& plugin_path
,
310 const base::FilePath
& profile_data_directory
) {
311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
313 if (filter_
&& !filter_
->CanLoadPlugin(render_process_id
, plugin_path
)) {
314 VLOG(1) << "Unable to load ppapi plugin: " << plugin_path
.MaybeAsASCII();
318 PpapiPluginProcessHost
* plugin_host
=
319 FindPpapiPluginProcess(plugin_path
, profile_data_directory
);
323 // Validate that the plugin is actually registered.
324 PepperPluginInfo
* info
= GetRegisteredPpapiPluginInfo(plugin_path
);
326 VLOG(1) << "Unable to find ppapi plugin registration for: "
327 << plugin_path
.MaybeAsASCII();
331 // Record when PPAPI Flash process is started for the first time.
332 static bool counted
= false;
333 if (!counted
&& info
->name
== kFlashPluginName
) {
335 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage",
336 START_PPAPI_FLASH_AT_LEAST_ONCE
,
337 FLASH_USAGE_ENUM_COUNT
);
340 // This plugin isn't loaded by any plugin process, so create a new process.
341 plugin_host
= PpapiPluginProcessHost::CreatePluginHost(
342 *info
, profile_data_directory
);
344 VLOG(1) << "Unable to create ppapi plugin process for: "
345 << plugin_path
.MaybeAsASCII();
351 PpapiPluginProcessHost
* PluginServiceImpl::FindOrStartPpapiBrokerProcess(
352 int render_process_id
,
353 const base::FilePath
& plugin_path
) {
354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
356 if (filter_
&& !filter_
->CanLoadPlugin(render_process_id
, plugin_path
))
359 PpapiPluginProcessHost
* plugin_host
= FindPpapiBrokerProcess(plugin_path
);
363 // Validate that the plugin is actually registered.
364 PepperPluginInfo
* info
= GetRegisteredPpapiPluginInfo(plugin_path
);
368 // TODO(ddorwin): Uncomment once out of process is supported.
369 // DCHECK(info->is_out_of_process);
371 // This broker isn't loaded by any broker process, so create a new process.
372 return PpapiPluginProcessHost::CreateBrokerHost(*info
);
375 void PluginServiceImpl::OpenChannelToNpapiPlugin(
376 int render_process_id
,
379 const GURL
& page_url
,
380 const std::string
& mime_type
,
381 PluginProcessHost::Client
* client
) {
382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
383 DCHECK(!ContainsKey(pending_plugin_clients_
, client
));
384 pending_plugin_clients_
.insert(client
);
386 // Make sure plugins are loaded if necessary.
387 PluginServiceFilterParams params
= {
391 client
->GetResourceContext()
393 GetPlugins(base::Bind(
394 &PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin
,
395 base::Unretained(this), params
, url
, mime_type
, client
));
398 void PluginServiceImpl::OpenChannelToPpapiPlugin(
399 int render_process_id
,
400 const base::FilePath
& plugin_path
,
401 const base::FilePath
& profile_data_directory
,
402 PpapiPluginProcessHost::PluginClient
* client
) {
403 PpapiPluginProcessHost
* plugin_host
= FindOrStartPpapiPluginProcess(
404 render_process_id
, plugin_path
, profile_data_directory
);
406 plugin_host
->OpenChannelToPlugin(client
);
409 client
->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId
, 0);
413 void PluginServiceImpl::OpenChannelToPpapiBroker(
414 int render_process_id
,
415 const base::FilePath
& path
,
416 PpapiPluginProcessHost::BrokerClient
* client
) {
417 PpapiPluginProcessHost
* plugin_host
= FindOrStartPpapiBrokerProcess(
418 render_process_id
, path
);
420 plugin_host
->OpenChannelToPlugin(client
);
423 client
->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId
, 0);
427 void PluginServiceImpl::CancelOpenChannelToNpapiPlugin(
428 PluginProcessHost::Client
* client
) {
429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
430 DCHECK(ContainsKey(pending_plugin_clients_
, client
));
431 pending_plugin_clients_
.erase(client
);
434 void PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin(
435 const PluginServiceFilterParams
& params
,
437 const std::string
& mime_type
,
438 PluginProcessHost::Client
* client
,
439 const std::vector
<WebPluginInfo
>&) {
440 GetAllowedPluginForOpenChannelToPlugin(
441 params
.render_process_id
, params
.render_frame_id
, url
, params
.page_url
,
442 mime_type
, client
, params
.resource_context
);
445 void PluginServiceImpl::GetAllowedPluginForOpenChannelToPlugin(
446 int render_process_id
,
449 const GURL
& page_url
,
450 const std::string
& mime_type
,
451 PluginProcessHost::Client
* client
,
452 ResourceContext
* resource_context
) {
454 bool allow_wildcard
= true;
455 bool found
= GetPluginInfo(
456 render_process_id
, render_frame_id
, resource_context
,
457 url
, page_url
, mime_type
, allow_wildcard
,
459 base::FilePath plugin_path
;
461 plugin_path
= info
.path
;
463 // Now we jump back to the IO thread to finish opening the channel.
464 BrowserThread::PostTask(
465 BrowserThread::IO
, FROM_HERE
,
466 base::Bind(&PluginServiceImpl::FinishOpenChannelToPlugin
,
467 base::Unretained(this),
473 void PluginServiceImpl::FinishOpenChannelToPlugin(
474 int render_process_id
,
475 const base::FilePath
& plugin_path
,
476 PluginProcessHost::Client
* client
) {
477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
479 // Make sure it hasn't been canceled yet.
480 if (!ContainsKey(pending_plugin_clients_
, client
))
482 pending_plugin_clients_
.erase(client
);
484 PluginProcessHost
* plugin_host
= FindOrStartNpapiPluginProcess(
485 render_process_id
, plugin_path
);
487 client
->OnFoundPluginProcessHost(plugin_host
);
488 plugin_host
->OpenChannelToPlugin(client
);
494 bool PluginServiceImpl::GetPluginInfoArray(
496 const std::string
& mime_type
,
498 std::vector
<WebPluginInfo
>* plugins
,
499 std::vector
<std::string
>* actual_mime_types
) {
500 bool use_stale
= false;
501 PluginList::Singleton()->GetPluginInfoArray(
502 url
, mime_type
, allow_wildcard
, &use_stale
, NPAPIPluginsSupported(),
503 plugins
, actual_mime_types
);
507 bool PluginServiceImpl::GetPluginInfo(int render_process_id
,
509 ResourceContext
* context
,
511 const GURL
& page_url
,
512 const std::string
& mime_type
,
516 std::string
* actual_mime_type
) {
517 std::vector
<WebPluginInfo
> plugins
;
518 std::vector
<std::string
> mime_types
;
519 bool stale
= GetPluginInfoArray(
520 url
, mime_type
, allow_wildcard
, &plugins
, &mime_types
);
524 for (size_t i
= 0; i
< plugins
.size(); ++i
) {
525 if (!filter_
|| filter_
->IsPluginAvailable(render_process_id
,
532 if (actual_mime_type
)
533 *actual_mime_type
= mime_types
[i
];
540 bool PluginServiceImpl::GetPluginInfoByPath(const base::FilePath
& plugin_path
,
541 WebPluginInfo
* info
) {
542 std::vector
<WebPluginInfo
> plugins
;
543 PluginList::Singleton()->GetPluginsNoRefresh(&plugins
);
545 for (std::vector
<WebPluginInfo
>::iterator it
= plugins
.begin();
548 if (it
->path
== plugin_path
) {
557 base::string16
PluginServiceImpl::GetPluginDisplayNameByPath(
558 const base::FilePath
& path
) {
559 base::string16 plugin_name
= path
.LossyDisplayName();
561 if (PluginService::GetInstance()->GetPluginInfoByPath(path
, &info
) &&
562 !info
.name
.empty()) {
563 plugin_name
= info
.name
;
564 #if defined(OS_MACOSX)
565 // Many plugins on the Mac have .plugin in the actual name, which looks
566 // terrible, so look for that and strip it off if present.
567 const std::string kPluginExtension
= ".plugin";
568 if (EndsWith(plugin_name
, base::ASCIIToUTF16(kPluginExtension
), true))
569 plugin_name
.erase(plugin_name
.length() - kPluginExtension
.length());
575 void PluginServiceImpl::GetPlugins(const GetPluginsCallback
& callback
) {
576 scoped_refptr
<base::MessageLoopProxy
> target_loop(
577 base::MessageLoop::current()->message_loop_proxy());
579 if (LoadPluginListInProcess()) {
580 BrowserThread::GetBlockingPool()->
581 PostSequencedWorkerTaskWithShutdownBehavior(
584 base::Bind(&PluginServiceImpl::GetPluginsInternal
,
585 base::Unretained(this),
586 target_loop
, callback
),
587 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
590 #if defined(OS_POSIX)
591 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
592 base::Bind(&PluginServiceImpl::GetPluginsOnIOThread
,
593 base::Unretained(this), target_loop
, callback
));
599 void PluginServiceImpl::GetPluginsInternal(
600 base::MessageLoopProxy
* target_loop
,
601 const PluginService::GetPluginsCallback
& callback
) {
602 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
603 plugin_list_token_
));
605 std::vector
<WebPluginInfo
> plugins
;
606 PluginList::Singleton()->GetPlugins(&plugins
, NPAPIPluginsSupported());
608 target_loop
->PostTask(FROM_HERE
,
609 base::Bind(callback
, plugins
));
612 #if defined(OS_POSIX)
613 void PluginServiceImpl::GetPluginsOnIOThread(
614 base::MessageLoopProxy
* target_loop
,
615 const GetPluginsCallback
& callback
) {
616 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
618 // If we switch back to loading plugins in process, then we need to make
619 // sure g_thread_init() gets called since plugins may call glib at load.
621 if (!plugin_loader_
.get())
622 plugin_loader_
= new PluginLoaderPosix
;
624 plugin_loader_
->GetPlugins(
625 base::Bind(&ForwardCallback
, make_scoped_refptr(target_loop
), callback
));
630 void PluginServiceImpl::OnKeyChanged(base::win::RegKey
* key
) {
631 key
->StartWatching(base::Bind(&PluginServiceImpl::OnKeyChanged
,
632 base::Unretained(this),
633 base::Unretained(key
)));
635 PluginList::Singleton()->RefreshPlugins();
636 PurgePluginListCache(NULL
, false);
638 #endif // defined(OS_WIN)
640 void PluginServiceImpl::RegisterPepperPlugins() {
641 ComputePepperPluginList(&ppapi_plugins_
);
642 for (size_t i
= 0; i
< ppapi_plugins_
.size(); ++i
) {
643 RegisterInternalPlugin(ppapi_plugins_
[i
].ToWebPluginInfo(), true);
647 // There should generally be very few plugins so a brute-force search is fine.
648 PepperPluginInfo
* PluginServiceImpl::GetRegisteredPpapiPluginInfo(
649 const base::FilePath
& plugin_path
) {
650 PepperPluginInfo
* info
= NULL
;
651 for (size_t i
= 0; i
< ppapi_plugins_
.size(); ++i
) {
652 if (ppapi_plugins_
[i
].path
== plugin_path
) {
653 info
= &ppapi_plugins_
[i
];
659 // We did not find the plugin in our list. But wait! the plugin can also
660 // be a latecomer, as it happens with pepper flash. This information
661 // can be obtained from the PluginList singleton and we can use it to
662 // construct it and add it to the list. This same deal needs to be done
663 // in the renderer side in PepperPluginRegistry.
664 WebPluginInfo webplugin_info
;
665 if (!GetPluginInfoByPath(plugin_path
, &webplugin_info
))
667 PepperPluginInfo new_pepper_info
;
668 if (!MakePepperPluginInfo(webplugin_info
, &new_pepper_info
))
670 ppapi_plugins_
.push_back(new_pepper_info
);
671 return &ppapi_plugins_
[ppapi_plugins_
.size() - 1];
674 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
676 void PluginServiceImpl::RegisterFilePathWatcher(FilePathWatcher
* watcher
,
677 const base::FilePath
& path
) {
678 bool result
= watcher
->Watch(path
, false,
679 base::Bind(&NotifyPluginDirChanged
));
684 void PluginServiceImpl::SetFilter(PluginServiceFilter
* filter
) {
688 PluginServiceFilter
* PluginServiceImpl::GetFilter() {
692 void PluginServiceImpl::ForcePluginShutdown(const base::FilePath
& plugin_path
) {
693 if (!BrowserThread::CurrentlyOn(BrowserThread::IO
)) {
694 BrowserThread::PostTask(
695 BrowserThread::IO
, FROM_HERE
,
696 base::Bind(&PluginServiceImpl::ForcePluginShutdown
,
697 base::Unretained(this), plugin_path
));
701 PluginProcessHost
* plugin
= FindNpapiPluginProcess(plugin_path
);
703 plugin
->ForceShutdown();
706 static const unsigned int kMaxCrashesPerInterval
= 3;
707 static const unsigned int kCrashesInterval
= 120;
709 void PluginServiceImpl::RegisterPluginCrash(const base::FilePath
& path
) {
710 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
711 std::map
<base::FilePath
, std::vector
<base::Time
> >::iterator i
=
712 crash_times_
.find(path
);
713 if (i
== crash_times_
.end()) {
714 crash_times_
[path
] = std::vector
<base::Time
>();
715 i
= crash_times_
.find(path
);
717 if (i
->second
.size() == kMaxCrashesPerInterval
) {
718 i
->second
.erase(i
->second
.begin());
720 base::Time time
= base::Time::Now();
721 i
->second
.push_back(time
);
724 bool PluginServiceImpl::IsPluginUnstable(const base::FilePath
& path
) {
725 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
726 std::map
<base::FilePath
, std::vector
<base::Time
> >::const_iterator i
=
727 crash_times_
.find(path
);
728 if (i
== crash_times_
.end()) {
731 if (i
->second
.size() != kMaxCrashesPerInterval
) {
734 base::TimeDelta delta
= base::Time::Now() - i
->second
[0];
735 return delta
.InSeconds() <= kCrashesInterval
;
738 void PluginServiceImpl::RefreshPlugins() {
739 PluginList::Singleton()->RefreshPlugins();
742 void PluginServiceImpl::AddExtraPluginPath(const base::FilePath
& path
) {
743 if (!NPAPIPluginsSupported()) {
744 // TODO(jam): remove and just have CHECK once we're sure this doesn't get
746 DVLOG(0) << "NPAPI plugins not supported";
749 PluginList::Singleton()->AddExtraPluginPath(path
);
752 void PluginServiceImpl::RemoveExtraPluginPath(const base::FilePath
& path
) {
753 PluginList::Singleton()->RemoveExtraPluginPath(path
);
756 void PluginServiceImpl::AddExtraPluginDir(const base::FilePath
& path
) {
757 PluginList::Singleton()->AddExtraPluginDir(path
);
760 void PluginServiceImpl::RegisterInternalPlugin(
761 const WebPluginInfo
& info
,
762 bool add_at_beginning
) {
763 if (!NPAPIPluginsSupported() &&
764 info
.type
== WebPluginInfo::PLUGIN_TYPE_NPAPI
) {
765 DVLOG(0) << "Don't register NPAPI plugins when they're not supported";
768 PluginList::Singleton()->RegisterInternalPlugin(info
, add_at_beginning
);
771 void PluginServiceImpl::UnregisterInternalPlugin(const base::FilePath
& path
) {
772 PluginList::Singleton()->UnregisterInternalPlugin(path
);
775 void PluginServiceImpl::GetInternalPlugins(
776 std::vector
<WebPluginInfo
>* plugins
) {
777 PluginList::Singleton()->GetInternalPlugins(plugins
);
780 bool PluginServiceImpl::NPAPIPluginsSupported() {
781 #if defined(OS_WIN) || defined(OS_MACOSX)
788 void PluginServiceImpl::DisablePluginsDiscoveryForTesting() {
789 PluginList::Singleton()->DisablePluginsDiscovery();
792 #if defined(OS_MACOSX)
793 void PluginServiceImpl::AppActivated() {
794 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
795 base::Bind(&NotifyPluginsOfActivation
));
797 #elif defined(OS_WIN)
799 bool GetPluginPropertyFromWindow(
800 HWND window
, const wchar_t* plugin_atom_property
,
801 base::string16
* plugin_property
) {
802 ATOM plugin_atom
= reinterpret_cast<ATOM
>(
803 GetPropW(window
, plugin_atom_property
));
804 if (plugin_atom
!= 0) {
805 WCHAR plugin_property_local
[MAX_PATH
] = {0};
806 GlobalGetAtomNameW(plugin_atom
,
807 plugin_property_local
,
808 ARRAYSIZE(plugin_property_local
));
809 *plugin_property
= plugin_property_local
;
815 bool PluginServiceImpl::GetPluginInfoFromWindow(
817 base::string16
* plugin_name
,
818 base::string16
* plugin_version
) {
819 if (!IsPluginWindow(window
))
823 DWORD process_id
= 0;
824 GetWindowThreadProcessId(window
, &process_id
);
826 if (!PluginProcessHost::GetWebPluginInfoFromPluginPid(process_id
, &info
))
829 *plugin_name
= info
.name
;
830 *plugin_version
= info
.version
;
834 bool PluginServiceImpl::IsPluginWindow(HWND window
) {
835 return gfx::GetClassName(window
) == base::string16(kNativeWindowClassName
);
839 bool PluginServiceImpl::PpapiDevChannelSupported(
840 BrowserContext
* browser_context
,
841 const GURL
& document_url
) {
842 return content::GetContentClient()->browser()->
843 IsPluginAllowedToUseDevChannelAPIs(browser_context
, document_url
);
846 } // namespace content