Replace static_casts with to* functions for document types.
[WebKit-https.git] / Source / WebCore / 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 #if ENABLE(SCRIPTED_SPEECH)
29
30 #include "SpeechRecognition.h"
31
32 #include "Document.h"
33 #include "ExceptionCode.h"
34 #include "Page.h"
35 #include "SpeechRecognitionController.h"
36 #include "SpeechRecognitionError.h"
37 #include "SpeechRecognitionEvent.h"
38
39 namespace WebCore {
40
41 PassRefPtr<SpeechRecognition> SpeechRecognition::create(ScriptExecutionContext* context)
42 {
43     RefPtr<SpeechRecognition> speechRecognition(adoptRef(new SpeechRecognition(context)));
44     speechRecognition->suspendIfNeeded();
45     return speechRecognition.release();
46 }
47
48 void SpeechRecognition::start(ExceptionCode& ec)
49 {
50     ASSERT(m_controller);
51     if (m_started) {
52         ec = INVALID_STATE_ERR;
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     m_controller->stop(this);
66 }
67
68 void SpeechRecognition::abort()
69 {
70     ASSERT(m_controller);
71     m_controller->abort(this);
72 }
73
74 void SpeechRecognition::didStartAudio()
75 {
76     dispatchEvent(Event::create(eventNames().audiostartEvent, /*canBubble=*/false, /*cancelable=*/false));
77 }
78
79 void SpeechRecognition::didStartSound()
80 {
81     dispatchEvent(Event::create(eventNames().soundstartEvent, /*canBubble=*/false, /*cancelable=*/false));
82 }
83
84 void SpeechRecognition::didStartSpeech()
85 {
86     dispatchEvent(Event::create(eventNames().speechstartEvent, /*canBubble=*/false, /*cancelable=*/false));
87 }
88
89 void SpeechRecognition::didEndSpeech()
90 {
91     dispatchEvent(Event::create(eventNames().speechendEvent, /*canBubble=*/false, /*cancelable=*/false));
92 }
93
94 void SpeechRecognition::didEndSound()
95 {
96     dispatchEvent(Event::create(eventNames().soundendEvent, /*canBubble=*/false, /*cancelable=*/false));
97 }
98
99 void SpeechRecognition::didEndAudio()
100 {
101     dispatchEvent(Event::create(eventNames().audioendEvent, /*canBubble=*/false, /*cancelable=*/false));
102 }
103
104 void SpeechRecognition::didReceiveResult(PassRefPtr<SpeechRecognitionResult> result, unsigned long resultIndex, PassRefPtr<SpeechRecognitionResultList> resultHistory)
105 {
106     dispatchEvent(SpeechRecognitionEvent::createResult(result, resultIndex, resultHistory));
107 }
108
109 void SpeechRecognition::didReceiveResults(const Vector<RefPtr<SpeechRecognitionResult> >& newFinalResults, const Vector<RefPtr<SpeechRecognitionResult> >& currentInterimResults)
110 {
111     unsigned long resultIndex = m_finalResults.size();
112
113     for (size_t i = 0; i < newFinalResults.size(); ++i)
114         m_finalResults.append(newFinalResults[i]);
115
116     Vector<RefPtr<SpeechRecognitionResult> > results = m_finalResults;
117     for (size_t i = 0; i < currentInterimResults.size(); ++i)
118         results.append(currentInterimResults[i]);
119
120     dispatchEvent(SpeechRecognitionEvent::createResult(resultIndex, results));
121 }
122
123 void SpeechRecognition::didReceiveNoMatch(PassRefPtr<SpeechRecognitionResult> result)
124 {
125     dispatchEvent(SpeechRecognitionEvent::createNoMatch(result));
126 }
127
128 void SpeechRecognition::didReceiveError(PassRefPtr<SpeechRecognitionError> error)
129 {
130     dispatchEvent(error);
131     m_started = false;
132 }
133
134 void SpeechRecognition::didStart()
135 {
136     dispatchEvent(Event::create(eventNames().startEvent, /*canBubble=*/false, /*cancelable=*/false));
137 }
138
139 void SpeechRecognition::didEnd()
140 {
141     m_started = false;
142     if (!m_stoppedByActiveDOMObject)
143         dispatchEvent(Event::create(eventNames().endEvent, /*canBubble=*/false, /*cancelable=*/false));
144     unsetPendingActivity(this);
145 }
146
147 const AtomicString& SpeechRecognition::interfaceName() const
148 {
149     return eventNames().interfaceForSpeechRecognition;
150 }
151
152 ScriptExecutionContext* SpeechRecognition::scriptExecutionContext() const
153 {
154     return ActiveDOMObject::scriptExecutionContext();
155 }
156
157 void SpeechRecognition::stop()
158 {
159     m_stoppedByActiveDOMObject = true;
160     if (hasPendingActivity())
161         abort();
162 }
163
164 SpeechRecognition::SpeechRecognition(ScriptExecutionContext* context)
165     : ActiveDOMObject(context, this)
166     , m_grammars(SpeechGrammarList::create()) // FIXME: The spec is not clear on the default value for the grammars attribute.
167     , m_continuous(false)
168     , m_interimResults(false)
169     , m_maxAlternatives(1)
170     , m_controller(0)
171     , m_stoppedByActiveDOMObject(false)
172     , m_started(false)
173 {
174     Document* document = toDocument(scriptExecutionContext());
175
176     Page* page = document->page();
177     ASSERT(page);
178
179     m_controller = SpeechRecognitionController::from(page);
180     ASSERT(m_controller);
181
182     // FIXME: Need to hook up with Page to get notified when the visibility changes.
183 }
184
185 SpeechRecognition::~SpeechRecognition()
186 {
187 }
188
189 } // namespace WebCore
190
191 #endif // ENABLE(SCRIPTED_SPEECH)