Chromium Code Reviews| 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 <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 const char kExtensionPrefix[] = "chrome-extension://"; | 50 const char kExtensionPrefix[] = "chrome-extension://"; |
| 51 | 51 |
| 52 bool RequiresBubble(int session_id) { | 52 bool RequiresBubble(int session_id) { |
| 53 return SpeechRecognitionManager::GetInstance()-> | 53 return SpeechRecognitionManager::GetInstance()-> |
| 54 GetSessionContext(session_id).requested_by_page_element; | 54 GetSessionContext(session_id).requested_by_page_element; |
| 55 } | 55 } |
| 56 | 56 |
| 57 bool RequiresTrayIcon(int session_id) { | 57 bool RequiresTrayIcon(int session_id) { |
| 58 return !RequiresBubble(session_id); | 58 return !RequiresBubble(session_id); |
| 59 } | 59 } |
| 60 | |
| 60 } // namespace | 61 } // namespace |
| 61 | 62 |
| 62 namespace speech { | 63 namespace speech { |
| 63 | 64 |
| 64 // Asynchronously fetches the PC and audio hardware/driver info if | 65 // Asynchronously fetches the PC and audio hardware/driver info if |
| 65 // the user has opted into UMA. This information is sent with speech input | 66 // the user has opted into UMA. This information is sent with speech input |
| 66 // requests to the server for identifying and improving quality issues with | 67 // requests to the server for identifying and improving quality issues with |
| 67 // specific device configurations. | 68 // specific device configurations. |
| 68 class ChromeSpeechRecognitionManagerDelegate::OptionalRequestInfo | 69 class ChromeSpeechRecognitionManagerDelegate::OptionalRequestInfo |
| 69 : public base::RefCountedThreadSafe<OptionalRequestInfo> { | 70 : public base::RefCountedThreadSafe<OptionalRequestInfo> { |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 460 // a long wait and disk seeks when they click on a UI element and start | 461 // a long wait and disk seeks when they click on a UI element and start |
| 461 // speaking. | 462 // speaking. |
| 462 optional_request_info_->Refresh(); | 463 optional_request_info_->Refresh(); |
| 463 } | 464 } |
| 464 *can_report_metrics = optional_request_info_->can_report_metrics(); | 465 *can_report_metrics = optional_request_info_->can_report_metrics(); |
| 465 *hardware_info = optional_request_info_->value(); | 466 *hardware_info = optional_request_info_->value(); |
| 466 } | 467 } |
| 467 | 468 |
| 468 void ChromeSpeechRecognitionManagerDelegate::CheckRecognitionIsAllowed( | 469 void ChromeSpeechRecognitionManagerDelegate::CheckRecognitionIsAllowed( |
| 469 int session_id, | 470 int session_id, |
| 470 base::Callback<void(int session_id, bool is_allowed)> callback) { | 471 base::Callback<void(bool ask_user, bool is_allowed)> callback) { |
| 471 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 472 | 473 |
| 473 const content::SpeechRecognitionSessionContext& context = | 474 const content::SpeechRecognitionSessionContext& context = |
| 474 SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id); | 475 SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id); |
| 475 | 476 |
| 476 // Make sure that initiators (extensions/web pages) properly set the | 477 // Make sure that initiators (extensions/web pages) properly set the |
| 477 // |render_process_id| field, which is needed later to retrieve the | 478 // |render_process_id| field, which is needed later to retrieve the |
| 478 // ChromeSpeechRecognitionPreferences associated to their profile. | 479 // ChromeSpeechRecognitionPreferences associated to their profile. |
| 479 DCHECK_NE(context.render_process_id, 0); | 480 DCHECK_NE(context.render_process_id, 0); |
| 480 | 481 |
| 481 // We don't need any particular check for sessions not using a bubble. In such | 482 // Check that the render view type is appropriate, and whether or not we |
| 482 // cases, we just notify it to the manager (calling-back synchronously, since | 483 // need to request permission from the user. |
| 483 // we remain in the IO thread). | |
| 484 if (RequiresTrayIcon(session_id)) { | |
| 485 callback.Run(session_id, true /* is_allowed */); | |
| 486 return; | |
| 487 } | |
| 488 | |
| 489 // Sessions using bubbles, conversely, need a check on the renderer view type. | |
| 490 // The check must be performed in the UI thread. We defer it posting to | |
| 491 // CheckRenderViewType, which will issue the callback on our behalf. | |
| 492 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 484 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 493 base::Bind(&CheckRenderViewType, | 485 base::Bind(&CheckRenderViewType, |
| 494 session_id, | 486 session_id, |
|
no longer working on chromium
2012/09/12 11:29:49
It looks to me that we don't use the session_id in
hans
2012/09/12 12:32:21
Done.
| |
| 495 callback, | 487 callback, |
| 496 context.render_process_id, | 488 context.render_process_id, |
| 497 context.render_view_id)); | 489 context.render_view_id, |
| 490 RequiresTrayIcon(session_id))); | |
| 498 } | 491 } |
| 499 | 492 |
| 500 content::SpeechRecognitionEventListener* | 493 content::SpeechRecognitionEventListener* |
| 501 ChromeSpeechRecognitionManagerDelegate::GetEventListener() { | 494 ChromeSpeechRecognitionManagerDelegate::GetEventListener() { |
| 502 return this; | 495 return this; |
| 503 } | 496 } |
| 504 | 497 |
| 505 void ChromeSpeechRecognitionManagerDelegate::ShowTrayIconOnUIThread( | 498 void ChromeSpeechRecognitionManagerDelegate::ShowTrayIconOnUIThread( |
| 506 const std::string& context_name, | 499 const std::string& context_name, |
| 507 int render_process_id, | 500 int render_process_id, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 530 profile->GetExtensionService()->GetExtensionById(extension_id, true); | 523 profile->GetExtensionService()->GetExtensionById(extension_id, true); |
| 531 DCHECK(extension); | 524 DCHECK(extension); |
| 532 initiator_name = UTF8ToUTF16(extension->name()); | 525 initiator_name = UTF8ToUTF16(extension->name()); |
| 533 } | 526 } |
| 534 | 527 |
| 535 tray_icon_controller->Show(initiator_name, show_notification); | 528 tray_icon_controller->Show(initiator_name, show_notification); |
| 536 } | 529 } |
| 537 | 530 |
| 538 void ChromeSpeechRecognitionManagerDelegate::CheckRenderViewType( | 531 void ChromeSpeechRecognitionManagerDelegate::CheckRenderViewType( |
| 539 int session_id, | 532 int session_id, |
| 540 base::Callback<void(int session_id, bool is_allowed)> callback, | 533 base::Callback<void(bool ask_user, bool is_allowed)> callback, |
| 541 int render_process_id, | 534 int render_process_id, |
| 542 int render_view_id) { | 535 int render_view_id, |
| 536 bool js_api) { | |
| 543 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 537 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 544 const content::RenderViewHost* render_view_host = | 538 const content::RenderViewHost* render_view_host = |
| 545 content::RenderViewHost::FromID(render_process_id, render_view_id); | 539 content::RenderViewHost::FromID(render_process_id, render_view_id); |
| 540 | |
| 546 bool allowed = false; | 541 bool allowed = false; |
| 547 if (render_view_host) { | 542 bool ask_permission = false; |
| 548 // For host delegates other than VIEW_TYPE_TAB_CONTENTS we can't reliably | 543 |
| 549 // show a popup, including the speech input bubble. In these cases for | 544 if (!render_view_host) { |
| 550 // privacy reasons we don't want to start recording if the user can't be | 545 if (!js_api) { |
| 551 // properly notified. An example of this is trying to show the speech input | 546 // If there is no render view, we cannot show the speech bubble, so this |
| 552 // bubble within an extension popup: http://crbug.com/92083. In these | 547 // is not allowed. |
| 553 // situations the speech input extension API should be used instead. | 548 allowed = false; |
| 554 WebContents* web_contents = | 549 ask_permission = false; |
| 555 WebContents::FromRenderViewHost(render_view_host); | 550 } else { |
| 556 chrome::ViewType view_type = chrome::GetViewType(web_contents); | 551 // This happens for extensions. Manifest should be checked for permission. |
| 557 if (view_type == chrome::VIEW_TYPE_TAB_CONTENTS) | |
| 558 allowed = true; | 552 allowed = true; |
| 553 ask_permission = false; | |
| 554 } | |
| 555 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 556 base::Bind(callback, ask_permission, allowed)); | |
| 557 return; | |
| 559 } | 558 } |
| 559 | |
| 560 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); | |
| 561 chrome::ViewType view_type = chrome::GetViewType(web_contents); | |
| 562 | |
| 563 if (view_type == chrome::VIEW_TYPE_TAB_CONTENTS) { | |
| 564 // If it is a tab, we can show the speech input bubble or ask for | |
| 565 // permission. | |
| 566 | |
| 567 allowed = true; | |
| 568 if (js_api) | |
| 569 ask_permission = true; | |
| 570 } | |
| 571 | |
| 560 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 572 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 561 base::Bind(callback, session_id, allowed)); | 573 base::Bind(callback, ask_permission, allowed)); |
| 562 } | 574 } |
| 563 | 575 |
| 564 SpeechRecognitionBubbleController* | 576 SpeechRecognitionBubbleController* |
| 565 ChromeSpeechRecognitionManagerDelegate::GetBubbleController() { | 577 ChromeSpeechRecognitionManagerDelegate::GetBubbleController() { |
| 566 if (!bubble_controller_.get()) | 578 if (!bubble_controller_.get()) |
| 567 bubble_controller_ = new SpeechRecognitionBubbleController(this); | 579 bubble_controller_ = new SpeechRecognitionBubbleController(this); |
| 568 return bubble_controller_.get(); | 580 return bubble_controller_.get(); |
| 569 } | 581 } |
| 570 | 582 |
| 571 SpeechRecognitionTrayIconController* | 583 SpeechRecognitionTrayIconController* |
| 572 ChromeSpeechRecognitionManagerDelegate::GetTrayIconController() { | 584 ChromeSpeechRecognitionManagerDelegate::GetTrayIconController() { |
| 573 if (!tray_icon_controller_.get()) | 585 if (!tray_icon_controller_.get()) |
| 574 tray_icon_controller_ = new SpeechRecognitionTrayIconController(); | 586 tray_icon_controller_ = new SpeechRecognitionTrayIconController(); |
| 575 return tray_icon_controller_.get(); | 587 return tray_icon_controller_.get(); |
| 576 } | 588 } |
| 577 | 589 |
| 578 | 590 |
| 579 } // namespace speech | 591 } // namespace speech |
| OLD | NEW |