2 * Copyright (C) 2010 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/speech/SpeechInput.h"
34 #if ENABLE(INPUT_SPEECH)
36 #include "core/speech/SpeechInputClient.h"
37 #include "wtf/PassOwnPtr.h"
41 SpeechInput::SpeechInput(SpeechInputClient* client)
45 m_client->setListener(this);
48 SpeechInput::~SpeechInput()
50 m_client->setListener(0);
53 PassOwnPtr<SpeechInput> SpeechInput::create(SpeechInputClient* client)
55 return adoptPtr(new SpeechInput(client));
58 int SpeechInput::registerListener(SpeechInputListener* listener)
61 // Check if already present.
62 for (HashMap<int, SpeechInputListener*>::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it)
63 ASSERT(it->value != listener);
66 m_listeners.add(m_nextListenerId, listener);
67 return m_nextListenerId++;
70 void SpeechInput::unregisterListener(int listenerId)
72 if (m_listeners.contains(listenerId))
73 m_listeners.remove(listenerId);
76 void SpeechInput::didCompleteRecording(int listenerId)
78 // Don't assert if not present as the element might have been removed by the page while
79 // this event was on the way.
80 if (m_listeners.contains(listenerId))
81 m_listeners.get(listenerId)->didCompleteRecording(listenerId);
84 void SpeechInput::didCompleteRecognition(int listenerId)
86 // Don't assert if not present as the element might have been removed by the page while
87 // this event was on the way.
88 if (m_listeners.contains(listenerId))
89 m_listeners.get(listenerId)->didCompleteRecognition(listenerId);
92 void SpeechInput::setRecognitionResult(int listenerId, const SpeechInputResultArray& result)
94 // Don't assert if not present as the element might have been removed by the page while
95 // this event was on the way.
96 if (m_listeners.contains(listenerId))
97 m_listeners.get(listenerId)->setRecognitionResult(listenerId, result);
100 bool SpeechInput::startRecognition(int listenerId, const IntRect& elementRect, const AtomicString& language, const String& grammar, SecurityOrigin* origin)
102 ASSERT(m_listeners.contains(listenerId));
103 return m_client->startRecognition(listenerId, elementRect, language, grammar, origin);
106 void SpeechInput::stopRecording(int listenerId)
108 ASSERT(m_listeners.contains(listenerId));
109 m_client->stopRecording(listenerId);
112 void SpeechInput::cancelRecognition(int listenerId)
114 ASSERT(m_listeners.contains(listenerId));
115 m_client->cancelRecognition(listenerId);
118 const char* SpeechInput::supplementName()
120 return "SpeechInput";
123 void provideSpeechInputTo(Page& page, SpeechInputClient* client)
125 SpeechInput::provideTo(page, SpeechInput::supplementName(), SpeechInput::create(client));
128 } // namespace WebCore
130 #endif // ENABLE(INPUT_SPEECH)