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 "content/browser/speech/speech_recognition_dispatcher_host.h" | 5 #include "content/browser/speech/speech_recognition_dispatcher_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "content/browser/browser_plugin/browser_plugin_guest.h" | 10 #include "content/browser/browser_plugin/browser_plugin_guest.h" |
11 #include "content/browser/renderer_host/render_view_host_impl.h" | 11 #include "content/browser/renderer_host/render_view_host_impl.h" |
12 #include "content/browser/speech/speech_recognition_manager_impl.h" | 12 #include "content/browser/speech/speech_recognition_manager_impl.h" |
13 #include "content/browser/web_contents/web_contents_impl.h" | 13 #include "content/browser/web_contents/web_contents_impl.h" |
14 #include "content/common/speech_recognition_messages.h" | 14 #include "content/common/speech_recognition_messages.h" |
15 #include "content/public/browser/speech_recognition_manager_delegate.h" | 15 #include "content/public/browser/speech_recognition_manager_delegate.h" |
16 #include "content/public/browser/speech_recognition_session_config.h" | 16 #include "content/public/browser/speech_recognition_session_config.h" |
17 #include "content/public/browser/speech_recognition_session_context.h" | 17 #include "content/public/browser/speech_recognition_session_context.h" |
18 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 SpeechRecognitionDispatcherHost::SpeechRecognitionDispatcherHost( | 22 SpeechRecognitionDispatcherHost::SpeechRecognitionDispatcherHost( |
23 bool is_guest, | 23 bool is_guest, |
24 int render_process_id, | 24 int render_process_id, |
25 net::URLRequestContextGetter* context_getter) | 25 net::URLRequestContextGetter* context_getter) |
26 : BrowserMessageFilter(SpeechRecognitionMsgStart), | 26 : BrowserMessageFilter(SpeechRecognitionMsgStart), |
27 is_guest_(is_guest), | 27 is_guest_(is_guest), |
28 render_process_id_(render_process_id), | 28 render_process_id_(render_process_id), |
29 context_getter_(context_getter) { | 29 context_getter_(context_getter), |
| 30 weak_factory_(this) { |
30 // Do not add any non-trivial initialization here, instead do it lazily when | 31 // Do not add any non-trivial initialization here, instead do it lazily when |
31 // required (e.g. see the method |SpeechRecognitionManager::GetInstance()|) or | 32 // required (e.g. see the method |SpeechRecognitionManager::GetInstance()|) or |
32 // add an Init() method. | 33 // add an Init() method. |
33 } | 34 } |
34 | 35 |
35 SpeechRecognitionDispatcherHost::~SpeechRecognitionDispatcherHost() { | 36 SpeechRecognitionDispatcherHost::~SpeechRecognitionDispatcherHost() { |
36 SpeechRecognitionManager::GetInstance()->AbortAllSessionsForListener(this); | 37 SpeechRecognitionManager::GetInstance()->AbortAllSessionsForRenderProcess( |
| 38 render_process_id_); |
| 39 weak_factory_.InvalidateWeakPtrs(); |
| 40 } |
| 41 |
| 42 base::WeakPtr<SpeechRecognitionDispatcherHost> |
| 43 SpeechRecognitionDispatcherHost::AsWeakPtr() { |
| 44 return weak_factory_.GetWeakPtr(); |
37 } | 45 } |
38 | 46 |
39 bool SpeechRecognitionDispatcherHost::OnMessageReceived( | 47 bool SpeechRecognitionDispatcherHost::OnMessageReceived( |
40 const IPC::Message& message, bool* message_was_ok) { | 48 const IPC::Message& message, bool* message_was_ok) { |
41 bool handled = true; | 49 bool handled = true; |
42 IPC_BEGIN_MESSAGE_MAP_EX(SpeechRecognitionDispatcherHost, message, | 50 IPC_BEGIN_MESSAGE_MAP_EX(SpeechRecognitionDispatcherHost, message, |
43 *message_was_ok) | 51 *message_was_ok) |
44 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest, | 52 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest, |
45 OnStartRequest) | 53 OnStartRequest) |
46 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest, | 54 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest, |
47 OnAbortRequest) | 55 OnAbortRequest) |
48 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest, | 56 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest, |
49 OnStopCaptureRequest) | 57 OnStopCaptureRequest) |
50 IPC_MESSAGE_UNHANDLED(handled = false) | 58 IPC_MESSAGE_UNHANDLED(handled = false) |
51 IPC_END_MESSAGE_MAP() | 59 IPC_END_MESSAGE_MAP() |
52 return handled; | 60 return handled; |
53 } | 61 } |
54 | 62 |
55 void SpeechRecognitionDispatcherHost::OverrideThreadForMessage( | 63 void SpeechRecognitionDispatcherHost::OverrideThreadForMessage( |
56 const IPC::Message& message, | 64 const IPC::Message& message, |
57 BrowserThread::ID* thread) { | 65 BrowserThread::ID* thread) { |
58 if (message.type() == SpeechRecognitionHostMsg_StartRequest::ID) | 66 if (message.type() == SpeechRecognitionHostMsg_StartRequest::ID) |
59 *thread = BrowserThread::UI; | 67 *thread = BrowserThread::UI; |
60 } | 68 } |
61 | 69 |
| 70 void SpeechRecognitionDispatcherHost::OnChannelClosing() { |
| 71 weak_factory_.InvalidateWeakPtrs(); |
| 72 } |
| 73 |
62 void SpeechRecognitionDispatcherHost::OnStartRequest( | 74 void SpeechRecognitionDispatcherHost::OnStartRequest( |
63 const SpeechRecognitionHostMsg_StartRequest_Params& params) { | 75 const SpeechRecognitionHostMsg_StartRequest_Params& params) { |
64 SpeechRecognitionHostMsg_StartRequest_Params input_params(params); | 76 SpeechRecognitionHostMsg_StartRequest_Params input_params(params); |
65 | 77 |
66 int embedder_render_process_id = 0; | 78 int embedder_render_process_id = 0; |
67 int embedder_render_view_id = MSG_ROUTING_NONE; | 79 int embedder_render_view_id = MSG_ROUTING_NONE; |
68 if (is_guest_) { | 80 if (is_guest_) { |
69 // If the speech API request was from a guest, save the context of the | 81 // If the speech API request was from a guest, save the context of the |
70 // embedder since we will use it to decide permission. | 82 // embedder since we will use it to decide permission. |
71 RenderViewHostImpl* render_view_host = | 83 RenderViewHostImpl* render_view_host = |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 config.is_legacy_api = false; | 134 config.is_legacy_api = false; |
123 config.language = params.language; | 135 config.language = params.language; |
124 config.grammars = params.grammars; | 136 config.grammars = params.grammars; |
125 config.max_hypotheses = params.max_hypotheses; | 137 config.max_hypotheses = params.max_hypotheses; |
126 config.origin_url = params.origin_url; | 138 config.origin_url = params.origin_url; |
127 config.initial_context = context; | 139 config.initial_context = context; |
128 config.url_request_context_getter = context_getter_.get(); | 140 config.url_request_context_getter = context_getter_.get(); |
129 config.filter_profanities = filter_profanities; | 141 config.filter_profanities = filter_profanities; |
130 config.continuous = params.continuous; | 142 config.continuous = params.continuous; |
131 config.interim_results = params.interim_results; | 143 config.interim_results = params.interim_results; |
132 config.event_listener = this; | 144 config.event_listener = this->AsWeakPtr(); |
133 | 145 |
134 int session_id = SpeechRecognitionManager::GetInstance()->CreateSession( | 146 int session_id = SpeechRecognitionManager::GetInstance()->CreateSession( |
135 config); | 147 config); |
136 DCHECK_NE(session_id, SpeechRecognitionManager::kSessionIDInvalid); | 148 DCHECK_NE(session_id, SpeechRecognitionManager::kSessionIDInvalid); |
137 SpeechRecognitionManager::GetInstance()->StartSession(session_id); | 149 SpeechRecognitionManager::GetInstance()->StartSession(session_id); |
138 } | 150 } |
139 | 151 |
140 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id, | 152 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id, |
141 int request_id) { | 153 int request_id) { |
142 int session_id = SpeechRecognitionManager::GetInstance()->GetSession( | 154 int session_id = SpeechRecognitionManager::GetInstance()->GetSession( |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange(int session_id, | 241 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange(int session_id, |
230 float volume, | 242 float volume, |
231 float noise_volume) { | 243 float noise_volume) { |
232 } | 244 } |
233 | 245 |
234 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete( | 246 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete( |
235 int session_id) { | 247 int session_id) { |
236 } | 248 } |
237 | 249 |
238 } // namespace content | 250 } // namespace content |
OLD | NEW |