Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / speech / SpeechInput.cpp
1 /*
2  * Copyright (C) 2010 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "core/speech/SpeechInput.h"
33
34 #if ENABLE(INPUT_SPEECH)
35
36 #include "core/speech/SpeechInputClient.h"
37 #include "wtf/PassOwnPtr.h"
38
39 namespace WebCore {
40
41 SpeechInput::SpeechInput(SpeechInputClient* client)
42     : m_client(client)
43     , m_nextListenerId(1)
44 {
45     m_client->setListener(this);
46 }
47
48 SpeechInput::~SpeechInput()
49 {
50     m_client->setListener(0);
51 }
52
53 PassOwnPtr<SpeechInput> SpeechInput::create(SpeechInputClient* client)
54 {
55     return adoptPtr(new SpeechInput(client));
56 }
57
58 int SpeechInput::registerListener(SpeechInputListener* listener)
59 {
60 #if defined(DEBUG)
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);
64 #endif
65
66     m_listeners.add(m_nextListenerId, listener);
67     return m_nextListenerId++;
68 }
69
70 void SpeechInput::unregisterListener(int listenerId)
71 {
72     if (m_listeners.contains(listenerId))
73         m_listeners.remove(listenerId);
74 }
75
76 void SpeechInput::didCompleteRecording(int listenerId)
77 {
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);
82 }
83
84 void SpeechInput::didCompleteRecognition(int listenerId)
85 {
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);
90 }
91
92 void SpeechInput::setRecognitionResult(int listenerId, const SpeechInputResultArray& result)
93 {
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);
98 }
99
100 bool SpeechInput::startRecognition(int listenerId, const IntRect& elementRect, const AtomicString& language, const String& grammar, SecurityOrigin* origin)
101 {
102     ASSERT(m_listeners.contains(listenerId));
103     return m_client->startRecognition(listenerId, elementRect, language, grammar, origin);
104 }
105
106 void SpeechInput::stopRecording(int listenerId)
107 {
108     ASSERT(m_listeners.contains(listenerId));
109     m_client->stopRecording(listenerId);
110 }
111
112 void SpeechInput::cancelRecognition(int listenerId)
113 {
114     ASSERT(m_listeners.contains(listenerId));
115     m_client->cancelRecognition(listenerId);
116 }
117
118 const char* SpeechInput::supplementName()
119 {
120     return "SpeechInput";
121 }
122
123 void provideSpeechInputTo(Page& page, SpeechInputClient* client)
124 {
125     SpeechInput::provideTo(page, SpeechInput::supplementName(), SpeechInput::create(client));
126 }
127
128 } // namespace WebCore
129
130 #endif // ENABLE(INPUT_SPEECH)