1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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/. */
6 
7 #include "SpeechRecognitionResultList.h"
8 
9 #include "mozilla/dom/SpeechRecognitionResultListBinding.h"
10 
11 #include "SpeechRecognition.h"
12 
13 namespace mozilla::dom {
14 
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(SpeechRecognitionResultList,mParent,mItems)15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(SpeechRecognitionResultList, mParent,
16                                       mItems)
17 NS_IMPL_CYCLE_COLLECTING_ADDREF(SpeechRecognitionResultList)
18 NS_IMPL_CYCLE_COLLECTING_RELEASE(SpeechRecognitionResultList)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechRecognitionResultList)
20   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21   NS_INTERFACE_MAP_ENTRY(nsISupports)
22 NS_INTERFACE_MAP_END
23 
24 SpeechRecognitionResultList::SpeechRecognitionResultList(
25     SpeechRecognition* aParent)
26     : mParent(aParent) {}
27 
28 SpeechRecognitionResultList::~SpeechRecognitionResultList() = default;
29 
GetParentObject() const30 nsISupports* SpeechRecognitionResultList::GetParentObject() const {
31   return static_cast<DOMEventTargetHelper*>(mParent.get());
32 }
33 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)34 JSObject* SpeechRecognitionResultList::WrapObject(
35     JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
36   return SpeechRecognitionResultList_Binding::Wrap(aCx, this, aGivenProto);
37 }
38 
39 already_AddRefed<SpeechRecognitionResult>
IndexedGetter(uint32_t aIndex,bool & aPresent)40 SpeechRecognitionResultList::IndexedGetter(uint32_t aIndex, bool& aPresent) {
41   if (aIndex >= Length()) {
42     aPresent = false;
43     return nullptr;
44   }
45 
46   aPresent = true;
47   return Item(aIndex);
48 }
49 
Length() const50 uint32_t SpeechRecognitionResultList::Length() const { return mItems.Length(); }
51 
Item(uint32_t aIndex)52 already_AddRefed<SpeechRecognitionResult> SpeechRecognitionResultList::Item(
53     uint32_t aIndex) {
54   RefPtr<SpeechRecognitionResult> result = mItems.ElementAt(aIndex);
55   return result.forget();
56 }
57 
58 }  // namespace mozilla::dom
59