tor-browser

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

nsPluginArray.h (4057B)


      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 nsPluginArray_h___
      8 #define nsPluginArray_h___
      9 
     10 #include "mozilla/Array.h"
     11 #include "nsCOMPtr.h"
     12 #include "nsString.h"
     13 #include "nsTArray.h"
     14 #include "nsWeakReference.h"
     15 #include "nsWrapperCache.h"
     16 
     17 class nsPIDOMWindowInner;
     18 class nsPluginElement;
     19 class nsMimeTypeArray;
     20 class nsMimeType;
     21 
     22 /**
     23 * Array class backing HTML's navigator.plugins.  This always holds references
     24 * to the hard-coded set of PDF plugins defined by HTML but it only consults
     25 * them if "pdfjs.disabled" is false.  There is never more than one of these
     26 * per DOM window.
     27 */
     28 class nsPluginArray final : public nsSupportsWeakReference,
     29                            public nsWrapperCache {
     30 public:
     31  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     32  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(nsPluginArray)
     33 
     34  explicit nsPluginArray(nsPIDOMWindowInner* aWindow);
     35  nsPIDOMWindowInner* GetParentObject() const;
     36  virtual JSObject* WrapObject(JSContext* aCx,
     37                               JS::Handle<JSObject*> aGivenProto) override;
     38 
     39  nsMimeTypeArray* MimeTypeArray() { return mMimeTypeArray; }
     40 
     41  // PluginArray WebIDL methods
     42  uint32_t Length() { return ForceNoPlugins() ? 0 : std::size(mPlugins); }
     43 
     44  nsPluginElement* Item(uint32_t aIndex) {
     45    bool unused;
     46    return IndexedGetter(aIndex, unused);
     47  }
     48 
     49  nsPluginElement* NamedItem(const nsAString& aName) {
     50    bool unused;
     51    return NamedGetter(aName, unused);
     52  }
     53 
     54  nsPluginElement* IndexedGetter(uint32_t aIndex, bool& aFound);
     55 
     56  nsPluginElement* NamedGetter(const nsAString& aName, bool& aFound);
     57 
     58  void GetSupportedNames(nsTArray<nsString>& aRetval);
     59 
     60  void Refresh() {}
     61 
     62 private:
     63  virtual ~nsPluginArray();
     64 
     65  bool ForceNoPlugins();
     66 
     67  RefPtr<nsMimeTypeArray> mMimeTypeArray;
     68  nsCOMPtr<nsPIDOMWindowInner> mWindow;
     69  mozilla::Array<RefPtr<nsPluginElement>, 5> mPlugins;
     70 };
     71 
     72 /**
     73 * Plugin class backing entries in HTML's navigator.plugins array.  There is
     74 * a fixed set of these, as defined by HTML.
     75 */
     76 class nsPluginElement final : public nsISupports, public nsWrapperCache {
     77 public:
     78  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     79  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(nsPluginElement)
     80 
     81  explicit nsPluginElement(nsPluginArray* aPluginArray,
     82                           nsPIDOMWindowInner* aWindow, const nsAString& aName);
     83 
     84  nsPluginArray* GetParentObject() const;
     85 
     86  virtual JSObject* WrapObject(JSContext* aCx,
     87                               JS::Handle<JSObject*> aGivenProto) override;
     88 
     89  // Plugin WebIDL methods
     90  void GetDescription(nsString& retval) const { retval = kDescription; }
     91 
     92  void GetFilename(nsString& retval) const { retval = kFilename; }
     93 
     94  void GetName(nsString& retval) const { retval = mName; }
     95  const nsString& Name() const { return mName; }
     96 
     97  nsMimeType* Item(uint32_t index) {
     98    bool unused;
     99    return IndexedGetter(index, unused);
    100  }
    101 
    102  nsMimeType* NamedItem(const nsAString& name) {
    103    bool unused;
    104    return NamedGetter(name, unused);
    105  }
    106 
    107  uint32_t Length();
    108 
    109  nsMimeType* IndexedGetter(uint32_t index, bool& found);
    110 
    111  nsMimeType* NamedGetter(const nsAString& name, bool& found);
    112 
    113  void GetSupportedNames(nsTArray<nsString>& retval);
    114 
    115 protected:
    116  virtual ~nsPluginElement() = default;
    117 
    118  nsMimeTypeArray* MimeTypeArray() { return mPluginArray->MimeTypeArray(); }
    119 
    120  static constexpr nsLiteralString kDescription =
    121      u"Portable Document Format"_ns;
    122  static constexpr nsLiteralString kFilename = u"internal-pdf-viewer"_ns;
    123 
    124  // Note that this creates an explicit reference cycle:
    125  //
    126  // nsPluginElement -> nsPluginArray -> nsPluginElement
    127  //
    128  // We rely on the cycle collector to break this cycle.
    129  RefPtr<nsPluginArray> mPluginArray;
    130  nsCOMPtr<nsPIDOMWindowInner> mWindow;
    131  nsString mName;
    132 };
    133 
    134 #endif /* nsPluginArray_h___ */