| 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" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 // SpeechRecognizerDelegate via a weak pointer that is only ever referenced from | 36 // SpeechRecognizerDelegate via a weak pointer that is only ever referenced from |
| 37 // the UI thread. | 37 // the UI thread. |
| 38 class SpeechRecognizer::EventListener | 38 class SpeechRecognizer::EventListener |
| 39 : public base::RefCountedThreadSafe<SpeechRecognizer::EventListener>, | 39 : public base::RefCountedThreadSafe<SpeechRecognizer::EventListener>, |
| 40 public content::SpeechRecognitionEventListener { | 40 public content::SpeechRecognitionEventListener { |
| 41 public: | 41 public: |
| 42 EventListener(const base::WeakPtr<SpeechRecognizerDelegate>& delegate, | 42 EventListener(const base::WeakPtr<SpeechRecognizerDelegate>& delegate, |
| 43 net::URLRequestContextGetter* url_request_context_getter, | 43 net::URLRequestContextGetter* url_request_context_getter, |
| 44 const std::string& locale); | 44 const std::string& locale); |
| 45 | 45 |
| 46 void StartOnIOThread(int render_process_id); | 46 void StartOnIOThread(int render_process_id, |
| 47 const std::string& auth_scope, |
| 48 const std::string& auth_token); |
| 47 void StopOnIOThread(); | 49 void StopOnIOThread(); |
| 48 | 50 |
| 49 private: | 51 private: |
| 50 friend class base::RefCountedThreadSafe<SpeechRecognizer::EventListener>; | 52 friend class base::RefCountedThreadSafe<SpeechRecognizer::EventListener>; |
| 51 ~EventListener(); | 53 ~EventListener(); |
| 52 | 54 |
| 53 void NotifyRecognitionStateChanged(SpeechRecognitionState new_state); | 55 void NotifyRecognitionStateChanged(SpeechRecognitionState new_state); |
| 54 | 56 |
| 55 void StartSpeechTimeout(); | 57 void StartSpeechTimeout(); |
| 56 void StopSpeechTimeout(); | 58 void StopSpeechTimeout(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 speech_timeout_(false, false), | 99 speech_timeout_(false, false), |
| 98 session_(kInvalidSessionId), | 100 session_(kInvalidSessionId), |
| 99 weak_factory_(this) { | 101 weak_factory_(this) { |
| 100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 101 } | 103 } |
| 102 | 104 |
| 103 SpeechRecognizer::EventListener::~EventListener() { | 105 SpeechRecognizer::EventListener::~EventListener() { |
| 104 DCHECK(!speech_timeout_.IsRunning()); | 106 DCHECK(!speech_timeout_.IsRunning()); |
| 105 } | 107 } |
| 106 | 108 |
| 107 void SpeechRecognizer::EventListener::StartOnIOThread(int render_process_id) { | 109 void SpeechRecognizer::EventListener::StartOnIOThread( |
| 110 int render_process_id, |
| 111 const std::string& auth_scope, |
| 112 const std::string& auth_token) { |
| 108 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 109 if (session_ != kInvalidSessionId) | 114 if (session_ != kInvalidSessionId) |
| 110 StopOnIOThread(); | 115 StopOnIOThread(); |
| 111 | 116 |
| 112 content::SpeechRecognitionSessionConfig config; | 117 content::SpeechRecognitionSessionConfig config; |
| 113 config.language = locale_; | 118 config.language = locale_; |
| 114 config.is_legacy_api = false; | 119 config.is_legacy_api = false; |
| 115 config.continuous = true; | 120 config.continuous = true; |
| 116 config.interim_results = true; | 121 config.interim_results = true; |
| 117 config.max_hypotheses = 1; | 122 config.max_hypotheses = 1; |
| 118 config.filter_profanities = true; | 123 config.filter_profanities = true; |
| 119 config.url_request_context_getter = url_request_context_getter_; | 124 config.url_request_context_getter = url_request_context_getter_; |
| 120 config.event_listener = weak_factory_.GetWeakPtr(); | 125 config.event_listener = weak_factory_.GetWeakPtr(); |
| 121 config.initial_context.render_process_id = render_process_id; | 126 config.initial_context.render_process_id = render_process_id; |
| 127 config.auth_scope = auth_scope; |
| 128 config.auth_token = auth_token; |
| 122 | 129 |
| 123 auto speech_instance = content::SpeechRecognitionManager::GetInstance(); | 130 auto speech_instance = content::SpeechRecognitionManager::GetInstance(); |
| 124 session_ = speech_instance->CreateSession(config); | 131 session_ = speech_instance->CreateSession(config); |
| 125 speech_instance->StartSession(session_); | 132 speech_instance->StartSession(session_); |
| 126 } | 133 } |
| 127 | 134 |
| 128 void SpeechRecognizer::EventListener::StopOnIOThread() { | 135 void SpeechRecognizer::EventListener::StopOnIOThread() { |
| 129 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 136 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 130 if (session_ == kInvalidSessionId) | 137 if (session_ == kInvalidSessionId) |
| 131 return; | 138 return; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 | 251 |
| 245 void SpeechRecognizer::EventListener::OnAudioEnd(int session_id) { | 252 void SpeechRecognizer::EventListener::OnAudioEnd(int session_id) { |
| 246 } | 253 } |
| 247 | 254 |
| 248 SpeechRecognizer::SpeechRecognizer( | 255 SpeechRecognizer::SpeechRecognizer( |
| 249 const base::WeakPtr<SpeechRecognizerDelegate>& delegate, | 256 const base::WeakPtr<SpeechRecognizerDelegate>& delegate, |
| 250 net::URLRequestContextGetter* url_request_context_getter, | 257 net::URLRequestContextGetter* url_request_context_getter, |
| 251 const std::string& locale) | 258 const std::string& locale) |
| 252 : delegate_(delegate), | 259 : delegate_(delegate), |
| 253 speech_event_listener_(new EventListener( | 260 speech_event_listener_(new EventListener( |
| 254 delegate, url_request_context_getter, locale)){ | 261 delegate, url_request_context_getter, locale)) { |
| 255 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 262 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 256 } | 263 } |
| 257 | 264 |
| 258 SpeechRecognizer::~SpeechRecognizer() { | 265 SpeechRecognizer::~SpeechRecognizer() { |
| 259 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 266 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 260 Stop(); | 267 Stop(); |
| 261 } | 268 } |
| 262 | 269 |
| 263 void SpeechRecognizer::Start() { | 270 void SpeechRecognizer::Start() { |
| 264 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 271 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 265 // The speech recognizer checks to see if the request is allowed by looking | 272 // The speech recognizer checks to see if the request is allowed by looking |
| 266 // up the renderer process. A renderer containing the app-list is hard-coded | 273 // up the renderer process. A renderer containing the app-list is hard-coded |
| 267 // to be allowed. | 274 // to be allowed. |
| 268 if (!delegate_) | 275 if (!delegate_) |
| 269 return; | 276 return; |
| 270 content::WebContents* contents = delegate_->GetSpeechContents(); | 277 content::WebContents* contents = delegate_->GetSpeechContents(); |
| 271 if (!contents) | 278 if (!contents) |
| 272 return; | 279 return; |
| 273 | 280 |
| 281 std::string auth_scope; |
| 282 std::string auth_token; |
| 283 delegate_->GetSpeechAuthParameters(&auth_scope, &auth_token); |
| 284 |
| 274 content::BrowserThread::PostTask( | 285 content::BrowserThread::PostTask( |
| 275 content::BrowserThread::IO, | 286 content::BrowserThread::IO, |
| 276 FROM_HERE, | 287 FROM_HERE, |
| 277 base::Bind(&SpeechRecognizer::EventListener::StartOnIOThread, | 288 base::Bind(&SpeechRecognizer::EventListener::StartOnIOThread, |
| 278 speech_event_listener_, | 289 speech_event_listener_, |
| 279 contents->GetRenderProcessHost()->GetID())); | 290 contents->GetRenderProcessHost()->GetID(), |
| 291 auth_scope, |
| 292 auth_token)); |
| 280 } | 293 } |
| 281 | 294 |
| 282 void SpeechRecognizer::Stop() { | 295 void SpeechRecognizer::Stop() { |
| 283 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 296 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 284 content::BrowserThread::PostTask( | 297 content::BrowserThread::PostTask( |
| 285 content::BrowserThread::IO, | 298 content::BrowserThread::IO, |
| 286 FROM_HERE, | 299 FROM_HERE, |
| 287 base::Bind(&SpeechRecognizer::EventListener::StopOnIOThread, | 300 base::Bind(&SpeechRecognizer::EventListener::StopOnIOThread, |
| 288 speech_event_listener_)); | 301 speech_event_listener_)); |
| 289 } | 302 } |
| 290 | 303 |
| 291 } // namespace app_list | 304 } // namespace app_list |
| OLD | NEW |