SpeechGrammarList.cpp (2712B)
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 "SpeechGrammarList.h" 8 9 #include "SpeechRecognition.h" 10 #include "mozilla/ErrorResult.h" 11 #include "mozilla/dom/SpeechGrammar.h" 12 #include "mozilla/dom/SpeechGrammarListBinding.h" 13 #include "nsCOMPtr.h" 14 15 namespace mozilla::dom { 16 17 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(SpeechGrammarList, mParent, mItems) 18 NS_IMPL_CYCLE_COLLECTING_ADDREF(SpeechGrammarList) 19 NS_IMPL_CYCLE_COLLECTING_RELEASE(SpeechGrammarList) 20 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechGrammarList) 21 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 22 NS_INTERFACE_MAP_ENTRY(nsISupports) 23 NS_INTERFACE_MAP_END 24 25 SpeechGrammarList::SpeechGrammarList(nsISupports* aParent) : mParent(aParent) {} 26 27 SpeechGrammarList::~SpeechGrammarList() = default; 28 29 already_AddRefed<SpeechGrammarList> SpeechGrammarList::Constructor( 30 const GlobalObject& aGlobal) { 31 RefPtr<SpeechGrammarList> speechGrammarList = 32 new SpeechGrammarList(aGlobal.GetAsSupports()); 33 return speechGrammarList.forget(); 34 } 35 36 JSObject* SpeechGrammarList::WrapObject(JSContext* aCx, 37 JS::Handle<JSObject*> aGivenProto) { 38 return SpeechGrammarList_Binding::Wrap(aCx, this, aGivenProto); 39 } 40 41 nsISupports* SpeechGrammarList::GetParentObject() const { return mParent; } 42 43 uint32_t SpeechGrammarList::Length() const { return mItems.Length(); } 44 45 already_AddRefed<SpeechGrammar> SpeechGrammarList::Item(uint32_t aIndex, 46 ErrorResult& aRv) { 47 RefPtr<SpeechGrammar> result = mItems.ElementAt(aIndex); 48 return result.forget(); 49 } 50 51 void SpeechGrammarList::AddFromURI(const nsAString& aSrc, 52 const Optional<float>& aWeight, 53 ErrorResult& aRv) { 54 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); 55 } 56 57 void SpeechGrammarList::AddFromString(const nsAString& aString, 58 const Optional<float>& aWeight, 59 ErrorResult& aRv) { 60 SpeechGrammar* speechGrammar = new SpeechGrammar(mParent); 61 speechGrammar->SetSrc(aString, aRv); 62 mItems.AppendElement(speechGrammar); 63 } 64 65 already_AddRefed<SpeechGrammar> SpeechGrammarList::IndexedGetter( 66 uint32_t aIndex, bool& aPresent, ErrorResult& aRv) { 67 if (aIndex >= Length()) { 68 aPresent = false; 69 return nullptr; 70 } 71 ErrorResult rv; 72 aPresent = true; 73 return Item(aIndex, rv); 74 } 75 76 } // namespace mozilla::dom