From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- dom/base/MozQueryInterface.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 dom/base/MozQueryInterface.cpp (limited to 'dom/base/MozQueryInterface.cpp') diff --git a/dom/base/MozQueryInterface.cpp b/dom/base/MozQueryInterface.cpp new file mode 100644 index 0000000000..aa6bc19346 --- /dev/null +++ b/dom/base/MozQueryInterface.cpp @@ -0,0 +1,92 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "ChromeUtils.h" +#include "MozQueryInterface.h" +#include "xptinfo.h" + +#include + +#include "jsapi.h" + +#include "mozilla/ErrorResult.h" +#include "xpcpublic.h" + +namespace mozilla::dom { + +constexpr size_t IID_SIZE = sizeof(nsIID); + +static_assert( + IID_SIZE == 16, + "Size of nsID struct changed. Please ensure this code is still valid."); + +static int CompareIIDs(const nsIID& aA, const nsIID& aB) { + return memcmp((void*)&aA.m0, (void*)&aB.m0, IID_SIZE); +} + +/* static */ +MozQueryInterface* ChromeUtils::GenerateQI( + const GlobalObject& aGlobal, const Sequence& aInterfaces) { + JSContext* cx = aGlobal.Context(); + + nsTArray ifaces; + + JS::Rooted iface(cx); + for (uint32_t idx = 0; idx < aInterfaces.Length(); ++idx) { + iface = aInterfaces[idx]; + + // Handle ID objects + if (Maybe id = xpc::JSValue2ID(cx, iface)) { + ifaces.AppendElement(*id); + continue; + } + + // Accept string valued names + if (iface.isString()) { + JS::UniqueChars name = JS_EncodeStringToLatin1(cx, iface.toString()); + + const nsXPTInterfaceInfo* iinfo = nsXPTInterfaceInfo::ByName(name.get()); + if (iinfo) { + ifaces.AppendElement(iinfo->IID()); + continue; + } + } + + // NOTE: We ignore unknown interfaces here because in some cases we try to + // pass them in to support multiple platforms. + } + + MOZ_ASSERT(!ifaces.Contains(NS_GET_IID(nsISupports), CompareIIDs)); + ifaces.AppendElement(NS_GET_IID(nsISupports)); + + ifaces.Sort(CompareIIDs); + + return new MozQueryInterface(std::move(ifaces)); +} + +bool MozQueryInterface::QueriesTo(const nsIID& aIID) const { + return mInterfaces.ContainsSorted(aIID, CompareIIDs); +} + +void MozQueryInterface::LegacyCall(JSContext* cx, JS::Handle thisv, + JS::Handle aIID, + JS::MutableHandle aResult, + ErrorResult& aRv) const { + Maybe id = xpc::JSValue2ID(cx, aIID); + if (id && QueriesTo(*id)) { + aResult.set(thisv); + } else { + aRv.Throw(NS_ERROR_NO_INTERFACE); + } +} + +bool MozQueryInterface::WrapObject(JSContext* aCx, + JS::Handle aGivenProto, + JS::MutableHandle aReflector) { + return MozQueryInterface_Binding::Wrap(aCx, this, aGivenProto, aReflector); +} + +} // namespace mozilla::dom -- cgit v1.2.3