Elim cr-checkbox
[chromium-blink-merge.git] / extensions / shell / browser / shell_speech_recognition_manager_delegate.cc
blob9fc2f65beda062ca487d249b95b5bd57b3c08015
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.
5 #include "extensions/shell/browser/shell_speech_recognition_manager_delegate.h"
7 #include "base/bind.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/speech_recognition_manager.h"
11 #include "content/public/browser/speech_recognition_session_context.h"
12 #include "content/public/browser/web_contents.h"
13 #include "extensions/browser/view_type_utils.h"
15 using content::BrowserThread;
16 using content::SpeechRecognitionManager;
17 using content::WebContents;
19 namespace extensions {
20 namespace speech {
22 ShellSpeechRecognitionManagerDelegate::ShellSpeechRecognitionManagerDelegate() {
25 ShellSpeechRecognitionManagerDelegate::
26 ~ShellSpeechRecognitionManagerDelegate() {
29 void ShellSpeechRecognitionManagerDelegate::OnRecognitionStart(int session_id) {
32 void ShellSpeechRecognitionManagerDelegate::OnAudioStart(int session_id) {
35 void ShellSpeechRecognitionManagerDelegate::OnEnvironmentEstimationComplete(
36 int session_id) {
39 void ShellSpeechRecognitionManagerDelegate::OnSoundStart(int session_id) {
42 void ShellSpeechRecognitionManagerDelegate::OnSoundEnd(int session_id) {
45 void ShellSpeechRecognitionManagerDelegate::OnAudioEnd(int session_id) {
48 void ShellSpeechRecognitionManagerDelegate::OnRecognitionEnd(int session_id) {
51 void ShellSpeechRecognitionManagerDelegate::OnRecognitionResults(
52 int session_id,
53 const content::SpeechRecognitionResults& result) {
56 void ShellSpeechRecognitionManagerDelegate::OnRecognitionError(
57 int session_id,
58 const content::SpeechRecognitionError& error) {
61 void ShellSpeechRecognitionManagerDelegate::OnAudioLevelsChange(
62 int session_id,
63 float volume,
64 float noise_volume) {
67 void ShellSpeechRecognitionManagerDelegate::GetDiagnosticInformation(
68 bool* can_report_metrics,
69 std::string* hardware_info) {
72 void ShellSpeechRecognitionManagerDelegate::CheckRecognitionIsAllowed(
73 int session_id,
74 base::Callback<void(bool ask_user, bool is_allowed)> callback) {
75 DCHECK_CURRENTLY_ON(BrowserThread::IO);
77 const content::SpeechRecognitionSessionContext& context =
78 SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
80 // Make sure that initiators (extensions/web pages) properly set the
81 // |render_process_id| field, which is needed later to retrieve the profile.
82 DCHECK_NE(context.render_process_id, 0);
84 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
85 base::Bind(&CheckRenderViewType,
86 callback,
87 context.render_process_id,
88 context.render_view_id));
91 content::SpeechRecognitionEventListener*
92 ShellSpeechRecognitionManagerDelegate::GetEventListener() {
93 return this;
96 bool ShellSpeechRecognitionManagerDelegate::FilterProfanities(
97 int render_process_id) {
98 // TODO(zork): Determine where this preference should come from.
99 return true;
102 // static
103 void ShellSpeechRecognitionManagerDelegate::CheckRenderViewType(
104 base::Callback<void(bool ask_user, bool is_allowed)> callback,
105 int render_process_id,
106 int render_view_id) {
107 DCHECK_CURRENTLY_ON(BrowserThread::UI);
108 const content::RenderViewHost* render_view_host =
109 content::RenderViewHost::FromID(render_process_id, render_view_id);
110 bool allowed = false;
111 bool check_permission = false;
113 if (render_view_host) {
114 WebContents* web_contents =
115 WebContents::FromRenderViewHost(render_view_host);
116 extensions::ViewType view_type = extensions::GetViewType(web_contents);
118 if (view_type == extensions::VIEW_TYPE_APP_WINDOW ||
119 view_type == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
120 allowed = true;
121 check_permission = true;
122 } else {
123 LOG(WARNING) << "Speech recognition only supported in Apps.";
127 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
128 base::Bind(callback, check_permission, allowed));
131 } // namespace speech
132 } // namespace extensions