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