Bug 1812499 [wpt PR 38184] - Simplify handling of name-to-subdir mapping in canvas...
[gecko.git] / dom / base / nsPluginArray.cpp
blob90b498fdde4413c27c919a00bdbd197216f7dd26
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsPluginArray.h"
9 #include "mozilla/dom/PluginArrayBinding.h"
10 #include "mozilla/dom/PluginBinding.h"
11 #include "mozilla/StaticPrefs_pdfjs.h"
13 #include "nsMimeTypeArray.h"
14 #include "nsPIDOMWindow.h"
15 #include "nsGlobalWindowInner.h"
16 #include "nsContentUtils.h"
18 using namespace mozilla;
19 using namespace mozilla::dom;
21 // These plugin and mime types are hard-coded by the HTML spec.
22 // The "main" plugin name is used with the only plugin that is
23 // referenced by MIME types (via nsMimeType::GetEnabledPlugin).
24 // The "extra" of the plugin names are associated with MIME types that
25 // reference the main plugin.
26 // This is all defined in the HTML spec, section 8.9.1.6
27 // "PDF Viewing Support".
28 static const nsLiteralString kMainPluginName = u"PDF Viewer"_ns;
29 static const nsLiteralString kExtraPluginNames[] = {
30 u"Chrome PDF Viewer"_ns, u"Chromium PDF Viewer"_ns,
31 u"Microsoft Edge PDF Viewer"_ns, u"WebKit built-in PDF"_ns};
32 static const nsLiteralString kMimeTypeNames[] = {u"application/pdf"_ns,
33 u"text/pdf"_ns};
35 nsPluginArray::nsPluginArray(nsPIDOMWindowInner* aWindow) : mWindow(aWindow) {
36 // Create the hard-coded PDF plugin types that share MIME type arrays.
37 mPlugins[0] = MakeRefPtr<nsPluginElement>(this, aWindow, kMainPluginName);
39 mozilla::Array<RefPtr<nsMimeType>, 2> mimeTypes;
40 for (uint32_t i = 0; i < ArrayLength(kMimeTypeNames); ++i) {
41 mimeTypes[i] = MakeRefPtr<nsMimeType>(mPlugins[0], kMimeTypeNames[i]);
43 mMimeTypeArray = MakeRefPtr<nsMimeTypeArray>(aWindow, mimeTypes);
45 for (uint32_t i = 0; i < ArrayLength(kExtraPluginNames); ++i) {
46 mPlugins[i + 1] =
47 MakeRefPtr<nsPluginElement>(this, aWindow, kExtraPluginNames[i]);
51 nsPluginArray::~nsPluginArray() = default;
53 nsPIDOMWindowInner* nsPluginArray::GetParentObject() const {
54 MOZ_ASSERT(mWindow);
55 return mWindow;
58 JSObject* nsPluginArray::WrapObject(JSContext* aCx,
59 JS::Handle<JSObject*> aGivenProto) {
60 return PluginArray_Binding::Wrap(aCx, this, aGivenProto);
63 nsPluginElement* nsPluginArray::IndexedGetter(uint32_t aIndex, bool& aFound) {
64 if (!ForceNoPlugins() && aIndex < ArrayLength(mPlugins)) {
65 aFound = true;
66 return mPlugins[aIndex];
69 aFound = false;
70 return nullptr;
73 nsPluginElement* nsPluginArray::NamedGetter(const nsAString& aName,
74 bool& aFound) {
75 if (ForceNoPlugins()) {
76 aFound = false;
77 return nullptr;
80 for (const auto& plugin : mPlugins) {
81 if (plugin->Name().Equals(aName)) {
82 aFound = true;
83 return plugin;
87 aFound = false;
88 return nullptr;
91 void nsPluginArray::GetSupportedNames(nsTArray<nsString>& aRetval) {
92 if (ForceNoPlugins()) {
93 return;
96 for (auto& plugin : mPlugins) {
97 aRetval.AppendElement(plugin->Name());
101 bool nsPluginArray::ForceNoPlugins() {
102 return StaticPrefs::pdfjs_disabled() &&
103 !nsGlobalWindowInner::Cast(mWindow)->ShouldResistFingerprinting();
106 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPluginArray)
107 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPluginArray)
108 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPluginArray)
109 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
110 NS_INTERFACE_MAP_ENTRY(nsISupports)
111 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
112 NS_INTERFACE_MAP_END
114 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WEAK(nsPluginArray, mPlugins[0],
115 mPlugins[1], mPlugins[2],
116 mPlugins[3], mPlugins[4],
117 mMimeTypeArray, mWindow)
119 // nsPluginElement implementation.
121 nsPluginElement::nsPluginElement(nsPluginArray* aPluginArray,
122 nsPIDOMWindowInner* aWindow,
123 const nsAString& aName)
124 : mPluginArray(aPluginArray), mWindow(aWindow), mName(aName) {}
126 nsPluginArray* nsPluginElement::GetParentObject() const { return mPluginArray; }
128 JSObject* nsPluginElement::WrapObject(JSContext* aCx,
129 JS::Handle<JSObject*> aGivenProto) {
130 return Plugin_Binding::Wrap(aCx, this, aGivenProto);
133 nsMimeType* nsPluginElement::IndexedGetter(uint32_t aIndex, bool& aFound) {
134 return MimeTypeArray()->IndexedGetter(aIndex, aFound);
137 nsMimeType* nsPluginElement::NamedGetter(const nsAString& aName, bool& aFound) {
138 return MimeTypeArray()->NamedGetter(aName, aFound);
141 void nsPluginElement::GetSupportedNames(nsTArray<nsString>& retval) {
142 return MimeTypeArray()->GetSupportedNames(retval);
145 uint32_t nsPluginElement::Length() { return MimeTypeArray()->Length(); }
147 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPluginElement)
148 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPluginElement)
149 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPluginElement)
150 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
151 NS_INTERFACE_MAP_ENTRY(nsISupports)
152 NS_INTERFACE_MAP_END
154 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsPluginElement, mWindow, mPluginArray)