tor-browser

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

nsMimeTypeArray.cpp (3054B)


      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 #include "nsMimeTypeArray.h"
      8 
      9 #include "mozilla/StaticPrefs_pdfjs.h"
     10 #include "mozilla/dom/MimeTypeArrayBinding.h"
     11 #include "mozilla/dom/MimeTypeBinding.h"
     12 #include "nsContentUtils.h"
     13 #include "nsPluginArray.h"
     14 
     15 using namespace mozilla;
     16 using namespace mozilla::dom;
     17 
     18 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsMimeTypeArray)
     19 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsMimeTypeArray)
     20 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsMimeTypeArray)
     21  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
     22  NS_INTERFACE_MAP_ENTRY(nsISupports)
     23 NS_INTERFACE_MAP_END
     24 
     25 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsMimeTypeArray, mWindow, mMimeTypes[0],
     26                                      mMimeTypes[1])
     27 
     28 nsMimeTypeArray::nsMimeTypeArray(
     29    nsPIDOMWindowInner* aWindow,
     30    const mozilla::Array<RefPtr<nsMimeType>, 2>& aMimeTypes)
     31    : mWindow(aWindow), mMimeTypes(aMimeTypes) {}
     32 
     33 nsMimeTypeArray::~nsMimeTypeArray() = default;
     34 
     35 JSObject* nsMimeTypeArray::WrapObject(JSContext* aCx,
     36                                      JS::Handle<JSObject*> aGivenProto) {
     37  return MimeTypeArray_Binding::Wrap(aCx, this, aGivenProto);
     38 }
     39 
     40 nsPIDOMWindowInner* nsMimeTypeArray::GetParentObject() const {
     41  MOZ_ASSERT(mWindow);
     42  return mWindow;
     43 }
     44 
     45 nsMimeType* nsMimeTypeArray::IndexedGetter(uint32_t aIndex, bool& aFound) {
     46  if (!ForceNoPlugins() && aIndex < std::size(mMimeTypes)) {
     47    aFound = true;
     48    return mMimeTypes[aIndex];
     49  }
     50 
     51  aFound = false;
     52  return nullptr;
     53 }
     54 
     55 nsMimeType* nsMimeTypeArray::NamedGetter(const nsAString& aName, bool& aFound) {
     56  if (ForceNoPlugins()) {
     57    aFound = false;
     58    return nullptr;
     59  }
     60 
     61  for (const auto& mimeType : mMimeTypes) {
     62    if (mimeType->Name().Equals(aName)) {
     63      aFound = true;
     64      return mimeType;
     65    }
     66  }
     67 
     68  aFound = false;
     69  return nullptr;
     70 }
     71 
     72 void nsMimeTypeArray::GetSupportedNames(nsTArray<nsString>& retval) {
     73  if (ForceNoPlugins()) {
     74    return;
     75  }
     76 
     77  for (auto& mimeType : mMimeTypes) {
     78    retval.AppendElement(mimeType->Name());
     79  }
     80 }
     81 
     82 bool nsMimeTypeArray::ForceNoPlugins() {
     83  return StaticPrefs::pdfjs_disabled() &&
     84         !nsContentUtils::ShouldResistFingerprinting(
     85             mWindow ? mWindow->GetDocShell() : nullptr, RFPTarget::PdfjsSpoof);
     86 }
     87 
     88 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsMimeType, mPluginElement)
     89 
     90 nsMimeType::nsMimeType(nsPluginElement* aPluginElement, const nsAString& aName)
     91    : mPluginElement(aPluginElement), mName(aName) {
     92  MOZ_ASSERT(aPluginElement);
     93 }
     94 
     95 nsMimeType::~nsMimeType() = default;
     96 
     97 JSObject* nsMimeType::WrapObject(JSContext* aCx,
     98                                 JS::Handle<JSObject*> aGivenProto) {
     99  return MimeType_Binding::Wrap(aCx, this, aGivenProto);
    100 }
    101 
    102 already_AddRefed<nsPluginElement> nsMimeType::EnabledPlugin() const {
    103  return do_AddRef(mPluginElement);
    104 }