shell_speech_recognition_manager_delegate.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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
  3. // found in the LICENSE file.
  4. #include "extensions/shell/browser/shell_speech_recognition_manager_delegate.h"
  5. #include "base/bind.h"
  6. #include "content/public/browser/browser_task_traits.h"
  7. #include "content/public/browser/browser_thread.h"
  8. #include "content/public/browser/render_frame_host.h"
  9. #include "content/public/browser/speech_recognition_manager.h"
  10. #include "content/public/browser/speech_recognition_session_context.h"
  11. #include "content/public/browser/web_contents.h"
  12. #include "extensions/browser/view_type_utils.h"
  13. #include "extensions/common/mojom/view_type.mojom.h"
  14. using content::BrowserThread;
  15. using content::SpeechRecognitionManager;
  16. using content::WebContents;
  17. namespace extensions {
  18. namespace speech {
  19. ShellSpeechRecognitionManagerDelegate::ShellSpeechRecognitionManagerDelegate() {
  20. }
  21. ShellSpeechRecognitionManagerDelegate::
  22. ~ShellSpeechRecognitionManagerDelegate() {
  23. }
  24. void ShellSpeechRecognitionManagerDelegate::OnRecognitionStart(int session_id) {
  25. }
  26. void ShellSpeechRecognitionManagerDelegate::OnAudioStart(int session_id) {
  27. }
  28. void ShellSpeechRecognitionManagerDelegate::OnEnvironmentEstimationComplete(
  29. int session_id) {
  30. }
  31. void ShellSpeechRecognitionManagerDelegate::OnSoundStart(int session_id) {
  32. }
  33. void ShellSpeechRecognitionManagerDelegate::OnSoundEnd(int session_id) {
  34. }
  35. void ShellSpeechRecognitionManagerDelegate::OnAudioEnd(int session_id) {
  36. }
  37. void ShellSpeechRecognitionManagerDelegate::OnRecognitionEnd(int session_id) {
  38. }
  39. void ShellSpeechRecognitionManagerDelegate::OnRecognitionResults(
  40. int session_id,
  41. const std::vector<blink::mojom::SpeechRecognitionResultPtr>& result) {}
  42. void ShellSpeechRecognitionManagerDelegate::OnRecognitionError(
  43. int session_id,
  44. const blink::mojom::SpeechRecognitionError& error) {}
  45. void ShellSpeechRecognitionManagerDelegate::OnAudioLevelsChange(
  46. int session_id,
  47. float volume,
  48. float noise_volume) {
  49. }
  50. void ShellSpeechRecognitionManagerDelegate::CheckRecognitionIsAllowed(
  51. int session_id,
  52. base::OnceCallback<void(bool ask_user, bool is_allowed)> callback) {
  53. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  54. const content::SpeechRecognitionSessionContext& context =
  55. SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
  56. // Make sure that initiators (extensions/web pages) properly set the
  57. // |render_process_id| field, which is needed later to retrieve the profile.
  58. DCHECK_NE(context.render_process_id, 0);
  59. content::GetUIThreadTaskRunner({})->PostTask(
  60. FROM_HERE,
  61. base::BindOnce(&CheckRenderFrameType, std::move(callback),
  62. context.render_process_id, context.render_frame_id));
  63. }
  64. content::SpeechRecognitionEventListener*
  65. ShellSpeechRecognitionManagerDelegate::GetEventListener() {
  66. return this;
  67. }
  68. bool ShellSpeechRecognitionManagerDelegate::FilterProfanities(
  69. int render_process_id) {
  70. // TODO(zork): Determine where this preference should come from.
  71. return true;
  72. }
  73. // static
  74. void ShellSpeechRecognitionManagerDelegate::CheckRenderFrameType(
  75. base::OnceCallback<void(bool ask_user, bool is_allowed)> callback,
  76. int render_process_id,
  77. int render_frame_id) {
  78. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  79. content::RenderFrameHost* render_frame_host =
  80. content::RenderFrameHost::FromID(render_process_id, render_frame_id);
  81. bool allowed = false;
  82. bool check_permission = false;
  83. if (render_frame_host) {
  84. WebContents* web_contents =
  85. WebContents::FromRenderFrameHost(render_frame_host);
  86. extensions::mojom::ViewType view_type =
  87. extensions::GetViewType(web_contents);
  88. if (view_type == extensions::mojom::ViewType::kAppWindow ||
  89. view_type == extensions::mojom::ViewType::kExtensionBackgroundPage) {
  90. allowed = true;
  91. check_permission = true;
  92. } else {
  93. LOG(WARNING) << "Speech recognition only supported in Apps.";
  94. }
  95. }
  96. content::GetIOThreadTaskRunner({})->PostTask(
  97. FROM_HERE,
  98. base::BindOnce(std::move(callback), check_permission, allowed));
  99. }
  100. } // namespace speech
  101. } // namespace extensions