| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/speech/chrome_speech_recognition_manager_delegate.h" | 5 #include "chrome/browser/speech/chrome_speech_recognition_manager_delegate.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| 11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/browser/speech/chrome_speech_recognition_preferences.h" |
| 16 #include "chrome/browser/speech/speech_recognition_tray_icon_controller.h" | 17 #include "chrome/browser/speech/speech_recognition_tray_icon_controller.h" |
| 17 #include "chrome/browser/tab_contents/tab_util.h" | 18 #include "chrome/browser/tab_contents/tab_util.h" |
| 18 #include "chrome/browser/view_type_utils.h" | 19 #include "chrome/browser/view_type_utils.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/resource_context.h" | 24 #include "content/public/browser/resource_context.h" |
| 23 #include "content/public/browser/speech_recognition_manager.h" | 25 #include "content/public/browser/speech_recognition_manager.h" |
| 24 #include "content/public/browser/speech_recognition_session_config.h" | 26 #include "content/public/browser/speech_recognition_session_config.h" |
| 25 #include "content/public/browser/speech_recognition_session_context.h" | 27 #include "content/public/browser/speech_recognition_session_context.h" |
| 26 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/common/speech_recognition_error.h" | 29 #include "content/public/common/speech_recognition_error.h" |
| 28 #include "content/public/common/speech_recognition_result.h" | 30 #include "content/public/common/speech_recognition_result.h" |
| 29 #include "grit/generated_resources.h" | 31 #include "grit/generated_resources.h" |
| 30 #include "net/url_request/url_request_context_getter.h" | 32 #include "net/url_request/url_request_context_getter.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 33 #include "ui/base/l10n/l10n_util.h" |
| 32 | 34 |
| 33 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
| 34 #include "chrome/installer/util/wmi.h" | 36 #include "chrome/installer/util/wmi.h" |
| 35 #endif | 37 #endif |
| 36 | 38 |
| 37 using content::BrowserThread; | 39 using content::BrowserThread; |
| 38 using content::SpeechRecognitionManager; | 40 using content::SpeechRecognitionManager; |
| 41 using content::SpeechRecognitionSessionContext; |
| 39 using content::WebContents; | 42 using content::WebContents; |
| 40 using content::SpeechRecognitionSessionContext; | |
| 41 | 43 |
| 42 namespace { | 44 namespace { |
| 43 const int kNoActiveBubble = | 45 const int kNoActiveBubble = |
| 44 content::SpeechRecognitionManager::kSessionIDInvalid; | 46 content::SpeechRecognitionManager::kSessionIDInvalid; |
| 45 | 47 |
| 46 bool RequiresBubble(int session_id) { | 48 bool RequiresBubble(int session_id) { |
| 47 return SpeechRecognitionManager::GetInstance()-> | 49 return SpeechRecognitionManager::GetInstance()-> |
| 48 GetSessionContext(session_id).requested_by_page_element; | 50 GetSessionContext(session_id).requested_by_page_element; |
| 49 } | 51 } |
| 50 | 52 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 | 205 |
| 204 // TODO(primiano) Why not creating directly the bubble in warmup mode? | 206 // TODO(primiano) Why not creating directly the bubble in warmup mode? |
| 205 GetBubbleController()->SetBubbleWarmUpMode(session_id); | 207 GetBubbleController()->SetBubbleWarmUpMode(session_id); |
| 206 } | 208 } |
| 207 } | 209 } |
| 208 | 210 |
| 209 void ChromeSpeechRecognitionManagerDelegate::OnAudioStart(int session_id) { | 211 void ChromeSpeechRecognitionManagerDelegate::OnAudioStart(int session_id) { |
| 210 if (RequiresBubble(session_id)) { | 212 if (RequiresBubble(session_id)) { |
| 211 GetBubbleController()->SetBubbleRecordingMode(session_id); | 213 GetBubbleController()->SetBubbleRecordingMode(session_id); |
| 212 } else if (RequiresTrayIcon(session_id)) { | 214 } else if (RequiresTrayIcon(session_id)) { |
| 215 // We post the action to the UI thread for sessions requiring a tray icon, |
| 216 // since ChromeSpeechRecognitionPreferences (which requires UI thread) is |
| 217 // involved for determining whether a security alert balloon is required. |
| 213 const content::SpeechRecognitionSessionContext& context = | 218 const content::SpeechRecognitionSessionContext& context = |
| 214 SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id); | 219 SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id); |
| 215 GetTrayIconController()->Show(context.context_name, | 220 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| 216 context.is_first_request_for_context); | 221 &ChromeSpeechRecognitionManagerDelegate::ShowTrayIconOnUIThread, |
| 222 context.context_name, |
| 223 context.render_process_id, |
| 224 scoped_refptr<SpeechRecognitionTrayIconController>( |
| 225 GetTrayIconController()))); |
| 217 } | 226 } |
| 218 } | 227 } |
| 219 | 228 |
| 220 void ChromeSpeechRecognitionManagerDelegate::OnEnvironmentEstimationComplete( | 229 void ChromeSpeechRecognitionManagerDelegate::OnEnvironmentEstimationComplete( |
| 221 int session_id) { | 230 int session_id) { |
| 222 } | 231 } |
| 223 | 232 |
| 224 void ChromeSpeechRecognitionManagerDelegate::OnSoundStart(int session_id) { | 233 void ChromeSpeechRecognitionManagerDelegate::OnSoundStart(int session_id) { |
| 225 } | 234 } |
| 226 | 235 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 } | 337 } |
| 329 *can_report_metrics = optional_request_info_->can_report_metrics(); | 338 *can_report_metrics = optional_request_info_->can_report_metrics(); |
| 330 *hardware_info = optional_request_info_->value(); | 339 *hardware_info = optional_request_info_->value(); |
| 331 } | 340 } |
| 332 | 341 |
| 333 void ChromeSpeechRecognitionManagerDelegate::CheckRecognitionIsAllowed( | 342 void ChromeSpeechRecognitionManagerDelegate::CheckRecognitionIsAllowed( |
| 334 int session_id, | 343 int session_id, |
| 335 base::Callback<void(int session_id, bool is_allowed)> callback) { | 344 base::Callback<void(int session_id, bool is_allowed)> callback) { |
| 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 345 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 337 | 346 |
| 347 const content::SpeechRecognitionSessionContext& context = |
| 348 SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id); |
| 349 |
| 350 // Make sure that initiators (extensions/web pages) properly set the |
| 351 // |render_process_id| field, which is needed later to retrieve the |
| 352 // ChromeSpeechRecognitionPreferences associated to their profile. |
| 353 DCHECK_NE(context.render_process_id, 0); |
| 354 |
| 338 // We don't need any particular check for sessions not using a bubble. In such | 355 // We don't need any particular check for sessions not using a bubble. In such |
| 339 // cases, we just notify it to the manager (calling-back synchronously, since | 356 // cases, we just notify it to the manager (calling-back synchronously, since |
| 340 // we remain in the IO thread). | 357 // we remain in the IO thread). |
| 341 if (RequiresTrayIcon(session_id)) { | 358 if (RequiresTrayIcon(session_id)) { |
| 342 callback.Run(session_id, true /* is_allowed */); | 359 callback.Run(session_id, true /* is_allowed */); |
| 343 return; | 360 return; |
| 344 } | 361 } |
| 345 | 362 |
| 346 // Sessions using bubbles, conversely, need a check on the renderer view type. | 363 // Sessions using bubbles, conversely, need a check on the renderer view type. |
| 347 // The check must be performed in the UI thread. We defer it posting to | 364 // The check must be performed in the UI thread. We defer it posting to |
| 348 // CheckRenderViewType, which will issue the callback on our behalf. | 365 // CheckRenderViewType, which will issue the callback on our behalf. |
| 349 const content::SpeechRecognitionSessionContext& context = | |
| 350 SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id); | |
| 351 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 366 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 352 base::Bind(&CheckRenderViewType, | 367 base::Bind(&CheckRenderViewType, |
| 353 session_id, | 368 session_id, |
| 354 callback, | 369 callback, |
| 355 context.render_process_id, | 370 context.render_process_id, |
| 356 context.render_view_id)); | 371 context.render_view_id)); |
| 357 } | 372 } |
| 358 | 373 |
| 359 content::SpeechRecognitionEventListener* | 374 content::SpeechRecognitionEventListener* |
| 360 ChromeSpeechRecognitionManagerDelegate::GetEventListener() { | 375 ChromeSpeechRecognitionManagerDelegate::GetEventListener() { |
| 361 return this; | 376 return this; |
| 362 } | 377 } |
| 363 | 378 |
| 379 void ChromeSpeechRecognitionManagerDelegate::ShowTrayIconOnUIThread( |
| 380 const std::string& context_name, |
| 381 int render_process_id, |
| 382 scoped_refptr<SpeechRecognitionTrayIconController> tray_icon_controller) { |
| 383 content::RenderProcessHost* render_process_host = |
| 384 content::RenderProcessHost::FromID(render_process_id); |
| 385 DCHECK(render_process_host); |
| 386 content::BrowserContext* browser_context = |
| 387 render_process_host->GetBrowserContext(); |
| 388 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 389 scoped_refptr<ChromeSpeechRecognitionPreferences> pref = |
| 390 ChromeSpeechRecognitionPreferences::GetForProfile(profile); |
| 391 bool show_notification = pref->ShouldShowSecurityNotification(context_name); |
| 392 |
| 393 tray_icon_controller->Show(ASCIIToUTF16(context_name), show_notification); |
| 394 if (show_notification) |
| 395 pref->SetHasShownSecurityNotification(context_name); |
| 396 } |
| 397 |
| 364 void ChromeSpeechRecognitionManagerDelegate::CheckRenderViewType( | 398 void ChromeSpeechRecognitionManagerDelegate::CheckRenderViewType( |
| 365 int session_id, | 399 int session_id, |
| 366 base::Callback<void(int session_id, bool is_allowed)> callback, | 400 base::Callback<void(int session_id, bool is_allowed)> callback, |
| 367 int render_process_id, | 401 int render_process_id, |
| 368 int render_view_id) { | 402 int render_view_id) { |
| 369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 370 const content::RenderViewHost* render_view_host = | 404 const content::RenderViewHost* render_view_host = |
| 371 content::RenderViewHost::FromID(render_process_id, render_view_id); | 405 content::RenderViewHost::FromID(render_process_id, render_view_id); |
| 372 bool allowed = false; | 406 bool allowed = false; |
| 373 if (render_view_host) { | 407 if (render_view_host) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 396 | 430 |
| 397 SpeechRecognitionTrayIconController* | 431 SpeechRecognitionTrayIconController* |
| 398 ChromeSpeechRecognitionManagerDelegate::GetTrayIconController() { | 432 ChromeSpeechRecognitionManagerDelegate::GetTrayIconController() { |
| 399 if (!tray_icon_controller_.get()) | 433 if (!tray_icon_controller_.get()) |
| 400 tray_icon_controller_ = new SpeechRecognitionTrayIconController(); | 434 tray_icon_controller_ = new SpeechRecognitionTrayIconController(); |
| 401 return tray_icon_controller_.get(); | 435 return tray_icon_controller_.get(); |
| 402 } | 436 } |
| 403 | 437 |
| 404 | 438 |
| 405 } // namespace speech | 439 } // namespace speech |
| OLD | NEW |