Bug 1734063 [wpt PR 31107] - [GridNG] Fix rounding of distributed free space to flexi...
[gecko.git] / xpcom / ds / nsSimpleEnumerator.cpp
blob7f7ca9d6747d2324acab0e14f1aee032278c3356
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/. */
7 #include "nsSimpleEnumerator.h"
9 #include "mozilla/dom/IteratorResultBinding.h"
10 #include "mozilla/dom/RootedDictionary.h"
11 #include "mozilla/dom/ToJSValue.h"
12 #include "mozilla/ResultExtensions.h"
13 #include "nsContentUtils.h"
15 using namespace mozilla;
16 using namespace mozilla::dom;
18 namespace {
20 class JSEnumerator final : public nsIJSEnumerator {
21 NS_DECL_ISUPPORTS
22 NS_DECL_NSIJSENUMERATOR
24 explicit JSEnumerator(nsISimpleEnumerator* aEnumerator, const nsID& aIID)
25 : mEnumerator(aEnumerator), mIID(aIID) {}
27 private:
28 ~JSEnumerator() = default;
30 nsCOMPtr<nsISimpleEnumerator> mEnumerator;
31 const nsID mIID;
34 } // anonymous namespace
36 nsresult JSEnumerator::Iterator(nsIJSEnumerator** aResult) {
37 RefPtr<JSEnumerator> result(this);
38 result.forget(aResult);
39 return NS_OK;
42 nsresult JSEnumerator::Next(JSContext* aCx, JS::MutableHandleValue aResult) {
43 RootedDictionary<IteratorResult> result(aCx);
45 nsCOMPtr<nsISupports> elem;
46 if (NS_FAILED(mEnumerator->GetNext(getter_AddRefs(elem)))) {
47 result.mDone = true;
48 } else {
49 result.mDone = false;
51 JS::RootedValue value(aCx);
52 MOZ_TRY(nsContentUtils::WrapNative(aCx, elem, &mIID, &value));
53 result.mValue = value;
56 if (!ToJSValue(aCx, result, aResult)) {
57 return NS_ERROR_OUT_OF_MEMORY;
59 return NS_OK;
62 NS_IMPL_ISUPPORTS(JSEnumerator, nsIJSEnumerator)
64 nsresult nsSimpleEnumerator::Iterator(nsIJSEnumerator** aResult) {
65 auto result = MakeRefPtr<JSEnumerator>(this, DefaultInterface());
66 result.forget(aResult);
67 return NS_OK;
70 nsresult nsSimpleEnumerator::Entries(const nsIID& aIface,
71 nsIJSEnumerator** aResult) {
72 auto result = MakeRefPtr<JSEnumerator>(this, aIface);
73 result.forget(aResult);
74 return NS_OK;
77 NS_IMPL_ISUPPORTS(nsSimpleEnumerator, nsISimpleEnumerator,
78 nsISimpleEnumeratorBase)