Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / SpeechRecognitionClientProxy.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 #include "web/SpeechRecognitionClientProxy.h"
28
29 #include "core/dom/ExecutionContext.h"
30 #include "modules/speech/SpeechGrammarList.h"
31 #include "modules/speech/SpeechRecognition.h"
32 #include "modules/speech/SpeechRecognitionError.h"
33 #include "modules/speech/SpeechRecognitionResult.h"
34 #include "modules/speech/SpeechRecognitionResultList.h"
35 #include "platform/weborigin/SecurityOrigin.h"
36 #include "public/web/WebSecurityOrigin.h"
37 #include "public/web/WebSpeechGrammar.h"
38 #include "public/web/WebSpeechRecognitionHandle.h"
39 #include "public/web/WebSpeechRecognitionParams.h"
40 #include "public/web/WebSpeechRecognitionResult.h"
41 #include "public/web/WebSpeechRecognizer.h"
42 #include "wtf/PassRefPtr.h"
43
44 namespace blink {
45
46 SpeechRecognitionClientProxy::~SpeechRecognitionClientProxy()
47 {
48 }
49
50 PassOwnPtr<SpeechRecognitionClientProxy> SpeechRecognitionClientProxy::create(WebSpeechRecognizer* recognizer)
51 {
52     return adoptPtr(new SpeechRecognitionClientProxy(recognizer));
53 }
54
55 void SpeechRecognitionClientProxy::start(SpeechRecognition* recognition, const SpeechGrammarList* grammarList, const String& lang, bool continuous, bool interimResults, unsigned long maxAlternatives)
56 {
57     WebVector<WebSpeechGrammar> webSpeechGrammars(static_cast<size_t>(grammarList->length()));
58     for (unsigned long i = 0; i < grammarList->length(); ++i)
59         webSpeechGrammars[i] = grammarList->item(i);
60
61     WebSpeechRecognitionParams params(webSpeechGrammars, lang, continuous, interimResults, maxAlternatives, WebSecurityOrigin(recognition->executionContext()->securityOrigin()));
62     m_recognizer->start(WebSpeechRecognitionHandle(recognition), params, this);
63 }
64
65 void SpeechRecognitionClientProxy::stop(SpeechRecognition* recognition)
66 {
67     m_recognizer->stop(WebSpeechRecognitionHandle(recognition), this);
68 }
69
70 void SpeechRecognitionClientProxy::abort(SpeechRecognition* recognition)
71 {
72     m_recognizer->abort(WebSpeechRecognitionHandle(recognition), this);
73 }
74
75 void SpeechRecognitionClientProxy::didStartAudio(const WebSpeechRecognitionHandle& handle)
76 {
77     SpeechRecognition* recognition(handle);
78     recognition->didStartAudio();
79 }
80
81 void SpeechRecognitionClientProxy::didStartSound(const WebSpeechRecognitionHandle& handle)
82 {
83     SpeechRecognition* recognition(handle);
84     recognition->didStartSound();
85     recognition->didStartSpeech();
86 }
87
88 void SpeechRecognitionClientProxy::didEndSound(const WebSpeechRecognitionHandle& handle)
89 {
90     SpeechRecognition* recognition(handle);
91     recognition->didEndSpeech();
92     recognition->didEndSound();
93 }
94
95 void SpeechRecognitionClientProxy::didEndAudio(const WebSpeechRecognitionHandle& handle)
96 {
97     SpeechRecognition* recognition(handle);
98     recognition->didEndAudio();
99 }
100
101 void SpeechRecognitionClientProxy::didReceiveResults(const WebSpeechRecognitionHandle& handle, const WebVector<WebSpeechRecognitionResult>& newFinalResults, const WebVector<WebSpeechRecognitionResult>& currentInterimResults)
102 {
103     SpeechRecognition* recognition(handle);
104
105     HeapVector<Member<SpeechRecognitionResult> > finalResultsVector(newFinalResults.size());
106     for (size_t i = 0; i < newFinalResults.size(); ++i)
107         finalResultsVector[i] = static_cast<SpeechRecognitionResult*>(newFinalResults[i]);
108
109     HeapVector<Member<SpeechRecognitionResult> > interimResultsVector(currentInterimResults.size());
110     for (size_t i = 0; i < currentInterimResults.size(); ++i)
111         interimResultsVector[i] = static_cast<SpeechRecognitionResult*>(currentInterimResults[i]);
112
113     recognition->didReceiveResults(finalResultsVector, interimResultsVector);
114 }
115
116 void SpeechRecognitionClientProxy::didReceiveNoMatch(const WebSpeechRecognitionHandle& handle, const WebSpeechRecognitionResult& result)
117 {
118     SpeechRecognition* recognition(handle);
119     recognition->didReceiveNoMatch(result);
120 }
121
122 void SpeechRecognitionClientProxy::didReceiveError(const WebSpeechRecognitionHandle& handle, const WebString& message, WebSpeechRecognizerClient::ErrorCode code)
123 {
124     SpeechRecognition* recognition(handle);
125     SpeechRecognitionError::ErrorCode errorCode = static_cast<SpeechRecognitionError::ErrorCode>(code);
126     recognition->didReceiveError(SpeechRecognitionError::create(errorCode, message));
127 }
128
129 void SpeechRecognitionClientProxy::didStart(const WebSpeechRecognitionHandle& handle)
130 {
131     SpeechRecognition* recognition(handle);
132     recognition->didStart();
133 }
134
135 void SpeechRecognitionClientProxy::didEnd(const WebSpeechRecognitionHandle& handle)
136 {
137     SpeechRecognition* recognition(handle);
138     recognition->didEnd();
139 }
140
141 SpeechRecognitionClientProxy::SpeechRecognitionClientProxy(WebSpeechRecognizer* recognizer)
142     : m_recognizer(recognizer)
143 {
144 }
145
146 } // namespace blink