aw_speech_recognition_manager_delegate.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2018 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 "android_webview/browser/aw_speech_recognition_manager_delegate.h"
  5. #include <string>
  6. #include "base/bind.h"
  7. #include "base/threading/thread_restrictions.h"
  8. #include "build/build_config.h"
  9. #include "components/prefs/pref_service.h"
  10. #include "content/public/browser/browser_task_traits.h"
  11. #include "content/public/browser/browser_thread.h"
  12. #include "content/public/browser/render_frame_host.h"
  13. #include "content/public/browser/render_process_host.h"
  14. #include "content/public/browser/speech_recognition_manager.h"
  15. #include "content/public/browser/speech_recognition_session_context.h"
  16. #include "content/public/browser/web_contents.h"
  17. #include "third_party/blink/public/mojom/speech/speech_recognition_error.mojom.h"
  18. #include "third_party/blink/public/mojom/speech/speech_recognition_result.mojom.h"
  19. using content::BrowserThread;
  20. namespace android_webview {
  21. AwSpeechRecognitionManagerDelegate::AwSpeechRecognitionManagerDelegate() {}
  22. AwSpeechRecognitionManagerDelegate::~AwSpeechRecognitionManagerDelegate() {}
  23. void AwSpeechRecognitionManagerDelegate::OnRecognitionStart(int session_id) {}
  24. void AwSpeechRecognitionManagerDelegate::OnAudioStart(int session_id) {}
  25. void AwSpeechRecognitionManagerDelegate::OnEnvironmentEstimationComplete(
  26. int session_id) {}
  27. void AwSpeechRecognitionManagerDelegate::OnSoundStart(int session_id) {}
  28. void AwSpeechRecognitionManagerDelegate::OnSoundEnd(int session_id) {}
  29. void AwSpeechRecognitionManagerDelegate::OnAudioEnd(int session_id) {}
  30. void AwSpeechRecognitionManagerDelegate::OnRecognitionResults(
  31. int session_id,
  32. const std::vector<blink::mojom::SpeechRecognitionResultPtr>& result) {}
  33. void AwSpeechRecognitionManagerDelegate::OnRecognitionError(
  34. int session_id,
  35. const blink::mojom::SpeechRecognitionError& error) {}
  36. void AwSpeechRecognitionManagerDelegate::OnAudioLevelsChange(
  37. int session_id,
  38. float volume,
  39. float noise_volume) {}
  40. void AwSpeechRecognitionManagerDelegate::OnRecognitionEnd(int session_id) {}
  41. void AwSpeechRecognitionManagerDelegate::CheckRecognitionIsAllowed(
  42. int session_id,
  43. base::OnceCallback<void(bool ask_user, bool is_allowed)> callback) {
  44. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  45. const content::SpeechRecognitionSessionContext& context =
  46. content::SpeechRecognitionManager::GetInstance()->GetSessionContext(
  47. session_id);
  48. // Make sure that initiators (extensions/web pages) properly set the
  49. // |render_process_id| field, which is needed later to retrieve the profile.
  50. DCHECK_NE(context.render_process_id, 0);
  51. int render_process_id = context.render_process_id;
  52. int render_frame_id = context.render_frame_id;
  53. if (context.embedder_render_process_id) {
  54. // If this is a request originated from a guest, we need to re-route the
  55. // permission check through the embedder (app).
  56. render_process_id = context.embedder_render_process_id;
  57. render_frame_id = context.embedder_render_frame_id;
  58. }
  59. // Check that the render frame type is appropriate, and whether or not we
  60. // need to request permission from the user.
  61. content::GetUIThreadTaskRunner({})->PostTask(
  62. FROM_HERE, base::BindOnce(&CheckRenderFrameType, std::move(callback),
  63. render_process_id, render_frame_id));
  64. }
  65. content::SpeechRecognitionEventListener*
  66. AwSpeechRecognitionManagerDelegate::GetEventListener() {
  67. return this;
  68. }
  69. bool AwSpeechRecognitionManagerDelegate::FilterProfanities(
  70. int render_process_id) {
  71. // TODO: to confirm whether this setting is relevant for android,
  72. // https://crbug.com/876801.
  73. return false;
  74. }
  75. // static.
  76. void AwSpeechRecognitionManagerDelegate::CheckRenderFrameType(
  77. base::OnceCallback<void(bool ask_user, bool is_allowed)> callback,
  78. int render_process_id,
  79. int render_frame_id) {
  80. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  81. // Regular tab contents.
  82. content::GetIOThreadTaskRunner({})->PostTask(
  83. FROM_HERE,
  84. base::BindOnce(std::move(callback), true /* check_permission */,
  85. true /* allowed */));
  86. }
  87. } // namespace android_webview