tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsMimeTypeArray.h (3506B)


      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 "mozilla/dom/BindingDeclarations.h"
     11 #include "nsCOMPtr.h"
     12 #include "nsPIDOMWindow.h"
     13 #include "nsTArray.h"
     14 #include "nsWrapperCache.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_WRAPPERCACHE_CLASS(nsMimeTypeArray)
     32 
     33  nsPIDOMWindowInner* GetParentObject() const;
     34  virtual JSObject* WrapObject(JSContext* aCx,
     35                               JS::Handle<JSObject*> aGivenProto) override;
     36 
     37  // MimeTypeArray WebIDL methods
     38  uint32_t Length() { return ForceNoPlugins() ? 0 : std::size(mMimeTypes); }
     39 
     40  nsMimeType* Item(uint32_t aIndex) {
     41    bool unused;
     42    return IndexedGetter(aIndex, unused);
     43  }
     44 
     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  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_NATIVE_WRAPPERCACHE_CLASS(nsMimeType)
     73 
     74  nsMimeType(nsPluginElement* aPluginElement, const nsAString& aName);
     75 
     76  nsPluginElement* GetParentObject() const { return mPluginElement; }
     77 
     78  virtual JSObject* WrapObject(JSContext* aCx,
     79                               JS::Handle<JSObject*> aGivenProto) override;
     80 
     81  // MimeType WebIDL methods
     82  void GetDescription(mozilla::dom::DOMString& retval) const {
     83    retval.SetKnownLiveString(kMimeDescription);
     84  }
     85 
     86  already_AddRefed<nsPluginElement> EnabledPlugin() const;
     87 
     88  void GetSuffixes(mozilla::dom::DOMString& retval) const {
     89    retval.SetKnownLiveString(kMimeSuffix);
     90  }
     91 
     92  void GetType(nsString& retval) const { retval = mName; }
     93  const nsString& Name() const { return mName; }
     94 
     95 protected:
     96  virtual ~nsMimeType();
     97 
     98  static constexpr nsLiteralString kMimeDescription =
     99      u"Portable Document Format"_ns;
    100  static constexpr nsLiteralString kMimeSuffix = u"pdf"_ns;
    101 
    102  // Note that this creates an explicit reference cycle:
    103  //
    104  // nsMimeType -> nsPluginElement -> nsPluginArray ->
    105  //    nsMimeTypeArray -> nsMimeType
    106  //
    107  // We rely on the cycle collector to break this cycle.
    108  RefPtr<nsPluginElement> mPluginElement;
    109  nsString mName;
    110 };
    111 
    112 #endif /* nsMimeTypeArray_h___ */