Android: Store language .pak files in res/raw rather than assets
[chromium-blink-merge.git] / content / browser / plugin_service_impl.cc
blob93140c27ef88a06e8a1539c2463f5b760dbcc7cc
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), 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::SingleThreadTaskRunner> target_task_runner(
593 base::ThreadTaskRunnerHandle::Get());
595 if (LoadPluginListInProcess()) {
596 BrowserThread::GetBlockingPool()
597 ->PostSequencedWorkerTaskWithShutdownBehavior(
598 plugin_list_token_, FROM_HERE,
599 base::Bind(&PluginServiceImpl::GetPluginsInternal,
600 base::Unretained(this), target_task_runner, callback),
601 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
602 return;
604 #if defined(OS_POSIX)
605 BrowserThread::PostTask(
606 BrowserThread::IO, FROM_HERE,
607 base::Bind(&PluginServiceImpl::GetPluginsOnIOThread,
608 base::Unretained(this), target_task_runner, callback));
609 #else
610 NOTREACHED();
611 #endif
614 void PluginServiceImpl::GetPluginsInternal(
615 base::SingleThreadTaskRunner* target_task_runner,
616 const PluginService::GetPluginsCallback& callback) {
617 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
618 plugin_list_token_));
620 std::vector<WebPluginInfo> plugins;
621 PluginList::Singleton()->GetPlugins(&plugins, NPAPIPluginsSupported());
623 target_task_runner->PostTask(FROM_HERE, base::Bind(callback, plugins));
626 #if defined(OS_POSIX)
627 void PluginServiceImpl::GetPluginsOnIOThread(
628 base::SingleThreadTaskRunner* target_task_runner,
629 const GetPluginsCallback& callback) {
630 DCHECK_CURRENTLY_ON(BrowserThread::IO);
632 // If we switch back to loading plugins in process, then we need to make
633 // sure g_thread_init() gets called since plugins may call glib at load.
635 if (!plugin_loader_.get())
636 plugin_loader_ = new PluginLoaderPosix;
638 plugin_loader_->GetPlugins(base::Bind(
639 &ForwardCallback, make_scoped_refptr(target_task_runner), callback));
641 #endif
643 #if defined(OS_WIN)
644 void PluginServiceImpl::OnKeyChanged(base::win::RegKey* key) {
645 key->StartWatching(base::Bind(&PluginServiceImpl::OnKeyChanged,
646 base::Unretained(this),
647 base::Unretained(key)));
649 PluginList::Singleton()->RefreshPlugins();
650 PurgePluginListCache(NULL, false);
652 #endif // defined(OS_WIN)
654 void PluginServiceImpl::RegisterPepperPlugins() {
655 ComputePepperPluginList(&ppapi_plugins_);
656 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
657 RegisterInternalPlugin(ppapi_plugins_[i].ToWebPluginInfo(), true);
661 // There should generally be very few plugins so a brute-force search is fine.
662 PepperPluginInfo* PluginServiceImpl::GetRegisteredPpapiPluginInfo(
663 const base::FilePath& plugin_path) {
664 PepperPluginInfo* info = NULL;
665 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
666 if (ppapi_plugins_[i].path == plugin_path) {
667 info = &ppapi_plugins_[i];
668 break;
671 if (info)
672 return info;
673 // We did not find the plugin in our list. But wait! the plugin can also
674 // be a latecomer, as it happens with pepper flash. This information
675 // can be obtained from the PluginList singleton and we can use it to
676 // construct it and add it to the list. This same deal needs to be done
677 // in the renderer side in PepperPluginRegistry.
678 WebPluginInfo webplugin_info;
679 if (!GetPluginInfoByPath(plugin_path, &webplugin_info))
680 return NULL;
681 PepperPluginInfo new_pepper_info;
682 if (!MakePepperPluginInfo(webplugin_info, &new_pepper_info))
683 return NULL;
684 ppapi_plugins_.push_back(new_pepper_info);
685 return &ppapi_plugins_[ppapi_plugins_.size() - 1];
688 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
689 // static
690 void PluginServiceImpl::RegisterFilePathWatcher(FilePathWatcher* watcher,
691 const base::FilePath& path) {
692 bool result = watcher->Watch(path, false,
693 base::Bind(&NotifyPluginDirChanged));
694 DCHECK(result);
696 #endif
698 void PluginServiceImpl::SetFilter(PluginServiceFilter* filter) {
699 filter_ = filter;
702 PluginServiceFilter* PluginServiceImpl::GetFilter() {
703 return filter_;
706 void PluginServiceImpl::ForcePluginShutdown(const base::FilePath& plugin_path) {
707 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
708 BrowserThread::PostTask(
709 BrowserThread::IO, FROM_HERE,
710 base::Bind(&PluginServiceImpl::ForcePluginShutdown,
711 base::Unretained(this), plugin_path));
712 return;
715 PluginProcessHost* plugin = FindNpapiPluginProcess(plugin_path);
716 if (plugin)
717 plugin->ForceShutdown();
720 static const unsigned int kMaxCrashesPerInterval = 3;
721 static const unsigned int kCrashesInterval = 120;
723 void PluginServiceImpl::RegisterPluginCrash(const base::FilePath& path) {
724 DCHECK_CURRENTLY_ON(BrowserThread::IO);
725 std::map<base::FilePath, std::vector<base::Time> >::iterator i =
726 crash_times_.find(path);
727 if (i == crash_times_.end()) {
728 crash_times_[path] = std::vector<base::Time>();
729 i = crash_times_.find(path);
731 if (i->second.size() == kMaxCrashesPerInterval) {
732 i->second.erase(i->second.begin());
734 base::Time time = base::Time::Now();
735 i->second.push_back(time);
738 bool PluginServiceImpl::IsPluginUnstable(const base::FilePath& path) {
739 DCHECK_CURRENTLY_ON(BrowserThread::IO);
740 std::map<base::FilePath, std::vector<base::Time> >::const_iterator i =
741 crash_times_.find(path);
742 if (i == crash_times_.end()) {
743 return false;
745 if (i->second.size() != kMaxCrashesPerInterval) {
746 return false;
748 base::TimeDelta delta = base::Time::Now() - i->second[0];
749 return delta.InSeconds() <= kCrashesInterval;
752 void PluginServiceImpl::RefreshPlugins() {
753 PluginList::Singleton()->RefreshPlugins();
756 void PluginServiceImpl::AddExtraPluginPath(const base::FilePath& path) {
757 if (!NPAPIPluginsSupported()) {
758 // TODO(jam): remove and just have CHECK once we're sure this doesn't get
759 // triggered.
760 DVLOG(0) << "NPAPI plugins not supported";
761 return;
763 PluginList::Singleton()->AddExtraPluginPath(path);
766 void PluginServiceImpl::RemoveExtraPluginPath(const base::FilePath& path) {
767 PluginList::Singleton()->RemoveExtraPluginPath(path);
770 void PluginServiceImpl::AddExtraPluginDir(const base::FilePath& path) {
771 PluginList::Singleton()->AddExtraPluginDir(path);
774 void PluginServiceImpl::RegisterInternalPlugin(
775 const WebPluginInfo& info,
776 bool add_at_beginning) {
777 // Internal plugins should never be NPAPI.
778 CHECK_NE(info.type, WebPluginInfo::PLUGIN_TYPE_NPAPI);
779 if (info.type == WebPluginInfo::PLUGIN_TYPE_NPAPI) {
780 DVLOG(0) << "Don't register NPAPI plugins when they're not supported";
781 return;
783 PluginList::Singleton()->RegisterInternalPlugin(info, add_at_beginning);
786 void PluginServiceImpl::UnregisterInternalPlugin(const base::FilePath& path) {
787 PluginList::Singleton()->UnregisterInternalPlugin(path);
790 void PluginServiceImpl::GetInternalPlugins(
791 std::vector<WebPluginInfo>* plugins) {
792 PluginList::Singleton()->GetInternalPlugins(plugins);
795 bool PluginServiceImpl::NPAPIPluginsSupported() {
796 static bool command_line_checked = false;
798 if (!command_line_checked) {
799 #if defined(OS_WIN) || defined(OS_MACOSX)
800 const base::CommandLine* command_line =
801 base::CommandLine::ForCurrentProcess();
802 npapi_plugins_enabled_ =
803 command_line->HasSwitch(switches::kEnableNpapiForTesting);
804 #if defined(OS_WIN)
805 // NPAPI plugins don't play well with Win32k renderer lockdown.
806 if (npapi_plugins_enabled_)
807 DisableWin32kRendererLockdown();
808 #endif
809 NPAPIPluginStatus status =
810 npapi_plugins_enabled_ ? NPAPI_STATUS_ENABLED : NPAPI_STATUS_DISABLED;
811 #else
812 NPAPIPluginStatus status = NPAPI_STATUS_UNSUPPORTED;
813 #endif
814 UMA_HISTOGRAM_ENUMERATION("Plugin.NPAPIStatus", status,
815 NPAPI_STATUS_ENUM_COUNT);
818 return npapi_plugins_enabled_;
821 void PluginServiceImpl::DisablePluginsDiscoveryForTesting() {
822 PluginList::Singleton()->DisablePluginsDiscovery();
825 #if defined(OS_MACOSX)
826 void PluginServiceImpl::AppActivated() {
827 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
828 base::Bind(&NotifyPluginsOfActivation));
830 #elif defined(OS_WIN)
832 bool GetPluginPropertyFromWindow(
833 HWND window, const wchar_t* plugin_atom_property,
834 base::string16* plugin_property) {
835 ATOM plugin_atom = static_cast<ATOM>(
836 reinterpret_cast<uintptr_t>(GetPropW(window, plugin_atom_property)));
837 if (plugin_atom != 0) {
838 WCHAR plugin_property_local[MAX_PATH] = {0};
839 GlobalGetAtomNameW(plugin_atom,
840 plugin_property_local,
841 ARRAYSIZE(plugin_property_local));
842 *plugin_property = plugin_property_local;
843 return true;
845 return false;
848 bool PluginServiceImpl::GetPluginInfoFromWindow(
849 HWND window,
850 base::string16* plugin_name,
851 base::string16* plugin_version) {
852 if (!IsPluginWindow(window))
853 return false;
856 DWORD process_id = 0;
857 GetWindowThreadProcessId(window, &process_id);
858 WebPluginInfo info;
859 if (!PluginProcessHost::GetWebPluginInfoFromPluginPid(process_id, &info))
860 return false;
862 *plugin_name = info.name;
863 *plugin_version = info.version;
864 return true;
867 bool PluginServiceImpl::IsPluginWindow(HWND window) {
868 return gfx::GetClassName(window) == base::string16(kNativeWindowClassName);
870 #endif
872 bool PluginServiceImpl::PpapiDevChannelSupported(
873 BrowserContext* browser_context,
874 const GURL& document_url) {
875 return content::GetContentClient()->browser()->
876 IsPluginAllowedToUseDevChannelAPIs(browser_context, document_url);
879 } // namespace content