Bug 1941128 - Turn off network.dns.native_https_query on Mac again
[gecko.git] / dom / bindings / test / TestInterfaceAsyncIterableDouble.cpp
bloba0ec0d64f0914840c85f72ba61a253edd7986d36
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/TestInterfaceAsyncIterableDouble.h"
8 #include "mozilla/dom/TestInterfaceJSMaplikeSetlikeIterableBinding.h"
9 #include "nsPIDOMWindow.h"
10 #include "mozilla/dom/BindingUtils.h"
12 namespace mozilla::dom {
14 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TestInterfaceAsyncIterableDouble, mParent)
16 NS_IMPL_CYCLE_COLLECTING_ADDREF(TestInterfaceAsyncIterableDouble)
17 NS_IMPL_CYCLE_COLLECTING_RELEASE(TestInterfaceAsyncIterableDouble)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TestInterfaceAsyncIterableDouble)
20 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21 NS_INTERFACE_MAP_ENTRY(nsISupports)
22 NS_INTERFACE_MAP_END
24 TestInterfaceAsyncIterableDouble::TestInterfaceAsyncIterableDouble(
25 nsPIDOMWindowInner* aParent)
26 : mParent(aParent) {
27 mValues.AppendElement(std::pair<nsString, nsString>(u"a"_ns, u"b"_ns));
28 mValues.AppendElement(std::pair<nsString, nsString>(u"c"_ns, u"d"_ns));
29 mValues.AppendElement(std::pair<nsString, nsString>(u"e"_ns, u"f"_ns));
32 // static
33 already_AddRefed<TestInterfaceAsyncIterableDouble>
34 TestInterfaceAsyncIterableDouble::Constructor(const GlobalObject& aGlobal,
35 ErrorResult& aRv) {
36 nsCOMPtr<nsPIDOMWindowInner> window =
37 do_QueryInterface(aGlobal.GetAsSupports());
38 if (!window) {
39 aRv.Throw(NS_ERROR_FAILURE);
40 return nullptr;
43 RefPtr<TestInterfaceAsyncIterableDouble> r =
44 new TestInterfaceAsyncIterableDouble(window);
45 return r.forget();
48 JSObject* TestInterfaceAsyncIterableDouble::WrapObject(
49 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
50 return TestInterfaceAsyncIterableDouble_Binding::Wrap(aCx, this, aGivenProto);
53 nsPIDOMWindowInner* TestInterfaceAsyncIterableDouble::GetParentObject() const {
54 return mParent;
57 already_AddRefed<Promise>
58 TestInterfaceAsyncIterableDouble::GetNextIterationResult(Iterator* aIterator,
59 ErrorResult& aRv) {
60 RefPtr<Promise> promise = Promise::Create(mParent->AsGlobal(), aRv);
61 if (NS_WARN_IF(aRv.Failed())) {
62 return nullptr;
65 NS_DispatchToMainThread(NewRunnableMethod<RefPtr<Iterator>, RefPtr<Promise>>(
66 "TestInterfaceAsyncIterableDouble::GetNextIterationResult", this,
67 &TestInterfaceAsyncIterableDouble::ResolvePromise, aIterator, promise));
69 return promise.forget();
72 void TestInterfaceAsyncIterableDouble::ResolvePromise(Iterator* aIterator,
73 Promise* aPromise) {
74 IteratorData& data = aIterator->Data();
76 // Test data: ['a', 'b'], ['c', 'd'], ['e', 'f']
77 uint32_t idx = data.mIndex;
78 if (idx >= mValues.Length()) {
79 iterator_utils::ResolvePromiseForFinished(aPromise);
80 } else {
81 switch (aIterator->GetIteratorType()) {
82 case IterableIteratorBase::IteratorType::Keys:
83 aPromise->MaybeResolve(mValues[idx].first);
84 break;
85 case IterableIteratorBase::IteratorType::Values:
86 aPromise->MaybeResolve(mValues[idx].second);
87 break;
88 case IterableIteratorBase::IteratorType::Entries:
89 iterator_utils::ResolvePromiseWithKeyAndValue(
90 aPromise, mValues[idx].first, mValues[idx].second);
91 break;
94 data.mIndex++;
98 } // namespace mozilla::dom