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

Side by Side Diff: content/browser/presentation/presentation_service_impl.cc

Issue 996173006: [Presentation API] Fix potential callback leaks in PSImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/presentation/presentation_service_impl.h" 5 #include "content/browser/presentation/presentation_service_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "content/browser/presentation/presentation_type_converters.h" 10 #include "content/browser/presentation/presentation_type_converters.h"
11 #include "content/public/browser/content_browser_client.h" 11 #include "content/public/browser/content_browser_client.h"
12 #include "content/public/browser/navigation_details.h" 12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/render_frame_host.h" 13 #include "content/public/browser/render_frame_host.h"
14 #include "content/public/browser/render_process_host.h" 14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/web_contents.h" 15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/content_client.h" 16 #include "content/public/common/content_client.h"
17 #include "content/public/common/frame_navigate_params.h" 17 #include "content/public/common/frame_navigate_params.h"
18 18
19 namespace content { 19 namespace content {
20 20
21 PresentationServiceImpl::PresentationServiceImpl( 21 PresentationServiceImpl::PresentationServiceImpl(
22 RenderFrameHost* render_frame_host, 22 RenderFrameHost* render_frame_host,
23 WebContents* web_contents, 23 WebContents* web_contents,
24 PresentationServiceDelegate* delegate) 24 PresentationServiceDelegate* delegate)
25 : WebContentsObserver(web_contents), 25 : WebContentsObserver(web_contents),
26 render_frame_host_(render_frame_host), 26 render_frame_host_(render_frame_host),
27 delegate_(delegate), 27 delegate_(delegate),
28 next_request_session_id_(0),
28 weak_factory_(this) { 29 weak_factory_(this) {
29 DCHECK(render_frame_host_); 30 DCHECK(render_frame_host_);
30 DCHECK(web_contents); 31 DCHECK(web_contents);
31 DVLOG(2) << "PresentationServiceImpl: " 32 DVLOG(2) << "PresentationServiceImpl: "
32 << render_frame_host_->GetProcess()->GetID() << ", " 33 << render_frame_host_->GetProcess()->GetID() << ", "
33 << render_frame_host_->GetRoutingID(); 34 << render_frame_host_->GetRoutingID();
34 if (delegate_) 35 if (delegate_)
35 delegate_->AddObserver(this); 36 delegate_->AddObserver(this);
36 } 37 }
37 38
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 const DefaultSessionMojoCallback& callback) { 127 const DefaultSessionMojoCallback& callback) {
127 NOTIMPLEMENTED(); 128 NOTIMPLEMENTED();
128 } 129 }
129 130
130 void PresentationServiceImpl::StartSession( 131 void PresentationServiceImpl::StartSession(
131 const mojo::String& presentation_url, 132 const mojo::String& presentation_url,
132 const mojo::String& presentation_id, 133 const mojo::String& presentation_id,
133 const NewSessionMojoCallback& callback) { 134 const NewSessionMojoCallback& callback) {
134 DVLOG(2) << "StartSession"; 135 DVLOG(2) << "StartSession";
135 if (!delegate_) { 136 if (!delegate_) {
136 callback.Run( 137 InvokeNewSessionMojoCallbackWithError(callback);
137 presentation::PresentationSessionInfoPtr(),
138 presentation::PresentationError::From(
139 PresentationError(PRESENTATION_ERROR_UNKNOWN, "")));
140 return; 138 return;
141 } 139 }
142 140
143 queued_start_session_requests_.push_back(make_linked_ptr( 141 queued_start_session_requests_.push_back(make_linked_ptr(
144 new StartSessionRequest(presentation_url, presentation_id, callback))); 142 new StartSessionRequest(presentation_url, presentation_id, callback)));
145 if (queued_start_session_requests_.size() == 1) 143 if (queued_start_session_requests_.size() == 1)
146 DoStartSession(presentation_url, presentation_id, callback); 144 DoStartSession(presentation_url, presentation_id, callback);
147 } 145 }
148 146
149 void PresentationServiceImpl::JoinSession( 147 void PresentationServiceImpl::JoinSession(
150 const mojo::String& presentation_url, 148 const mojo::String& presentation_url,
151 const mojo::String& presentation_id, 149 const mojo::String& presentation_id,
152 const NewSessionMojoCallback& callback) { 150 const NewSessionMojoCallback& callback) {
153 DVLOG(2) << "JoinSession"; 151 DVLOG(2) << "JoinSession";
154 if (!delegate_) { 152 if (!delegate_) {
155 callback.Run( 153 InvokeNewSessionMojoCallbackWithError(callback);
156 presentation::PresentationSessionInfoPtr(),
157 presentation::PresentationError::From(
158 PresentationError(PRESENTATION_ERROR_UNKNOWN, "")));
159 return; 154 return;
160 } 155 }
161 156
157 ++next_request_session_id_;
158 pending_session_cbs_[next_request_session_id_].reset(
159 new NewSessionMojoCallback(callback));
162 delegate_->JoinSession( 160 delegate_->JoinSession(
163 render_frame_host_->GetProcess()->GetID(), 161 render_frame_host_->GetProcess()->GetID(),
164 render_frame_host_->GetRoutingID(), 162 render_frame_host_->GetRoutingID(),
165 presentation_url, 163 presentation_url,
166 presentation_id, 164 presentation_id,
167 // TODO(imcheng): These callbacks may be dropped. http://crbug.com/468575
168 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionSucceeded, 165 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionSucceeded,
169 weak_factory_.GetWeakPtr(), false, callback), 166 weak_factory_.GetWeakPtr(), false, next_request_session_id_),
170 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionError, 167 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionError,
171 weak_factory_.GetWeakPtr(), false, callback)); 168 weak_factory_.GetWeakPtr(), false, next_request_session_id_));
172 } 169 }
173 170
174 void PresentationServiceImpl::HandleQueuedStartSessionRequests() { 171 void PresentationServiceImpl::HandleQueuedStartSessionRequests() {
175 DCHECK(!queued_start_session_requests_.empty()); 172 DCHECK(!queued_start_session_requests_.empty());
176 queued_start_session_requests_.pop_front(); 173 queued_start_session_requests_.pop_front();
177 if (!queued_start_session_requests_.empty()) { 174 if (!queued_start_session_requests_.empty()) {
178 const linked_ptr<StartSessionRequest>& request = 175 const linked_ptr<StartSessionRequest>& request =
179 queued_start_session_requests_.front(); 176 queued_start_session_requests_.front();
180 DoStartSession(request->presentation_url, 177 DoStartSession(request->presentation_url,
181 request->presentation_id, 178 request->presentation_id,
182 request->callback); 179 request->callback);
183 } 180 }
184 } 181 }
185 182
186 void PresentationServiceImpl::DoStartSession( 183 void PresentationServiceImpl::DoStartSession(
187 const std::string& presentation_url, 184 const std::string& presentation_url,
188 const std::string& presentation_id, 185 const std::string& presentation_id,
189 const NewSessionMojoCallback& callback) { 186 const NewSessionMojoCallback& callback) {
187 ++next_request_session_id_;
whywhat 2015/03/25 17:05:54 I'd encapsulate this logic into something like: i
imcheng 2015/03/25 23:03:42 Done.
188 pending_session_cbs_[next_request_session_id_].reset(
189 new NewSessionMojoCallback(callback));
190 delegate_->StartSession( 190 delegate_->StartSession(
191 render_frame_host_->GetProcess()->GetID(), 191 render_frame_host_->GetProcess()->GetID(),
192 render_frame_host_->GetRoutingID(), 192 render_frame_host_->GetRoutingID(),
193 presentation_url, 193 presentation_url,
194 presentation_id, 194 presentation_id,
195 // TODO(imcheng): These callbacks may be dropped. http://crbug.com/468575
196 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionSucceeded, 195 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionSucceeded,
197 weak_factory_.GetWeakPtr(), true, callback), 196 weak_factory_.GetWeakPtr(), true, next_request_session_id_),
198 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionError, 197 base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionError,
199 weak_factory_.GetWeakPtr(), true, callback)); 198 weak_factory_.GetWeakPtr(), true, next_request_session_id_));
200 } 199 }
201 200
202 void PresentationServiceImpl::OnStartOrJoinSessionSucceeded( 201 void PresentationServiceImpl::OnStartOrJoinSessionSucceeded(
203 bool is_start_session, 202 bool is_start_session,
204 const NewSessionMojoCallback& callback, 203 int request_session_id,
205 const PresentationSessionInfo& session_info) { 204 const PresentationSessionInfo& session_info) {
206 callback.Run( 205 RunAndEraseNewMojoCallback(
206 request_session_id,
207 presentation::PresentationSessionInfo::From(session_info), 207 presentation::PresentationSessionInfo::From(session_info),
208 presentation::PresentationErrorPtr()); 208 presentation::PresentationErrorPtr());
209 if (is_start_session) 209 if (is_start_session)
210 HandleQueuedStartSessionRequests(); 210 HandleQueuedStartSessionRequests();
211 } 211 }
212 212
213 void PresentationServiceImpl::OnStartOrJoinSessionError( 213 void PresentationServiceImpl::OnStartOrJoinSessionError(
214 bool is_start_session, 214 bool is_start_session,
215 const NewSessionMojoCallback& callback, 215 int request_session_id,
216 const PresentationError& error) { 216 const PresentationError& error) {
217 callback.Run( 217 RunAndEraseNewMojoCallback(
218 request_session_id,
218 presentation::PresentationSessionInfoPtr(), 219 presentation::PresentationSessionInfoPtr(),
219 presentation::PresentationError::From(error)); 220 presentation::PresentationError::From(error));
220 if (is_start_session) 221 if (is_start_session)
221 HandleQueuedStartSessionRequests(); 222 HandleQueuedStartSessionRequests();
222 } 223 }
223 224
225 void PresentationServiceImpl::RunAndEraseNewMojoCallback(
226 int request_session_id,
227 presentation::PresentationSessionInfoPtr session,
228 presentation::PresentationErrorPtr error) {
229 auto it = pending_session_cbs_.find(request_session_id);
230 if (it == pending_session_cbs_.end())
231 return;
232
233 DCHECK(it->second.get());
234 it->second->Run(session.Pass(), error.Pass());
235 pending_session_cbs_.erase(it);
236 }
237
224 void PresentationServiceImpl::DoSetDefaultPresentationUrl( 238 void PresentationServiceImpl::DoSetDefaultPresentationUrl(
225 const std::string& default_presentation_url, 239 const std::string& default_presentation_url,
226 const std::string& default_presentation_id) { 240 const std::string& default_presentation_id) {
227 DCHECK(delegate_); 241 DCHECK(delegate_);
228 delegate_->SetDefaultPresentationUrl( 242 delegate_->SetDefaultPresentationUrl(
229 render_frame_host_->GetProcess()->GetID(), 243 render_frame_host_->GetProcess()->GetID(),
230 render_frame_host_->GetRoutingID(), 244 render_frame_host_->GetRoutingID(),
231 default_presentation_url, 245 default_presentation_url,
232 default_presentation_id); 246 default_presentation_id);
233 default_presentation_url_ = default_presentation_url; 247 default_presentation_url_ = default_presentation_url;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 void PresentationServiceImpl::Reset() { 334 void PresentationServiceImpl::Reset() {
321 DVLOG(2) << "PresentationServiceImpl::Reset"; 335 DVLOG(2) << "PresentationServiceImpl::Reset";
322 if (delegate_) { 336 if (delegate_) {
323 delegate_->Reset( 337 delegate_->Reset(
324 render_frame_host_->GetProcess()->GetID(), 338 render_frame_host_->GetProcess()->GetID(),
325 render_frame_host_->GetRoutingID()); 339 render_frame_host_->GetRoutingID());
326 } 340 }
327 341
328 default_presentation_url_.clear(); 342 default_presentation_url_.clear();
329 default_presentation_id_.clear(); 343 default_presentation_id_.clear();
330 for (const auto& context : availability_contexts_) { 344 for (const auto& context_entry : availability_contexts_) {
331 context.second->OnScreenAvailabilityChanged(false); 345 context_entry.second->OnScreenAvailabilityChanged(false);
332 } 346 }
333 availability_contexts_.clear(); 347 availability_contexts_.clear();
334 // TODO(imcheng): This may drop callbacks. See http://crbug.com/468575. 348 for (auto& request_ptr : queued_start_session_requests_) {
349 InvokeNewSessionMojoCallbackWithError(request_ptr->callback);
350 }
335 queued_start_session_requests_.clear(); 351 queued_start_session_requests_.clear();
352 for (auto& pending_entry : pending_session_cbs_) {
353 InvokeNewSessionMojoCallbackWithError(*pending_entry.second);
354 }
355 pending_session_cbs_.clear();
356 }
357
358 void PresentationServiceImpl::InvokeNewSessionMojoCallbackWithError(
359 const NewSessionMojoCallback& callback) {
360 callback.Run(
361 presentation::PresentationSessionInfoPtr(),
362 presentation::PresentationError::From(
363 PresentationError(PRESENTATION_ERROR_UNKNOWN, "Internal error")));
336 } 364 }
337 365
338 void PresentationServiceImpl::OnDelegateDestroyed() { 366 void PresentationServiceImpl::OnDelegateDestroyed() {
339 DVLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed"; 367 DVLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed";
340 delegate_ = nullptr; 368 delegate_ = nullptr;
341 Reset(); 369 Reset();
342 } 370 }
343 371
344 PresentationServiceImpl::ScreenAvailabilityContext::ScreenAvailabilityContext( 372 PresentationServiceImpl::ScreenAvailabilityContext::ScreenAvailabilityContext(
345 const std::string& presentation_url) 373 const std::string& presentation_url)
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 : presentation_url(presentation_url), 435 : presentation_url(presentation_url),
408 presentation_id(presentation_id), 436 presentation_id(presentation_id),
409 callback(callback) { 437 callback(callback) {
410 } 438 }
411 439
412 PresentationServiceImpl::StartSessionRequest::~StartSessionRequest() { 440 PresentationServiceImpl::StartSessionRequest::~StartSessionRequest() {
413 } 441 }
414 442
415 } // namespace content 443 } // namespace content
416 444
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698