tor-browser

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

MozQueryInterface.cpp (2797B)


      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 "MozQueryInterface.h"
      8 
      9 #include <string.h>
     10 
     11 #include "ChromeUtils.h"
     12 #include "jsapi.h"
     13 #include "mozilla/ErrorResult.h"
     14 #include "xpcpublic.h"
     15 #include "xptinfo.h"
     16 
     17 namespace mozilla::dom {
     18 
     19 constexpr size_t IID_SIZE = sizeof(nsIID);
     20 
     21 static_assert(
     22    IID_SIZE == 16,
     23    "Size of nsID struct changed. Please ensure this code is still valid.");
     24 
     25 static int CompareIIDs(const nsIID& aA, const nsIID& aB) {
     26  return memcmp((void*)&aA.m0, (void*)&aB.m0, IID_SIZE);
     27 }
     28 
     29 /* static */
     30 UniquePtr<MozQueryInterface> ChromeUtils::GenerateQI(
     31    const GlobalObject& aGlobal, const Sequence<JS::Value>& aInterfaces) {
     32  JSContext* cx = aGlobal.Context();
     33 
     34  nsTArray<nsIID> ifaces;
     35 
     36  JS::Rooted<JS::Value> iface(cx);
     37  for (uint32_t idx = 0; idx < aInterfaces.Length(); ++idx) {
     38    iface = aInterfaces[idx];
     39 
     40    // Handle ID objects
     41    if (Maybe<nsID> id = xpc::JSValue2ID(cx, iface)) {
     42      ifaces.AppendElement(*id);
     43      continue;
     44    }
     45 
     46    // Accept string valued names
     47    if (iface.isString()) {
     48      JS::UniqueChars name = JS_EncodeStringToLatin1(cx, iface.toString());
     49 
     50      const nsXPTInterfaceInfo* iinfo = nsXPTInterfaceInfo::ByName(name.get());
     51      if (iinfo) {
     52        ifaces.AppendElement(iinfo->IID());
     53        continue;
     54      }
     55    }
     56 
     57    // NOTE: We ignore unknown interfaces here because in some cases we try to
     58    // pass them in to support multiple platforms.
     59  }
     60 
     61  MOZ_ASSERT(!ifaces.Contains(NS_GET_IID(nsISupports), CompareIIDs));
     62  ifaces.AppendElement(NS_GET_IID(nsISupports));
     63 
     64  ifaces.Sort(CompareIIDs);
     65 
     66  return MakeUnique<MozQueryInterface>(std::move(ifaces));
     67 }
     68 
     69 bool MozQueryInterface::QueriesTo(const nsIID& aIID) const {
     70  return mInterfaces.ContainsSorted(aIID, CompareIIDs);
     71 }
     72 
     73 void MozQueryInterface::LegacyCall(JSContext* cx, JS::Handle<JS::Value> thisv,
     74                                   JS::Handle<JS::Value> aIID,
     75                                   JS::MutableHandle<JS::Value> aResult,
     76                                   ErrorResult& aRv) const {
     77  Maybe<nsID> id = xpc::JSValue2ID(cx, aIID);
     78  if (id && QueriesTo(*id)) {
     79    aResult.set(thisv);
     80  } else {
     81    aRv.Throw(NS_ERROR_NO_INTERFACE);
     82  }
     83 }
     84 
     85 bool MozQueryInterface::WrapObject(JSContext* aCx,
     86                                   JS::Handle<JSObject*> aGivenProto,
     87                                   JS::MutableHandle<JSObject*> aReflector) {
     88  return MozQueryInterface_Binding::Wrap(aCx, this, aGivenProto, aReflector);
     89 }
     90 
     91 }  // namespace mozilla::dom