Bug 1686668 [wpt PR 27185] - Update wpt metadata, a=testonly
[gecko.git] / dom / base / nsMimeTypeArray.cpp
blob14f47359189a01959399807e72b02cca0316e033
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 "nsMimeTypeArray.h"
9 #include "mozilla/dom/MimeTypeArrayBinding.h"
10 #include "mozilla/dom/MimeTypeBinding.h"
11 #include "nsPIDOMWindow.h"
12 #include "nsPluginArray.h"
13 #include "mozilla/dom/Navigator.h"
14 #include "nsServiceManagerUtils.h"
15 #include "nsContentUtils.h"
16 #include "nsPluginTags.h"
18 using namespace mozilla;
19 using namespace mozilla::dom;
21 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsMimeTypeArray)
22 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsMimeTypeArray)
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsMimeTypeArray)
24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
25 NS_INTERFACE_MAP_ENTRY(nsISupports)
26 NS_INTERFACE_MAP_END
28 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsMimeTypeArray, mWindow, mMimeTypes,
29 mCTPMimeTypes)
31 nsMimeTypeArray::nsMimeTypeArray(nsPIDOMWindowInner* aWindow)
32 : mWindow(aWindow) {}
34 nsMimeTypeArray::~nsMimeTypeArray() = default;
36 JSObject* nsMimeTypeArray::WrapObject(JSContext* aCx,
37 JS::Handle<JSObject*> aGivenProto) {
38 return MimeTypeArray_Binding::Wrap(aCx, this, aGivenProto);
41 void nsMimeTypeArray::Refresh() {
42 mMimeTypes.Clear();
43 mCTPMimeTypes.Clear();
46 nsPIDOMWindowInner* nsMimeTypeArray::GetParentObject() const {
47 MOZ_ASSERT(mWindow);
48 return mWindow;
51 nsMimeType* nsMimeTypeArray::Item(uint32_t aIndex, CallerType aCallerType) {
52 bool unused;
53 return IndexedGetter(aIndex, unused, aCallerType);
56 nsMimeType* nsMimeTypeArray::NamedItem(const nsAString& aName,
57 CallerType aCallerType) {
58 bool unused;
59 return NamedGetter(aName, unused, aCallerType);
62 nsMimeType* nsMimeTypeArray::IndexedGetter(uint32_t aIndex, bool& aFound,
63 CallerType aCallerType) {
64 aFound = false;
66 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
67 return nullptr;
70 EnsurePluginMimeTypes();
72 if (aIndex >= mMimeTypes.Length()) {
73 return nullptr;
76 aFound = true;
78 return mMimeTypes[aIndex];
81 static nsMimeType* FindMimeType(const nsTArray<RefPtr<nsMimeType>>& aMimeTypes,
82 const nsAString& aType) {
83 for (uint32_t i = 0; i < aMimeTypes.Length(); ++i) {
84 nsMimeType* mimeType = aMimeTypes[i];
85 if (aType.Equals(mimeType->Type())) {
86 return mimeType;
90 return nullptr;
93 nsMimeType* nsMimeTypeArray::NamedGetter(const nsAString& aName, bool& aFound,
94 CallerType aCallerType) {
95 aFound = false;
97 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
98 return nullptr;
101 EnsurePluginMimeTypes();
103 nsString lowerName(aName);
104 ToLowerCase(lowerName);
106 nsMimeType* mimeType = FindMimeType(mMimeTypes, lowerName);
107 if (mimeType) {
108 aFound = true;
109 return mimeType;
111 nsMimeType* hiddenType = FindMimeType(mCTPMimeTypes, lowerName);
112 if (hiddenType) {
113 nsPluginArray::NotifyHiddenPluginTouched(hiddenType->GetEnabledPlugin());
116 return nullptr;
119 uint32_t nsMimeTypeArray::Length(CallerType aCallerType) {
120 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
121 return 0;
124 EnsurePluginMimeTypes();
126 return mMimeTypes.Length();
129 void nsMimeTypeArray::GetSupportedNames(nsTArray<nsString>& aRetval,
130 CallerType aCallerType) {
131 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
132 return;
135 EnsurePluginMimeTypes();
137 for (uint32_t i = 0; i < mMimeTypes.Length(); ++i) {
138 aRetval.AppendElement(mMimeTypes[i]->Type());
142 void nsMimeTypeArray::EnsurePluginMimeTypes() {
143 if (!mMimeTypes.IsEmpty() || !mWindow) {
144 return;
147 RefPtr<Navigator> navigator = mWindow->Navigator();
149 nsPluginArray* pluginArray = navigator->GetPlugins(IgnoreErrors());
150 if (!pluginArray) {
151 return;
154 pluginArray->GetMimeTypes(mMimeTypes);
155 pluginArray->GetCTPMimeTypes(mCTPMimeTypes);
158 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsMimeType, AddRef)
159 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsMimeType, Release)
161 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsMimeType, mWindow, mPluginElement)
163 nsMimeType::nsMimeType(nsPIDOMWindowInner* aWindow,
164 nsPluginElement* aPluginElement, const nsAString& aType,
165 const nsAString& aDescription,
166 const nsAString& aExtension)
167 : mWindow(aWindow),
168 mPluginElement(aPluginElement),
169 mType(aType),
170 mDescription(aDescription),
171 mExtension(aExtension) {
172 MOZ_ASSERT(aPluginElement);
175 nsMimeType::~nsMimeType() = default;
177 nsPIDOMWindowInner* nsMimeType::GetParentObject() const {
178 MOZ_ASSERT(mWindow);
179 return mWindow;
182 JSObject* nsMimeType::WrapObject(JSContext* aCx,
183 JS::Handle<JSObject*> aGivenProto) {
184 return MimeType_Binding::Wrap(aCx, this, aGivenProto);
187 void nsMimeType::GetDescription(nsString& aRetval) const {
188 aRetval = mDescription;
191 nsPluginElement* nsMimeType::GetEnabledPlugin() const {
192 // mPluginElement might be null if we got unlinked but are still somehow being
193 // called into.
194 if (!mPluginElement || !mPluginElement->PluginTag()->IsEnabled()) {
195 return nullptr;
197 return mPluginElement;
200 void nsMimeType::GetSuffixes(nsString& aRetval) const { aRetval = mExtension; }
202 void nsMimeType::GetType(nsString& aRetval) const { aRetval = mType; }