OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/app_list/speech_recognizer.h" | 5 #include "chrome/browser/ui/app_list/speech_recognizer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
11 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
12 #include "chrome/browser/ui/app_list/speech_recognizer_delegate.h" | 12 #include "chrome/browser/ui/app_list/speech_recognizer_delegate.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/speech_recognition_event_listener.h" | 15 #include "content/public/browser/speech_recognition_event_listener.h" |
16 #include "content/public/browser/speech_recognition_manager.h" | 16 #include "content/public/browser/speech_recognition_manager.h" |
17 #include "content/public/browser/speech_recognition_session_config.h" | 17 #include "content/public/browser/speech_recognition_session_config.h" |
| 18 #include "content/public/browser/speech_recognition_session_preamble.h" |
18 #include "content/public/common/child_process_host.h" | 19 #include "content/public/common/child_process_host.h" |
19 #include "content/public/common/speech_recognition_error.h" | 20 #include "content/public/common/speech_recognition_error.h" |
20 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
21 #include "ui/app_list/speech_ui_model_observer.h" | 22 #include "ui/app_list/speech_ui_model_observer.h" |
22 | 23 |
23 namespace app_list { | 24 namespace app_list { |
24 | 25 |
25 // Length of timeout to cancel recognition if there's no speech heard. | 26 // Length of timeout to cancel recognition if there's no speech heard. |
26 static const int kNoSpeechTimeoutInSeconds = 5; | 27 static const int kNoSpeechTimeoutInSeconds = 5; |
27 | 28 |
28 // Invalid speech session. | 29 // Invalid speech session. |
29 static const int kInvalidSessionId = -1; | 30 static const int kInvalidSessionId = -1; |
30 | 31 |
31 // Speech recognizer listener. This is separate from SpeechRecognizer because | 32 // Speech recognizer listener. This is separate from SpeechRecognizer because |
32 // the speech recognition engine must function from the IO thread. Because of | 33 // the speech recognition engine must function from the IO thread. Because of |
33 // this, the lifecycle of this class must be decoupled from the lifecycle of | 34 // this, the lifecycle of this class must be decoupled from the lifecycle of |
34 // SpeechRecognizer. To avoid circular references, this class has no reference | 35 // SpeechRecognizer. To avoid circular references, this class has no reference |
35 // to SpeechRecognizer. Instead, it has a reference to the | 36 // to SpeechRecognizer. Instead, it has a reference to the |
36 // SpeechRecognizerDelegate via a weak pointer that is only ever referenced from | 37 // SpeechRecognizerDelegate via a weak pointer that is only ever referenced from |
37 // the UI thread. | 38 // the UI thread. |
38 class SpeechRecognizer::EventListener | 39 class SpeechRecognizer::EventListener |
39 : public base::RefCountedThreadSafe<SpeechRecognizer::EventListener>, | 40 : public base::RefCountedThreadSafe<SpeechRecognizer::EventListener>, |
40 public content::SpeechRecognitionEventListener { | 41 public content::SpeechRecognitionEventListener { |
41 public: | 42 public: |
42 EventListener(const base::WeakPtr<SpeechRecognizerDelegate>& delegate, | 43 EventListener(const base::WeakPtr<SpeechRecognizerDelegate>& delegate, |
43 net::URLRequestContextGetter* url_request_context_getter, | 44 net::URLRequestContextGetter* url_request_context_getter, |
44 const std::string& locale); | 45 const std::string& locale); |
45 | 46 |
46 void StartOnIOThread(const std::string& auth_scope, | 47 void StartOnIOThread( |
47 const std::string& auth_token); | 48 const std::string& auth_scope, |
| 49 const std::string& auth_token, |
| 50 const scoped_refptr<content::SpeechRecognitionSessionPreamble>& preamble); |
48 void StopOnIOThread(); | 51 void StopOnIOThread(); |
49 | 52 |
50 private: | 53 private: |
51 friend class base::RefCountedThreadSafe<SpeechRecognizer::EventListener>; | 54 friend class base::RefCountedThreadSafe<SpeechRecognizer::EventListener>; |
52 ~EventListener(); | 55 ~EventListener(); |
53 | 56 |
54 void NotifyRecognitionStateChanged(SpeechRecognitionState new_state); | 57 void NotifyRecognitionStateChanged(SpeechRecognitionState new_state); |
55 | 58 |
56 void StartSpeechTimeout(); | 59 void StartSpeechTimeout(); |
57 void StopSpeechTimeout(); | 60 void StopSpeechTimeout(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 weak_factory_(this) { | 103 weak_factory_(this) { |
101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
102 } | 105 } |
103 | 106 |
104 SpeechRecognizer::EventListener::~EventListener() { | 107 SpeechRecognizer::EventListener::~EventListener() { |
105 DCHECK(!speech_timeout_.IsRunning()); | 108 DCHECK(!speech_timeout_.IsRunning()); |
106 } | 109 } |
107 | 110 |
108 void SpeechRecognizer::EventListener::StartOnIOThread( | 111 void SpeechRecognizer::EventListener::StartOnIOThread( |
109 const std::string& auth_scope, | 112 const std::string& auth_scope, |
110 const std::string& auth_token) { | 113 const std::string& auth_token, |
| 114 const scoped_refptr<content::SpeechRecognitionSessionPreamble>& preamble) { |
111 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 115 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
112 if (session_ != kInvalidSessionId) | 116 if (session_ != kInvalidSessionId) |
113 StopOnIOThread(); | 117 StopOnIOThread(); |
114 | 118 |
115 content::SpeechRecognitionSessionConfig config; | 119 content::SpeechRecognitionSessionConfig config; |
116 config.language = locale_; | 120 config.language = locale_; |
117 config.is_legacy_api = false; | 121 config.is_legacy_api = false; |
118 config.continuous = true; | 122 config.continuous = true; |
119 config.interim_results = true; | 123 config.interim_results = true; |
120 config.max_hypotheses = 1; | 124 config.max_hypotheses = 1; |
121 config.filter_profanities = true; | 125 config.filter_profanities = true; |
122 config.url_request_context_getter = url_request_context_getter_; | 126 config.url_request_context_getter = url_request_context_getter_; |
123 config.event_listener = weak_factory_.GetWeakPtr(); | 127 config.event_listener = weak_factory_.GetWeakPtr(); |
124 // kInvalidUniqueID is not a valid render process, so the speech permission | 128 // kInvalidUniqueID is not a valid render process, so the speech permission |
125 // check allows the request through. | 129 // check allows the request through. |
126 config.initial_context.render_process_id = | 130 config.initial_context.render_process_id = |
127 content::ChildProcessHost::kInvalidUniqueID; | 131 content::ChildProcessHost::kInvalidUniqueID; |
128 config.auth_scope = auth_scope; | 132 config.auth_scope = auth_scope; |
129 config.auth_token = auth_token; | 133 config.auth_token = auth_token; |
| 134 config.preamble = preamble; |
130 | 135 |
131 auto speech_instance = content::SpeechRecognitionManager::GetInstance(); | 136 auto speech_instance = content::SpeechRecognitionManager::GetInstance(); |
132 session_ = speech_instance->CreateSession(config); | 137 session_ = speech_instance->CreateSession(config); |
133 speech_instance->StartSession(session_); | 138 speech_instance->StartSession(session_); |
134 } | 139 } |
135 | 140 |
136 void SpeechRecognizer::EventListener::StopOnIOThread() { | 141 void SpeechRecognizer::EventListener::StopOnIOThread() { |
137 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 142 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
138 if (session_ == kInvalidSessionId) | 143 if (session_ == kInvalidSessionId) |
139 return; | 144 return; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 speech_event_listener_(new EventListener( | 266 speech_event_listener_(new EventListener( |
262 delegate, url_request_context_getter, locale)) { | 267 delegate, url_request_context_getter, locale)) { |
263 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 268 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
264 } | 269 } |
265 | 270 |
266 SpeechRecognizer::~SpeechRecognizer() { | 271 SpeechRecognizer::~SpeechRecognizer() { |
267 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
268 Stop(); | 273 Stop(); |
269 } | 274 } |
270 | 275 |
271 void SpeechRecognizer::Start() { | 276 void SpeechRecognizer::Start( |
| 277 const scoped_refptr<content::SpeechRecognitionSessionPreamble>& preamble) { |
272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 278 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
273 std::string auth_scope; | 279 std::string auth_scope; |
274 std::string auth_token; | 280 std::string auth_token; |
275 delegate_->GetSpeechAuthParameters(&auth_scope, &auth_token); | 281 delegate_->GetSpeechAuthParameters(&auth_scope, &auth_token); |
276 | 282 |
277 content::BrowserThread::PostTask( | 283 content::BrowserThread::PostTask( |
278 content::BrowserThread::IO, | 284 content::BrowserThread::IO, |
279 FROM_HERE, | 285 FROM_HERE, |
280 base::Bind(&SpeechRecognizer::EventListener::StartOnIOThread, | 286 base::Bind(&SpeechRecognizer::EventListener::StartOnIOThread, |
281 speech_event_listener_, | 287 speech_event_listener_, |
282 auth_scope, | 288 auth_scope, |
283 auth_token)); | 289 auth_token, |
| 290 preamble)); |
284 } | 291 } |
285 | 292 |
286 void SpeechRecognizer::Stop() { | 293 void SpeechRecognizer::Stop() { |
287 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 294 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
288 content::BrowserThread::PostTask( | 295 content::BrowserThread::PostTask( |
289 content::BrowserThread::IO, | 296 content::BrowserThread::IO, |
290 FROM_HERE, | 297 FROM_HERE, |
291 base::Bind(&SpeechRecognizer::EventListener::StopOnIOThread, | 298 base::Bind(&SpeechRecognizer::EventListener::StopOnIOThread, |
292 speech_event_listener_)); | 299 speech_event_listener_)); |
293 } | 300 } |
294 | 301 |
295 } // namespace app_list | 302 } // namespace app_list |
OLD | NEW |