Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 2 | // 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 Zhang | d4f2c7ad | 2021-05-13 20:10:12 | [diff] [blame] | 7 | #include "base/containers/contains.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 8 | #include "base/functional/bind.h" |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 9 | #include "content/browser/plugin_service_impl.h" |
Ehsan Karamad | 91413d7 | 2019-03-22 16:37:48 | [diff] [blame] | 10 | #include "content/public/browser/content_browser_client.h" |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 11 | #include "content/public/browser/plugin_service_filter.h" |
Clark DuVall | 1df2052b | 2019-08-05 19:58:46 | [diff] [blame] | 12 | #include "content/public/browser/render_process_host.h" |
Ehsan Karamad | 91413d7 | 2019-03-22 16:37:48 | [diff] [blame] | 13 | #include "content/public/common/content_client.h" |
K. Moon | 9ae6ef2c | 2022-08-18 01:30:06 | [diff] [blame] | 14 | #include "content/public/common/webplugininfo.h" |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 15 | |
| 16 | namespace content { |
| 17 | |
| 18 | namespace { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 19 | constexpr auto kPluginRefreshThreshold = base::Seconds(3); |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 20 | } // namespace |
| 21 | |
Clark DuVall | 1df2052b | 2019-08-05 19:58:46 | [diff] [blame] | 22 | PluginRegistryImpl::PluginRegistryImpl(int render_process_id) |
| 23 | : render_process_id_(render_process_id) {} |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 24 | |
K. Moon | 1829fee | 2022-01-26 02:36:02 | [diff] [blame] | 25 | PluginRegistryImpl::~PluginRegistryImpl() = default; |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 26 | |
Miyoung Shin | ad0de7f | 2019-09-02 08:55:11 | [diff] [blame] | 27 | void PluginRegistryImpl::Bind( |
| 28 | mojo::PendingReceiver<blink::mojom::PluginRegistry> receiver) { |
| 29 | receivers_.Add(this, std::move(receiver)); |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 30 | } |
| 31 | |
K. Moon | 1829fee | 2022-01-26 02:36:02 | [diff] [blame] | 32 | void PluginRegistryImpl::GetPlugins(bool refresh, GetPluginsCallback callback) { |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 33 | 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. Moon | 1829fee | 2022-01-26 02:36:02 | [diff] [blame] | 50 | plugin_service->GetPlugins( |
| 51 | base::BindOnce(&PluginRegistryImpl::GetPluginsComplete, |
| 52 | weak_factory_.GetWeakPtr(), std::move(callback))); |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void PluginRegistryImpl::GetPluginsComplete( |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 56 | GetPluginsCallback callback, |
| 57 | const std::vector<WebPluginInfo>& all_plugins) { |
| 58 | PluginServiceFilter* filter = PluginServiceImpl::GetInstance()->GetFilter(); |
| 59 | std::vector<blink::mojom::PluginInfoPtr> plugins; |
Clark DuVall | 1df2052b | 2019-08-05 19:58:46 | [diff] [blame] | 60 | RenderProcessHost* rph = RenderProcessHost::FromID(render_process_id_); |
| 61 | if (!rph) { |
| 62 | std::move(callback).Run(std::move(plugins)); |
| 63 | return; |
| 64 | } |
| 65 | |
Ehsan Karamad | 91413d7 | 2019-03-22 16:37:48 | [diff] [blame] | 66 | base::flat_set<std::string> mime_handler_view_mime_types = |
Ehsan Karamad | 466529d | 2019-05-24 03:24:43 | [diff] [blame] | 67 | GetContentClient()->browser()->GetPluginMimeTypesWithExternalHandlers( |
Clark DuVall | 1df2052b | 2019-08-05 19:58:46 | [diff] [blame] | 68 | rph->GetBrowserContext()); |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 69 | |
K. Moon | 343ae35 | 2021-10-27 22:10:54 | [diff] [blame] | 70 | for (const auto& plugin : all_plugins) { |
Rohit Bhatia | 23df04c | 2022-08-24 22:42:14 | [diff] [blame] | 71 | if (!filter || |
| 72 | filter->IsPluginAvailable(rph->GetBrowserContext(), plugin)) { |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 73 | 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 Karamad | 466529d | 2019-05-24 03:24:43 | [diff] [blame] | 78 | plugin_blink->may_use_external_handler = false; |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 79 | 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 Karamad | 466529d | 2019-05-24 03:24:43 | [diff] [blame] | 85 | if (!plugin_blink->may_use_external_handler) { |
Jan Wilken Dörrie | 77c581a | 2019-06-07 16:25:06 | [diff] [blame] | 86 | plugin_blink->may_use_external_handler = |
| 87 | base::Contains(mime_handler_view_mime_types, mime_type.mime_type); |
Ehsan Karamad | 91413d7 | 2019-03-22 16:37:48 | [diff] [blame] | 88 | } |
Jeremy Roman | b702474 | 2018-06-18 22:00:22 | [diff] [blame] | 89 | } |
| 90 | plugins.push_back(std::move(plugin_blink)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | std::move(callback).Run(std::move(plugins)); |
| 95 | } |
| 96 | |
| 97 | } // namespace content |