summaryrefslogtreecommitdiffstats
path: root/dom/base/MozQueryInterface.cpp
blob: 032b827b3b115aaabc0bad9f6150b80df8748cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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 <string.h>

#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 */
UniquePtr<MozQueryInterface> ChromeUtils::GenerateQI(
    const GlobalObject& aGlobal, const Sequence<JS::Value>& aInterfaces) {
  JSContext* cx = aGlobal.Context();

  nsTArray<nsIID> ifaces;

  JS::Rooted<JS::Value> iface(cx);
  for (uint32_t idx = 0; idx < aInterfaces.Length(); ++idx) {
    iface = aInterfaces[idx];

    // Handle ID objects
    if (Maybe<nsID> 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 MakeUnique<MozQueryInterface>(std::move(ifaces));
}

bool MozQueryInterface::QueriesTo(const nsIID& aIID) const {
  return mInterfaces.ContainsSorted(aIID, CompareIIDs);
}

void MozQueryInterface::LegacyCall(JSContext* cx, JS::Handle<JS::Value> thisv,
                                   JS::Handle<JS::Value> aIID,
                                   JS::MutableHandle<JS::Value> aResult,
                                   ErrorResult& aRv) const {
  Maybe<nsID> 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<JSObject*> aGivenProto,
                                   JS::MutableHandle<JSObject*> aReflector) {
  return MozQueryInterface_Binding::Wrap(aCx, this, aGivenProto, aReflector);
}

}  // namespace mozilla::dom