Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: content/browser/speech/speech_recognition_dispatcher_host.cc

Issue 170243005: Making sure that SpeechRecognitionDispatcherHost is not used after free (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed comments Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 }
40
41 base::WeakPtr<SpeechRecognitionDispatcherHost>
42 SpeechRecognitionDispatcherHost::AsWeakPtr() {
43 return weak_factory_.GetWeakPtr();
37 } 44 }
38 45
39 bool SpeechRecognitionDispatcherHost::OnMessageReceived( 46 bool SpeechRecognitionDispatcherHost::OnMessageReceived(
40 const IPC::Message& message, bool* message_was_ok) { 47 const IPC::Message& message, bool* message_was_ok) {
41 bool handled = true; 48 bool handled = true;
42 IPC_BEGIN_MESSAGE_MAP_EX(SpeechRecognitionDispatcherHost, message, 49 IPC_BEGIN_MESSAGE_MAP_EX(SpeechRecognitionDispatcherHost, message,
43 *message_was_ok) 50 *message_was_ok)
44 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest, 51 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StartRequest,
45 OnStartRequest) 52 OnStartRequest)
46 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest, 53 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_AbortRequest,
47 OnAbortRequest) 54 OnAbortRequest)
48 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest, 55 IPC_MESSAGE_HANDLER(SpeechRecognitionHostMsg_StopCaptureRequest,
49 OnStopCaptureRequest) 56 OnStopCaptureRequest)
50 IPC_MESSAGE_UNHANDLED(handled = false) 57 IPC_MESSAGE_UNHANDLED(handled = false)
51 IPC_END_MESSAGE_MAP() 58 IPC_END_MESSAGE_MAP()
52 return handled; 59 return handled;
53 } 60 }
54 61
55 void SpeechRecognitionDispatcherHost::OverrideThreadForMessage( 62 void SpeechRecognitionDispatcherHost::OverrideThreadForMessage(
56 const IPC::Message& message, 63 const IPC::Message& message,
57 BrowserThread::ID* thread) { 64 BrowserThread::ID* thread) {
58 if (message.type() == SpeechRecognitionHostMsg_StartRequest::ID) 65 if (message.type() == SpeechRecognitionHostMsg_StartRequest::ID)
59 *thread = BrowserThread::UI; 66 *thread = BrowserThread::UI;
60 } 67 }
61 68
69 void SpeechRecognitionDispatcherHost::OnChannelClosing() {
70 weak_factory_.InvalidateWeakPtrs();
71 }
72
62 void SpeechRecognitionDispatcherHost::OnStartRequest( 73 void SpeechRecognitionDispatcherHost::OnStartRequest(
63 const SpeechRecognitionHostMsg_StartRequest_Params& params) { 74 const SpeechRecognitionHostMsg_StartRequest_Params& params) {
64 SpeechRecognitionHostMsg_StartRequest_Params input_params(params); 75 SpeechRecognitionHostMsg_StartRequest_Params input_params(params);
65 76
66 int embedder_render_process_id = 0; 77 int embedder_render_process_id = 0;
67 int embedder_render_view_id = MSG_ROUTING_NONE; 78 int embedder_render_view_id = MSG_ROUTING_NONE;
68 if (is_guest_) { 79 if (is_guest_) {
69 // If the speech API request was from a guest, save the context of the 80 // If the speech API request was from a guest, save the context of the
70 // embedder since we will use it to decide permission. 81 // embedder since we will use it to decide permission.
71 RenderViewHostImpl* render_view_host = 82 RenderViewHostImpl* render_view_host =
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 config.is_legacy_api = false; 133 config.is_legacy_api = false;
123 config.language = params.language; 134 config.language = params.language;
124 config.grammars = params.grammars; 135 config.grammars = params.grammars;
125 config.max_hypotheses = params.max_hypotheses; 136 config.max_hypotheses = params.max_hypotheses;
126 config.origin_url = params.origin_url; 137 config.origin_url = params.origin_url;
127 config.initial_context = context; 138 config.initial_context = context;
128 config.url_request_context_getter = context_getter_.get(); 139 config.url_request_context_getter = context_getter_.get();
129 config.filter_profanities = filter_profanities; 140 config.filter_profanities = filter_profanities;
130 config.continuous = params.continuous; 141 config.continuous = params.continuous;
131 config.interim_results = params.interim_results; 142 config.interim_results = params.interim_results;
132 config.event_listener = this; 143 config.event_listener = AsWeakPtr();
133 144
134 int session_id = SpeechRecognitionManager::GetInstance()->CreateSession( 145 int session_id = SpeechRecognitionManager::GetInstance()->CreateSession(
135 config); 146 config);
136 DCHECK_NE(session_id, SpeechRecognitionManager::kSessionIDInvalid); 147 DCHECK_NE(session_id, SpeechRecognitionManager::kSessionIDInvalid);
137 SpeechRecognitionManager::GetInstance()->StartSession(session_id); 148 SpeechRecognitionManager::GetInstance()->StartSession(session_id);
138 } 149 }
139 150
140 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id, 151 void SpeechRecognitionDispatcherHost::OnAbortRequest(int render_view_id,
141 int request_id) { 152 int request_id) {
142 int session_id = SpeechRecognitionManager::GetInstance()->GetSession( 153 int session_id = SpeechRecognitionManager::GetInstance()->GetSession(
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange(int session_id, 240 void SpeechRecognitionDispatcherHost::OnAudioLevelsChange(int session_id,
230 float volume, 241 float volume,
231 float noise_volume) { 242 float noise_volume) {
232 } 243 }
233 244
234 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete( 245 void SpeechRecognitionDispatcherHost::OnEnvironmentEstimationComplete(
235 int session_id) { 246 int session_id) {
236 } 247 }
237 248
238 } // namespace content 249 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/speech/speech_recognition_dispatcher_host.h ('k') | content/browser/speech/speech_recognition_manager_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698