Move Tuple to base namespace.
[chromium-blink-merge.git] / content / browser / plugin_service_impl.cc
blob5e3f194c88f5b1215a1b3cc7ea76037d04c20c50
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/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::MessageLoopProxy* target_loop,
129 const PluginService::GetPluginsCallback& callback,
130 const std::vector<WebPluginInfo>& plugins) {
131 target_loop->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 (EndsWith(plugin_name, base::ASCIIToUTF16(kPluginExtension), true))
585 plugin_name.erase(plugin_name.length() - kPluginExtension.length());
586 #endif // OS_MACOSX
588 return plugin_name;
591 void PluginServiceImpl::GetPlugins(const GetPluginsCallback& callback) {
592 scoped_refptr<base::MessageLoopProxy> target_loop(
593 base::MessageLoop::current()->message_loop_proxy());
595 if (LoadPluginListInProcess()) {
596 BrowserThread::GetBlockingPool()->
597 PostSequencedWorkerTaskWithShutdownBehavior(
598 plugin_list_token_,
599 FROM_HERE,
600 base::Bind(&PluginServiceImpl::GetPluginsInternal,
601 base::Unretained(this),
602 target_loop, callback),
603 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
604 return;
606 #if defined(OS_POSIX)
607 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
608 base::Bind(&PluginServiceImpl::GetPluginsOnIOThread,
609 base::Unretained(this), target_loop, callback));
610 #else
611 NOTREACHED();
612 #endif
615 void PluginServiceImpl::GetPluginsInternal(
616 base::MessageLoopProxy* target_loop,
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_loop->PostTask(FROM_HERE,
625 base::Bind(callback, plugins));
628 #if defined(OS_POSIX)
629 void PluginServiceImpl::GetPluginsOnIOThread(
630 base::MessageLoopProxy* target_loop,
631 const GetPluginsCallback& callback) {
632 DCHECK_CURRENTLY_ON(BrowserThread::IO);
634 // If we switch back to loading plugins in process, then we need to make
635 // sure g_thread_init() gets called since plugins may call glib at load.
637 if (!plugin_loader_.get())
638 plugin_loader_ = new PluginLoaderPosix;
640 plugin_loader_->GetPlugins(
641 base::Bind(&ForwardCallback, make_scoped_refptr(target_loop), callback));
643 #endif
645 #if defined(OS_WIN)
646 void PluginServiceImpl::OnKeyChanged(base::win::RegKey* key) {
647 key->StartWatching(base::Bind(&PluginServiceImpl::OnKeyChanged,
648 base::Unretained(this),
649 base::Unretained(key)));
651 PluginList::Singleton()->RefreshPlugins();
652 PurgePluginListCache(NULL, false);
654 #endif // defined(OS_WIN)
656 void PluginServiceImpl::RegisterPepperPlugins() {
657 ComputePepperPluginList(&ppapi_plugins_);
658 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
659 RegisterInternalPlugin(ppapi_plugins_[i].ToWebPluginInfo(), true);
663 // There should generally be very few plugins so a brute-force search is fine.
664 PepperPluginInfo* PluginServiceImpl::GetRegisteredPpapiPluginInfo(
665 const base::FilePath& plugin_path) {
666 PepperPluginInfo* info = NULL;
667 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
668 if (ppapi_plugins_[i].path == plugin_path) {
669 info = &ppapi_plugins_[i];
670 break;
673 if (info)
674 return info;
675 // We did not find the plugin in our list. But wait! the plugin can also
676 // be a latecomer, as it happens with pepper flash. This information
677 // can be obtained from the PluginList singleton and we can use it to
678 // construct it and add it to the list. This same deal needs to be done
679 // in the renderer side in PepperPluginRegistry.
680 WebPluginInfo webplugin_info;
681 if (!GetPluginInfoByPath(plugin_path, &webplugin_info))
682 return NULL;
683 PepperPluginInfo new_pepper_info;
684 if (!MakePepperPluginInfo(webplugin_info, &new_pepper_info))
685 return NULL;
686 ppapi_plugins_.push_back(new_pepper_info);
687 return &ppapi_plugins_[ppapi_plugins_.size() - 1];
690 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
691 // static
692 void PluginServiceImpl::RegisterFilePathWatcher(FilePathWatcher* watcher,
693 const base::FilePath& path) {
694 bool result = watcher->Watch(path, false,
695 base::Bind(&NotifyPluginDirChanged));
696 DCHECK(result);
698 #endif
700 void PluginServiceImpl::SetFilter(PluginServiceFilter* filter) {
701 filter_ = filter;
704 PluginServiceFilter* PluginServiceImpl::GetFilter() {
705 return filter_;
708 void PluginServiceImpl::ForcePluginShutdown(const base::FilePath& plugin_path) {
709 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
710 BrowserThread::PostTask(
711 BrowserThread::IO, FROM_HERE,
712 base::Bind(&PluginServiceImpl::ForcePluginShutdown,
713 base::Unretained(this), plugin_path));
714 return;
717 PluginProcessHost* plugin = FindNpapiPluginProcess(plugin_path);
718 if (plugin)
719 plugin->ForceShutdown();
722 static const unsigned int kMaxCrashesPerInterval = 3;
723 static const unsigned int kCrashesInterval = 120;
725 void PluginServiceImpl::RegisterPluginCrash(const base::FilePath& path) {
726 DCHECK_CURRENTLY_ON(BrowserThread::IO);
727 std::map<base::FilePath, std::vector<base::Time> >::iterator i =
728 crash_times_.find(path);
729 if (i == crash_times_.end()) {
730 crash_times_[path] = std::vector<base::Time>();
731 i = crash_times_.find(path);
733 if (i->second.size() == kMaxCrashesPerInterval) {
734 i->second.erase(i->second.begin());
736 base::Time time = base::Time::Now();
737 i->second.push_back(time);
740 bool PluginServiceImpl::IsPluginUnstable(const base::FilePath& path) {
741 DCHECK_CURRENTLY_ON(BrowserThread::IO);
742 std::map<base::FilePath, std::vector<base::Time> >::const_iterator i =
743 crash_times_.find(path);
744 if (i == crash_times_.end()) {
745 return false;
747 if (i->second.size() != kMaxCrashesPerInterval) {
748 return false;
750 base::TimeDelta delta = base::Time::Now() - i->second[0];
751 return delta.InSeconds() <= kCrashesInterval;
754 void PluginServiceImpl::RefreshPlugins() {
755 PluginList::Singleton()->RefreshPlugins();
758 void PluginServiceImpl::AddExtraPluginPath(const base::FilePath& path) {
759 if (!NPAPIPluginsSupported()) {
760 // TODO(jam): remove and just have CHECK once we're sure this doesn't get
761 // triggered.
762 DVLOG(0) << "NPAPI plugins not supported";
763 return;
765 PluginList::Singleton()->AddExtraPluginPath(path);
768 void PluginServiceImpl::RemoveExtraPluginPath(const base::FilePath& path) {
769 PluginList::Singleton()->RemoveExtraPluginPath(path);
772 void PluginServiceImpl::AddExtraPluginDir(const base::FilePath& path) {
773 PluginList::Singleton()->AddExtraPluginDir(path);
776 void PluginServiceImpl::RegisterInternalPlugin(
777 const WebPluginInfo& info,
778 bool add_at_beginning) {
779 // Internal plugins should never be NPAPI.
780 CHECK_NE(info.type, WebPluginInfo::PLUGIN_TYPE_NPAPI);
781 if (info.type == WebPluginInfo::PLUGIN_TYPE_NPAPI) {
782 DVLOG(0) << "Don't register NPAPI plugins when they're not supported";
783 return;
785 PluginList::Singleton()->RegisterInternalPlugin(info, add_at_beginning);
788 void PluginServiceImpl::UnregisterInternalPlugin(const base::FilePath& path) {
789 PluginList::Singleton()->UnregisterInternalPlugin(path);
792 void PluginServiceImpl::GetInternalPlugins(
793 std::vector<WebPluginInfo>* plugins) {
794 PluginList::Singleton()->GetInternalPlugins(plugins);
797 bool PluginServiceImpl::NPAPIPluginsSupported() {
798 static bool command_line_checked = false;
800 if (!command_line_checked) {
801 #if defined(OS_WIN) || defined(OS_MACOSX)
802 const base::CommandLine* command_line =
803 base::CommandLine::ForCurrentProcess();
804 npapi_plugins_enabled_ =
805 command_line->HasSwitch(switches::kEnableNpapiForTesting);
806 NPAPIPluginStatus status =
807 npapi_plugins_enabled_ ? NPAPI_STATUS_ENABLED : NPAPI_STATUS_DISABLED;
808 #else
809 NPAPIPluginStatus status = NPAPI_STATUS_UNSUPPORTED;
810 #endif
811 UMA_HISTOGRAM_ENUMERATION("Plugin.NPAPIStatus", status,
812 NPAPI_STATUS_ENUM_COUNT);
815 return npapi_plugins_enabled_;
818 void PluginServiceImpl::DisablePluginsDiscoveryForTesting() {
819 PluginList::Singleton()->DisablePluginsDiscovery();
822 #if defined(OS_MACOSX)
823 void PluginServiceImpl::AppActivated() {
824 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
825 base::Bind(&NotifyPluginsOfActivation));
827 #elif defined(OS_WIN)
829 bool GetPluginPropertyFromWindow(
830 HWND window, const wchar_t* plugin_atom_property,
831 base::string16* plugin_property) {
832 ATOM plugin_atom = static_cast<ATOM>(
833 reinterpret_cast<uintptr_t>(GetPropW(window, plugin_atom_property)));
834 if (plugin_atom != 0) {
835 WCHAR plugin_property_local[MAX_PATH] = {0};
836 GlobalGetAtomNameW(plugin_atom,
837 plugin_property_local,
838 ARRAYSIZE(plugin_property_local));
839 *plugin_property = plugin_property_local;
840 return true;
842 return false;
845 bool PluginServiceImpl::GetPluginInfoFromWindow(
846 HWND window,
847 base::string16* plugin_name,
848 base::string16* plugin_version) {
849 if (!IsPluginWindow(window))
850 return false;
853 DWORD process_id = 0;
854 GetWindowThreadProcessId(window, &process_id);
855 WebPluginInfo info;
856 if (!PluginProcessHost::GetWebPluginInfoFromPluginPid(process_id, &info))
857 return false;
859 *plugin_name = info.name;
860 *plugin_version = info.version;
861 return true;
864 bool PluginServiceImpl::IsPluginWindow(HWND window) {
865 return gfx::GetClassName(window) == base::string16(kNativeWindowClassName);
867 #endif
869 bool PluginServiceImpl::PpapiDevChannelSupported(
870 BrowserContext* browser_context,
871 const GURL& document_url) {
872 return content::GetContentClient()->browser()->
873 IsPluginAllowedToUseDevChannelAPIs(browser_context, document_url);
876 } // namespace content