Enable NPAPI if policy has plugin policies.
[chromium-blink-merge.git] / content / browser / plugin_service_impl.cc
blob286bbd41ce85fe7522a35d5152690614cf3df25a
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/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"
33 #if defined(OS_WIN)
34 #include "content/common/plugin_constants_win.h"
35 #include "ui/gfx/win/hwnd_util.h"
36 #endif
38 #if defined(OS_POSIX)
39 #include "content/browser/plugin_loader_posix.h"
40 #endif
42 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
43 using ::base::FilePathWatcher;
44 #endif
46 namespace content {
47 namespace {
49 // This enum is used to collect Flash usage data.
50 enum FlashUsage {
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 enum NPAPIPluginStatus {
63 // Platform does not support NPAPI.
64 NPAPI_STATUS_UNSUPPORTED,
65 // Platform supports NPAPI and NPAPI is disabled.
66 NPAPI_STATUS_DISABLED,
67 // Platform supports NPAPI and NPAPI is enabled.
68 NPAPI_STATUS_ENABLED,
69 NPAPI_STATUS_ENUM_COUNT
72 bool LoadPluginListInProcess() {
73 #if defined(OS_WIN)
74 return true;
75 #else
76 // If on POSIX, we don't want to load the list of NPAPI plugins in-process as
77 // that causes instability.
79 // Can't load the plugins on the utility thread when in single process mode
80 // since that requires GTK which can only be used on the main thread.
81 if (RenderProcessHost::run_renderer_in_process())
82 return true;
84 return !PluginService::GetInstance()->NPAPIPluginsSupported();
85 #endif
88 // Callback set on the PluginList to assert that plugin loading happens on the
89 // correct thread.
90 void WillLoadPluginsCallback(
91 base::SequencedWorkerPool::SequenceToken token) {
92 if (LoadPluginListInProcess()) {
93 CHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
94 token));
95 } else {
96 CHECK(false) << "Plugin loading should happen out-of-process.";
100 #if defined(OS_MACOSX)
101 void NotifyPluginsOfActivation() {
102 DCHECK_CURRENTLY_ON(BrowserThread::IO);
104 for (PluginProcessHostIterator iter; !iter.Done(); ++iter)
105 iter->OnAppActivation();
107 #endif
109 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
110 void NotifyPluginDirChanged(const base::FilePath& path, bool error) {
111 if (error) {
112 // TODO(pastarmovj): Add some sensible error handling. Maybe silently
113 // stopping the watcher would be enough. Or possibly restart it.
114 NOTREACHED();
115 return;
117 VLOG(1) << "Watched path changed: " << path.value();
118 // Make the plugin list update itself
119 PluginList::Singleton()->RefreshPlugins();
120 BrowserThread::PostTask(
121 BrowserThread::UI, FROM_HERE,
122 base::Bind(&PluginService::PurgePluginListCache,
123 static_cast<BrowserContext*>(NULL), false));
125 #endif
127 void ForwardCallback(base::MessageLoopProxy* target_loop,
128 const PluginService::GetPluginsCallback& callback,
129 const std::vector<WebPluginInfo>& plugins) {
130 target_loop->PostTask(FROM_HERE, base::Bind(callback, plugins));
133 } // namespace
135 // static
136 PluginService* PluginService::GetInstance() {
137 return PluginServiceImpl::GetInstance();
140 void PluginService::PurgePluginListCache(BrowserContext* browser_context,
141 bool reload_pages) {
142 for (RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator();
143 !it.IsAtEnd(); it.Advance()) {
144 RenderProcessHost* host = it.GetCurrentValue();
145 if (!browser_context || host->GetBrowserContext() == browser_context)
146 host->Send(new ViewMsg_PurgePluginListCache(reload_pages));
150 // static
151 PluginServiceImpl* PluginServiceImpl::GetInstance() {
152 return Singleton<PluginServiceImpl>::get();
155 PluginServiceImpl::PluginServiceImpl()
156 : npapi_plugins_enabled_(false), filter_(NULL) {
157 // Collect the total number of browser processes (which create
158 // PluginServiceImpl objects, to be precise). The number is used to normalize
159 // the number of processes which start at least one NPAPI/PPAPI Flash process.
160 static bool counted = false;
161 if (!counted) {
162 counted = true;
163 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage", TOTAL_BROWSER_PROCESSES,
164 FLASH_USAGE_ENUM_COUNT);
168 PluginServiceImpl::~PluginServiceImpl() {
169 // Make sure no plugin channel requests have been leaked.
170 DCHECK(pending_plugin_clients_.empty());
173 void PluginServiceImpl::Init() {
174 plugin_list_token_ = BrowserThread::GetBlockingPool()->GetSequenceToken();
175 PluginList::Singleton()->set_will_load_plugins_callback(
176 base::Bind(&WillLoadPluginsCallback, plugin_list_token_));
178 RegisterPepperPlugins();
180 // Load any specified on the command line as well.
181 const base::CommandLine* command_line =
182 base::CommandLine::ForCurrentProcess();
183 base::FilePath path =
184 command_line->GetSwitchValuePath(switches::kLoadPlugin);
185 if (!path.empty())
186 AddExtraPluginPath(path);
187 path = command_line->GetSwitchValuePath(switches::kExtraPluginDir);
188 if (!path.empty())
189 PluginList::Singleton()->AddExtraPluginDir(path);
191 if (command_line->HasSwitch(switches::kDisablePluginsDiscovery))
192 PluginList::Singleton()->DisablePluginsDiscovery();
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,
201 kRegistryMozillaPlugins,
202 KEY_NOTIFY) == ERROR_SUCCESS) {
203 base::win::RegKey::ChangeCallback callback =
204 base::Bind(&PluginServiceImpl::OnKeyChanged, base::Unretained(this),
205 base::Unretained(&hkcu_key_));
206 hkcu_key_.StartWatching(callback);
208 if (hklm_key_.Create(HKEY_LOCAL_MACHINE,
209 kRegistryMozillaPlugins,
210 KEY_NOTIFY) == ERROR_SUCCESS) {
211 base::win::RegKey::ChangeCallback callback =
212 base::Bind(&PluginServiceImpl::OnKeyChanged, base::Unretained(this),
213 base::Unretained(&hklm_key_));
214 hklm_key_.StartWatching(callback);
216 #endif
217 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
218 // On ChromeOS the user can't install plugins anyway and on Windows all
219 // important plugins register themselves in the registry so no need to do that.
221 // Get the list of all paths for registering the FilePathWatchers
222 // that will track and if needed reload the list of plugins on runtime.
223 std::vector<base::FilePath> plugin_dirs;
224 PluginList::Singleton()->GetPluginDirectories(&plugin_dirs);
226 for (size_t i = 0; i < plugin_dirs.size(); ++i) {
227 // FilePathWatcher can not handle non-absolute paths under windows.
228 // We don't watch for file changes in windows now but if this should ever
229 // be extended to Windows these lines might save some time of debugging.
230 #if defined(OS_WIN)
231 if (!plugin_dirs[i].IsAbsolute())
232 continue;
233 #endif
234 FilePathWatcher* watcher = new FilePathWatcher();
235 VLOG(1) << "Watching for changes in: " << plugin_dirs[i].value();
236 BrowserThread::PostTask(
237 BrowserThread::FILE, FROM_HERE,
238 base::Bind(&PluginServiceImpl::RegisterFilePathWatcher, watcher,
239 plugin_dirs[i]));
240 file_watchers_.push_back(watcher);
242 #endif
245 PluginProcessHost* PluginServiceImpl::FindNpapiPluginProcess(
246 const base::FilePath& plugin_path) {
247 for (PluginProcessHostIterator iter; !iter.Done(); ++iter) {
248 if (iter->info().path == plugin_path)
249 return *iter;
252 return NULL;
255 PpapiPluginProcessHost* PluginServiceImpl::FindPpapiPluginProcess(
256 const base::FilePath& plugin_path,
257 const base::FilePath& profile_data_directory) {
258 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
259 if (iter->plugin_path() == plugin_path &&
260 iter->profile_data_directory() == profile_data_directory) {
261 return *iter;
264 return NULL;
267 PpapiPluginProcessHost* PluginServiceImpl::FindPpapiBrokerProcess(
268 const base::FilePath& broker_path) {
269 for (PpapiBrokerProcessHostIterator iter; !iter.Done(); ++iter) {
270 if (iter->plugin_path() == broker_path)
271 return *iter;
274 return NULL;
277 PluginProcessHost* PluginServiceImpl::FindOrStartNpapiPluginProcess(
278 int render_process_id,
279 const base::FilePath& plugin_path) {
280 DCHECK_CURRENTLY_ON(BrowserThread::IO);
282 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path))
283 return NULL;
285 PluginProcessHost* plugin_host = FindNpapiPluginProcess(plugin_path);
286 if (plugin_host)
287 return plugin_host;
289 WebPluginInfo info;
290 if (!GetPluginInfoByPath(plugin_path, &info)) {
291 return NULL;
294 // Record when NPAPI Flash process is started for the first time.
295 static bool counted = false;
296 if (!counted && base::UTF16ToUTF8(info.name) == kFlashPluginName) {
297 counted = true;
298 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage",
299 START_NPAPI_FLASH_AT_LEAST_ONCE,
300 FLASH_USAGE_ENUM_COUNT);
302 #if defined(OS_CHROMEOS)
303 // TODO(ihf): Move to an earlier place once crbug.com/314301 is fixed. For now
304 // we still want Plugin.FlashUsage recorded if we end up here.
305 LOG(WARNING) << "Refusing to start npapi plugin on ChromeOS.";
306 return NULL;
307 #endif
308 // This plugin isn't loaded by any plugin process, so create a new process.
309 scoped_ptr<PluginProcessHost> new_host(new PluginProcessHost());
310 if (!new_host->Init(info)) {
311 NOTREACHED(); // Init is not expected to fail.
312 return NULL;
314 return new_host.release();
317 PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiPluginProcess(
318 int render_process_id,
319 const base::FilePath& plugin_path,
320 const base::FilePath& profile_data_directory) {
321 DCHECK_CURRENTLY_ON(BrowserThread::IO);
323 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path)) {
324 VLOG(1) << "Unable to load ppapi plugin: " << plugin_path.MaybeAsASCII();
325 return NULL;
328 PpapiPluginProcessHost* plugin_host =
329 FindPpapiPluginProcess(plugin_path, profile_data_directory);
330 if (plugin_host)
331 return plugin_host;
333 // Validate that the plugin is actually registered.
334 PepperPluginInfo* info = GetRegisteredPpapiPluginInfo(plugin_path);
335 if (!info) {
336 VLOG(1) << "Unable to find ppapi plugin registration for: "
337 << plugin_path.MaybeAsASCII();
338 return NULL;
341 // Record when PPAPI Flash process is started for the first time.
342 static bool counted = false;
343 if (!counted && info->name == kFlashPluginName) {
344 counted = true;
345 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage",
346 START_PPAPI_FLASH_AT_LEAST_ONCE,
347 FLASH_USAGE_ENUM_COUNT);
350 // This plugin isn't loaded by any plugin process, so create a new process.
351 plugin_host = PpapiPluginProcessHost::CreatePluginHost(
352 *info, profile_data_directory);
353 if (!plugin_host) {
354 VLOG(1) << "Unable to create ppapi plugin process for: "
355 << plugin_path.MaybeAsASCII();
358 return plugin_host;
361 PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiBrokerProcess(
362 int render_process_id,
363 const base::FilePath& plugin_path) {
364 DCHECK_CURRENTLY_ON(BrowserThread::IO);
366 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path))
367 return NULL;
369 PpapiPluginProcessHost* plugin_host = FindPpapiBrokerProcess(plugin_path);
370 if (plugin_host)
371 return plugin_host;
373 // Validate that the plugin is actually registered.
374 PepperPluginInfo* info = GetRegisteredPpapiPluginInfo(plugin_path);
375 if (!info)
376 return NULL;
378 // TODO(ddorwin): Uncomment once out of process is supported.
379 // DCHECK(info->is_out_of_process);
381 // This broker isn't loaded by any broker process, so create a new process.
382 return PpapiPluginProcessHost::CreateBrokerHost(*info);
385 void PluginServiceImpl::OpenChannelToNpapiPlugin(
386 int render_process_id,
387 int render_frame_id,
388 const GURL& url,
389 const GURL& page_url,
390 const std::string& mime_type,
391 PluginProcessHost::Client* client) {
392 DCHECK_CURRENTLY_ON(BrowserThread::IO);
393 DCHECK(!ContainsKey(pending_plugin_clients_, client));
394 pending_plugin_clients_.insert(client);
396 // Make sure plugins are loaded if necessary.
397 PluginServiceFilterParams params = {
398 render_process_id,
399 render_frame_id,
400 page_url,
401 client->GetResourceContext()
403 GetPlugins(base::Bind(
404 &PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin,
405 base::Unretained(this), params, url, mime_type, client));
408 void PluginServiceImpl::OpenChannelToPpapiPlugin(
409 int render_process_id,
410 const base::FilePath& plugin_path,
411 const base::FilePath& profile_data_directory,
412 PpapiPluginProcessHost::PluginClient* client) {
413 PpapiPluginProcessHost* plugin_host = FindOrStartPpapiPluginProcess(
414 render_process_id, plugin_path, profile_data_directory);
415 if (plugin_host) {
416 plugin_host->OpenChannelToPlugin(client);
417 } else {
418 // Send error.
419 client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0);
423 void PluginServiceImpl::OpenChannelToPpapiBroker(
424 int render_process_id,
425 const base::FilePath& path,
426 PpapiPluginProcessHost::BrokerClient* client) {
427 PpapiPluginProcessHost* plugin_host = FindOrStartPpapiBrokerProcess(
428 render_process_id, path);
429 if (plugin_host) {
430 plugin_host->OpenChannelToPlugin(client);
431 } else {
432 // Send error.
433 client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0);
437 void PluginServiceImpl::CancelOpenChannelToNpapiPlugin(
438 PluginProcessHost::Client* client) {
439 DCHECK_CURRENTLY_ON(BrowserThread::IO);
440 DCHECK(ContainsKey(pending_plugin_clients_, client));
441 pending_plugin_clients_.erase(client);
444 void PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin(
445 const PluginServiceFilterParams& params,
446 const GURL& url,
447 const std::string& mime_type,
448 PluginProcessHost::Client* client,
449 const std::vector<WebPluginInfo>&) {
450 GetAllowedPluginForOpenChannelToPlugin(
451 params.render_process_id, params.render_frame_id, url, params.page_url,
452 mime_type, client, params.resource_context);
455 void PluginServiceImpl::GetAllowedPluginForOpenChannelToPlugin(
456 int render_process_id,
457 int render_frame_id,
458 const GURL& url,
459 const GURL& page_url,
460 const std::string& mime_type,
461 PluginProcessHost::Client* client,
462 ResourceContext* resource_context) {
463 WebPluginInfo info;
464 bool allow_wildcard = true;
465 bool found = GetPluginInfo(
466 render_process_id, render_frame_id, resource_context,
467 url, page_url, mime_type, allow_wildcard,
468 NULL, &info, NULL);
469 base::FilePath plugin_path;
470 if (found)
471 plugin_path = info.path;
473 // Now we jump back to the IO thread to finish opening the channel.
474 BrowserThread::PostTask(
475 BrowserThread::IO, FROM_HERE,
476 base::Bind(&PluginServiceImpl::FinishOpenChannelToPlugin,
477 base::Unretained(this),
478 render_process_id,
479 plugin_path,
480 client));
483 void PluginServiceImpl::FinishOpenChannelToPlugin(
484 int render_process_id,
485 const base::FilePath& plugin_path,
486 PluginProcessHost::Client* client) {
487 DCHECK_CURRENTLY_ON(BrowserThread::IO);
489 // Make sure it hasn't been canceled yet.
490 if (!ContainsKey(pending_plugin_clients_, client))
491 return;
492 pending_plugin_clients_.erase(client);
494 PluginProcessHost* plugin_host = FindOrStartNpapiPluginProcess(
495 render_process_id, plugin_path);
496 if (plugin_host) {
497 client->OnFoundPluginProcessHost(plugin_host);
498 plugin_host->OpenChannelToPlugin(client);
499 } else {
500 client->OnError();
504 bool PluginServiceImpl::GetPluginInfoArray(
505 const GURL& url,
506 const std::string& mime_type,
507 bool allow_wildcard,
508 std::vector<WebPluginInfo>* plugins,
509 std::vector<std::string>* actual_mime_types) {
510 bool use_stale = false;
511 PluginList::Singleton()->GetPluginInfoArray(
512 url, mime_type, allow_wildcard, &use_stale, NPAPIPluginsSupported(),
513 plugins, actual_mime_types);
514 return use_stale;
517 bool PluginServiceImpl::GetPluginInfo(int render_process_id,
518 int render_frame_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 WebPluginInfo* info,
526 std::string* actual_mime_type) {
527 std::vector<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_frame_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 WebPluginInfo* info) {
552 std::vector<WebPluginInfo> plugins;
553 PluginList::Singleton()->GetPluginsNoRefresh(&plugins);
555 for (std::vector<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 base::string16 PluginServiceImpl::GetPluginDisplayNameByPath(
568 const base::FilePath& path) {
569 base::string16 plugin_name = path.LossyDisplayName();
570 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, base::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 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
602 base::Bind(&PluginServiceImpl::GetPluginsOnIOThread,
603 base::Unretained(this), target_loop, callback));
604 #else
605 NOTREACHED();
606 #endif
609 void PluginServiceImpl::GetPluginsInternal(
610 base::MessageLoopProxy* target_loop,
611 const PluginService::GetPluginsCallback& callback) {
612 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
613 plugin_list_token_));
615 std::vector<WebPluginInfo> plugins;
616 PluginList::Singleton()->GetPlugins(&plugins, NPAPIPluginsSupported());
618 target_loop->PostTask(FROM_HERE,
619 base::Bind(callback, plugins));
622 #if defined(OS_POSIX)
623 void PluginServiceImpl::GetPluginsOnIOThread(
624 base::MessageLoopProxy* target_loop,
625 const GetPluginsCallback& callback) {
626 DCHECK_CURRENTLY_ON(BrowserThread::IO);
628 // If we switch back to loading plugins in process, then we need to make
629 // sure g_thread_init() gets called since plugins may call glib at load.
631 if (!plugin_loader_.get())
632 plugin_loader_ = new PluginLoaderPosix;
634 plugin_loader_->GetPlugins(
635 base::Bind(&ForwardCallback, make_scoped_refptr(target_loop), callback));
637 #endif
639 #if defined(OS_WIN)
640 void PluginServiceImpl::OnKeyChanged(base::win::RegKey* key) {
641 key->StartWatching(base::Bind(&PluginServiceImpl::OnKeyChanged,
642 base::Unretained(this),
643 base::Unretained(key)));
645 PluginList::Singleton()->RefreshPlugins();
646 PurgePluginListCache(NULL, false);
648 #endif // defined(OS_WIN)
650 void PluginServiceImpl::RegisterPepperPlugins() {
651 ComputePepperPluginList(&ppapi_plugins_);
652 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
653 RegisterInternalPlugin(ppapi_plugins_[i].ToWebPluginInfo(), true);
657 // There should generally be very few plugins so a brute-force search is fine.
658 PepperPluginInfo* PluginServiceImpl::GetRegisteredPpapiPluginInfo(
659 const base::FilePath& plugin_path) {
660 PepperPluginInfo* info = NULL;
661 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
662 if (ppapi_plugins_[i].path == plugin_path) {
663 info = &ppapi_plugins_[i];
664 break;
667 if (info)
668 return info;
669 // We did not find the plugin in our list. But wait! the plugin can also
670 // be a latecomer, as it happens with pepper flash. This information
671 // can be obtained from the PluginList singleton and we can use it to
672 // construct it and add it to the list. This same deal needs to be done
673 // in the renderer side in PepperPluginRegistry.
674 WebPluginInfo webplugin_info;
675 if (!GetPluginInfoByPath(plugin_path, &webplugin_info))
676 return NULL;
677 PepperPluginInfo new_pepper_info;
678 if (!MakePepperPluginInfo(webplugin_info, &new_pepper_info))
679 return NULL;
680 ppapi_plugins_.push_back(new_pepper_info);
681 return &ppapi_plugins_[ppapi_plugins_.size() - 1];
684 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
685 // static
686 void PluginServiceImpl::RegisterFilePathWatcher(FilePathWatcher* watcher,
687 const base::FilePath& path) {
688 bool result = watcher->Watch(path, false,
689 base::Bind(&NotifyPluginDirChanged));
690 DCHECK(result);
692 #endif
694 void PluginServiceImpl::SetFilter(PluginServiceFilter* filter) {
695 filter_ = filter;
698 PluginServiceFilter* PluginServiceImpl::GetFilter() {
699 return filter_;
702 void PluginServiceImpl::ForcePluginShutdown(const base::FilePath& plugin_path) {
703 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
704 BrowserThread::PostTask(
705 BrowserThread::IO, FROM_HERE,
706 base::Bind(&PluginServiceImpl::ForcePluginShutdown,
707 base::Unretained(this), plugin_path));
708 return;
711 PluginProcessHost* plugin = FindNpapiPluginProcess(plugin_path);
712 if (plugin)
713 plugin->ForceShutdown();
716 static const unsigned int kMaxCrashesPerInterval = 3;
717 static const unsigned int kCrashesInterval = 120;
719 void PluginServiceImpl::RegisterPluginCrash(const base::FilePath& path) {
720 DCHECK_CURRENTLY_ON(BrowserThread::IO);
721 std::map<base::FilePath, std::vector<base::Time> >::iterator i =
722 crash_times_.find(path);
723 if (i == crash_times_.end()) {
724 crash_times_[path] = std::vector<base::Time>();
725 i = crash_times_.find(path);
727 if (i->second.size() == kMaxCrashesPerInterval) {
728 i->second.erase(i->second.begin());
730 base::Time time = base::Time::Now();
731 i->second.push_back(time);
734 bool PluginServiceImpl::IsPluginUnstable(const base::FilePath& path) {
735 DCHECK_CURRENTLY_ON(BrowserThread::IO);
736 std::map<base::FilePath, std::vector<base::Time> >::const_iterator i =
737 crash_times_.find(path);
738 if (i == crash_times_.end()) {
739 return false;
741 if (i->second.size() != kMaxCrashesPerInterval) {
742 return false;
744 base::TimeDelta delta = base::Time::Now() - i->second[0];
745 return delta.InSeconds() <= kCrashesInterval;
748 void PluginServiceImpl::RefreshPlugins() {
749 PluginList::Singleton()->RefreshPlugins();
752 void PluginServiceImpl::AddExtraPluginPath(const base::FilePath& path) {
753 if (!NPAPIPluginsSupported()) {
754 // TODO(jam): remove and just have CHECK once we're sure this doesn't get
755 // triggered.
756 DVLOG(0) << "NPAPI plugins not supported";
757 return;
759 PluginList::Singleton()->AddExtraPluginPath(path);
762 void PluginServiceImpl::RemoveExtraPluginPath(const base::FilePath& path) {
763 PluginList::Singleton()->RemoveExtraPluginPath(path);
766 void PluginServiceImpl::AddExtraPluginDir(const base::FilePath& path) {
767 PluginList::Singleton()->AddExtraPluginDir(path);
770 void PluginServiceImpl::RegisterInternalPlugin(
771 const WebPluginInfo& info,
772 bool add_at_beginning) {
773 // Internal plugins should never be NPAPI.
774 CHECK_NE(info.type, WebPluginInfo::PLUGIN_TYPE_NPAPI);
775 if (info.type == WebPluginInfo::PLUGIN_TYPE_NPAPI) {
776 DVLOG(0) << "Don't register NPAPI plugins when they're not supported";
777 return;
779 PluginList::Singleton()->RegisterInternalPlugin(info, add_at_beginning);
782 void PluginServiceImpl::UnregisterInternalPlugin(const base::FilePath& path) {
783 PluginList::Singleton()->UnregisterInternalPlugin(path);
786 void PluginServiceImpl::GetInternalPlugins(
787 std::vector<WebPluginInfo>* plugins) {
788 PluginList::Singleton()->GetInternalPlugins(plugins);
791 bool PluginServiceImpl::NPAPIPluginsSupported() {
792 if (npapi_plugins_enabled_)
793 return true;
795 static bool command_line_checked = false;
797 if (!command_line_checked) {
798 #if defined(OS_WIN) || defined(OS_MACOSX)
799 const base::CommandLine* command_line =
800 base::CommandLine::ForCurrentProcess();
801 npapi_plugins_enabled_ = command_line->HasSwitch(switches::kEnableNpapi);
802 NPAPIPluginStatus status =
803 npapi_plugins_enabled_ ? NPAPI_STATUS_ENABLED : NPAPI_STATUS_DISABLED;
804 #else
805 NPAPIPluginStatus status = NPAPI_STATUS_UNSUPPORTED;
806 #endif
807 UMA_HISTOGRAM_ENUMERATION("Plugin.NPAPIStatus", status,
808 NPAPI_STATUS_ENUM_COUNT);
811 return npapi_plugins_enabled_;
814 void PluginServiceImpl::DisablePluginsDiscoveryForTesting() {
815 PluginList::Singleton()->DisablePluginsDiscovery();
818 void PluginServiceImpl::EnableNpapiPlugins() {
819 npapi_plugins_enabled_ = true;
820 RefreshPlugins();
821 BrowserThread::PostTask(
822 BrowserThread::UI, FROM_HERE,
823 base::Bind(&PluginService::PurgePluginListCache,
824 static_cast<BrowserContext*>(NULL), false));
827 #if defined(OS_MACOSX)
828 void PluginServiceImpl::AppActivated() {
829 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
830 base::Bind(&NotifyPluginsOfActivation));
832 #elif defined(OS_WIN)
834 bool GetPluginPropertyFromWindow(
835 HWND window, const wchar_t* plugin_atom_property,
836 base::string16* plugin_property) {
837 ATOM plugin_atom = reinterpret_cast<ATOM>(
838 GetPropW(window, plugin_atom_property));
839 if (plugin_atom != 0) {
840 WCHAR plugin_property_local[MAX_PATH] = {0};
841 GlobalGetAtomNameW(plugin_atom,
842 plugin_property_local,
843 ARRAYSIZE(plugin_property_local));
844 *plugin_property = plugin_property_local;
845 return true;
847 return false;
850 bool PluginServiceImpl::GetPluginInfoFromWindow(
851 HWND window,
852 base::string16* plugin_name,
853 base::string16* plugin_version) {
854 if (!IsPluginWindow(window))
855 return false;
858 DWORD process_id = 0;
859 GetWindowThreadProcessId(window, &process_id);
860 WebPluginInfo info;
861 if (!PluginProcessHost::GetWebPluginInfoFromPluginPid(process_id, &info))
862 return false;
864 *plugin_name = info.name;
865 *plugin_version = info.version;
866 return true;
869 bool PluginServiceImpl::IsPluginWindow(HWND window) {
870 return gfx::GetClassName(window) == base::string16(kNativeWindowClassName);
872 #endif
874 bool PluginServiceImpl::PpapiDevChannelSupported(
875 BrowserContext* browser_context,
876 const GURL& document_url) {
877 return content::GetContentClient()->browser()->
878 IsPluginAllowedToUseDevChannelAPIs(browser_context, document_url);
881 } // namespace content