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/ExceptionMessages.h"
31 #include "bindings/v8/ExceptionState.h"
32 #include "core/dom/Document.h"
33 #include "core/dom/ExceptionCode.h"
34 #include "core/page/Page.h"
35 #include "modules/speech/SpeechRecognitionController.h"
36 #include "modules/speech/SpeechRecognitionError.h"
37 #include "modules/speech/SpeechRecognitionEvent.h"
41 PassRefPtr<SpeechRecognition> SpeechRecognition::create(ExecutionContext* context)
43 RefPtr<SpeechRecognition> speechRecognition(adoptRef(new SpeechRecognition(context)));
44 speechRecognition->suspendIfNeeded();
45 return speechRecognition.release();
48 void SpeechRecognition::start(ExceptionState& es)
52 es.throwDOMException(InvalidStateError, ExceptionMessages::failedToExecute("start", "SpeechRecognition", "recognition has already started."));
56 setPendingActivity(this);
57 m_finalResults.clear();
58 m_controller->start(this, m_grammars.get(), m_lang, m_continuous, m_interimResults, m_maxAlternatives);
62 void SpeechRecognition::stopFunction()
65 if (m_started && !m_stopping) {
67 m_controller->stop(this);
71 void SpeechRecognition::abort()
74 if (m_started && !m_stopping) {
76 m_controller->abort(this);
80 void SpeechRecognition::didStartAudio()
82 dispatchEvent(Event::create(EventTypeNames::audiostart));
85 void SpeechRecognition::didStartSound()
87 dispatchEvent(Event::create(EventTypeNames::soundstart));
90 void SpeechRecognition::didStartSpeech()
92 dispatchEvent(Event::create(EventTypeNames::speechstart));
95 void SpeechRecognition::didEndSpeech()
97 dispatchEvent(Event::create(EventTypeNames::speechend));
100 void SpeechRecognition::didEndSound()
102 dispatchEvent(Event::create(EventTypeNames::soundend));
105 void SpeechRecognition::didEndAudio()
107 dispatchEvent(Event::create(EventTypeNames::audioend));
110 void SpeechRecognition::didReceiveResults(const Vector<RefPtr<SpeechRecognitionResult> >& newFinalResults, const Vector<RefPtr<SpeechRecognitionResult> >& currentInterimResults)
112 unsigned long resultIndex = m_finalResults.size();
114 for (size_t i = 0; i < newFinalResults.size(); ++i)
115 m_finalResults.append(newFinalResults[i]);
117 Vector<RefPtr<SpeechRecognitionResult> > results = m_finalResults;
118 for (size_t i = 0; i < currentInterimResults.size(); ++i)
119 results.append(currentInterimResults[i]);
121 dispatchEvent(SpeechRecognitionEvent::createResult(resultIndex, results));
124 void SpeechRecognition::didReceiveNoMatch(PassRefPtr<SpeechRecognitionResult> result)
126 dispatchEvent(SpeechRecognitionEvent::createNoMatch(result));
129 void SpeechRecognition::didReceiveError(PassRefPtr<SpeechRecognitionError> error)
131 dispatchEvent(error);
135 void SpeechRecognition::didStart()
137 dispatchEvent(Event::create(EventTypeNames::start));
140 void SpeechRecognition::didEnd()
144 if (!m_stoppedByActiveDOMObject)
145 dispatchEvent(Event::create(EventTypeNames::end));
146 unsetPendingActivity(this);
149 const AtomicString& SpeechRecognition::interfaceName() const
151 return EventTargetNames::SpeechRecognition;
154 ExecutionContext* SpeechRecognition::executionContext() const
156 return ActiveDOMObject::executionContext();
159 void SpeechRecognition::stop()
161 m_stoppedByActiveDOMObject = true;
162 if (hasPendingActivity())
166 SpeechRecognition::SpeechRecognition(ExecutionContext* context)
167 : ActiveDOMObject(context)
168 , m_grammars(SpeechGrammarList::create()) // FIXME: The spec is not clear on the default value for the grammars attribute.
169 , m_continuous(false)
170 , m_interimResults(false)
171 , m_maxAlternatives(1)
173 , m_stoppedByActiveDOMObject(false)
177 ScriptWrappable::init(this);
178 Document* document = toDocument(executionContext());
180 Page* page = document->page();
183 m_controller = SpeechRecognitionController::from(page);
184 ASSERT(m_controller);
186 // FIXME: Need to hook up with Page to get notified when the visibility changes.
189 SpeechRecognition::~SpeechRecognition()
193 } // namespace WebCore