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
)
28 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsMimeTypeArray
, mWindow
, mMimeTypes
,
31 nsMimeTypeArray::nsMimeTypeArray(nsPIDOMWindowInner
* 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() {
43 mCTPMimeTypes
.Clear();
46 nsPIDOMWindowInner
* nsMimeTypeArray::GetParentObject() const {
51 nsMimeType
* nsMimeTypeArray::Item(uint32_t aIndex
, CallerType aCallerType
) {
53 return IndexedGetter(aIndex
, unused
, aCallerType
);
56 nsMimeType
* nsMimeTypeArray::NamedItem(const nsAString
& aName
,
57 CallerType aCallerType
) {
59 return NamedGetter(aName
, unused
, aCallerType
);
62 nsMimeType
* nsMimeTypeArray::IndexedGetter(uint32_t aIndex
, bool& aFound
,
63 CallerType aCallerType
) {
66 if (nsContentUtils::ResistFingerprinting(aCallerType
)) {
70 EnsurePluginMimeTypes();
72 if (aIndex
>= mMimeTypes
.Length()) {
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())) {
93 nsMimeType
* nsMimeTypeArray::NamedGetter(const nsAString
& aName
, bool& aFound
,
94 CallerType aCallerType
) {
97 if (nsContentUtils::ResistFingerprinting(aCallerType
)) {
101 EnsurePluginMimeTypes();
103 nsString
lowerName(aName
);
104 ToLowerCase(lowerName
);
106 nsMimeType
* mimeType
= FindMimeType(mMimeTypes
, lowerName
);
111 nsMimeType
* hiddenType
= FindMimeType(mCTPMimeTypes
, lowerName
);
113 nsPluginArray::NotifyHiddenPluginTouched(hiddenType
->GetEnabledPlugin());
119 uint32_t nsMimeTypeArray::Length(CallerType aCallerType
) {
120 if (nsContentUtils::ResistFingerprinting(aCallerType
)) {
124 EnsurePluginMimeTypes();
126 return mMimeTypes
.Length();
129 void nsMimeTypeArray::GetSupportedNames(nsTArray
<nsString
>& aRetval
,
130 CallerType aCallerType
) {
131 if (nsContentUtils::ResistFingerprinting(aCallerType
)) {
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
) {
147 RefPtr
<Navigator
> navigator
= mWindow
->Navigator();
149 nsPluginArray
* pluginArray
= navigator
->GetPlugins(IgnoreErrors());
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
)
168 mPluginElement(aPluginElement
),
170 mDescription(aDescription
),
171 mExtension(aExtension
) {
172 MOZ_ASSERT(aPluginElement
);
175 nsMimeType::~nsMimeType() = default;
177 nsPIDOMWindowInner
* nsMimeType::GetParentObject() const {
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
194 if (!mPluginElement
|| !mPluginElement
->PluginTag()->IsEnabled()) {
197 return mPluginElement
;
200 void nsMimeType::GetSuffixes(nsString
& aRetval
) const { aRetval
= mExtension
; }
202 void nsMimeType::GetType(nsString
& aRetval
) const { aRetval
= mType
; }