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