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 | |
Matt Menke | 7b2266e | 2018-06-07 19:32:09 | [diff] [blame] | 8 | #include <memory> |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | #include "base/macros.h" |
| 12 | #include "base/memory/weak_ptr.h" |
| 13 | #include "base/timer/timer.h" |
Christopher Grant | f47c6e5 | 2019-01-02 20:05:11 | [diff] [blame] | 14 | #include "chrome/browser/vr/vr_base_export.h" |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 15 | |
| 16 | namespace content { |
| 17 | class SpeechRecognitionManager; |
| 18 | } |
| 19 | |
Matt Menke | 7b2266e | 2018-06-07 19:32:09 | [diff] [blame] | 20 | namespace network { |
Dominic Farolino | bc280d2 | 2019-12-05 05:49:24 | [diff] [blame] | 21 | class PendingSharedURLLoaderFactory; |
Matt Menke | 7b2266e | 2018-06-07 19:32:09 | [diff] [blame] | 22 | } |
| 23 | |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 24 | namespace vr { |
| 25 | |
| 26 | // Note that speech recognition is activated on VR UI thread. This means it |
| 27 | // usually involves 3 threads. In the simplest case, the thread communication |
| 28 | // looks like the following: |
| 29 | // VR UI thread Browser thread IO thread |
| 30 | // | | | |
| 31 | // |----ActivateVS----->| | |
| 32 | // | |------Start------> | |
| 33 | // | | | |
| 34 | // | |<-NotifyStateChange-| |
| 35 | // |<--OnSRStateChanged-| | |
| 36 | // | | | |
| 37 | // | |<--OnSpeechResult---| |
| 38 | // |<--OnSRStateChanged-| | |
| 39 | // | navigate | |
| 40 | // | | | |
| 41 | // VS = voice search, SR = speech recognition |
| 42 | |
| 43 | enum SpeechRecognitionState { |
| 44 | SPEECH_RECOGNITION_OFF = 0, |
| 45 | SPEECH_RECOGNITION_READY, |
Biao She | c8521874 | 2017-10-26 21:48:15 | [diff] [blame] | 46 | SPEECH_RECOGNITION_END, |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 47 | SPEECH_RECOGNITION_RECOGNIZING, |
| 48 | SPEECH_RECOGNITION_IN_SPEECH, |
Biao She | 537a729 | 2017-11-10 18:18:42 | [diff] [blame] | 49 | SPEECH_RECOGNITION_TRY_AGAIN, |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 50 | SPEECH_RECOGNITION_NETWORK_ERROR, |
| 51 | }; |
| 52 | |
Biao She | 407f1bae | 2017-11-27 23:19:18 | [diff] [blame] | 53 | // These enums are used for histogram. Do NOT renumber or delete these enums. |
| 54 | enum VoiceSearchEndState { |
| 55 | VOICE_SEARCH_OPEN_SEARCH_PAGE = 0, |
| 56 | VOICE_SEARCH_CANCEL = 1, |
| 57 | VOICE_SEARCH_TRY_AGAIN = 2, |
| 58 | COUNT, |
| 59 | }; |
| 60 | |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 61 | class VoiceResultDelegate { |
| 62 | public: |
Biao She | a9dc6e7 | 2017-11-23 15:58:17 | [diff] [blame] | 63 | virtual ~VoiceResultDelegate() {} |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 64 | virtual void OnVoiceResults(const base::string16& result) = 0; |
| 65 | }; |
| 66 | |
Biao She | c8521874 | 2017-10-26 21:48:15 | [diff] [blame] | 67 | class BrowserUiInterface; |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 68 | class SpeechRecognizerOnIO; |
| 69 | |
| 70 | // An interface for IO to communicate with browser UI thread. |
| 71 | // This is used by SpeechRecognizerOnIO class who lives on IO thread. |
| 72 | class IOBrowserUIInterface { |
| 73 | public: |
| 74 | // Receive a speech recognition result. |is_final| indicated whether the |
| 75 | // result is an intermediate or final result. If |is_final| is true, then the |
| 76 | // recognizer stops and no more results will be returned. |
| 77 | virtual void OnSpeechResult(const base::string16& query, bool is_final) = 0; |
| 78 | |
| 79 | // Invoked regularly to indicate the average sound volume. |
| 80 | virtual void OnSpeechSoundLevelChanged(float level) = 0; |
| 81 | |
| 82 | // Invoked when the state of speech recognition is changed. |
| 83 | virtual void OnSpeechRecognitionStateChanged( |
| 84 | SpeechRecognitionState new_state) = 0; |
| 85 | |
| 86 | protected: |
| 87 | virtual ~IOBrowserUIInterface() {} |
| 88 | }; |
| 89 | |
| 90 | // SpeechRecognizer is a wrapper around the speech recognition engine that |
| 91 | // simplifies its use from the UI thread. This class handles all setup/shutdown, |
| 92 | // collection of results, error cases, and threading. |
Christopher Grant | f47c6e5 | 2019-01-02 20:05:11 | [diff] [blame] | 93 | class VR_BASE_EXPORT SpeechRecognizer : public IOBrowserUIInterface { |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 94 | public: |
Dominic Farolino | bc280d2 | 2019-12-05 05:49:24 | [diff] [blame] | 95 | // |pending_shared_url_loader_factory| must be for a creating a |
Matt Menke | 7b2266e | 2018-06-07 19:32:09 | [diff] [blame] | 96 | // SharedURLLoaderFactory that can be used on the IO Thread. |
Matt Menke | 68f625b | 2018-07-30 20:40:06 | [diff] [blame] | 97 | SpeechRecognizer(VoiceResultDelegate* delegate, |
| 98 | BrowserUiInterface* ui, |
Dominic Farolino | bc280d2 | 2019-12-05 05:49:24 | [diff] [blame] | 99 | std::unique_ptr<network::PendingSharedURLLoaderFactory> |
| 100 | pending_shared_url_loader_factory, |
Matt Menke | 68f625b | 2018-07-30 20:40:06 | [diff] [blame] | 101 | const std::string& accept_language, |
| 102 | const std::string& locale); |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 103 | ~SpeechRecognizer() override; |
| 104 | |
| 105 | // Start/stop the speech recognizer. |
| 106 | // Must be called on the UI thread. |
| 107 | void Start(); |
| 108 | void Stop(); |
| 109 | |
| 110 | // Overridden from vr::IOBrowserUIInterface: |
| 111 | void OnSpeechResult(const base::string16& query, bool is_final) override; |
| 112 | void OnSpeechSoundLevelChanged(float level) override; |
| 113 | void OnSpeechRecognitionStateChanged( |
| 114 | vr::SpeechRecognitionState new_state) override; |
| 115 | |
| 116 | void GetSpeechAuthParameters(std::string* auth_scope, |
| 117 | std::string* auth_token); |
| 118 | |
| 119 | static void SetManagerForTest(content::SpeechRecognitionManager* manager); |
tzik | d4eba7a | 2018-07-10 01:12:14 | [diff] [blame] | 120 | void SetSpeechTimerForTest(std::unique_ptr<base::OneShotTimer> speech_timer); |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | VoiceResultDelegate* delegate_; |
Biao She | c8521874 | 2017-10-26 21:48:15 | [diff] [blame] | 124 | BrowserUiInterface* ui_; |
Matt Menke | 7b2266e | 2018-06-07 19:32:09 | [diff] [blame] | 125 | |
| 126 | // Non-null until first Start() call, at which point it's moved to the IO |
| 127 | // thread. |
Dominic Farolino | bc280d2 | 2019-12-05 05:49:24 | [diff] [blame] | 128 | std::unique_ptr<network::PendingSharedURLLoaderFactory> |
| 129 | pending_shared_url_loader_factory_; |
Matt Menke | 7b2266e | 2018-06-07 19:32:09 | [diff] [blame] | 130 | |
Matt Menke | 68f625b | 2018-07-30 20:40:06 | [diff] [blame] | 131 | const std::string accept_language_; |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 132 | std::string locale_; |
Biao She | a9dc6e7 | 2017-11-23 15:58:17 | [diff] [blame] | 133 | base::string16 final_result_; |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 134 | |
| 135 | // Note that this object is destroyed on IO thread. |
| 136 | std::unique_ptr<SpeechRecognizerOnIO> speech_recognizer_on_io_; |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 137 | base::WeakPtrFactory<SpeechRecognizer> weak_factory_{this}; |
Biao She | 03d022e | 2017-10-20 21:17:02 | [diff] [blame] | 138 | |
| 139 | DISALLOW_COPY_AND_ASSIGN(SpeechRecognizer); |
| 140 | }; |
| 141 | |
| 142 | } // namespace vr |
| 143 | |
| 144 | #endif // CHROME_BROWSER_VR_SPEECH_RECOGNIZER_H_ |