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