no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / xpcom / ds / nsSimpleEnumerator.cpp
blob0be7b684b7883dbe76a7b0853d93f2522f339c15
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 "mozilla/Try.h"
14 #include "nsContentUtils.h"
16 using namespace mozilla;
17 using namespace mozilla::dom;
19 namespace {
21 class JSEnumerator final : public nsIJSEnumerator {
22 NS_DECL_ISUPPORTS
23 NS_DECL_NSIJSENUMERATOR
25 explicit JSEnumerator(nsISimpleEnumerator* aEnumerator, const nsID& aIID)
26 : mEnumerator(aEnumerator), mIID(aIID) {}
28 private:
29 ~JSEnumerator() = default;
31 nsCOMPtr<nsISimpleEnumerator> mEnumerator;
32 const nsID mIID;
35 } // anonymous namespace
37 nsresult JSEnumerator::Iterator(nsIJSEnumerator** aResult) {
38 RefPtr<JSEnumerator> result(this);
39 result.forget(aResult);
40 return NS_OK;
43 nsresult JSEnumerator::Next(JSContext* aCx, JS::MutableHandleValue aResult) {
44 RootedDictionary<IteratorResult> result(aCx);
46 nsCOMPtr<nsISupports> elem;
47 if (NS_FAILED(mEnumerator->GetNext(getter_AddRefs(elem)))) {
48 result.mDone = true;
49 } else {
50 result.mDone = false;
52 JS::RootedValue value(aCx);
53 MOZ_TRY(nsContentUtils::WrapNative(aCx, elem, &mIID, &value));
54 result.mValue = value;
57 if (!ToJSValue(aCx, result, aResult)) {
58 return NS_ERROR_OUT_OF_MEMORY;
60 return NS_OK;
63 NS_IMPL_ISUPPORTS(JSEnumerator, nsIJSEnumerator)
65 nsresult nsSimpleEnumerator::Iterator(nsIJSEnumerator** aResult) {
66 auto result = MakeRefPtr<JSEnumerator>(this, DefaultInterface());
67 result.forget(aResult);
68 return NS_OK;
71 nsresult nsSimpleEnumerator::Entries(const nsIID& aIface,
72 nsIJSEnumerator** aResult) {
73 auto result = MakeRefPtr<JSEnumerator>(this, aIface);
74 result.forget(aResult);
75 return NS_OK;
78 NS_IMPL_ISUPPORTS(nsSimpleEnumerator, nsISimpleEnumerator,
79 nsISimpleEnumeratorBase)