Bug 1941128 - Turn off network.dns.native_https_query on Mac again
[gecko.git] / dom / bindings / test / TestInterfaceAsyncIterableDoubleUnion.cpp
blobb0b16532ed1ca6e5bfb7edc0d8825163e2654aad
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/TestInterfaceAsyncIterableDoubleUnion.h"
8 #include "mozilla/dom/TestInterfaceJSMaplikeSetlikeIterableBinding.h"
9 #include "nsPIDOMWindow.h"
10 #include "mozilla/dom/BindingUtils.h"
11 #include "mozilla/dom/UnionTypes.h"
13 namespace mozilla::dom {
15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TestInterfaceAsyncIterableDoubleUnion,
16 mParent)
18 NS_IMPL_CYCLE_COLLECTING_ADDREF(TestInterfaceAsyncIterableDoubleUnion)
19 NS_IMPL_CYCLE_COLLECTING_RELEASE(TestInterfaceAsyncIterableDoubleUnion)
21 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TestInterfaceAsyncIterableDoubleUnion)
22 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
23 NS_INTERFACE_MAP_ENTRY(nsISupports)
24 NS_INTERFACE_MAP_END
26 TestInterfaceAsyncIterableDoubleUnion::TestInterfaceAsyncIterableDoubleUnion(
27 nsPIDOMWindowInner* aParent)
28 : mParent(aParent) {
29 OwningStringOrLong a;
30 a.SetAsLong() = 1;
31 mValues.AppendElement(std::pair<nsString, OwningStringOrLong>(u"long"_ns, a));
32 a.SetAsString() = u"a"_ns;
33 mValues.AppendElement(
34 std::pair<nsString, OwningStringOrLong>(u"string"_ns, a));
37 // static
38 already_AddRefed<TestInterfaceAsyncIterableDoubleUnion>
39 TestInterfaceAsyncIterableDoubleUnion::Constructor(const GlobalObject& aGlobal,
40 ErrorResult& aRv) {
41 nsCOMPtr<nsPIDOMWindowInner> window =
42 do_QueryInterface(aGlobal.GetAsSupports());
43 if (!window) {
44 aRv.Throw(NS_ERROR_FAILURE);
45 return nullptr;
48 RefPtr<TestInterfaceAsyncIterableDoubleUnion> r =
49 new TestInterfaceAsyncIterableDoubleUnion(window);
50 return r.forget();
53 JSObject* TestInterfaceAsyncIterableDoubleUnion::WrapObject(
54 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
55 return TestInterfaceAsyncIterableDoubleUnion_Binding::Wrap(aCx, this,
56 aGivenProto);
59 nsPIDOMWindowInner* TestInterfaceAsyncIterableDoubleUnion::GetParentObject()
60 const {
61 return mParent;
64 already_AddRefed<Promise>
65 TestInterfaceAsyncIterableDoubleUnion::GetNextIterationResult(
66 Iterator* aIterator, ErrorResult& aRv) {
67 RefPtr<Promise> promise = Promise::Create(mParent->AsGlobal(), aRv);
68 if (NS_WARN_IF(aRv.Failed())) {
69 return nullptr;
72 NS_DispatchToMainThread(NewRunnableMethod<RefPtr<Iterator>, RefPtr<Promise>>(
73 "TestInterfaceAsyncIterableDoubleUnion::GetNextIterationResult", this,
74 &TestInterfaceAsyncIterableDoubleUnion::ResolvePromise, aIterator,
75 promise));
77 return promise.forget();
80 void TestInterfaceAsyncIterableDoubleUnion::ResolvePromise(Iterator* aIterator,
81 Promise* aPromise) {
82 IteratorData& data = aIterator->Data();
84 // Test data:
85 // [long, 1], [string, "a"]
86 uint32_t idx = data.mIndex;
87 if (idx >= mValues.Length()) {
88 iterator_utils::ResolvePromiseForFinished(aPromise);
89 } else {
90 switch (aIterator->GetIteratorType()) {
91 case IterableIteratorBase::IteratorType::Keys:
92 aPromise->MaybeResolve(mValues[idx].first);
93 break;
94 case IterableIteratorBase::IteratorType::Values:
95 aPromise->MaybeResolve(mValues[idx].second);
96 break;
97 case IterableIteratorBase::IteratorType::Entries:
98 iterator_utils::ResolvePromiseWithKeyAndValue(
99 aPromise, mValues[idx].first, mValues[idx].second);
100 break;
103 data.mIndex++;
107 } // namespace mozilla::dom