Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 29 | #include "wtf/PassRefPtr.h" |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 30 | #include "WebSecurityOrigin.h" |
| 31 | #include "WebSpeechGrammar.h" |
| 32 | #include "WebSpeechRecognitionHandle.h" |
| 33 | #include "WebSpeechRecognitionParams.h" |
| 34 | #include "WebSpeechRecognitionResult.h" |
| 35 | #include "WebSpeechRecognizer.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 36 | #include "core/dom/ExecutionContext.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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" |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 42 | #include "platform/weborigin/SecurityOrigin.h" |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace WebCore; |
| 45 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 46 | namespace blink { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 63 | WebSpeechRecognitionParams params(webSpeechGrammars, lang, continuous, interimResults, maxAlternatives, WebSecurityOrigin(recognition->executionContext()->securityOrigin())); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 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 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 79 | RefPtrWillBeRawPtr<SpeechRecognition> recognition = PassRefPtrWillBeRawPtr<SpeechRecognition>(handle); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 80 | recognition->didStartAudio(); |
| 81 | } |
| 82 | |
| 83 | void SpeechRecognitionClientProxy::didStartSound(const WebSpeechRecognitionHandle& handle) |
| 84 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 85 | RefPtrWillBeRawPtr<SpeechRecognition> recognition = PassRefPtrWillBeRawPtr<SpeechRecognition>(handle); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 86 | recognition->didStartSound(); |
| 87 | recognition->didStartSpeech(); |
| 88 | } |
| 89 | |
| 90 | void SpeechRecognitionClientProxy::didEndSound(const WebSpeechRecognitionHandle& handle) |
| 91 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 92 | RefPtrWillBeRawPtr<SpeechRecognition> recognition = PassRefPtrWillBeRawPtr<SpeechRecognition>(handle); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 93 | recognition->didEndSpeech(); |
| 94 | recognition->didEndSound(); |
| 95 | } |
| 96 | |
| 97 | void SpeechRecognitionClientProxy::didEndAudio(const WebSpeechRecognitionHandle& handle) |
| 98 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 99 | RefPtrWillBeRawPtr<SpeechRecognition> recognition = PassRefPtrWillBeRawPtr<SpeechRecognition>(handle); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 100 | recognition->didEndAudio(); |
| 101 | } |
| 102 | |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 103 | void SpeechRecognitionClientProxy::didReceiveResults(const WebSpeechRecognitionHandle& handle, const WebVector<WebSpeechRecognitionResult>& newFinalResults, const WebVector<WebSpeechRecognitionResult>& currentInterimResults) |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 104 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 105 | RefPtrWillBeRawPtr<SpeechRecognition> recognition = PassRefPtrWillBeRawPtr<SpeechRecognition>(handle); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 106 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 107 | WillBeHeapVector<RefPtrWillBeMember<SpeechRecognitionResult> > finalResultsVector(newFinalResults.size()); |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 108 | for (size_t i = 0; i < newFinalResults.size(); ++i) |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 109 | finalResultsVector[i] = PassRefPtrWillBeRawPtr<SpeechRecognitionResult>(newFinalResults[i]); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 110 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 111 | WillBeHeapVector<RefPtrWillBeMember<SpeechRecognitionResult> > interimResultsVector(currentInterimResults.size()); |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 112 | for (size_t i = 0; i < currentInterimResults.size(); ++i) |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 113 | interimResultsVector[i] = PassRefPtrWillBeRawPtr<SpeechRecognitionResult>(currentInterimResults[i]); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 114 | |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 115 | recognition->didReceiveResults(finalResultsVector, interimResultsVector); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void SpeechRecognitionClientProxy::didReceiveNoMatch(const WebSpeechRecognitionHandle& handle, const WebSpeechRecognitionResult& result) |
| 119 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 120 | RefPtrWillBeRawPtr<SpeechRecognition> recognition = PassRefPtrWillBeRawPtr<SpeechRecognition>(handle); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 121 | recognition->didReceiveNoMatch(result); |
| 122 | } |
| 123 | |
| 124 | void SpeechRecognitionClientProxy::didReceiveError(const WebSpeechRecognitionHandle& handle, const WebString& message, WebSpeechRecognizerClient::ErrorCode code) |
| 125 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 126 | RefPtrWillBeRawPtr<SpeechRecognition> recognition = PassRefPtrWillBeRawPtr<SpeechRecognition>(handle); |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 127 | SpeechRecognitionError::ErrorCode errorCode = static_cast<SpeechRecognitionError::ErrorCode>(code); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 128 | recognition->didReceiveError(SpeechRecognitionError::create(errorCode, message)); |
| 129 | } |
| 130 | |
| 131 | void SpeechRecognitionClientProxy::didStart(const WebSpeechRecognitionHandle& handle) |
| 132 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 133 | RefPtrWillBeRawPtr<SpeechRecognition> recognition = PassRefPtrWillBeRawPtr<SpeechRecognition>(handle); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 134 | recognition->didStart(); |
| 135 | } |
| 136 | |
| 137 | void SpeechRecognitionClientProxy::didEnd(const WebSpeechRecognitionHandle& handle) |
| 138 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 139 | RefPtrWillBeRawPtr<SpeechRecognition> recognition = PassRefPtrWillBeRawPtr<SpeechRecognition>(handle); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 140 | recognition->didEnd(); |
| 141 | } |
| 142 | |
| 143 | SpeechRecognitionClientProxy::SpeechRecognitionClientProxy(WebSpeechRecognizer* recognizer) |
| 144 | : m_recognizer(recognizer) |
| 145 | { |
| 146 | } |
| 147 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 148 | } // namespace blink |