Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / modules / speech / SpeechRecognition.cpp
blobbfe1b4e8914246920194e2d77b2cdeb21acb2e61
1 /*
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
6 * are met:
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.
26 #include "config.h"
28 #include "modules/speech/SpeechRecognition.h"
30 #include "bindings/core/v8/ExceptionState.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/ExceptionCode.h"
33 #include "core/page/Page.h"
34 #include "modules/mediastream/MediaStreamTrack.h"
35 #include "modules/speech/SpeechRecognitionController.h"
36 #include "modules/speech/SpeechRecognitionError.h"
37 #include "modules/speech/SpeechRecognitionEvent.h"
39 namespace blink {
41 SpeechRecognition* SpeechRecognition::create(ExecutionContext* context)
43 ASSERT(context && context->isDocument());
44 Document* document = toDocument(context);
45 ASSERT(document);
46 SpeechRecognition* speechRecognition = new SpeechRecognition(document->page(), context);
47 speechRecognition->suspendIfNeeded();
48 return speechRecognition;
51 void SpeechRecognition::start(ExceptionState& exceptionState)
53 if (!m_controller)
54 return;
56 if (m_started) {
57 exceptionState.throwDOMException(InvalidStateError, "recognition has already started.");
58 return;
61 m_finalResults.clear();
62 m_controller->start(this, m_grammars, m_lang, m_serviceURI, m_continuous, m_interimResults, m_maxAlternatives, m_audioTrack);
63 m_started = true;
66 void SpeechRecognition::stopFunction()
68 if (!m_controller)
69 return;
71 if (m_started && !m_stopping) {
72 m_stopping = true;
73 m_controller->stop(this);
77 void SpeechRecognition::abort()
79 if (!m_controller)
80 return;
82 if (m_started && !m_stopping) {
83 m_stopping = true;
84 m_controller->abort(this);
88 void SpeechRecognition::didStartAudio()
90 dispatchEvent(Event::create(EventTypeNames::audiostart));
93 void SpeechRecognition::didStartSound()
95 dispatchEvent(Event::create(EventTypeNames::soundstart));
98 void SpeechRecognition::didStartSpeech()
100 dispatchEvent(Event::create(EventTypeNames::speechstart));
103 void SpeechRecognition::didEndSpeech()
105 dispatchEvent(Event::create(EventTypeNames::speechend));
108 void SpeechRecognition::didEndSound()
110 dispatchEvent(Event::create(EventTypeNames::soundend));
113 void SpeechRecognition::didEndAudio()
115 dispatchEvent(Event::create(EventTypeNames::audioend));
118 void SpeechRecognition::didReceiveResults(const HeapVector<Member<SpeechRecognitionResult>>& newFinalResults, const HeapVector<Member<SpeechRecognitionResult>>& currentInterimResults)
120 size_t resultIndex = m_finalResults.size();
122 for (size_t i = 0; i < newFinalResults.size(); ++i)
123 m_finalResults.append(newFinalResults[i]);
125 HeapVector<Member<SpeechRecognitionResult>> results = m_finalResults;
126 for (size_t i = 0; i < currentInterimResults.size(); ++i)
127 results.append(currentInterimResults[i]);
129 dispatchEvent(SpeechRecognitionEvent::createResult(resultIndex, results));
132 void SpeechRecognition::didReceiveNoMatch(SpeechRecognitionResult* result)
134 dispatchEvent(SpeechRecognitionEvent::createNoMatch(result));
137 void SpeechRecognition::didReceiveError(PassRefPtrWillBeRawPtr<SpeechRecognitionError> error)
139 dispatchEvent(error);
140 m_started = false;
143 void SpeechRecognition::didStart()
145 dispatchEvent(Event::create(EventTypeNames::start));
148 void SpeechRecognition::didEnd()
150 m_started = false;
151 m_stopping = false;
152 if (!m_stoppedByActiveDOMObject)
153 dispatchEvent(Event::create(EventTypeNames::end));
156 const AtomicString& SpeechRecognition::interfaceName() const
158 return EventTargetNames::SpeechRecognition;
161 ExecutionContext* SpeechRecognition::executionContext() const
163 return ActiveDOMObject::executionContext();
166 void SpeechRecognition::stop()
168 m_stoppedByActiveDOMObject = true;
169 if (hasPendingActivity())
170 abort();
173 bool SpeechRecognition::hasPendingActivity() const
175 return m_started;
178 SpeechRecognition::SpeechRecognition(Page* page, ExecutionContext* context)
179 : PageLifecycleObserver(page)
180 , ActiveDOMObject(context)
181 , m_grammars(SpeechGrammarList::create()) // FIXME: The spec is not clear on the default value for the grammars attribute.
182 , m_audioTrack(nullptr)
183 , m_continuous(false)
184 , m_interimResults(false)
185 , m_maxAlternatives(1)
186 , m_controller(SpeechRecognitionController::from(page))
187 , m_stoppedByActiveDOMObject(false)
188 , m_started(false)
189 , m_stopping(false)
191 // FIXME: Need to hook up with Page to get notified when the visibility changes.
194 SpeechRecognition::~SpeechRecognition()
198 void SpeechRecognition::contextDestroyed()
200 m_controller = nullptr;
201 PageLifecycleObserver::contextDestroyed();
204 DEFINE_TRACE(SpeechRecognition)
206 visitor->trace(m_grammars);
207 visitor->trace(m_audioTrack);
208 visitor->trace(m_controller);
209 visitor->trace(m_finalResults);
210 RefCountedGarbageCollectedEventTargetWithInlineData<SpeechRecognition>::trace(visitor);
211 PageLifecycleObserver::trace(visitor);
212 ActiveDOMObject::trace(visitor);
215 } // namespace blink