blob: 69e55efe0a71b3eb9cb154ae4871c2757943db26 [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 Grant7e73a4f2018-05-30 21:13:1414#include "chrome/browser/vr/vr_export.h"
Biao She03d022e2017-10-20 21:17:0215
16namespace content {
17class SpeechRecognitionManager;
18}
19
20namespace net {
21class URLRequestContextGetter;
22}
23
Matt Menke7b2266e2018-06-07 19:32:0924namespace network {
25class SharedURLLoaderFactoryInfo;
26}
27
Biao She03d022e2017-10-20 21:17:0228namespace 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
47enum SpeechRecognitionState {
48 SPEECH_RECOGNITION_OFF = 0,
49 SPEECH_RECOGNITION_READY,
Biao Shec85218742017-10-26 21:48:1550 SPEECH_RECOGNITION_END,
Biao She03d022e2017-10-20 21:17:0251 SPEECH_RECOGNITION_RECOGNIZING,
52 SPEECH_RECOGNITION_IN_SPEECH,
Biao She537a7292017-11-10 18:18:4253 SPEECH_RECOGNITION_TRY_AGAIN,
Biao She03d022e2017-10-20 21:17:0254 SPEECH_RECOGNITION_NETWORK_ERROR,
55};
56
Biao She407f1bae2017-11-27 23:19:1857// These enums are used for histogram. Do NOT renumber or delete these enums.
58enum VoiceSearchEndState {
59 VOICE_SEARCH_OPEN_SEARCH_PAGE = 0,
60 VOICE_SEARCH_CANCEL = 1,
61 VOICE_SEARCH_TRY_AGAIN = 2,
62 COUNT,
63};
64
Biao She03d022e2017-10-20 21:17:0265class VoiceResultDelegate {
66 public:
Biao Shea9dc6e72017-11-23 15:58:1767 virtual ~VoiceResultDelegate() {}
Biao She03d022e2017-10-20 21:17:0268 virtual void OnVoiceResults(const base::string16& result) = 0;
69};
70
Biao Shec85218742017-10-26 21:48:1571class BrowserUiInterface;
Biao She03d022e2017-10-20 21:17:0272class 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.
76class 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 Grant7e73a4f2018-05-30 21:13:1497class VR_EXPORT SpeechRecognizer : public IOBrowserUIInterface {
Biao She03d022e2017-10-20 21:17:0298 public:
Matt Menke7b2266e2018-06-07 19:32:0999 // |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 She03d022e2017-10-20 21:17:02108 ~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 Shec85218742017-10-26 21:48:15129 BrowserUiInterface* ui_;
Matt Menke7b2266e2018-06-07 19:32:09130
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 She03d022e2017-10-20 21:17:02138 std::string locale_;
Biao Shea9dc6e72017-11-23 15:58:17139 base::string16 final_result_;
Biao She03d022e2017-10-20 21:17:02140
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_