1942a50c7150e8f22058e249e2b8233e32dcef25
[platform/framework/web/crosswalk.git] / src / content / browser / speech / speech_recognition_dispatcher_host.cc
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
3 // found in the LICENSE file.
4
5 #include "content/browser/speech/speech_recognition_dispatcher_host.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/lazy_instance.h"
10 #include "content/browser/browser_plugin/browser_plugin_guest.h"
11 #include "content/browser/child_process_security_policy_impl.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/speech/speech_recognition_manager_impl.h"
14 #include "content/browser/web_contents/web_contents_impl.h"
15 #include "content/common/speech_recognition_messages.h"
16 #include "content/public/browser/speech_recognition_manager_delegate.h"
17 #include "content/public/browser/speech_recognition_session_config.h"
18 #include "content/public/browser/speech_recognition_session_context.h"
19 #include "content/public/common/content_switches.h"
20
21 namespace content {
22
23 SpeechRecognitionDispatcherHost::SpeechRecognitionDispatcherHost(
24     int render_process_id,
25     net::URLRequestContextGetter* context_getter)
26     : BrowserMessageFilter(SpeechRecognitionMsgStart),
27       render_process_id_(render_process_id),
28       context_getter_(context_getter),
29       weak_factory_(this) {
30   // Do not add any non-trivial initialization here, instead do it lazily when
31   // required (e.g. see the method |SpeechRecognitionManager::GetInstance()|) or
32   // add an Init() method.
33 }
34
35 SpeechRecognitionDispatcherHost::~SpeechRecognitionDispatcherHost() {
36   SpeechRecognitionManager::GetInstance()->AbortAllSessionsForRenderProcess(
37       render_process_id_);
38 }
39
40 base::WeakPtr<SpeechRecognitionDispatcherHost>
41 SpeechRecognitionDispatcherHost::AsWeakPtr() {
42   return weak_factory_.GetWeakPtr();
43 }
44
45 bool SpeechRecognitionDispatcherHost::OnMessageReceived(
46     const IPC::Message& message) {
47   bool handled = true;
48   IPC_BEGIN_MESSAGE_MAP(SpeechRecognitionDispatcherHost, message)
49     IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest,
50                         OnStartRequest)
51     IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest,
52                         OnAbortRequest)
53     IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest,
54                         OnStopCaptureRequest)
55     IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortAllRequests,
56                         OnAbortAllRequests)
57     IPC_MESSAGE_UNHANDLED(handled = false)
58   IPC_END_MESSAGE_MAP()
59   return handled;
60 }
61
62 void SpeechRecognitionDispatcherHost::OverrideThreadForMessage(
63     const IPC::Message& message,
64     BrowserThread::ID* thread) {
65   if (message.type() == SpeechRecognitionHostMsg_StartRequest::ID)
66     *thread = BrowserThread::UI;
67 }
68
69 void SpeechRecognitionDispatcherHost::OnChannelClosing() {
70   weak_factory_.InvalidateWeakPtrs();
71 }
72
73 void SpeechRecognitionDispatcherHost::OnStartRequest(
74     const SpeechRecognitionHostMsg_StartRequest_Params& params) {
75   SpeechRecognitionHostMsg_StartRequest_Params input_params(params);
76
77   // Check that the origin specified by the renderer process is one
78   // that it is allowed to access.
79   if (params.origin_url != "null" &&
80       !ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
81           render_process_id_, GURL(params.origin_url))) {
82     LOG(ERROR) << "SRDH::OnStartRequest, disallowed origin: "
83                << params.origin_url;
84     return;
85   }
86
87   int embedder_render_process_id = 0;
88   int embedder_render_view_id = MSG_ROUTING_NONE;
89   RenderViewHostImpl* render_view_host =
90       RenderViewHostImpl::FromID(render_process_id_, params.render_view_id);
91   WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
92       WebContents::FromRenderViewHost(render_view_host));
93   BrowserPluginGuest* guest = web_contents->GetBrowserPluginGuest();
94   if (guest) {
95     // If the speech API request was from a guest, save the context of the
96     // embedder since we will use it to decide permission.
97     embedder_render_process_id =
98         guest->embedder_web_contents()->GetRenderProcessHost()->GetID();
99     DCHECK_NE(embedder_render_process_id, 0);
100     embedder_render_view_id =
101         guest->embedder_web_contents()->GetRenderViewHost()->GetRoutingID();
102     DCHECK_NE(embedder_render_view_id, MSG_ROUTING_NONE);
103   }
104
105   // TODO(lazyboy): Check if filter_profanities should use |render_process_id|
106   // instead of |render_process_id_|.
107   bool filter_profanities =
108       SpeechRecognitionManagerImpl::GetInstance() &&
109       SpeechRecognitionManagerImpl::GetInstance()->delegate() &&
110       SpeechRecognitionManagerImpl::GetInstance()->delegate()->
111           FilterProfanities(render_process_id_);
112
113   // TODO(miu): This is a hack to allow SpeechRecognition to operate with the
114   // MediaStreamManager, which partitions requests per RenderFrame, not per
115   // RenderView.  http://crbug.com/390749
116   const int params_render_frame_id = render_view_host ?
117       render_view_host->GetMainFrame()->GetRoutingID() : MSG_ROUTING_NONE;
118
119   BrowserThread::PostTask(
120       BrowserThread::IO,
121       FROM_HERE,
122       base::Bind(&SpeechRecognitionDispatcherHost::OnStartRequestOnIO,
123                  this,
124                  embedder_render_process_id,
125                  embedder_render_view_id,
126                  input_params,
127                  params_render_frame_id,
128                  filter_profanities));
129 }
130
131 void SpeechRecognitionDispatcherHost::OnStartRequestOnIO(
132     int embedder_render_process_id,
133     int embedder_render_view_id,
134     const SpeechRecognitionHostMsg_StartRequest_Params& params,
135     int params_render_frame_id,
136     bool filter_profanities) {
137   SpeechRecognitionSessionContext context;
138   context.context_name = params.origin_url;
139   context.render_process_id = render_process_id_;
140   context.render_view_id = params.render_view_id;
141   context.render_frame_id = params_render_frame_id;
142   context.embedder_render_process_id = embedder_render_process_id;
143   context.embedder_render_view_id = embedder_render_view_id;
144   if (embedder_render_process_id)
145     context.guest_render_view_id = params.render_view_id;
146   context.request_id = params.request_id;
147
148   SpeechRecognitionSessionConfig config;
149   config.is_legacy_api = false;
150   config.language = params.language;
151   config.grammars = params.grammars;
152   config.max_hypotheses = params.max_hypotheses;
153   config.origin_url = params.origin_url;
154   config.initial_context = context;
155   config.url_request_context_getter = context_getter_.get();
156   config.filter_profanities = filter_profanities;
157   config.continuous = params.continuous;
158   config.interim_results = params.interim_results;
159   config.event_listener = AsWeakPtr();
160
161   int session_id = SpeechRecognitionManager::GetInstance()->CreateSession(
162       config);
163   DCHECK_NE(session_id, SpeechRecognitionManager::kSessionIDInvalid);
164   SpeechRecognitionManager::GetInstance()->StartSession(session_id);
165 }
166
167 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id,
168                                                      int request_id) {
169   int session_id = SpeechRecognitionManager::GetInstance()->GetSession(
170       render_process_id_, render_view_id, request_id);
171
172   // The renderer might provide an invalid |request_id| if the session was not
173   // started as expected, e.g., due to unsatisfied security requirements.
174   if (session_id != SpeechRecognitionManager::kSessionIDInvalid)
175     SpeechRecognitionManager::GetInstance()->AbortSession(session_id);
176 }
177
178 void SpeechRecognitionDispatcherHost::OnAbortAllRequests(int render_view_id) {
179   SpeechRecognitionManager::GetInstance()->AbortAllSessionsForRenderView(
180       render_process_id_, render_view_id);
181 }
182
183 void SpeechRecognitionDispatcherHost::OnStopCaptureRequest(
184     int render_view_id, int request_id) {
185   int session_id = SpeechRecognitionManager::GetInstance()->GetSession(
186       render_process_id_, render_view_id, request_id);
187
188   // The renderer might provide an invalid |request_id| if the session was not
189   // started as expected, e.g., due to unsatisfied security requirements.
190   if (session_id != SpeechRecognitionManager::kSessionIDInvalid) {
191     SpeechRecognitionManager::GetInstance()->StopAudioCaptureForSession(
192         session_id);
193   }
194 }
195
196 // -------- SpeechRecognitionEventListener interface implementation -----------
197
198 void SpeechRecognitionDispatcherHost::OnRecognitionStart(int session_id) {
199   const SpeechRecognitionSessionContext& context =
200       SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
201   Send(new SpeechRecognitionMsg_Started(context.render_view_id,
202                                         context.request_id));
203 }
204
205 void SpeechRecognitionDispatcherHost::OnAudioStart(int session_id) {
206   const SpeechRecognitionSessionContext& context =
207       SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
208   Send(new SpeechRecognitionMsg_AudioStarted(context.render_view_id,
209                                              context.request_id));
210 }
211
212 void SpeechRecognitionDispatcherHost::OnSoundStart(int session_id) {
213   const SpeechRecognitionSessionContext& context =
214       SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
215   Send(new SpeechRecognitionMsg_SoundStarted(context.render_view_id,
216                                              context.request_id));
217 }
218
219 void SpeechRecognitionDispatcherHost::OnSoundEnd(int session_id) {
220   const SpeechRecognitionSessionContext& context =
221       SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
222   Send(new SpeechRecognitionMsg_SoundEnded(context.render_view_id,
223                                            context.request_id));
224 }
225
226 void SpeechRecognitionDispatcherHost::OnAudioEnd(int session_id) {
227   const SpeechRecognitionSessionContext& context =
228       SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
229   Send(new SpeechRecognitionMsg_AudioEnded(context.render_view_id,
230                                            context.request_id));
231 }
232
233 void SpeechRecognitionDispatcherHost::OnRecognitionEnd(int session_id) {
234   const SpeechRecognitionSessionContext& context =
235       SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
236   Send(new SpeechRecognitionMsg_Ended(context.render_view_id,
237                                       context.request_id));
238 }
239
240 void SpeechRecognitionDispatcherHost::OnRecognitionResults(
241     int session_id,
242     const SpeechRecognitionResults& results) {
243   const SpeechRecognitionSessionContext& context =
244       SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
245   Send(new SpeechRecognitionMsg_ResultRetrieved(context.render_view_id,
246                                                 context.request_id,
247                                                 results));
248 }
249
250 void SpeechRecognitionDispatcherHost::OnRecognitionError(
251     int session_id,
252     const SpeechRecognitionError& error) {
253   const SpeechRecognitionSessionContext& context =
254       SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
255   Send(new SpeechRecognitionMsg_ErrorOccurred(context.render_view_id,
256                                               context.request_id,
257                                               error));
258 }
259
260 // The events below are currently not used by speech JS APIs implementation.
261 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange(int session_id,
262                                                           float volume,
263                                                           float noise_volume) {
264 }
265
266 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete(
267     int session_id) {
268 }
269
270 }  // namespace content