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