FontFaceSetIterator.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/FontFaceSetIterator.h"
  6. namespace mozilla {
  7. namespace dom {
  8. NS_IMPL_CYCLE_COLLECTION(FontFaceSetIterator, mFontFaceSet)
  9. NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(FontFaceSetIterator, AddRef)
  10. NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(FontFaceSetIterator, Release)
  11. FontFaceSetIterator::FontFaceSetIterator(FontFaceSet* aFontFaceSet,
  12. bool aIsKeyAndValue)
  13. : mFontFaceSet(aFontFaceSet)
  14. , mNextIndex(0)
  15. , mIsKeyAndValue(aIsKeyAndValue)
  16. {
  17. MOZ_COUNT_CTOR(FontFaceSetIterator);
  18. }
  19. FontFaceSetIterator::~FontFaceSetIterator()
  20. {
  21. MOZ_COUNT_DTOR(FontFaceSetIterator);
  22. }
  23. bool
  24. FontFaceSetIterator::WrapObject(JSContext* aCx,
  25. JS::Handle<JSObject*> aGivenProto,
  26. JS::MutableHandle<JSObject*> aReflector)
  27. {
  28. return FontFaceSetIteratorBinding::Wrap(aCx, this, aGivenProto, aReflector);
  29. }
  30. void
  31. FontFaceSetIterator::Next(JSContext* aCx, FontFaceSetIteratorResult& aResult,
  32. ErrorResult& aRv)
  33. {
  34. if (!mFontFaceSet) {
  35. aResult.mDone = true;
  36. return;
  37. }
  38. FontFace* face = mFontFaceSet->GetFontFaceAt(mNextIndex++);
  39. if (!face) {
  40. aResult.mValue.setUndefined();
  41. aResult.mDone = true;
  42. mFontFaceSet = nullptr;
  43. return;
  44. }
  45. JS::Rooted<JS::Value> value(aCx);
  46. if (!ToJSValue(aCx, face, &value)) {
  47. aRv.Throw(NS_ERROR_FAILURE);
  48. return;
  49. }
  50. if (mIsKeyAndValue) {
  51. JS::AutoValueArray<2> values(aCx);
  52. values[0].set(value);
  53. values[1].set(value);
  54. JS::Rooted<JSObject*> array(aCx);
  55. array = JS_NewArrayObject(aCx, values);
  56. if (array) {
  57. aResult.mValue.setObject(*array);
  58. }
  59. } else {
  60. aResult.mValue = value;
  61. }
  62. aResult.mDone = false;
  63. }
  64. } // namespace dom
  65. } // namespace mozilla