blob: e297e4076392acaf2168787a18e8b9e457636b94 [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
8#include <string>
9
10#include "base/macros.h"
11#include "base/memory/weak_ptr.h"
12#include "base/timer/timer.h"
13
14namespace content {
15class SpeechRecognitionManager;
16}
17
18namespace net {
19class URLRequestContextGetter;
20}
21
22namespace 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
41enum SpeechRecognitionState {
42 SPEECH_RECOGNITION_OFF = 0,
43 SPEECH_RECOGNITION_READY,
Biao Shec85218742017-10-26 21:48:1544 SPEECH_RECOGNITION_END,
Biao She03d022e2017-10-20 21:17:0245 SPEECH_RECOGNITION_RECOGNIZING,
46 SPEECH_RECOGNITION_IN_SPEECH,
Biao She537a7292017-11-10 18:18:4247 SPEECH_RECOGNITION_TRY_AGAIN,
Biao She03d022e2017-10-20 21:17:0248 SPEECH_RECOGNITION_NETWORK_ERROR,
49};
50
Biao She407f1bae2017-11-27 23:19:1851// These enums are used for histogram. Do NOT renumber or delete these enums.
52enum VoiceSearchEndState {
53 VOICE_SEARCH_OPEN_SEARCH_PAGE = 0,
54 VOICE_SEARCH_CANCEL = 1,
55 VOICE_SEARCH_TRY_AGAIN = 2,
56 COUNT,
57};
58
Biao She03d022e2017-10-20 21:17:0259class VoiceResultDelegate {
60 public:
Biao Shea9dc6e72017-11-23 15:58:1761 virtual ~VoiceResultDelegate() {}
Biao She03d022e2017-10-20 21:17:0262 virtual void OnVoiceResults(const base::string16& result) = 0;
63};
64
Biao Shec85218742017-10-26 21:48:1565class BrowserUiInterface;
Biao She03d022e2017-10-20 21:17:0266class 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.
70class 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.
91class SpeechRecognizer : public IOBrowserUIInterface {
92 public:
93 SpeechRecognizer(VoiceResultDelegate* delegate,
Biao Shec85218742017-10-26 21:48:1594 BrowserUiInterface* ui,
Biao She03d022e2017-10-20 21:17:0295 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 Shec85218742017-10-26 21:48:15118 BrowserUiInterface* ui_;
Biao She03d022e2017-10-20 21:17:02119 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
120 std::string locale_;
Biao Shea9dc6e72017-11-23 15:58:17121 base::string16 final_result_;
Biao She03d022e2017-10-20 21:17:02122
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_