cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / browser / plugin_service_impl.cc
blobd19f6861c162b2a289d2ca1ccf85a13f3d21bf66
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/path_service.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/threading/thread.h"
19 #include "content/browser/ppapi_plugin_process_host.h"
20 #include "content/browser/renderer_host/render_process_host_impl.h"
21 #include "content/browser/renderer_host/render_view_host_impl.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 bool LoadPluginListInProcess() {
64 #if defined(OS_WIN)
65 return true;
66 #else
67 // If on POSIX, we don't want to load the list of NPAPI plugins in-process as
68 // that causes instability.
70 // Can't load the plugins on the utility thread when in single process mode
71 // since that requires GTK which can only be used on the main thread.
72 if (RenderProcessHost::run_renderer_in_process())
73 return true;
75 return !PluginService::GetInstance()->NPAPIPluginsSupported();
76 #endif
79 // Callback set on the PluginList to assert that plugin loading happens on the
80 // correct thread.
81 void WillLoadPluginsCallback(
82 base::SequencedWorkerPool::SequenceToken token) {
83 if (LoadPluginListInProcess()) {
84 CHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
85 token));
86 } else {
87 CHECK(false) << "Plugin loading should happen out-of-process.";
91 #if defined(OS_MACOSX)
92 void NotifyPluginsOfActivation() {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
95 for (PluginProcessHostIterator iter; !iter.Done(); ++iter)
96 iter->OnAppActivation();
98 #endif
100 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
101 void NotifyPluginDirChanged(const base::FilePath& path, bool error) {
102 if (error) {
103 // TODO(pastarmovj): Add some sensible error handling. Maybe silently
104 // stopping the watcher would be enough. Or possibly restart it.
105 NOTREACHED();
106 return;
108 VLOG(1) << "Watched path changed: " << path.value();
109 // Make the plugin list update itself
110 PluginList::Singleton()->RefreshPlugins();
111 BrowserThread::PostTask(
112 BrowserThread::UI, FROM_HERE,
113 base::Bind(&PluginService::PurgePluginListCache,
114 static_cast<BrowserContext*>(NULL), false));
116 #endif
118 void ForwardCallback(base::MessageLoopProxy* target_loop,
119 const PluginService::GetPluginsCallback& callback,
120 const std::vector<WebPluginInfo>& plugins) {
121 target_loop->PostTask(FROM_HERE, base::Bind(callback, plugins));
124 } // namespace
126 // static
127 PluginService* PluginService::GetInstance() {
128 return PluginServiceImpl::GetInstance();
131 void PluginService::PurgePluginListCache(BrowserContext* browser_context,
132 bool reload_pages) {
133 for (RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator();
134 !it.IsAtEnd(); it.Advance()) {
135 RenderProcessHost* host = it.GetCurrentValue();
136 if (!browser_context || host->GetBrowserContext() == browser_context)
137 host->Send(new ViewMsg_PurgePluginListCache(reload_pages));
141 // static
142 PluginServiceImpl* PluginServiceImpl::GetInstance() {
143 return Singleton<PluginServiceImpl>::get();
146 PluginServiceImpl::PluginServiceImpl()
147 : filter_(NULL) {
148 // Collect the total number of browser processes (which create
149 // PluginServiceImpl objects, to be precise). The number is used to normalize
150 // the number of processes which start at least one NPAPI/PPAPI Flash process.
151 static bool counted = false;
152 if (!counted) {
153 counted = true;
154 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage", TOTAL_BROWSER_PROCESSES,
155 FLASH_USAGE_ENUM_COUNT);
159 PluginServiceImpl::~PluginServiceImpl() {
160 // Make sure no plugin channel requests have been leaked.
161 DCHECK(pending_plugin_clients_.empty());
164 void PluginServiceImpl::Init() {
165 plugin_list_token_ = BrowserThread::GetBlockingPool()->GetSequenceToken();
166 PluginList::Singleton()->set_will_load_plugins_callback(
167 base::Bind(&WillLoadPluginsCallback, plugin_list_token_));
169 RegisterPepperPlugins();
171 // Load any specified on the command line as well.
172 const base::CommandLine* command_line =
173 base::CommandLine::ForCurrentProcess();
174 base::FilePath path =
175 command_line->GetSwitchValuePath(switches::kLoadPlugin);
176 if (!path.empty())
177 AddExtraPluginPath(path);
178 path = command_line->GetSwitchValuePath(switches::kExtraPluginDir);
179 if (!path.empty())
180 PluginList::Singleton()->AddExtraPluginDir(path);
182 if (command_line->HasSwitch(switches::kDisablePluginsDiscovery))
183 PluginList::Singleton()->DisablePluginsDiscovery();
186 void PluginServiceImpl::StartWatchingPlugins() {
187 // Start watching for changes in the plugin list. This means watching
188 // for changes in the Windows registry keys and on both Windows and POSIX
189 // watch for changes in the paths that are expected to contain plugins.
190 #if defined(OS_WIN)
191 if (hkcu_key_.Create(HKEY_CURRENT_USER,
192 kRegistryMozillaPlugins,
193 KEY_NOTIFY) == ERROR_SUCCESS) {
194 base::win::RegKey::ChangeCallback callback =
195 base::Bind(&PluginServiceImpl::OnKeyChanged, base::Unretained(this),
196 base::Unretained(&hkcu_key_));
197 hkcu_key_.StartWatching(callback);
199 if (hklm_key_.Create(HKEY_LOCAL_MACHINE,
200 kRegistryMozillaPlugins,
201 KEY_NOTIFY) == ERROR_SUCCESS) {
202 base::win::RegKey::ChangeCallback callback =
203 base::Bind(&PluginServiceImpl::OnKeyChanged, base::Unretained(this),
204 base::Unretained(&hkcu_key_));
205 hklm_key_.StartWatching(callback);
207 #endif
208 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
209 // On ChromeOS the user can't install plugins anyway and on Windows all
210 // important plugins register themselves in the registry so no need to do that.
212 // Get the list of all paths for registering the FilePathWatchers
213 // that will track and if needed reload the list of plugins on runtime.
214 std::vector<base::FilePath> plugin_dirs;
215 PluginList::Singleton()->GetPluginDirectories(&plugin_dirs);
217 for (size_t i = 0; i < plugin_dirs.size(); ++i) {
218 // FilePathWatcher can not handle non-absolute paths under windows.
219 // We don't watch for file changes in windows now but if this should ever
220 // be extended to Windows these lines might save some time of debugging.
221 #if defined(OS_WIN)
222 if (!plugin_dirs[i].IsAbsolute())
223 continue;
224 #endif
225 FilePathWatcher* watcher = new FilePathWatcher();
226 VLOG(1) << "Watching for changes in: " << plugin_dirs[i].value();
227 BrowserThread::PostTask(
228 BrowserThread::FILE, FROM_HERE,
229 base::Bind(&PluginServiceImpl::RegisterFilePathWatcher, watcher,
230 plugin_dirs[i]));
231 file_watchers_.push_back(watcher);
233 #endif
236 PluginProcessHost* PluginServiceImpl::FindNpapiPluginProcess(
237 const base::FilePath& plugin_path) {
238 for (PluginProcessHostIterator iter; !iter.Done(); ++iter) {
239 if (iter->info().path == plugin_path)
240 return *iter;
243 return NULL;
246 PpapiPluginProcessHost* PluginServiceImpl::FindPpapiPluginProcess(
247 const base::FilePath& plugin_path,
248 const base::FilePath& profile_data_directory) {
249 for (PpapiPluginProcessHostIterator iter; !iter.Done(); ++iter) {
250 if (iter->plugin_path() == plugin_path &&
251 iter->profile_data_directory() == profile_data_directory) {
252 return *iter;
255 return NULL;
258 PpapiPluginProcessHost* PluginServiceImpl::FindPpapiBrokerProcess(
259 const base::FilePath& broker_path) {
260 for (PpapiBrokerProcessHostIterator iter; !iter.Done(); ++iter) {
261 if (iter->plugin_path() == broker_path)
262 return *iter;
265 return NULL;
268 PluginProcessHost* PluginServiceImpl::FindOrStartNpapiPluginProcess(
269 int render_process_id,
270 const base::FilePath& plugin_path) {
271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
273 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path))
274 return NULL;
276 PluginProcessHost* plugin_host = FindNpapiPluginProcess(plugin_path);
277 if (plugin_host)
278 return plugin_host;
280 WebPluginInfo info;
281 if (!GetPluginInfoByPath(plugin_path, &info)) {
282 return NULL;
285 // Record when NPAPI Flash process is started for the first time.
286 static bool counted = false;
287 if (!counted && base::UTF16ToUTF8(info.name) == kFlashPluginName) {
288 counted = true;
289 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage",
290 START_NPAPI_FLASH_AT_LEAST_ONCE,
291 FLASH_USAGE_ENUM_COUNT);
293 #if defined(OS_CHROMEOS)
294 // TODO(ihf): Move to an earlier place once crbug.com/314301 is fixed. For now
295 // we still want Plugin.FlashUsage recorded if we end up here.
296 LOG(WARNING) << "Refusing to start npapi plugin on ChromeOS.";
297 return NULL;
298 #endif
299 // This plugin isn't loaded by any plugin process, so create a new process.
300 scoped_ptr<PluginProcessHost> new_host(new PluginProcessHost());
301 if (!new_host->Init(info)) {
302 NOTREACHED(); // Init is not expected to fail.
303 return NULL;
305 return new_host.release();
308 PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiPluginProcess(
309 int render_process_id,
310 const base::FilePath& plugin_path,
311 const base::FilePath& profile_data_directory) {
312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
314 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path)) {
315 VLOG(1) << "Unable to load ppapi plugin: " << plugin_path.MaybeAsASCII();
316 return NULL;
319 PpapiPluginProcessHost* plugin_host =
320 FindPpapiPluginProcess(plugin_path, profile_data_directory);
321 if (plugin_host)
322 return plugin_host;
324 // Validate that the plugin is actually registered.
325 PepperPluginInfo* info = GetRegisteredPpapiPluginInfo(plugin_path);
326 if (!info) {
327 VLOG(1) << "Unable to find ppapi plugin registration for: "
328 << plugin_path.MaybeAsASCII();
329 return NULL;
332 // Record when PPAPI Flash process is started for the first time.
333 static bool counted = false;
334 if (!counted && info->name == kFlashPluginName) {
335 counted = true;
336 UMA_HISTOGRAM_ENUMERATION("Plugin.FlashUsage",
337 START_PPAPI_FLASH_AT_LEAST_ONCE,
338 FLASH_USAGE_ENUM_COUNT);
341 // This plugin isn't loaded by any plugin process, so create a new process.
342 plugin_host = PpapiPluginProcessHost::CreatePluginHost(
343 *info, profile_data_directory);
344 if (!plugin_host) {
345 VLOG(1) << "Unable to create ppapi plugin process for: "
346 << plugin_path.MaybeAsASCII();
349 return plugin_host;
352 PpapiPluginProcessHost* PluginServiceImpl::FindOrStartPpapiBrokerProcess(
353 int render_process_id,
354 const base::FilePath& plugin_path) {
355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
357 if (filter_ && !filter_->CanLoadPlugin(render_process_id, plugin_path))
358 return NULL;
360 PpapiPluginProcessHost* plugin_host = FindPpapiBrokerProcess(plugin_path);
361 if (plugin_host)
362 return plugin_host;
364 // Validate that the plugin is actually registered.
365 PepperPluginInfo* info = GetRegisteredPpapiPluginInfo(plugin_path);
366 if (!info)
367 return NULL;
369 // TODO(ddorwin): Uncomment once out of process is supported.
370 // DCHECK(info->is_out_of_process);
372 // This broker isn't loaded by any broker process, so create a new process.
373 return PpapiPluginProcessHost::CreateBrokerHost(*info);
376 void PluginServiceImpl::OpenChannelToNpapiPlugin(
377 int render_process_id,
378 int render_frame_id,
379 const GURL& url,
380 const GURL& page_url,
381 const std::string& mime_type,
382 PluginProcessHost::Client* client) {
383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
384 DCHECK(!ContainsKey(pending_plugin_clients_, client));
385 pending_plugin_clients_.insert(client);
387 // Make sure plugins are loaded if necessary.
388 PluginServiceFilterParams params = {
389 render_process_id,
390 render_frame_id,
391 page_url,
392 client->GetResourceContext()
394 GetPlugins(base::Bind(
395 &PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin,
396 base::Unretained(this), params, url, mime_type, client));
399 void PluginServiceImpl::OpenChannelToPpapiPlugin(
400 int render_process_id,
401 const base::FilePath& plugin_path,
402 const base::FilePath& profile_data_directory,
403 PpapiPluginProcessHost::PluginClient* client) {
404 PpapiPluginProcessHost* plugin_host = FindOrStartPpapiPluginProcess(
405 render_process_id, plugin_path, profile_data_directory);
406 if (plugin_host) {
407 plugin_host->OpenChannelToPlugin(client);
408 } else {
409 // Send error.
410 client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0);
414 void PluginServiceImpl::OpenChannelToPpapiBroker(
415 int render_process_id,
416 const base::FilePath& path,
417 PpapiPluginProcessHost::BrokerClient* client) {
418 PpapiPluginProcessHost* plugin_host = FindOrStartPpapiBrokerProcess(
419 render_process_id, path);
420 if (plugin_host) {
421 plugin_host->OpenChannelToPlugin(client);
422 } else {
423 // Send error.
424 client->OnPpapiChannelOpened(IPC::ChannelHandle(), base::kNullProcessId, 0);
428 void PluginServiceImpl::CancelOpenChannelToNpapiPlugin(
429 PluginProcessHost::Client* client) {
430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
431 DCHECK(ContainsKey(pending_plugin_clients_, client));
432 pending_plugin_clients_.erase(client);
435 void PluginServiceImpl::ForwardGetAllowedPluginForOpenChannelToPlugin(
436 const PluginServiceFilterParams& params,
437 const GURL& url,
438 const std::string& mime_type,
439 PluginProcessHost::Client* client,
440 const std::vector<WebPluginInfo>&) {
441 GetAllowedPluginForOpenChannelToPlugin(
442 params.render_process_id, params.render_frame_id, url, params.page_url,
443 mime_type, client, params.resource_context);
446 void PluginServiceImpl::GetAllowedPluginForOpenChannelToPlugin(
447 int render_process_id,
448 int render_frame_id,
449 const GURL& url,
450 const GURL& page_url,
451 const std::string& mime_type,
452 PluginProcessHost::Client* client,
453 ResourceContext* resource_context) {
454 WebPluginInfo info;
455 bool allow_wildcard = true;
456 bool found = GetPluginInfo(
457 render_process_id, render_frame_id, resource_context,
458 url, page_url, mime_type, allow_wildcard,
459 NULL, &info, NULL);
460 base::FilePath plugin_path;
461 if (found)
462 plugin_path = info.path;
464 // Now we jump back to the IO thread to finish opening the channel.
465 BrowserThread::PostTask(
466 BrowserThread::IO, FROM_HERE,
467 base::Bind(&PluginServiceImpl::FinishOpenChannelToPlugin,
468 base::Unretained(this),
469 render_process_id,
470 plugin_path,
471 client));
474 void PluginServiceImpl::FinishOpenChannelToPlugin(
475 int render_process_id,
476 const base::FilePath& plugin_path,
477 PluginProcessHost::Client* client) {
478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
480 // Make sure it hasn't been canceled yet.
481 if (!ContainsKey(pending_plugin_clients_, client))
482 return;
483 pending_plugin_clients_.erase(client);
485 PluginProcessHost* plugin_host = FindOrStartNpapiPluginProcess(
486 render_process_id, plugin_path);
487 if (plugin_host) {
488 client->OnFoundPluginProcessHost(plugin_host);
489 plugin_host->OpenChannelToPlugin(client);
490 } else {
491 client->OnError();
495 bool PluginServiceImpl::GetPluginInfoArray(
496 const GURL& url,
497 const std::string& mime_type,
498 bool allow_wildcard,
499 std::vector<WebPluginInfo>* plugins,
500 std::vector<std::string>* actual_mime_types) {
501 bool use_stale = false;
502 PluginList::Singleton()->GetPluginInfoArray(
503 url, mime_type, allow_wildcard, &use_stale, NPAPIPluginsSupported(),
504 plugins, actual_mime_types);
505 return use_stale;
508 bool PluginServiceImpl::GetPluginInfo(int render_process_id,
509 int render_frame_id,
510 ResourceContext* context,
511 const GURL& url,
512 const GURL& page_url,
513 const std::string& mime_type,
514 bool allow_wildcard,
515 bool* is_stale,
516 WebPluginInfo* info,
517 std::string* actual_mime_type) {
518 std::vector<WebPluginInfo> plugins;
519 std::vector<std::string> mime_types;
520 bool stale = GetPluginInfoArray(
521 url, mime_type, allow_wildcard, &plugins, &mime_types);
522 if (is_stale)
523 *is_stale = stale;
525 for (size_t i = 0; i < plugins.size(); ++i) {
526 if (!filter_ || filter_->IsPluginAvailable(render_process_id,
527 render_frame_id,
528 context,
529 url,
530 page_url,
531 &plugins[i])) {
532 *info = plugins[i];
533 if (actual_mime_type)
534 *actual_mime_type = mime_types[i];
535 return true;
538 return false;
541 bool PluginServiceImpl::GetPluginInfoByPath(const base::FilePath& plugin_path,
542 WebPluginInfo* info) {
543 std::vector<WebPluginInfo> plugins;
544 PluginList::Singleton()->GetPluginsNoRefresh(&plugins);
546 for (std::vector<WebPluginInfo>::iterator it = plugins.begin();
547 it != plugins.end();
548 ++it) {
549 if (it->path == plugin_path) {
550 *info = *it;
551 return true;
555 return false;
558 base::string16 PluginServiceImpl::GetPluginDisplayNameByPath(
559 const base::FilePath& path) {
560 base::string16 plugin_name = path.LossyDisplayName();
561 WebPluginInfo info;
562 if (PluginService::GetInstance()->GetPluginInfoByPath(path, &info) &&
563 !info.name.empty()) {
564 plugin_name = info.name;
565 #if defined(OS_MACOSX)
566 // Many plugins on the Mac have .plugin in the actual name, which looks
567 // terrible, so look for that and strip it off if present.
568 const std::string kPluginExtension = ".plugin";
569 if (EndsWith(plugin_name, base::ASCIIToUTF16(kPluginExtension), true))
570 plugin_name.erase(plugin_name.length() - kPluginExtension.length());
571 #endif // OS_MACOSX
573 return plugin_name;
576 void PluginServiceImpl::GetPlugins(const GetPluginsCallback& callback) {
577 scoped_refptr<base::MessageLoopProxy> target_loop(
578 base::MessageLoop::current()->message_loop_proxy());
580 if (LoadPluginListInProcess()) {
581 BrowserThread::GetBlockingPool()->
582 PostSequencedWorkerTaskWithShutdownBehavior(
583 plugin_list_token_,
584 FROM_HERE,
585 base::Bind(&PluginServiceImpl::GetPluginsInternal,
586 base::Unretained(this),
587 target_loop, callback),
588 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
589 return;
591 #if defined(OS_POSIX)
592 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
593 base::Bind(&PluginServiceImpl::GetPluginsOnIOThread,
594 base::Unretained(this), target_loop, callback));
595 #else
596 NOTREACHED();
597 #endif
600 void PluginServiceImpl::GetPluginsInternal(
601 base::MessageLoopProxy* target_loop,
602 const PluginService::GetPluginsCallback& callback) {
603 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
604 plugin_list_token_));
606 std::vector<WebPluginInfo> plugins;
607 PluginList::Singleton()->GetPlugins(&plugins, NPAPIPluginsSupported());
609 target_loop->PostTask(FROM_HERE,
610 base::Bind(callback, plugins));
613 #if defined(OS_POSIX)
614 void PluginServiceImpl::GetPluginsOnIOThread(
615 base::MessageLoopProxy* target_loop,
616 const GetPluginsCallback& callback) {
617 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
619 // If we switch back to loading plugins in process, then we need to make
620 // sure g_thread_init() gets called since plugins may call glib at load.
622 if (!plugin_loader_.get())
623 plugin_loader_ = new PluginLoaderPosix;
625 plugin_loader_->GetPlugins(
626 base::Bind(&ForwardCallback, make_scoped_refptr(target_loop), callback));
628 #endif
630 #if defined(OS_WIN)
631 void PluginServiceImpl::OnKeyChanged(base::win::RegKey* key) {
632 key->StartWatching(base::Bind(&PluginServiceImpl::OnKeyChanged,
633 base::Unretained(this),
634 base::Unretained(&hkcu_key_)));
636 PluginList::Singleton()->RefreshPlugins();
637 PurgePluginListCache(NULL, false);
639 #endif // defined(OS_WIN)
641 void PluginServiceImpl::RegisterPepperPlugins() {
642 ComputePepperPluginList(&ppapi_plugins_);
643 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
644 RegisterInternalPlugin(ppapi_plugins_[i].ToWebPluginInfo(), true);
648 // There should generally be very few plugins so a brute-force search is fine.
649 PepperPluginInfo* PluginServiceImpl::GetRegisteredPpapiPluginInfo(
650 const base::FilePath& plugin_path) {
651 PepperPluginInfo* info = NULL;
652 for (size_t i = 0; i < ppapi_plugins_.size(); ++i) {
653 if (ppapi_plugins_[i].path == plugin_path) {
654 info = &ppapi_plugins_[i];
655 break;
658 if (info)
659 return info;
660 // We did not find the plugin in our list. But wait! the plugin can also
661 // be a latecomer, as it happens with pepper flash. This information
662 // can be obtained from the PluginList singleton and we can use it to
663 // construct it and add it to the list. This same deal needs to be done
664 // in the renderer side in PepperPluginRegistry.
665 WebPluginInfo webplugin_info;
666 if (!GetPluginInfoByPath(plugin_path, &webplugin_info))
667 return NULL;
668 PepperPluginInfo new_pepper_info;
669 if (!MakePepperPluginInfo(webplugin_info, &new_pepper_info))
670 return NULL;
671 ppapi_plugins_.push_back(new_pepper_info);
672 return &ppapi_plugins_[ppapi_plugins_.size() - 1];
675 #if defined(OS_POSIX) && !defined(OS_OPENBSD) && !defined(OS_ANDROID)
676 // static
677 void PluginServiceImpl::RegisterFilePathWatcher(FilePathWatcher* watcher,
678 const base::FilePath& path) {
679 bool result = watcher->Watch(path, false,
680 base::Bind(&NotifyPluginDirChanged));
681 DCHECK(result);
683 #endif
685 void PluginServiceImpl::SetFilter(PluginServiceFilter* filter) {
686 filter_ = filter;
689 PluginServiceFilter* PluginServiceImpl::GetFilter() {
690 return filter_;
693 void PluginServiceImpl::ForcePluginShutdown(const base::FilePath& plugin_path) {
694 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
695 BrowserThread::PostTask(
696 BrowserThread::IO, FROM_HERE,
697 base::Bind(&PluginServiceImpl::ForcePluginShutdown,
698 base::Unretained(this), plugin_path));
699 return;
702 PluginProcessHost* plugin = FindNpapiPluginProcess(plugin_path);
703 if (plugin)
704 plugin->ForceShutdown();
707 static const unsigned int kMaxCrashesPerInterval = 3;
708 static const unsigned int kCrashesInterval = 120;
710 void PluginServiceImpl::RegisterPluginCrash(const base::FilePath& path) {
711 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
712 std::map<base::FilePath, std::vector<base::Time> >::iterator i =
713 crash_times_.find(path);
714 if (i == crash_times_.end()) {
715 crash_times_[path] = std::vector<base::Time>();
716 i = crash_times_.find(path);
718 if (i->second.size() == kMaxCrashesPerInterval) {
719 i->second.erase(i->second.begin());
721 base::Time time = base::Time::Now();
722 i->second.push_back(time);
725 bool PluginServiceImpl::IsPluginUnstable(const base::FilePath& path) {
726 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
727 std::map<base::FilePath, std::vector<base::Time> >::const_iterator i =
728 crash_times_.find(path);
729 if (i == crash_times_.end()) {
730 return false;
732 if (i->second.size() != kMaxCrashesPerInterval) {
733 return false;
735 base::TimeDelta delta = base::Time::Now() - i->second[0];
736 return delta.InSeconds() <= kCrashesInterval;
739 void PluginServiceImpl::RefreshPlugins() {
740 PluginList::Singleton()->RefreshPlugins();
743 void PluginServiceImpl::AddExtraPluginPath(const base::FilePath& path) {
744 if (!NPAPIPluginsSupported()) {
745 // TODO(jam): remove and just have CHECK once we're sure this doesn't get
746 // triggered.
747 DVLOG(0) << "NPAPI plugins not supported";
748 return;
750 PluginList::Singleton()->AddExtraPluginPath(path);
753 void PluginServiceImpl::RemoveExtraPluginPath(const base::FilePath& path) {
754 PluginList::Singleton()->RemoveExtraPluginPath(path);
757 void PluginServiceImpl::AddExtraPluginDir(const base::FilePath& path) {
758 PluginList::Singleton()->AddExtraPluginDir(path);
761 void PluginServiceImpl::RegisterInternalPlugin(
762 const WebPluginInfo& info,
763 bool add_at_beginning) {
764 if (!NPAPIPluginsSupported() &&
765 info.type == WebPluginInfo::PLUGIN_TYPE_NPAPI) {
766 DVLOG(0) << "Don't register NPAPI plugins when they're not supported";
767 return;
769 PluginList::Singleton()->RegisterInternalPlugin(info, add_at_beginning);
772 void PluginServiceImpl::UnregisterInternalPlugin(const base::FilePath& path) {
773 PluginList::Singleton()->UnregisterInternalPlugin(path);
776 void PluginServiceImpl::GetInternalPlugins(
777 std::vector<WebPluginInfo>* plugins) {
778 PluginList::Singleton()->GetInternalPlugins(plugins);
781 bool PluginServiceImpl::NPAPIPluginsSupported() {
782 #if defined(OS_WIN) || defined(OS_MACOSX)
783 return true;
784 #else
785 return false;
786 #endif
789 void PluginServiceImpl::DisablePluginsDiscoveryForTesting() {
790 PluginList::Singleton()->DisablePluginsDiscovery();
793 #if defined(OS_MACOSX)
794 void PluginServiceImpl::AppActivated() {
795 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
796 base::Bind(&NotifyPluginsOfActivation));
798 #elif defined(OS_WIN)
800 bool GetPluginPropertyFromWindow(
801 HWND window, const wchar_t* plugin_atom_property,
802 base::string16* plugin_property) {
803 ATOM plugin_atom = reinterpret_cast<ATOM>(
804 GetPropW(window, plugin_atom_property));
805 if (plugin_atom != 0) {
806 WCHAR plugin_property_local[MAX_PATH] = {0};
807 GlobalGetAtomNameW(plugin_atom,
808 plugin_property_local,
809 ARRAYSIZE(plugin_property_local));
810 *plugin_property = plugin_property_local;
811 return true;
813 return false;
816 bool PluginServiceImpl::GetPluginInfoFromWindow(
817 HWND window,
818 base::string16* plugin_name,
819 base::string16* plugin_version) {
820 if (!IsPluginWindow(window))
821 return false;
824 DWORD process_id = 0;
825 GetWindowThreadProcessId(window, &process_id);
826 WebPluginInfo info;
827 if (!PluginProcessHost::GetWebPluginInfoFromPluginPid(process_id, &info))
828 return false;
830 *plugin_name = info.name;
831 *plugin_version = info.version;
832 return true;
835 bool PluginServiceImpl::IsPluginWindow(HWND window) {
836 return gfx::GetClassName(window) == base::string16(kNativeWindowClassName);
838 #endif
840 bool PluginServiceImpl::PpapiDevChannelSupported(
841 BrowserContext* browser_context,
842 const GURL& document_url) {
843 return content::GetContentClient()->browser()->
844 IsPluginAllowedToUseDevChannelAPIs(browser_context, document_url);
847 } // namespace content