- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / speech / SpeechRecognition.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
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.
12  *
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.
24  */
25
26 #include "config.h"
27
28 #include "modules/speech/SpeechRecognition.h"
29
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"
38
39 namespace WebCore {
40
41 PassRefPtr<SpeechRecognition> SpeechRecognition::create(ExecutionContext* context)
42 {
43     RefPtr<SpeechRecognition> speechRecognition(adoptRef(new SpeechRecognition(context)));
44     speechRecognition->suspendIfNeeded();
45     return speechRecognition.release();
46 }
47
48 void SpeechRecognition::start(ExceptionState& es)
49 {
50     ASSERT(m_controller);
51     if (m_started) {
52         es.throwDOMException(InvalidStateError, ExceptionMessages::failedToExecute("start", "SpeechRecognition", "recognition has already started."));
53         return;
54     }
55
56     setPendingActivity(this);
57     m_finalResults.clear();
58     m_controller->start(this, m_grammars.get(), m_lang, m_continuous, m_interimResults, m_maxAlternatives);
59     m_started = true;
60 }
61
62 void SpeechRecognition::stopFunction()
63 {
64     ASSERT(m_controller);
65     if (m_started && !m_stopping) {
66         m_stopping = true;
67         m_controller->stop(this);
68     }
69 }
70
71 void SpeechRecognition::abort()
72 {
73     ASSERT(m_controller);
74     if (m_started && !m_stopping) {
75         m_stopping = true;
76         m_controller->abort(this);
77     }
78 }
79
80 void SpeechRecognition::didStartAudio()
81 {
82     dispatchEvent(Event::create(EventTypeNames::audiostart));
83 }
84
85 void SpeechRecognition::didStartSound()
86 {
87     dispatchEvent(Event::create(EventTypeNames::soundstart));
88 }
89
90 void SpeechRecognition::didStartSpeech()
91 {
92     dispatchEvent(Event::create(EventTypeNames::speechstart));
93 }
94
95 void SpeechRecognition::didEndSpeech()
96 {
97     dispatchEvent(Event::create(EventTypeNames::speechend));
98 }
99
100 void SpeechRecognition::didEndSound()
101 {
102     dispatchEvent(Event::create(EventTypeNames::soundend));
103 }
104
105 void SpeechRecognition::didEndAudio()
106 {
107     dispatchEvent(Event::create(EventTypeNames::audioend));
108 }
109
110 void SpeechRecognition::didReceiveResults(const Vector<RefPtr<SpeechRecognitionResult> >& newFinalResults, const Vector<RefPtr<SpeechRecognitionResult> >& currentInterimResults)
111 {
112     unsigned long resultIndex = m_finalResults.size();
113
114     for (size_t i = 0; i < newFinalResults.size(); ++i)
115         m_finalResults.append(newFinalResults[i]);
116
117     Vector<RefPtr<SpeechRecognitionResult> > results = m_finalResults;
118     for (size_t i = 0; i < currentInterimResults.size(); ++i)
119         results.append(currentInterimResults[i]);
120
121     dispatchEvent(SpeechRecognitionEvent::createResult(resultIndex, results));
122 }
123
124 void SpeechRecognition::didReceiveNoMatch(PassRefPtr<SpeechRecognitionResult> result)
125 {
126     dispatchEvent(SpeechRecognitionEvent::createNoMatch(result));
127 }
128
129 void SpeechRecognition::didReceiveError(PassRefPtr<SpeechRecognitionError> error)
130 {
131     dispatchEvent(error);
132     m_started = false;
133 }
134
135 void SpeechRecognition::didStart()
136 {
137     dispatchEvent(Event::create(EventTypeNames::start));
138 }
139
140 void SpeechRecognition::didEnd()
141 {
142     m_started = false;
143     m_stopping = false;
144     if (!m_stoppedByActiveDOMObject)
145         dispatchEvent(Event::create(EventTypeNames::end));
146     unsetPendingActivity(this);
147 }
148
149 const AtomicString& SpeechRecognition::interfaceName() const
150 {
151     return EventTargetNames::SpeechRecognition;
152 }
153
154 ExecutionContext* SpeechRecognition::executionContext() const
155 {
156     return ActiveDOMObject::executionContext();
157 }
158
159 void SpeechRecognition::stop()
160 {
161     m_stoppedByActiveDOMObject = true;
162     if (hasPendingActivity())
163         abort();
164 }
165
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)
172     , m_controller(0)
173     , m_stoppedByActiveDOMObject(false)
174     , m_started(false)
175     , m_stopping(false)
176 {
177     ScriptWrappable::init(this);
178     Document* document = toDocument(executionContext());
179
180     Page* page = document->page();
181     ASSERT(page);
182
183     m_controller = SpeechRecognitionController::from(page);
184     ASSERT(m_controller);
185
186     // FIXME: Need to hook up with Page to get notified when the visibility changes.
187 }
188
189 SpeechRecognition::~SpeechRecognition()
190 {
191 }
192
193 } // namespace WebCore