| 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(content::RenderFrameHost*); |
| 47 void StopOnIOThread(); | 47 void StopOnIOThread(); |
| 48 | 48 |
| 49 private: | 49 private: |
| 50 friend class base::RefCountedThreadSafe<SpeechRecognizer::EventListener>; | 50 friend class base::RefCountedThreadSafe<SpeechRecognizer::EventListener>; |
| 51 ~EventListener(); | 51 ~EventListener(); |
| 52 | 52 |
| 53 void NotifyRecognitionStateChanged(SpeechRecognitionState new_state); | 53 void NotifyRecognitionStateChanged(SpeechRecognitionState new_state); |
| 54 | 54 |
| 55 void StartSpeechTimeout(); | 55 void StartSpeechTimeout(); |
| 56 void StopSpeechTimeout(); | 56 void StopSpeechTimeout(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 speech_timeout_(false, false), | 97 speech_timeout_(false, false), |
| 98 session_(kInvalidSessionId), | 98 session_(kInvalidSessionId), |
| 99 weak_factory_(this) { | 99 weak_factory_(this) { |
| 100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 101 } | 101 } |
| 102 | 102 |
| 103 SpeechRecognizer::EventListener::~EventListener() { | 103 SpeechRecognizer::EventListener::~EventListener() { |
| 104 DCHECK(!speech_timeout_.IsRunning()); | 104 DCHECK(!speech_timeout_.IsRunning()); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void SpeechRecognizer::EventListener::StartOnIOThread(int render_process_id) { | 107 void SpeechRecognizer::EventListener::StartOnIOThread( |
| 108 content::RenderFrameHost* render_frame_host) { |
| 108 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 109 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 109 if (session_ != kInvalidSessionId) | 110 if (session_ != kInvalidSessionId) |
| 110 StopOnIOThread(); | 111 StopOnIOThread(); |
| 111 | 112 |
| 112 content::SpeechRecognitionSessionConfig config; | 113 content::SpeechRecognitionSessionConfig config; |
| 113 config.language = locale_; | 114 config.language = locale_; |
| 114 config.is_legacy_api = false; | 115 config.is_legacy_api = false; |
| 115 config.continuous = true; | 116 config.continuous = true; |
| 116 config.interim_results = true; | 117 config.interim_results = true; |
| 117 config.max_hypotheses = 1; | 118 config.max_hypotheses = 1; |
| 118 config.filter_profanities = true; | 119 config.filter_profanities = true; |
| 119 config.url_request_context_getter = url_request_context_getter_; | 120 config.url_request_context_getter = url_request_context_getter_; |
| 120 config.event_listener = weak_factory_.GetWeakPtr(); | 121 config.event_listener = weak_factory_.GetWeakPtr(); |
| 121 config.initial_context.render_process_id = render_process_id; | 122 config.initial_context.render_frame_host = render_frame_host; |
| 122 | 123 |
| 123 auto speech_instance = content::SpeechRecognitionManager::GetInstance(); | 124 auto speech_instance = content::SpeechRecognitionManager::GetInstance(); |
| 124 session_ = speech_instance->CreateSession(config); | 125 session_ = speech_instance->CreateSession(config); |
| 125 speech_instance->StartSession(session_); | 126 speech_instance->StartSession(session_); |
| 126 } | 127 } |
| 127 | 128 |
| 128 void SpeechRecognizer::EventListener::StopOnIOThread() { | 129 void SpeechRecognizer::EventListener::StopOnIOThread() { |
| 129 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 130 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 130 if (session_ == kInvalidSessionId) | 131 if (session_ == kInvalidSessionId) |
| 131 return; | 132 return; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 return; | 270 return; |
| 270 content::WebContents* contents = delegate_->GetSpeechContents(); | 271 content::WebContents* contents = delegate_->GetSpeechContents(); |
| 271 if (!contents) | 272 if (!contents) |
| 272 return; | 273 return; |
| 273 | 274 |
| 274 content::BrowserThread::PostTask( | 275 content::BrowserThread::PostTask( |
| 275 content::BrowserThread::IO, | 276 content::BrowserThread::IO, |
| 276 FROM_HERE, | 277 FROM_HERE, |
| 277 base::Bind(&SpeechRecognizer::EventListener::StartOnIOThread, | 278 base::Bind(&SpeechRecognizer::EventListener::StartOnIOThread, |
| 278 speech_event_listener_, | 279 speech_event_listener_, |
| 279 contents->GetRenderProcessHost()->GetID())); | 280 contents->GetMainFrame())); |
| 280 } | 281 } |
| 281 | 282 |
| 282 void SpeechRecognizer::Stop() { | 283 void SpeechRecognizer::Stop() { |
| 283 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 284 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 284 content::BrowserThread::PostTask( | 285 content::BrowserThread::PostTask( |
| 285 content::BrowserThread::IO, | 286 content::BrowserThread::IO, |
| 286 FROM_HERE, | 287 FROM_HERE, |
| 287 base::Bind(&SpeechRecognizer::EventListener::StopOnIOThread, | 288 base::Bind(&SpeechRecognizer::EventListener::StopOnIOThread, |
| 288 speech_event_listener_)); | 289 speech_event_listener_)); |
| 289 } | 290 } |
| 290 | 291 |
| 291 } // namespace app_list | 292 } // namespace app_list |
| OLD | NEW |