blob: 417475239567526fd366630f517c5215a3fe1258 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2018 The Chromium Authors
Jeremy Romanb7024742018-06-18 22:00:222// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/renderer_host/plugin_registry_impl.h"
6
Lei Zhangd4f2c7ad2021-05-13 20:10:127#include "base/containers/contains.h"
Avi Drissmanadac21992023-01-11 23:46:398#include "base/functional/bind.h"
Jeremy Romanb7024742018-06-18 22:00:229#include "content/browser/plugin_service_impl.h"
Ehsan Karamad91413d72019-03-22 16:37:4810#include "content/public/browser/content_browser_client.h"
Jeremy Romanb7024742018-06-18 22:00:2211#include "content/public/browser/plugin_service_filter.h"
Clark DuVall1df2052b2019-08-05 19:58:4612#include "content/public/browser/render_process_host.h"
Ehsan Karamad91413d72019-03-22 16:37:4813#include "content/public/common/content_client.h"
K. Moon9ae6ef2c2022-08-18 01:30:0614#include "content/public/common/webplugininfo.h"
Jeremy Romanb7024742018-06-18 22:00:2215
16namespace content {
17
18namespace {
Peter Kastinge5a38ed2021-10-02 03:06:3519constexpr auto kPluginRefreshThreshold = base::Seconds(3);
Jeremy Romanb7024742018-06-18 22:00:2220} // namespace
21
Clark DuVall1df2052b2019-08-05 19:58:4622PluginRegistryImpl::PluginRegistryImpl(int render_process_id)
23 : render_process_id_(render_process_id) {}
Jeremy Romanb7024742018-06-18 22:00:2224
K. Moon1829fee2022-01-26 02:36:0225PluginRegistryImpl::~PluginRegistryImpl() = default;
Jeremy Romanb7024742018-06-18 22:00:2226
Miyoung Shinad0de7f2019-09-02 08:55:1127void PluginRegistryImpl::Bind(
28 mojo::PendingReceiver<blink::mojom::PluginRegistry> receiver) {
29 receivers_.Add(this, std::move(receiver));
Jeremy Romanb7024742018-06-18 22:00:2230}
31
K. Moon1829fee2022-01-26 02:36:0232void PluginRegistryImpl::GetPlugins(bool refresh, GetPluginsCallback callback) {
Jeremy Romanb7024742018-06-18 22:00:2233 auto* plugin_service = PluginServiceImpl::GetInstance();
34
35 // Don't refresh if the specified threshold has not been passed. Note that
36 // this check is performed before off-loading to the file thread. The reason
37 // we do this is that some pages tend to request that the list of plugins be
38 // refreshed at an excessive rate. This instigates disk scanning, as the list
39 // is accumulated by doing multiple reads from disk. This effect is
40 // multiplied when we have several pages requesting this operation.
41 if (refresh) {
42 const base::TimeTicks now = base::TimeTicks::Now();
43 if (now - last_plugin_refresh_time_ >= kPluginRefreshThreshold) {
44 // Only refresh if the threshold hasn't been exceeded yet.
45 plugin_service->RefreshPlugins();
46 last_plugin_refresh_time_ = now;
47 }
48 }
49
K. Moon1829fee2022-01-26 02:36:0250 plugin_service->GetPlugins(
51 base::BindOnce(&PluginRegistryImpl::GetPluginsComplete,
52 weak_factory_.GetWeakPtr(), std::move(callback)));
Jeremy Romanb7024742018-06-18 22:00:2253}
54
55void PluginRegistryImpl::GetPluginsComplete(
Jeremy Romanb7024742018-06-18 22:00:2256 GetPluginsCallback callback,
57 const std::vector<WebPluginInfo>& all_plugins) {
58 PluginServiceFilter* filter = PluginServiceImpl::GetInstance()->GetFilter();
59 std::vector<blink::mojom::PluginInfoPtr> plugins;
Clark DuVall1df2052b2019-08-05 19:58:4660 RenderProcessHost* rph = RenderProcessHost::FromID(render_process_id_);
61 if (!rph) {
62 std::move(callback).Run(std::move(plugins));
63 return;
64 }
65
Ehsan Karamad91413d72019-03-22 16:37:4866 base::flat_set<std::string> mime_handler_view_mime_types =
Ehsan Karamad466529d2019-05-24 03:24:4367 GetContentClient()->browser()->GetPluginMimeTypesWithExternalHandlers(
Clark DuVall1df2052b2019-08-05 19:58:4668 rph->GetBrowserContext());
Jeremy Romanb7024742018-06-18 22:00:2269
K. Moon343ae352021-10-27 22:10:5470 for (const auto& plugin : all_plugins) {
Rohit Bhatia23df04c2022-08-24 22:42:1471 if (!filter ||
72 filter->IsPluginAvailable(rph->GetBrowserContext(), plugin)) {
Jeremy Romanb7024742018-06-18 22:00:2273 auto plugin_blink = blink::mojom::PluginInfo::New();
74 plugin_blink->name = plugin.name;
75 plugin_blink->description = plugin.desc;
76 plugin_blink->filename = plugin.path.BaseName();
77 plugin_blink->background_color = plugin.background_color;
Ehsan Karamad466529d2019-05-24 03:24:4378 plugin_blink->may_use_external_handler = false;
Jeremy Romanb7024742018-06-18 22:00:2279 for (const auto& mime_type : plugin.mime_types) {
80 auto mime_type_blink = blink::mojom::PluginMimeType::New();
81 mime_type_blink->mime_type = mime_type.mime_type;
82 mime_type_blink->description = mime_type.description;
83 mime_type_blink->file_extensions = mime_type.file_extensions;
84 plugin_blink->mime_types.push_back(std::move(mime_type_blink));
Ehsan Karamad466529d2019-05-24 03:24:4385 if (!plugin_blink->may_use_external_handler) {
Jan Wilken Dörrie77c581a2019-06-07 16:25:0686 plugin_blink->may_use_external_handler =
87 base::Contains(mime_handler_view_mime_types, mime_type.mime_type);
Ehsan Karamad91413d72019-03-22 16:37:4888 }
Jeremy Romanb7024742018-06-18 22:00:2289 }
90 plugins.push_back(std::move(plugin_blink));
91 }
92 }
93
94 std::move(callback).Run(std::move(plugins));
95}
96
97} // namespace content