Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_VR_SPEECH_RECOGNIZER_H_ |
| 6 | #define CHROME_BROWSER_VR_SPEECH_RECOGNIZER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/macros.h" |
| 11 | #include "base/memory/weak_ptr.h" |
| 12 | #include "base/timer/timer.h" |
| 13 | |
| 14 | namespace content { |
| 15 | class SpeechRecognitionManager; |
| 16 | } |
| 17 | |
| 18 | namespace net { |
| 19 | class URLRequestContextGetter; |
| 20 | } |
| 21 | |
| 22 | namespace vr { |
| 23 | |
| 24 | // Note that speech recognition is activated on VR UI thread. This means it |
| 25 | // usually involves 3 threads. In the simplest case, the thread communication |
| 26 | // looks like the following: |
| 27 | // VR UI thread Browser thread IO thread |
| 28 | // | | | |
| 29 | // |----ActivateVS----->| | |
| 30 | // | |------Start------> | |
| 31 | // | | | |
| 32 | // | |<-NotifyStateChange-| |
| 33 | // |<--OnSRStateChanged-| | |
| 34 | // | | | |
| 35 | // | |<--OnSpeechResult---| |
| 36 | // |<--OnSRStateChanged-| | |
| 37 | // | navigate | |
| 38 | // | | | |
| 39 | // VS = voice search, SR = speech recognition |
| 40 | |
| 41 | enum SpeechRecognitionState { |
| 42 | SPEECH_RECOGNITION_OFF = 0, |
| 43 | SPEECH_RECOGNITION_READY, |
Biao She | c8521874 | 2017-10-26 21:48:15 | [diff] [blame] | 44 | SPEECH_RECOGNITION_END, |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 45 | SPEECH_RECOGNITION_RECOGNIZING, |
| 46 | SPEECH_RECOGNITION_IN_SPEECH, |
Biao She | 537a729 | 2017-11-10 18:18:42 | [diff] [blame] | 47 | SPEECH_RECOGNITION_TRY_AGAIN, |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 48 | SPEECH_RECOGNITION_NETWORK_ERROR, |
| 49 | }; |
| 50 | |
Biao She | 407f1bae | 2017-11-27 23:19:18 | [diff] [blame^] | 51 | // These enums are used for histogram. Do NOT renumber or delete these enums. |
| 52 | enum VoiceSearchEndState { |
| 53 | VOICE_SEARCH_OPEN_SEARCH_PAGE = 0, |
| 54 | VOICE_SEARCH_CANCEL = 1, |
| 55 | VOICE_SEARCH_TRY_AGAIN = 2, |
| 56 | COUNT, |
| 57 | }; |
| 58 | |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 59 | class VoiceResultDelegate { |
| 60 | public: |
Biao She | a9dc6e7 | 2017-11-23 15:58:17 | [diff] [blame] | 61 | virtual ~VoiceResultDelegate() {} |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 62 | virtual void OnVoiceResults(const base::string16& result) = 0; |
| 63 | }; |
| 64 | |
Biao She | c8521874 | 2017-10-26 21:48:15 | [diff] [blame] | 65 | class BrowserUiInterface; |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 66 | class SpeechRecognizerOnIO; |
| 67 | |
| 68 | // An interface for IO to communicate with browser UI thread. |
| 69 | // This is used by SpeechRecognizerOnIO class who lives on IO thread. |
| 70 | class IOBrowserUIInterface { |
| 71 | public: |
| 72 | // Receive a speech recognition result. |is_final| indicated whether the |
| 73 | // result is an intermediate or final result. If |is_final| is true, then the |
| 74 | // recognizer stops and no more results will be returned. |
| 75 | virtual void OnSpeechResult(const base::string16& query, bool is_final) = 0; |
| 76 | |
| 77 | // Invoked regularly to indicate the average sound volume. |
| 78 | virtual void OnSpeechSoundLevelChanged(float level) = 0; |
| 79 | |
| 80 | // Invoked when the state of speech recognition is changed. |
| 81 | virtual void OnSpeechRecognitionStateChanged( |
| 82 | SpeechRecognitionState new_state) = 0; |
| 83 | |
| 84 | protected: |
| 85 | virtual ~IOBrowserUIInterface() {} |
| 86 | }; |
| 87 | |
| 88 | // SpeechRecognizer is a wrapper around the speech recognition engine that |
| 89 | // simplifies its use from the UI thread. This class handles all setup/shutdown, |
| 90 | // collection of results, error cases, and threading. |
| 91 | class SpeechRecognizer : public IOBrowserUIInterface { |
| 92 | public: |
| 93 | SpeechRecognizer(VoiceResultDelegate* delegate, |
Biao She | c8521874 | 2017-10-26 21:48:15 | [diff] [blame] | 94 | BrowserUiInterface* ui, |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 95 | net::URLRequestContextGetter* url_request_context_getter, |
| 96 | const std::string& locale); |
| 97 | ~SpeechRecognizer() override; |
| 98 | |
| 99 | // Start/stop the speech recognizer. |
| 100 | // Must be called on the UI thread. |
| 101 | void Start(); |
| 102 | void Stop(); |
| 103 | |
| 104 | // Overridden from vr::IOBrowserUIInterface: |
| 105 | void OnSpeechResult(const base::string16& query, bool is_final) override; |
| 106 | void OnSpeechSoundLevelChanged(float level) override; |
| 107 | void OnSpeechRecognitionStateChanged( |
| 108 | vr::SpeechRecognitionState new_state) override; |
| 109 | |
| 110 | void GetSpeechAuthParameters(std::string* auth_scope, |
| 111 | std::string* auth_token); |
| 112 | |
| 113 | static void SetManagerForTest(content::SpeechRecognitionManager* manager); |
| 114 | void SetSpeechTimerForTest(std::unique_ptr<base::Timer> speech_timer); |
| 115 | |
| 116 | private: |
| 117 | VoiceResultDelegate* delegate_; |
Biao She | c8521874 | 2017-10-26 21:48:15 | [diff] [blame] | 118 | BrowserUiInterface* ui_; |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 119 | scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 120 | std::string locale_; |
Biao She | a9dc6e7 | 2017-11-23 15:58:17 | [diff] [blame] | 121 | base::string16 final_result_; |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 122 | |
| 123 | // Note that this object is destroyed on IO thread. |
| 124 | std::unique_ptr<SpeechRecognizerOnIO> speech_recognizer_on_io_; |
| 125 | base::WeakPtrFactory<SpeechRecognizer> weak_factory_; |
| 126 | |
| 127 | DISALLOW_COPY_AND_ASSIGN(SpeechRecognizer); |
| 128 | }; |
| 129 | |
| 130 | } // namespace vr |
| 131 | |
| 132 | #endif // CHROME_BROWSER_VR_SPEECH_RECOGNIZER_H_ |