Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: chrome/browser/ui/app_list/speech_recognizer.cc

Issue 797493002: Eliminate the launcher speech recognizer's dependency on the start page WebContents. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/ui/app_list/speech_recognizer_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/web_contents.h" 18 #include "content/public/common/child_process_host.h"
19 #include "content/public/common/speech_recognition_error.h" 19 #include "content/public/common/speech_recognition_error.h"
20 #include "net/url_request/url_request_context_getter.h" 20 #include "net/url_request/url_request_context_getter.h"
21 #include "ui/app_list/speech_ui_model_observer.h" 21 #include "ui/app_list/speech_ui_model_observer.h"
22 22
23 namespace app_list { 23 namespace app_list {
24 24
25 // Length of timeout to cancel recognition if there's no speech heard. 25 // Length of timeout to cancel recognition if there's no speech heard.
26 static const int kNoSpeechTimeoutInSeconds = 5; 26 static const int kNoSpeechTimeoutInSeconds = 5;
27 27
28 // Invalid speech session. 28 // Invalid speech session.
29 static const int kInvalidSessionId = -1; 29 static const int kInvalidSessionId = -1;
30 30
31 // Speech recognizer listener. This is separate from SpeechRecognizer because 31 // Speech recognizer listener. This is separate from SpeechRecognizer because
32 // the speech recognition engine must function from the IO thread. Because of 32 // 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 33 // this, the lifecycle of this class must be decoupled from the lifecycle of
34 // SpeechRecognizer. To avoid circular references, this class has no reference 34 // SpeechRecognizer. To avoid circular references, this class has no reference
35 // to SpeechRecognizer. Instead, it has a reference to the 35 // to SpeechRecognizer. Instead, it has a reference to the
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(const std::string& auth_scope,
47 const std::string& auth_scope,
48 const std::string& auth_token); 47 const std::string& auth_token);
49 void StopOnIOThread(); 48 void StopOnIOThread();
50 49
51 private: 50 private:
52 friend class base::RefCountedThreadSafe<SpeechRecognizer::EventListener>; 51 friend class base::RefCountedThreadSafe<SpeechRecognizer::EventListener>;
53 ~EventListener(); 52 ~EventListener();
54 53
55 void NotifyRecognitionStateChanged(SpeechRecognitionState new_state); 54 void NotifyRecognitionStateChanged(SpeechRecognitionState new_state);
56 55
57 void StartSpeechTimeout(); 56 void StartSpeechTimeout();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 session_(kInvalidSessionId), 99 session_(kInvalidSessionId),
101 weak_factory_(this) { 100 weak_factory_(this) {
102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
103 } 102 }
104 103
105 SpeechRecognizer::EventListener::~EventListener() { 104 SpeechRecognizer::EventListener::~EventListener() {
106 DCHECK(!speech_timeout_.IsRunning()); 105 DCHECK(!speech_timeout_.IsRunning());
107 } 106 }
108 107
109 void SpeechRecognizer::EventListener::StartOnIOThread( 108 void SpeechRecognizer::EventListener::StartOnIOThread(
110 int render_process_id,
111 const std::string& auth_scope, 109 const std::string& auth_scope,
112 const std::string& auth_token) { 110 const std::string& auth_token) {
113 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 111 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
114 if (session_ != kInvalidSessionId) 112 if (session_ != kInvalidSessionId)
115 StopOnIOThread(); 113 StopOnIOThread();
116 114
117 content::SpeechRecognitionSessionConfig config; 115 content::SpeechRecognitionSessionConfig config;
118 config.language = locale_; 116 config.language = locale_;
119 config.is_legacy_api = false; 117 config.is_legacy_api = false;
120 config.continuous = true; 118 config.continuous = true;
121 config.interim_results = true; 119 config.interim_results = true;
122 config.max_hypotheses = 1; 120 config.max_hypotheses = 1;
123 config.filter_profanities = true; 121 config.filter_profanities = true;
124 config.url_request_context_getter = url_request_context_getter_; 122 config.url_request_context_getter = url_request_context_getter_;
125 config.event_listener = weak_factory_.GetWeakPtr(); 123 config.event_listener = weak_factory_.GetWeakPtr();
126 config.initial_context.render_process_id = render_process_id; 124 // kInvalidUniqueID is not a valid render process, so the speech permission
125 // check allows the request through.
126 config.initial_context.render_process_id =
127 content::ChildProcessHost::kInvalidUniqueID;
127 config.auth_scope = auth_scope; 128 config.auth_scope = auth_scope;
128 config.auth_token = auth_token; 129 config.auth_token = auth_token;
129 130
130 auto speech_instance = content::SpeechRecognitionManager::GetInstance(); 131 auto speech_instance = content::SpeechRecognitionManager::GetInstance();
131 session_ = speech_instance->CreateSession(config); 132 session_ = speech_instance->CreateSession(config);
132 speech_instance->StartSession(session_); 133 speech_instance->StartSession(session_);
133 } 134 }
134 135
135 void SpeechRecognizer::EventListener::StopOnIOThread() { 136 void SpeechRecognizer::EventListener::StopOnIOThread() {
136 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 137 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 263 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
263 } 264 }
264 265
265 SpeechRecognizer::~SpeechRecognizer() { 266 SpeechRecognizer::~SpeechRecognizer() {
266 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 267 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
267 Stop(); 268 Stop();
268 } 269 }
269 270
270 void SpeechRecognizer::Start() { 271 void SpeechRecognizer::Start() {
271 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
272 // The speech recognizer checks to see if the request is allowed by looking
273 // up the renderer process. A renderer containing the app-list is hard-coded
274 // to be allowed.
275 if (!delegate_)
276 return;
277 content::WebContents* contents = delegate_->GetSpeechContents();
278 if (!contents)
279 return;
280
281 std::string auth_scope; 273 std::string auth_scope;
282 std::string auth_token; 274 std::string auth_token;
283 delegate_->GetSpeechAuthParameters(&auth_scope, &auth_token); 275 delegate_->GetSpeechAuthParameters(&auth_scope, &auth_token);
284 276
285 content::BrowserThread::PostTask( 277 content::BrowserThread::PostTask(
286 content::BrowserThread::IO, 278 content::BrowserThread::IO,
287 FROM_HERE, 279 FROM_HERE,
288 base::Bind(&SpeechRecognizer::EventListener::StartOnIOThread, 280 base::Bind(&SpeechRecognizer::EventListener::StartOnIOThread,
289 speech_event_listener_, 281 speech_event_listener_,
290 contents->GetRenderProcessHost()->GetID(),
291 auth_scope, 282 auth_scope,
292 auth_token)); 283 auth_token));
293 } 284 }
294 285
295 void SpeechRecognizer::Stop() { 286 void SpeechRecognizer::Stop() {
296 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 287 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
297 content::BrowserThread::PostTask( 288 content::BrowserThread::PostTask(
298 content::BrowserThread::IO, 289 content::BrowserThread::IO,
299 FROM_HERE, 290 FROM_HERE,
300 base::Bind(&SpeechRecognizer::EventListener::StopOnIOThread, 291 base::Bind(&SpeechRecognizer::EventListener::StopOnIOThread,
301 speech_event_listener_)); 292 speech_event_listener_));
302 } 293 }
303 294
304 } // namespace app_list 295 } // namespace app_list
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/app_list/speech_recognizer_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698