blob: fb6cc0609a7a257fefb9c45aab0cfa25b18588e8 [file] [log] [blame]
Biao She03d022e2017-10-20 21:17:021// 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 Menke7b2266e2018-06-07 19:32:098#include <memory>
Biao She03d022e2017-10-20 21:17:029#include <string>
10
11#include "base/macros.h"
12#include "base/memory/weak_ptr.h"
13#include "base/timer/timer.h"
Christopher Grantf47c6e52019-01-02 20:05:1114#include "chrome/browser/vr/vr_base_export.h"
Biao She03d022e2017-10-20 21:17:0215
16namespace content {
17class SpeechRecognitionManager;
18}
19
Matt Menke7b2266e2018-06-07 19:32:0920namespace network {
Dominic Farolinobc280d22019-12-05 05:49:2421class PendingSharedURLLoaderFactory;
Matt Menke7b2266e2018-06-07 19:32:0922}
23
Biao She03d022e2017-10-20 21:17:0224namespace 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
43enum SpeechRecognitionState {
44 SPEECH_RECOGNITION_OFF = 0,
45 SPEECH_RECOGNITION_READY,
Biao Shec85218742017-10-26 21:48:1546 SPEECH_RECOGNITION_END,
Biao She03d022e2017-10-20 21:17:0247 SPEECH_RECOGNITION_RECOGNIZING,
48 SPEECH_RECOGNITION_IN_SPEECH,
Biao She537a7292017-11-10 18:18:4249 SPEECH_RECOGNITION_TRY_AGAIN,
Biao She03d022e2017-10-20 21:17:0250 SPEECH_RECOGNITION_NETWORK_ERROR,
51};
52
Biao She407f1bae2017-11-27 23:19:1853// These enums are used for histogram. Do NOT renumber or delete these enums.
54enum VoiceSearchEndState {
55 VOICE_SEARCH_OPEN_SEARCH_PAGE = 0,
56 VOICE_SEARCH_CANCEL = 1,
57 VOICE_SEARCH_TRY_AGAIN = 2,
58 COUNT,
59};
60
Biao She03d022e2017-10-20 21:17:0261class VoiceResultDelegate {
62 public:
Biao Shea9dc6e72017-11-23 15:58:1763 virtual ~VoiceResultDelegate() {}
Biao She03d022e2017-10-20 21:17:0264 virtual void OnVoiceResults(const base::string16& result) = 0;
65};
66
Biao Shec85218742017-10-26 21:48:1567class BrowserUiInterface;
Biao She03d022e2017-10-20 21:17:0268class 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.
72class 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 Grantf47c6e52019-01-02 20:05:1193class VR_BASE_EXPORT SpeechRecognizer : public IOBrowserUIInterface {
Biao She03d022e2017-10-20 21:17:0294 public:
Dominic Farolinobc280d22019-12-05 05:49:2495 // |pending_shared_url_loader_factory| must be for a creating a
Matt Menke7b2266e2018-06-07 19:32:0996 // SharedURLLoaderFactory that can be used on the IO Thread.
Matt Menke68f625b2018-07-30 20:40:0697 SpeechRecognizer(VoiceResultDelegate* delegate,
98 BrowserUiInterface* ui,
Dominic Farolinobc280d22019-12-05 05:49:2499 std::unique_ptr<network::PendingSharedURLLoaderFactory>
100 pending_shared_url_loader_factory,
Matt Menke68f625b2018-07-30 20:40:06101 const std::string& accept_language,
102 const std::string& locale);
Biao She03d022e2017-10-20 21:17:02103 ~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);
tzikd4eba7a2018-07-10 01:12:14120 void SetSpeechTimerForTest(std::unique_ptr<base::OneShotTimer> speech_timer);
Biao She03d022e2017-10-20 21:17:02121
122 private:
123 VoiceResultDelegate* delegate_;
Biao Shec85218742017-10-26 21:48:15124 BrowserUiInterface* ui_;
Matt Menke7b2266e2018-06-07 19:32:09125
126 // Non-null until first Start() call, at which point it's moved to the IO
127 // thread.
Dominic Farolinobc280d22019-12-05 05:49:24128 std::unique_ptr<network::PendingSharedURLLoaderFactory>
129 pending_shared_url_loader_factory_;
Matt Menke7b2266e2018-06-07 19:32:09130
Matt Menke68f625b2018-07-30 20:40:06131 const std::string accept_language_;
Biao She03d022e2017-10-20 21:17:02132 std::string locale_;
Biao Shea9dc6e72017-11-23 15:58:17133 base::string16 final_result_;
Biao She03d022e2017-10-20 21:17:02134
135 // Note that this object is destroyed on IO thread.
136 std::unique_ptr<SpeechRecognizerOnIO> speech_recognizer_on_io_;
Jeremy Roman495db682019-07-12 16:03:24137 base::WeakPtrFactory<SpeechRecognizer> weak_factory_{this};
Biao She03d022e2017-10-20 21:17:02138
139 DISALLOW_COPY_AND_ASSIGN(SpeechRecognizer);
140};
141
142} // namespace vr
143
144#endif // CHROME_BROWSER_VR_SPEECH_RECOGNIZER_H_