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/. */
6 
7 #ifndef nsMimeTypeArray_h___
8 #define nsMimeTypeArray_h___
9 
10 #include "nsWrapperCache.h"
11 #include "nsCOMPtr.h"
12 #include "nsPIDOMWindow.h"
13 #include "nsTArray.h"
14 #include "mozilla/dom/BindingDeclarations.h"
15 
16 class nsMimeType;
17 class nsPluginElement;
18 
19 /**
20  * Array class backing HTML's navigator.mimeTypes.  This always holds
21  * references to the hard-coded set of PDF MIME types defined by HTML but it
22  * only consults them if "pdfjs.disabled" is false.  There is never more
23  * than one of these per DOM window.
24  */
25 class nsMimeTypeArray final : public nsISupports, public nsWrapperCache {
26  public:
27   nsMimeTypeArray(nsPIDOMWindowInner* aWindow,
28                   const mozilla::Array<RefPtr<nsMimeType>, 2>& aMimeTypes);
29 
30   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
31   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsMimeTypeArray)
32 
33   nsPIDOMWindowInner* GetParentObject() const;
34   virtual JSObject* WrapObject(JSContext* aCx,
35                                JS::Handle<JSObject*> aGivenProto) override;
36 
37   // MimeTypeArray WebIDL methods
Length()38   uint32_t Length() { return ForceNoPlugins() ? 0 : ArrayLength(mMimeTypes); }
39 
Item(uint32_t aIndex)40   nsMimeType* Item(uint32_t aIndex) {
41     bool unused;
42     return IndexedGetter(aIndex, unused);
43   }
44 
NamedItem(const nsAString & aName)45   nsMimeType* NamedItem(const nsAString& aName) {
46     bool unused;
47     return NamedGetter(aName, unused);
48   }
49 
50   nsMimeType* IndexedGetter(uint32_t index, bool& found);
51 
52   nsMimeType* NamedGetter(const nsAString& name, bool& found);
53 
54   void GetSupportedNames(nsTArray<nsString>& retval);
55 
56  protected:
57   virtual ~nsMimeTypeArray();
58 
59   static bool ForceNoPlugins();
60 
61   nsCOMPtr<nsPIDOMWindowInner> mWindow;
62   mozilla::Array<RefPtr<nsMimeType>, 2> mMimeTypes;
63 };
64 
65 /**
66  * Mime type class backing entries in HTML's navigator.mimeTypes array.  There
67  * is a fixed set of these, as defined by HTML.
68  */
69 class nsMimeType final : public nsWrapperCache {
70  public:
71   NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(nsMimeType)
72   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(nsMimeType)
73 
74   nsMimeType(nsPluginElement* aPluginElement, const nsAString& aName);
75 
GetParentObject()76   nsPluginElement* GetParentObject() const { return mPluginElement; }
77 
78   virtual JSObject* WrapObject(JSContext* aCx,
79                                JS::Handle<JSObject*> aGivenProto) override;
80 
81   // MimeType WebIDL methods
GetDescription(mozilla::dom::DOMString & retval)82   void GetDescription(mozilla::dom::DOMString& retval) const {
83     retval.SetKnownLiveString(kMimeDescription);
84   }
85 
EnabledPlugin()86   already_AddRefed<nsPluginElement> EnabledPlugin() const {
87     return do_AddRef(mPluginElement);
88   }
89 
GetSuffixes(mozilla::dom::DOMString & retval)90   void GetSuffixes(mozilla::dom::DOMString& retval) const {
91     retval.SetKnownLiveString(kMimeSuffix);
92   }
93 
GetType(nsString & retval)94   void GetType(nsString& retval) const { retval = mName; }
Name()95   const nsString& Name() const { return mName; }
96 
97  protected:
98   virtual ~nsMimeType();
99 
100   static constexpr nsLiteralString kMimeDescription =
101       u"Portable Document Format"_ns;
102   static constexpr nsLiteralString kMimeSuffix = u"pdf"_ns;
103 
104   // Note that this creates an explicit reference cycle:
105   //
106   // nsMimeType -> nsPluginElement -> nsPluginArray ->
107   //    nsMimeTypeArray -> nsMimeType
108   //
109   // We rely on the cycle collector to break this cycle.
110   RefPtr<nsPluginElement> mPluginElement;
111   nsString mName;
112 };
113 
114 #endif /* nsMimeTypeArray_h___ */
115