Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / dom / base / nsPluginArray.cpp
blob7517f0506cf65c6c4c36d6d24f4ad9cf4702ea0e
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 "nsContentUtils.h"
17 using namespace mozilla;
18 using namespace mozilla::dom;
20 // These plugin and mime types are hard-coded by the HTML spec.
21 // The "main" plugin name is used with the only plugin that is
22 // referenced by MIME types (via nsMimeType::GetEnabledPlugin).
23 // The "extra" of the plugin names are associated with MIME types that
24 // reference the main plugin.
25 // This is all defined in the HTML spec, section 8.9.1.6
26 // "PDF Viewing Support".
27 static const nsLiteralString kMainPluginName = u"PDF Viewer"_ns;
28 static const nsLiteralString kExtraPluginNames[] = {
29 u"Chrome PDF Viewer"_ns, u"Chromium PDF Viewer"_ns,
30 u"Microsoft Edge PDF Viewer"_ns, u"WebKit built-in PDF"_ns};
31 static const nsLiteralString kMimeTypeNames[] = {u"application/pdf"_ns,
32 u"text/pdf"_ns};
34 nsPluginArray::nsPluginArray(nsPIDOMWindowInner* aWindow) : mWindow(aWindow) {
35 // Create the hard-coded PDF plugin types that share MIME type arrays.
36 mPlugins[0] = MakeRefPtr<nsPluginElement>(this, aWindow, kMainPluginName);
38 mozilla::Array<RefPtr<nsMimeType>, 2> mimeTypes;
39 for (uint32_t i = 0; i < ArrayLength(kMimeTypeNames); ++i) {
40 mimeTypes[i] = MakeRefPtr<nsMimeType>(mPlugins[0], kMimeTypeNames[i]);
42 mMimeTypeArray = MakeRefPtr<nsMimeTypeArray>(aWindow, mimeTypes);
44 for (uint32_t i = 0; i < ArrayLength(kExtraPluginNames); ++i) {
45 mPlugins[i + 1] =
46 MakeRefPtr<nsPluginElement>(this, aWindow, kExtraPluginNames[i]);
50 nsPluginArray::~nsPluginArray() = default;
52 nsPIDOMWindowInner* nsPluginArray::GetParentObject() const {
53 MOZ_ASSERT(mWindow);
54 return mWindow;
57 JSObject* nsPluginArray::WrapObject(JSContext* aCx,
58 JS::Handle<JSObject*> aGivenProto) {
59 return PluginArray_Binding::Wrap(aCx, this, aGivenProto);
62 nsPluginElement* nsPluginArray::IndexedGetter(uint32_t aIndex, bool& aFound) {
63 if (!ForceNoPlugins() && aIndex < ArrayLength(mPlugins)) {
64 aFound = true;
65 return mPlugins[aIndex];
68 aFound = false;
69 return nullptr;
72 nsPluginElement* nsPluginArray::NamedGetter(const nsAString& aName,
73 bool& aFound) {
74 if (ForceNoPlugins()) {
75 aFound = false;
76 return nullptr;
79 for (const auto& plugin : mPlugins) {
80 if (plugin->Name().Equals(aName)) {
81 aFound = true;
82 return plugin;
86 aFound = false;
87 return nullptr;
90 void nsPluginArray::GetSupportedNames(nsTArray<nsString>& aRetval) {
91 if (ForceNoPlugins()) {
92 return;
95 for (auto& plugin : mPlugins) {
96 aRetval.AppendElement(plugin->Name());
100 bool nsPluginArray::ForceNoPlugins() { return StaticPrefs::pdfjs_disabled(); }
102 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPluginArray)
103 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPluginArray)
104 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPluginArray)
105 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
106 NS_INTERFACE_MAP_ENTRY(nsISupports)
107 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
108 NS_INTERFACE_MAP_END
110 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WEAK(nsPluginArray, mPlugins[0],
111 mPlugins[1], mPlugins[2],
112 mPlugins[3], mPlugins[4],
113 mMimeTypeArray, mWindow)
115 // nsPluginElement implementation.
117 nsPluginElement::nsPluginElement(nsPluginArray* aPluginArray,
118 nsPIDOMWindowInner* aWindow,
119 const nsAString& aName)
120 : mPluginArray(aPluginArray), mWindow(aWindow), mName(aName) {}
122 nsPluginArray* nsPluginElement::GetParentObject() const { return mPluginArray; }
124 JSObject* nsPluginElement::WrapObject(JSContext* aCx,
125 JS::Handle<JSObject*> aGivenProto) {
126 return Plugin_Binding::Wrap(aCx, this, aGivenProto);
129 nsMimeType* nsPluginElement::IndexedGetter(uint32_t aIndex, bool& aFound) {
130 return MimeTypeArray()->IndexedGetter(aIndex, aFound);
133 nsMimeType* nsPluginElement::NamedGetter(const nsAString& aName, bool& aFound) {
134 return MimeTypeArray()->NamedGetter(aName, aFound);
137 void nsPluginElement::GetSupportedNames(nsTArray<nsString>& retval) {
138 return MimeTypeArray()->GetSupportedNames(retval);
141 uint32_t nsPluginElement::Length() { return MimeTypeArray()->Length(); }
143 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPluginElement)
144 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPluginElement)
145 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPluginElement)
146 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
147 NS_INTERFACE_MAP_ENTRY(nsISupports)
148 NS_INTERFACE_MAP_END
150 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsPluginElement, mWindow, mPluginArray)