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

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

Issue 2477573002: [Presentation API] (3rd) (1-UA) Split PresentationServiceDelegateImpl(PSDImpl) (Closed)
Patch Set: Created 4 years, 1 month 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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <algorithm> 9 #include <algorithm>
10 #include <utility> 10 #include <utility>
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 callback.Run(blink::mojom::PresentationSessionInfoPtr(), 119 callback.Run(blink::mojom::PresentationSessionInfoPtr(),
120 blink::mojom::PresentationError::From(PresentationError( 120 blink::mojom::PresentationError::From(PresentationError(
121 PRESENTATION_ERROR_UNKNOWN, "Internal error"))); 121 PRESENTATION_ERROR_UNKNOWN, "Internal error")));
122 } 122 }
123 123
124 } // namespace 124 } // namespace
125 125
126 PresentationServiceImpl::PresentationServiceImpl( 126 PresentationServiceImpl::PresentationServiceImpl(
127 RenderFrameHost* render_frame_host, 127 RenderFrameHost* render_frame_host,
128 WebContents* web_contents, 128 WebContents* web_contents,
129 PresentationServiceDelegate* delegate) 129 ControllerPresentationServiceDelegate* controller_delegate,
130 ReceiverPresentationServiceDelegate* receiver_delegate)
130 : WebContentsObserver(web_contents), 131 : WebContentsObserver(web_contents),
131 delegate_(delegate), 132 controller_delegate_(controller_delegate),
133 receiver_delegate_(receiver_delegate),
132 start_session_request_id_(kInvalidRequestSessionId), 134 start_session_request_id_(kInvalidRequestSessionId),
133 weak_factory_(this) { 135 weak_factory_(this) {
134 DCHECK(render_frame_host); 136 DCHECK(render_frame_host);
135 DCHECK(web_contents); 137 DCHECK(web_contents);
136 CHECK(render_frame_host->IsRenderFrameLive()); 138 CHECK(render_frame_host->IsRenderFrameLive());
137 139
138 render_process_id_ = render_frame_host->GetProcess()->GetID(); 140 render_process_id_ = render_frame_host->GetProcess()->GetID();
139 render_frame_id_ = render_frame_host->GetRoutingID(); 141 render_frame_id_ = render_frame_host->GetRoutingID();
140 DVLOG(2) << "PresentationServiceImpl: " 142 DVLOG(2) << "PresentationServiceImpl: "
141 << render_process_id_ << ", " << render_frame_id_; 143 << render_process_id_ << ", " << render_frame_id_;
142 if (delegate_) 144
143 delegate_->AddObserver(render_process_id_, render_frame_id_, this); 145 if (auto delegate = GetPresentationServiceDelegate())
mark a. foltz 2016/11/08 23:40:52 Prefer auto delegate* const here and elsewhere
zhaobin 2016/11/10 04:14:00 Done.
146 delegate->AddObserver(render_process_id_, render_frame_id_, this);
144 } 147 }
145 148
146 PresentationServiceImpl::~PresentationServiceImpl() { 149 PresentationServiceImpl::~PresentationServiceImpl() {
147 if (delegate_) 150 DVLOG(2) << __FUNCTION__ << ": " << render_process_id_ << ", "
148 delegate_->RemoveObserver(render_process_id_, render_frame_id_); 151 << render_frame_id_;
152
153 if (auto delegate = GetPresentationServiceDelegate())
154 delegate->RemoveObserver(render_process_id_, render_frame_id_);
149 } 155 }
150 156
151 // static 157 // static
152 void PresentationServiceImpl::CreateMojoService( 158 void PresentationServiceImpl::CreateMojoService(
153 RenderFrameHost* render_frame_host, 159 RenderFrameHost* render_frame_host,
154 mojo::InterfaceRequest<blink::mojom::PresentationService> request) { 160 mojo::InterfaceRequest<blink::mojom::PresentationService> request) {
155 DVLOG(2) << "CreateMojoService"; 161 DVLOG(2) << "CreateMojoService";
156 WebContents* web_contents = 162 WebContents* web_contents =
157 WebContents::FromRenderFrameHost(render_frame_host); 163 WebContents::FromRenderFrameHost(render_frame_host);
158 DCHECK(web_contents); 164 DCHECK(web_contents);
159 165
160 // This object will be deleted when the RenderFrameHost is about to be 166 // This object will be deleted when the RenderFrameHost is about to be
161 // deleted (RenderFrameDeleted). 167 // deleted (RenderFrameDeleted).
162 PresentationServiceImpl* impl = new PresentationServiceImpl( 168 PresentationServiceImpl* impl = new PresentationServiceImpl(
163 render_frame_host, 169 render_frame_host, web_contents,
164 web_contents, 170 GetContentClient()->browser()->GetControllerPresentationServiceDelegate(
165 GetContentClient()->browser()->GetPresentationServiceDelegate( 171 web_contents),
172 GetContentClient()->browser()->GetReceiverPresentationServiceDelegate(
166 web_contents)); 173 web_contents));
174 if (impl->receiver_delegate_) {
mark a. foltz 2016/11/08 23:40:52 When would |receiver_delegate_| be null? It looks
zhaobin 2016/11/10 04:14:00 Sorry for the confusion. It is set in offscreen_ta
175 // In current implementation, web contents can be controller or receiver
176 // but not both.
177 impl->controller_delegate_ = nullptr;
178 }
mark a. foltz 2016/11/08 23:40:52 else { DCHECK(controller_delegate_); }
zhaobin 2016/11/10 04:14:00 Code removed.
167 impl->Bind(std::move(request)); 179 impl->Bind(std::move(request));
168 } 180 }
169 181
170 void PresentationServiceImpl::Bind( 182 void PresentationServiceImpl::Bind(
171 mojo::InterfaceRequest<blink::mojom::PresentationService> request) { 183 mojo::InterfaceRequest<blink::mojom::PresentationService> request) {
172 binding_.reset(new mojo::Binding<blink::mojom::PresentationService>( 184 binding_.reset(new mojo::Binding<blink::mojom::PresentationService>(
173 this, std::move(request))); 185 this, std::move(request)));
174 } 186 }
175 187
176 void PresentationServiceImpl::SetClient( 188 void PresentationServiceImpl::SetClient(
177 blink::mojom::PresentationServiceClientPtr client) { 189 blink::mojom::PresentationServiceClientPtr client) {
178 DCHECK(!client_.get()); 190 DCHECK(!client_.get());
179 // TODO(imcheng): Set ErrorHandler to listen for errors. 191 // TODO(imcheng): Set ErrorHandler to listen for errors.
180 client_ = std::move(client); 192 client_ = std::move(client);
193
194 if (receiver_delegate_) {
195 receiver_delegate_->RegisterReceiverConnectionAvailableCallback(
196 base::Bind(&PresentationServiceImpl::OnReceiverConnectionAvailable,
197 weak_factory_.GetWeakPtr()));
198 }
181 } 199 }
182 200
183 void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) { 201 void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) {
184 DVLOG(2) << "ListenForScreenAvailability " << url.spec(); 202 DVLOG(2) << "ListenForScreenAvailability " << url.spec();
185 if (!delegate_) { 203 if (!controller_delegate_) {
186 client_->OnScreenAvailabilityUpdated(url, false); 204 client_->OnScreenAvailabilityUpdated(url, false);
187 return; 205 return;
188 } 206 }
189 207
190 if (screen_availability_listeners_.count(url)) 208 if (screen_availability_listeners_.count(url))
191 return; 209 return;
192 210
193 std::unique_ptr<ScreenAvailabilityListenerImpl> listener( 211 std::unique_ptr<ScreenAvailabilityListenerImpl> listener(
194 new ScreenAvailabilityListenerImpl(url, this)); 212 new ScreenAvailabilityListenerImpl(url, this));
195 if (delegate_->AddScreenAvailabilityListener( 213 if (controller_delegate_->AddScreenAvailabilityListener(
196 render_process_id_, 214 render_process_id_, render_frame_id_, listener.get())) {
197 render_frame_id_,
198 listener.get())) {
199 screen_availability_listeners_[url] = std::move(listener); 215 screen_availability_listeners_[url] = std::move(listener);
200 } else { 216 } else {
201 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; 217 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
202 } 218 }
203 } 219 }
204 220
205 void PresentationServiceImpl::StopListeningForScreenAvailability( 221 void PresentationServiceImpl::StopListeningForScreenAvailability(
206 const GURL& url) { 222 const GURL& url) {
207 DVLOG(2) << "StopListeningForScreenAvailability " << url.spec(); 223 DVLOG(2) << "StopListeningForScreenAvailability " << url.spec();
208 if (!delegate_) 224 if (!controller_delegate_)
209 return; 225 return;
210 226
211 auto listener_it = screen_availability_listeners_.find(url); 227 auto listener_it = screen_availability_listeners_.find(url);
212 if (listener_it == screen_availability_listeners_.end()) 228 if (listener_it == screen_availability_listeners_.end())
213 return; 229 return;
214 230
215 delegate_->RemoveScreenAvailabilityListener( 231 controller_delegate_->RemoveScreenAvailabilityListener(
216 render_process_id_, render_frame_id_, listener_it->second.get()); 232 render_process_id_, render_frame_id_, listener_it->second.get());
217 screen_availability_listeners_.erase(listener_it); 233 screen_availability_listeners_.erase(listener_it);
218 } 234 }
219 235
220 void PresentationServiceImpl::StartSession( 236 void PresentationServiceImpl::StartSession(
221 const std::vector<GURL>& presentation_urls, 237 const std::vector<GURL>& presentation_urls,
222 const NewSessionCallback& callback) { 238 const NewSessionCallback& callback) {
223 DVLOG(2) << "StartSession"; 239 DVLOG(2) << "StartSession";
224 if (!delegate_) { 240 if (!controller_delegate_) {
225 callback.Run( 241 callback.Run(
226 blink::mojom::PresentationSessionInfoPtr(), 242 blink::mojom::PresentationSessionInfoPtr(),
227 blink::mojom::PresentationError::From(PresentationError( 243 blink::mojom::PresentationError::From(PresentationError(
228 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found."))); 244 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found.")));
229 return; 245 return;
230 } 246 }
231 247
232 // There is a StartSession request in progress. To avoid queueing up 248 // There is a StartSession request in progress. To avoid queueing up
233 // requests, the incoming request is rejected. 249 // requests, the incoming request is rejected.
234 if (start_session_request_id_ != kInvalidRequestSessionId) { 250 if (start_session_request_id_ != kInvalidRequestSessionId) {
235 InvokeNewSessionCallbackWithError(callback); 251 InvokeNewSessionCallbackWithError(callback);
236 return; 252 return;
237 } 253 }
238 254
239 start_session_request_id_ = GetNextRequestSessionId(); 255 start_session_request_id_ = GetNextRequestSessionId();
240 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback)); 256 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback));
241 delegate_->StartSession( 257 controller_delegate_->StartSession(
242 render_process_id_, render_frame_id_, presentation_urls, 258 render_process_id_, render_frame_id_, presentation_urls,
243 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded, 259 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded,
244 weak_factory_.GetWeakPtr(), start_session_request_id_), 260 weak_factory_.GetWeakPtr(), start_session_request_id_),
245 base::Bind(&PresentationServiceImpl::OnStartSessionError, 261 base::Bind(&PresentationServiceImpl::OnStartSessionError,
246 weak_factory_.GetWeakPtr(), start_session_request_id_)); 262 weak_factory_.GetWeakPtr(), start_session_request_id_));
247 } 263 }
248 264
249 void PresentationServiceImpl::JoinSession( 265 void PresentationServiceImpl::JoinSession(
250 const std::vector<GURL>& presentation_urls, 266 const std::vector<GURL>& presentation_urls,
251 const base::Optional<std::string>& presentation_id, 267 const base::Optional<std::string>& presentation_id,
252 const NewSessionCallback& callback) { 268 const NewSessionCallback& callback) {
253 DVLOG(2) << "JoinSession"; 269 DVLOG(2) << "JoinSession";
254 if (!delegate_) { 270 if (!controller_delegate_) {
255 callback.Run(blink::mojom::PresentationSessionInfoPtr(), 271 callback.Run(blink::mojom::PresentationSessionInfoPtr(),
256 blink::mojom::PresentationError::From(PresentationError( 272 blink::mojom::PresentationError::From(PresentationError(
257 PRESENTATION_ERROR_NO_PRESENTATION_FOUND, 273 PRESENTATION_ERROR_NO_PRESENTATION_FOUND,
258 "Error joining route: No matching route"))); 274 "Error joining route: No matching route")));
259 return; 275 return;
260 } 276 }
261 277
262 int request_session_id = RegisterJoinSessionCallback(callback); 278 int request_session_id = RegisterJoinSessionCallback(callback);
263 if (request_session_id == kInvalidRequestSessionId) { 279 if (request_session_id == kInvalidRequestSessionId) {
264 InvokeNewSessionCallbackWithError(callback); 280 InvokeNewSessionCallbackWithError(callback);
265 return; 281 return;
266 } 282 }
267 delegate_->JoinSession( 283 controller_delegate_->JoinSession(
268 render_process_id_, render_frame_id_, presentation_urls, 284 render_process_id_, render_frame_id_, presentation_urls,
269 presentation_id.value_or(std::string()), 285 presentation_id.value_or(std::string()),
270 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded, 286 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded,
271 weak_factory_.GetWeakPtr(), request_session_id), 287 weak_factory_.GetWeakPtr(), request_session_id),
272 base::Bind(&PresentationServiceImpl::OnJoinSessionError, 288 base::Bind(&PresentationServiceImpl::OnJoinSessionError,
273 weak_factory_.GetWeakPtr(), request_session_id)); 289 weak_factory_.GetWeakPtr(), request_session_id));
274 } 290 }
275 291
276 int PresentationServiceImpl::RegisterJoinSessionCallback( 292 int PresentationServiceImpl::RegisterJoinSessionCallback(
277 const NewSessionCallback& callback) { 293 const NewSessionCallback& callback) {
278 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests) 294 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests)
279 return kInvalidRequestSessionId; 295 return kInvalidRequestSessionId;
280 296
281 int request_id = GetNextRequestSessionId(); 297 int request_id = GetNextRequestSessionId();
282 pending_join_session_cbs_[request_id].reset( 298 pending_join_session_cbs_[request_id].reset(
283 new NewSessionCallbackWrapper(callback)); 299 new NewSessionCallbackWrapper(callback));
284 return request_id; 300 return request_id;
285 } 301 }
286 302
287 void PresentationServiceImpl::ListenForConnectionStateChange( 303 void PresentationServiceImpl::ListenForConnectionStateChange(
288 const PresentationSessionInfo& connection) { 304 const PresentationSessionInfo& connection) {
289 if (delegate_) { 305 if (controller_delegate_) {
290 delegate_->ListenForConnectionStateChange( 306 controller_delegate_->ListenForConnectionStateChange(
291 render_process_id_, render_frame_id_, connection, 307 render_process_id_, render_frame_id_, connection,
292 base::Bind(&PresentationServiceImpl::OnConnectionStateChanged, 308 base::Bind(&PresentationServiceImpl::OnConnectionStateChanged,
293 weak_factory_.GetWeakPtr(), connection)); 309 weak_factory_.GetWeakPtr(), connection));
294 } 310 }
295 } 311 }
296 312
297 void PresentationServiceImpl::OnStartSessionSucceeded( 313 void PresentationServiceImpl::OnStartSessionSucceeded(
298 int request_session_id, 314 int request_session_id,
299 const PresentationSessionInfo& session_info) { 315 const PresentationSessionInfo& session_info) {
300 DVLOG(2) << "PresentationServiceImpl::OnStartSessionSucceeded" 316 DVLOG(2) << "PresentationServiceImpl::OnStartSessionSucceeded"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 371
356 DCHECK(it->second.get()); 372 DCHECK(it->second.get());
357 it->second->Run(std::move(session), std::move(error)); 373 it->second->Run(std::move(session), std::move(error));
358 pending_join_session_cbs_.erase(it); 374 pending_join_session_cbs_.erase(it);
359 return true; 375 return true;
360 } 376 }
361 377
362 void PresentationServiceImpl::SetDefaultPresentationUrls( 378 void PresentationServiceImpl::SetDefaultPresentationUrls(
363 const std::vector<GURL>& presentation_urls) { 379 const std::vector<GURL>& presentation_urls) {
364 DVLOG(2) << "SetDefaultPresentationUrls"; 380 DVLOG(2) << "SetDefaultPresentationUrls";
365 if (!delegate_) 381 if (!controller_delegate_)
366 return; 382 return;
367 383
368 if (default_presentation_urls_ == presentation_urls) 384 if (default_presentation_urls_ == presentation_urls)
369 return; 385 return;
370 386
371 default_presentation_urls_ = presentation_urls; 387 default_presentation_urls_ = presentation_urls;
372 delegate_->SetDefaultPresentationUrls( 388 controller_delegate_->SetDefaultPresentationUrls(
373 render_process_id_, render_frame_id_, presentation_urls, 389 render_process_id_, render_frame_id_, presentation_urls,
374 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, 390 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted,
375 weak_factory_.GetWeakPtr())); 391 weak_factory_.GetWeakPtr()));
376 } 392 }
377 393
378 void PresentationServiceImpl::SendSessionMessage( 394 void PresentationServiceImpl::SendSessionMessage(
379 blink::mojom::PresentationSessionInfoPtr session, 395 blink::mojom::PresentationSessionInfoPtr session,
380 blink::mojom::SessionMessagePtr session_message, 396 blink::mojom::SessionMessagePtr session_message,
381 const SendSessionMessageCallback& callback) { 397 const SendSessionMessageCallback& callback) {
382 DVLOG(2) << "SendSessionMessage"; 398 DVLOG(2) << "SendSessionMessage"
mark a. foltz 2016/11/08 23:40:52 Nit: Move this log below L406, where the message s
zhaobin 2016/11/10 04:14:00 Prefer keeping it here as it makes debugging easie
399 << " [id]: " << session->id;
383 DCHECK(!session_message.is_null()); 400 DCHECK(!session_message.is_null());
384 // send_message_callback_ should be null by now, otherwise resetting of 401 // send_message_callback_ should be null by now, otherwise resetting of
385 // send_message_callback_ with new callback will drop the old callback. 402 // send_message_callback_ with new callback will drop the old callback.
386 if (!delegate_ || send_message_callback_) { 403 if (!controller_delegate_ || send_message_callback_) {
387 callback.Run(false); 404 callback.Run(false);
388 return; 405 return;
389 } 406 }
390 407
391 send_message_callback_.reset(new SendSessionMessageCallback(callback)); 408 send_message_callback_.reset(new SendSessionMessageCallback(callback));
392 delegate_->SendMessage( 409 controller_delegate_->SendMessage(
393 render_process_id_, render_frame_id_, 410 render_process_id_, render_frame_id_,
394 session.To<PresentationSessionInfo>(), 411 session.To<PresentationSessionInfo>(),
395 GetPresentationSessionMessage(std::move(session_message)), 412 GetPresentationSessionMessage(std::move(session_message)),
396 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, 413 base::Bind(&PresentationServiceImpl::OnSendMessageCallback,
397 weak_factory_.GetWeakPtr())); 414 weak_factory_.GetWeakPtr()));
398 } 415 }
399 416
400 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { 417 void PresentationServiceImpl::OnSendMessageCallback(bool sent) {
401 // It is possible that Reset() is invoked before receiving this callback. 418 // It is possible that Reset() is invoked before receiving this callback.
402 // So, always check send_message_callback_ for non-null. 419 // So, always check send_message_callback_ for non-null.
403 if (send_message_callback_) { 420 if (send_message_callback_) {
404 send_message_callback_->Run(sent); 421 send_message_callback_->Run(sent);
405 send_message_callback_.reset(); 422 send_message_callback_.reset();
406 } 423 }
407 } 424 }
408 425
409 void PresentationServiceImpl::CloseConnection( 426 void PresentationServiceImpl::CloseConnection(
410 const GURL& presentation_url, 427 const GURL& presentation_url,
411 const std::string& presentation_id) { 428 const std::string& presentation_id) {
412 DVLOG(2) << "CloseConnection " << presentation_id; 429 DVLOG(2) << "CloseConnection " << presentation_id;
413 if (delegate_) 430 if (controller_delegate_)
414 delegate_->CloseConnection(render_process_id_, render_frame_id_, 431 controller_delegate_->CloseConnection(render_process_id_, render_frame_id_,
415 presentation_id); 432 presentation_id);
416 } 433 }
417 434
418 void PresentationServiceImpl::Terminate(const GURL& presentation_url, 435 void PresentationServiceImpl::Terminate(const GURL& presentation_url,
419 const std::string& presentation_id) { 436 const std::string& presentation_id) {
420 DVLOG(2) << "Terminate " << presentation_id; 437 DVLOG(2) << "Terminate " << presentation_id;
421 if (delegate_) 438 if (controller_delegate_)
422 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id); 439 controller_delegate_->Terminate(render_process_id_, render_frame_id_,
440 presentation_id);
423 } 441 }
424 442
425 void PresentationServiceImpl::OnConnectionStateChanged( 443 void PresentationServiceImpl::OnConnectionStateChanged(
426 const PresentationSessionInfo& connection, 444 const PresentationSessionInfo& connection,
427 const PresentationConnectionStateChangeInfo& info) { 445 const PresentationConnectionStateChangeInfo& info) {
428 DCHECK(client_.get()); 446 DCHECK(client_.get());
429 if (info.state == PRESENTATION_CONNECTION_STATE_CLOSED) { 447 if (info.state == PRESENTATION_CONNECTION_STATE_CLOSED) {
430 client_->OnConnectionClosed( 448 client_->OnConnectionClosed(
431 blink::mojom::PresentationSessionInfo::From(connection), 449 blink::mojom::PresentationSessionInfo::From(connection),
432 content::PresentationConnectionCloseReasonToMojo(info.close_reason), 450 content::PresentationConnectionCloseReasonToMojo(info.close_reason),
433 info.message); 451 info.message);
434 } else { 452 } else {
435 client_->OnConnectionStateChanged( 453 client_->OnConnectionStateChanged(
436 blink::mojom::PresentationSessionInfo::From(connection), 454 blink::mojom::PresentationSessionInfo::From(connection),
437 PresentationConnectionStateToMojo(info.state)); 455 PresentationConnectionStateToMojo(info.state));
438 } 456 }
439 } 457 }
440 458
441 bool PresentationServiceImpl::FrameMatches( 459 bool PresentationServiceImpl::FrameMatches(
442 content::RenderFrameHost* render_frame_host) const { 460 content::RenderFrameHost* render_frame_host) const {
443 if (!render_frame_host) 461 if (!render_frame_host)
444 return false; 462 return false;
445 463
446 return render_frame_host->GetProcess()->GetID() == render_process_id_ && 464 return render_frame_host->GetProcess()->GetID() == render_process_id_ &&
447 render_frame_host->GetRoutingID() == render_frame_id_; 465 render_frame_host->GetRoutingID() == render_frame_id_;
448 } 466 }
449 467
468 PresentationServiceDelegateBase*
469 PresentationServiceImpl::GetPresentationServiceDelegate() {
470 if (receiver_delegate_)
mark a. foltz 2016/11/08 23:40:52 Use a ternary return
zhaobin 2016/11/10 04:14:00 Done.
471 return receiver_delegate_;
472
473 return controller_delegate_;
474 }
475
450 void PresentationServiceImpl::ListenForSessionMessages( 476 void PresentationServiceImpl::ListenForSessionMessages(
451 blink::mojom::PresentationSessionInfoPtr session) { 477 blink::mojom::PresentationSessionInfoPtr session) {
452 DVLOG(2) << "ListenForSessionMessages"; 478 DVLOG(2) << "ListenForSessionMessages";
453 if (!delegate_) 479 if (!controller_delegate_)
454 return; 480 return;
455 481
456 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>()); 482 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>());
457 delegate_->ListenForSessionMessages( 483 controller_delegate_->ListenForSessionMessages(
458 render_process_id_, render_frame_id_, session_info, 484 render_process_id_, render_frame_id_, session_info,
459 base::Bind(&PresentationServiceImpl::OnSessionMessages, 485 base::Bind(&PresentationServiceImpl::OnSessionMessages,
460 weak_factory_.GetWeakPtr(), session_info)); 486 weak_factory_.GetWeakPtr(), session_info));
461 } 487 }
462 488
489 void PresentationServiceImpl::SetPresentationConnection(
490 blink::mojom::PresentationSessionInfoPtr session,
491 blink::mojom::PresentationConnectionPtr connection) {
492 DVLOG(2) << "SetPresentationConnection";
493
494 if (!controller_delegate_)
495 return;
496
497 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>());
498 controller_delegate_->ConnectToOffscreenPresentation(
499 render_process_id_, render_frame_id_, session_info,
500 std::move(connection));
501 }
502
463 void PresentationServiceImpl::OnSessionMessages( 503 void PresentationServiceImpl::OnSessionMessages(
464 const PresentationSessionInfo& session, 504 const PresentationSessionInfo& session,
465 const ScopedVector<PresentationSessionMessage>& messages, 505 const ScopedVector<PresentationSessionMessage>& messages,
466 bool pass_ownership) { 506 bool pass_ownership) {
467 DCHECK(client_); 507 DCHECK(client_);
468 508 DVLOG(2) << "OnSessionMessages"
469 DVLOG(2) << "OnSessionMessages"; 509 << " [id]: " << session.presentation_id;
470 std::vector<blink::mojom::SessionMessagePtr> mojo_messages(messages.size()); 510 std::vector<blink::mojom::SessionMessagePtr> mojo_messages(messages.size());
471 std::transform(messages.begin(), messages.end(), mojo_messages.begin(), 511 std::transform(messages.begin(), messages.end(), mojo_messages.begin(),
472 [pass_ownership](PresentationSessionMessage* message) { 512 [pass_ownership](PresentationSessionMessage* message) {
473 return ToMojoSessionMessage(message, pass_ownership); 513 return ToMojoSessionMessage(message, pass_ownership);
474 }); 514 });
475 515
476 client_->OnSessionMessagesReceived( 516 client_->OnSessionMessagesReceived(
477 blink::mojom::PresentationSessionInfo::From(session), 517 blink::mojom::PresentationSessionInfo::From(session),
478 std::move(mojo_messages)); 518 std::move(mojo_messages));
479 } 519 }
480 520
521 void PresentationServiceImpl::OnReceiverConnectionAvailable(
522 const content::PresentationSessionInfo& session_info,
523 PresentationConnectionPtr&& controller) {
524 DVLOG(2) << "PresentationServiceImpl::OnReceiverConnectionAvailable";
525
526 // client_->OnReceiverConnectionAvailable(
mark a. foltz 2016/11/08 23:40:52 Did you mean to comment this out?
zhaobin 2016/11/10 04:14:00 Yes, client_->OnReceiverConnectionAvailable() is n
527 // blink::mojom::PresentationSessionInfo::From(session_info),
528 // std::move(controller));
529 }
530
481 void PresentationServiceImpl::DidNavigateAnyFrame( 531 void PresentationServiceImpl::DidNavigateAnyFrame(
482 content::RenderFrameHost* render_frame_host, 532 content::RenderFrameHost* render_frame_host,
483 const content::LoadCommittedDetails& details, 533 const content::LoadCommittedDetails& details,
484 const content::FrameNavigateParams& params) { 534 const content::FrameNavigateParams& params) {
485 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; 535 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame";
486 if (!FrameMatches(render_frame_host)) 536 if (!FrameMatches(render_frame_host))
487 return; 537 return;
488 538
489 std::string prev_url_host = details.previous_url.host(); 539 std::string prev_url_host = details.previous_url.host();
490 std::string curr_url_host = params.url.host(); 540 std::string curr_url_host = params.url.host();
(...skipping 25 matching lines...) Expand all
516 void PresentationServiceImpl::WebContentsDestroyed() { 566 void PresentationServiceImpl::WebContentsDestroyed() {
517 LOG(ERROR) << "PresentationServiceImpl is being deleted in " 567 LOG(ERROR) << "PresentationServiceImpl is being deleted in "
518 << "WebContentsDestroyed()! This shouldn't happen since it " 568 << "WebContentsDestroyed()! This shouldn't happen since it "
519 << "should've been deleted during RenderFrameDeleted()."; 569 << "should've been deleted during RenderFrameDeleted().";
520 Reset(); 570 Reset();
521 delete this; 571 delete this;
522 } 572 }
523 573
524 void PresentationServiceImpl::Reset() { 574 void PresentationServiceImpl::Reset() {
525 DVLOG(2) << "PresentationServiceImpl::Reset"; 575 DVLOG(2) << "PresentationServiceImpl::Reset";
526 if (delegate_) 576
527 delegate_->Reset(render_process_id_, render_frame_id_); 577 if (auto delegate = GetPresentationServiceDelegate())
578 delegate->Reset(render_process_id_, render_frame_id_);
528 579
529 default_presentation_urls_.clear(); 580 default_presentation_urls_.clear();
530 581
531 screen_availability_listeners_.clear(); 582 screen_availability_listeners_.clear();
532 583
533 start_session_request_id_ = kInvalidRequestSessionId; 584 start_session_request_id_ = kInvalidRequestSessionId;
534 pending_start_session_cb_.reset(); 585 pending_start_session_cb_.reset();
535 586
536 pending_join_session_cbs_.clear(); 587 pending_join_session_cbs_.clear();
537 588
538 if (on_session_messages_callback_.get()) { 589 if (on_session_messages_callback_.get()) {
539 on_session_messages_callback_->Run( 590 on_session_messages_callback_->Run(
540 mojo::Array<blink::mojom::SessionMessagePtr>()); 591 mojo::Array<blink::mojom::SessionMessagePtr>());
541 on_session_messages_callback_.reset(); 592 on_session_messages_callback_.reset();
542 } 593 }
543 594
544 if (send_message_callback_) { 595 if (send_message_callback_) {
545 // Run the callback with false, indicating the renderer to stop sending 596 // Run the callback with false, indicating the renderer to stop sending
546 // the requests and invalidate all pending requests. 597 // the requests and invalidate all pending requests.
547 send_message_callback_->Run(false); 598 send_message_callback_->Run(false);
548 send_message_callback_.reset(); 599 send_message_callback_.reset();
549 } 600 }
550 } 601 }
551 602
552 void PresentationServiceImpl::OnDelegateDestroyed() { 603 void PresentationServiceImpl::OnDelegateDestroyed() {
553 DVLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed"; 604 DVLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed";
554 delegate_ = nullptr; 605 controller_delegate_ = nullptr;
606 receiver_delegate_ = nullptr;
555 Reset(); 607 Reset();
556 } 608 }
557 609
558 void PresentationServiceImpl::OnDefaultPresentationStarted( 610 void PresentationServiceImpl::OnDefaultPresentationStarted(
559 const PresentationSessionInfo& connection) { 611 const PresentationSessionInfo& connection) {
560 DVLOG(2) << "PresentationServiceImpl::OnDefaultPresentationStarted" 612 DVLOG(2) << "PresentationServiceImpl::OnDefaultPresentationStarted"
561 << " [is_offscreen]: " << connection.is_offscreen; 613 << " [is_offscreen]: " << connection.is_offscreen;
562 DCHECK(client_.get()); 614 DCHECK(client_.get());
563 client_->OnDefaultSessionStarted( 615 client_->OnDefaultSessionStarted(
564 blink::mojom::PresentationSessionInfo::From(connection)); 616 blink::mojom::PresentationSessionInfo::From(connection));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 657
606 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( 658 void PresentationServiceImpl::NewSessionCallbackWrapper::Run(
607 blink::mojom::PresentationSessionInfoPtr session, 659 blink::mojom::PresentationSessionInfoPtr session,
608 blink::mojom::PresentationErrorPtr error) { 660 blink::mojom::PresentationErrorPtr error) {
609 DCHECK(!callback_.is_null()); 661 DCHECK(!callback_.is_null());
610 callback_.Run(std::move(session), std::move(error)); 662 callback_.Run(std::move(session), std::move(error));
611 callback_.Reset(); 663 callback_.Reset();
612 } 664 }
613 665
614 } // namespace content 666 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698