2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "modules/speech/SpeechRecognition.h"
30 #include "bindings/v8/ExceptionState.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/ExceptionCode.h"
33 #include "core/page/Page.h"
34 #include "modules/speech/SpeechRecognitionController.h"
35 #include "modules/speech/SpeechRecognitionError.h"
36 #include "modules/speech/SpeechRecognitionEvent.h"
40 SpeechRecognition* SpeechRecognition::create(ExecutionContext* context)
42 SpeechRecognition* speechRecognition = adoptRefCountedGarbageCollectedWillBeNoop(new SpeechRecognition(context));
43 speechRecognition->suspendIfNeeded();
44 return speechRecognition;
47 void SpeechRecognition::start(ExceptionState& exceptionState)
51 exceptionState.throwDOMException(InvalidStateError, "recognition has already started.");
55 m_finalResults.clear();
56 m_controller->start(this, m_grammars.get(), m_lang, m_continuous, m_interimResults, m_maxAlternatives);
60 void SpeechRecognition::stopFunction()
63 if (m_started && !m_stopping) {
65 m_controller->stop(this);
69 void SpeechRecognition::abort()
72 if (m_started && !m_stopping) {
74 m_controller->abort(this);
78 void SpeechRecognition::didStartAudio()
80 dispatchEvent(Event::create(EventTypeNames::audiostart));
83 void SpeechRecognition::didStartSound()
85 dispatchEvent(Event::create(EventTypeNames::soundstart));
88 void SpeechRecognition::didStartSpeech()
90 dispatchEvent(Event::create(EventTypeNames::speechstart));
93 void SpeechRecognition::didEndSpeech()
95 dispatchEvent(Event::create(EventTypeNames::speechend));
98 void SpeechRecognition::didEndSound()
100 dispatchEvent(Event::create(EventTypeNames::soundend));
103 void SpeechRecognition::didEndAudio()
105 dispatchEvent(Event::create(EventTypeNames::audioend));
108 void SpeechRecognition::didReceiveResults(const HeapVector<Member<SpeechRecognitionResult> >& newFinalResults, const HeapVector<Member<SpeechRecognitionResult> >& currentInterimResults)
110 unsigned long resultIndex = m_finalResults.size();
112 for (size_t i = 0; i < newFinalResults.size(); ++i)
113 m_finalResults.append(newFinalResults[i]);
115 HeapVector<Member<SpeechRecognitionResult> > results = m_finalResults;
116 for (size_t i = 0; i < currentInterimResults.size(); ++i)
117 results.append(currentInterimResults[i]);
119 dispatchEvent(SpeechRecognitionEvent::createResult(resultIndex, results));
122 void SpeechRecognition::didReceiveNoMatch(SpeechRecognitionResult* result)
124 dispatchEvent(SpeechRecognitionEvent::createNoMatch(result));
127 void SpeechRecognition::didReceiveError(PassRefPtrWillBeRawPtr<SpeechRecognitionError> error)
129 dispatchEvent(error);
133 void SpeechRecognition::didStart()
135 dispatchEvent(Event::create(EventTypeNames::start));
138 void SpeechRecognition::didEnd()
142 if (!m_stoppedByActiveDOMObject)
143 dispatchEvent(Event::create(EventTypeNames::end));
146 const AtomicString& SpeechRecognition::interfaceName() const
148 return EventTargetNames::SpeechRecognition;
151 ExecutionContext* SpeechRecognition::executionContext() const
153 return ActiveDOMObject::executionContext();
156 void SpeechRecognition::stop()
158 m_stoppedByActiveDOMObject = true;
159 if (hasPendingActivity())
163 bool SpeechRecognition::hasPendingActivity() const
168 SpeechRecognition::SpeechRecognition(ExecutionContext* context)
169 : ActiveDOMObject(context)
170 , m_grammars(SpeechGrammarList::create()) // FIXME: The spec is not clear on the default value for the grammars attribute.
171 , m_continuous(false)
172 , m_interimResults(false)
173 , m_maxAlternatives(1)
174 , m_controller(nullptr)
175 , m_stoppedByActiveDOMObject(false)
179 ScriptWrappable::init(this);
180 Document* document = toDocument(executionContext());
182 Page* page = document->page();
185 m_controller = SpeechRecognitionController::from(page);
186 ASSERT(m_controller);
188 // FIXME: Need to hook up with Page to get notified when the visibility changes.
191 SpeechRecognition::~SpeechRecognition()
195 void SpeechRecognition::trace(Visitor* visitor)
197 visitor->trace(m_grammars);
199 visitor->trace(m_controller);
201 visitor->trace(m_finalResults);
202 EventTargetWithInlineData::trace(visitor);
205 } // namespace WebCore