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 // cases, we just notify it to the manager (calling-back synchronously, since | |
| 483 // we remain in the IO thread). | |
| 484 if (RequiresTrayIcon(session_id)) { | 482 if (RequiresTrayIcon(session_id)) { |
| 485 callback.Run(session_id, true /* is_allowed */); | 483 // Check that the render view type is appropriate, and whether or not we |
| 484 // need to request permission from the user. | |
| 485 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 486 base::Bind(&CheckRenderViewTypeForTrayIcon, | |
| 487 session_id, | |
| 488 callback, | |
| 489 context.render_process_id, | |
| 490 context.render_view_id)); | |
| 491 return; | |
| 492 } else { | |
| 493 // Sessions not requiring permission need to check the renderer view type. | |
| 494 // The check must be performed in the UI thread. We defer it posting to | |
| 495 // CheckRenderViewType, which will issue the callback on our behalf. | |
| 496 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 497 base::Bind(&CheckRenderViewTypeForBubble, | |
| 498 session_id, | |
| 499 callback, | |
| 500 context.render_process_id, | |
| 501 context.render_view_id)); | |
| 486 return; | 502 return; |
| 487 } | 503 } |
| 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, | |
| 493 base::Bind(&CheckRenderViewType, | |
| 494 session_id, | |
| 495 callback, | |
| 496 context.render_process_id, | |
| 497 context.render_view_id)); | |
| 498 } | 504 } |
| 499 | 505 |
| 500 content::SpeechRecognitionEventListener* | 506 content::SpeechRecognitionEventListener* |
| 501 ChromeSpeechRecognitionManagerDelegate::GetEventListener() { | 507 ChromeSpeechRecognitionManagerDelegate::GetEventListener() { |
| 502 return this; | 508 return this; |
| 503 } | 509 } |
| 504 | 510 |
| 505 void ChromeSpeechRecognitionManagerDelegate::ShowTrayIconOnUIThread( | 511 void ChromeSpeechRecognitionManagerDelegate::ShowTrayIconOnUIThread( |
| 506 const std::string& context_name, | 512 const std::string& context_name, |
| 507 int render_process_id, | 513 int render_process_id, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 528 context_name.substr(sizeof(kExtensionPrefix) - 1); | 534 context_name.substr(sizeof(kExtensionPrefix) - 1); |
| 529 const extensions::Extension* extension = | 535 const extensions::Extension* extension = |
| 530 profile->GetExtensionService()->GetExtensionById(extension_id, true); | 536 profile->GetExtensionService()->GetExtensionById(extension_id, true); |
| 531 DCHECK(extension); | 537 DCHECK(extension); |
| 532 initiator_name = UTF8ToUTF16(extension->name()); | 538 initiator_name = UTF8ToUTF16(extension->name()); |
| 533 } | 539 } |
| 534 | 540 |
| 535 tray_icon_controller->Show(initiator_name, show_notification); | 541 tray_icon_controller->Show(initiator_name, show_notification); |
| 536 } | 542 } |
| 537 | 543 |
| 538 void ChromeSpeechRecognitionManagerDelegate::CheckRenderViewType( | 544 void ChromeSpeechRecognitionManagerDelegate::CheckRenderViewTypeForBubble( |
| 539 int session_id, | 545 int session_id, |
| 540 base::Callback<void(int session_id, bool is_allowed)> callback, | 546 base::Callback<void(bool ask_user, bool is_allowed)> callback, |
| 541 int render_process_id, | 547 int render_process_id, |
| 542 int render_view_id) { | 548 int render_view_id) { |
| 543 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 549 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 544 const content::RenderViewHost* render_view_host = | 550 const content::RenderViewHost* render_view_host = |
| 545 content::RenderViewHost::FromID(render_process_id, render_view_id); | 551 content::RenderViewHost::FromID(render_process_id, render_view_id); |
| 546 bool allowed = false; | 552 bool allowed = false; |
| 547 if (render_view_host) { | 553 if (render_view_host) { |
| 548 // For host delegates other than VIEW_TYPE_TAB_CONTENTS we can't reliably | 554 // For host delegates other than VIEW_TYPE_TAB_CONTENTS we can't reliably |
| 549 // show a popup, including the speech input bubble. In these cases for | 555 // show a popup, including the speech input bubble. In these cases for |
| 550 // privacy reasons we don't want to start recording if the user can't be | 556 // privacy reasons we don't want to start recording if the user can't be |
| 551 // properly notified. An example of this is trying to show the speech input | 557 // properly notified. An example of this is trying to show the speech input |
| 552 // bubble within an extension popup: http://crbug.com/92083. In these | 558 // bubble within an extension popup: http://crbug.com/92083. In these |
| 553 // situations the speech input extension API should be used instead. | 559 // situations the speech input extension API should be used instead. |
| 554 WebContents* web_contents = | 560 WebContents* web_contents = |
| 555 WebContents::FromRenderViewHost(render_view_host); | 561 WebContents::FromRenderViewHost(render_view_host); |
| 556 chrome::ViewType view_type = chrome::GetViewType(web_contents); | 562 chrome::ViewType view_type = chrome::GetViewType(web_contents); |
| 557 if (view_type == chrome::VIEW_TYPE_TAB_CONTENTS) | 563 if (view_type == chrome::VIEW_TYPE_TAB_CONTENTS) |
| 558 allowed = true; | 564 allowed = true; |
| 559 } | 565 } |
| 560 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 566 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 561 base::Bind(callback, session_id, allowed)); | 567 base::Bind(callback, false, allowed)); |
| 568 } | |
| 569 | |
| 570 void ChromeSpeechRecognitionManagerDelegate::CheckRenderViewTypeForTrayIcon( | |
|
Satish
2012/09/12 06:15:09
there is a lot of shared code between this and the
hans
2012/09/12 09:46:58
Done.
| |
| 571 int session_id, | |
| 572 base::Callback<void(bool ask_user, bool is_allowed)> callback, | |
| 573 int render_process_id, | |
| 574 int render_view_id) { | |
| 575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 576 const content::RenderViewHost* render_view_host = | |
| 577 content::RenderViewHost::FromID(render_process_id, render_view_id); | |
| 578 if (!render_view_host) { | |
| 579 // This happens for extensions. Manifest should be checked for permission. | |
| 580 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 581 base::Bind(callback, false, true)); | |
| 582 return; | |
| 583 } | |
| 584 | |
| 585 WebContents* web_contents = WebContents::FromRenderViewHost(render_view_host); | |
| 586 chrome::ViewType view_type = chrome::GetViewType(web_contents); | |
| 587 if (view_type != chrome::VIEW_TYPE_TAB_CONTENTS) { | |
| 588 // If there is no tab we don't allow, because we wouldn't be able to show an | |
| 589 // infobar. | |
| 590 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 591 base::Bind(callback, false, false)); | |
| 592 return; | |
| 593 } | |
| 594 | |
| 595 // Call back saying that we need to ask the user for permission. | |
| 596 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 597 base::Bind(callback, true, true)); | |
| 562 } | 598 } |
| 563 | 599 |
| 564 SpeechRecognitionBubbleController* | 600 SpeechRecognitionBubbleController* |
| 565 ChromeSpeechRecognitionManagerDelegate::GetBubbleController() { | 601 ChromeSpeechRecognitionManagerDelegate::GetBubbleController() { |
| 566 if (!bubble_controller_.get()) | 602 if (!bubble_controller_.get()) |
| 567 bubble_controller_ = new SpeechRecognitionBubbleController(this); | 603 bubble_controller_ = new SpeechRecognitionBubbleController(this); |
| 568 return bubble_controller_.get(); | 604 return bubble_controller_.get(); |
| 569 } | 605 } |
| 570 | 606 |
| 571 SpeechRecognitionTrayIconController* | 607 SpeechRecognitionTrayIconController* |
| 572 ChromeSpeechRecognitionManagerDelegate::GetTrayIconController() { | 608 ChromeSpeechRecognitionManagerDelegate::GetTrayIconController() { |
| 573 if (!tray_icon_controller_.get()) | 609 if (!tray_icon_controller_.get()) |
| 574 tray_icon_controller_ = new SpeechRecognitionTrayIconController(); | 610 tray_icon_controller_ = new SpeechRecognitionTrayIconController(); |
| 575 return tray_icon_controller_.get(); | 611 return tray_icon_controller_.get(); |
| 576 } | 612 } |
| 577 | 613 |
| 578 | 614 |
| 579 } // namespace speech | 615 } // namespace speech |
| OLD | NEW |