Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <string> | |
| 11 #include <utility> | 10 #include <utility> |
| 12 #include <vector> | |
| 13 | 11 |
| 14 #include "base/logging.h" | 12 #include "base/logging.h" |
| 15 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 16 #include "content/browser/presentation/presentation_type_converters.h" | 14 #include "content/browser/presentation/presentation_type_converters.h" |
| 17 #include "content/public/browser/content_browser_client.h" | 15 #include "content/public/browser/content_browser_client.h" |
| 18 #include "content/public/browser/navigation_details.h" | 16 #include "content/public/browser/navigation_details.h" |
| 19 #include "content/public/browser/presentation_session_message.h" | 17 #include "content/public/browser/presentation_session_message.h" |
| 20 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
| 21 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 } | 174 } |
| 177 | 175 |
| 178 void PresentationServiceImpl::SetClient( | 176 void PresentationServiceImpl::SetClient( |
| 179 blink::mojom::PresentationServiceClientPtr client) { | 177 blink::mojom::PresentationServiceClientPtr client) { |
| 180 DCHECK(!client_.get()); | 178 DCHECK(!client_.get()); |
| 181 // TODO(imcheng): Set ErrorHandler to listen for errors. | 179 // TODO(imcheng): Set ErrorHandler to listen for errors. |
| 182 client_ = std::move(client); | 180 client_ = std::move(client); |
| 183 } | 181 } |
| 184 | 182 |
| 185 void PresentationServiceImpl::ListenForScreenAvailability( | 183 void PresentationServiceImpl::ListenForScreenAvailability( |
| 186 const std::string& url) { | 184 url::mojom::UrlPtr url) { |
| 187 DVLOG(2) << "ListenForScreenAvailability " << url; | 185 DVLOG(2) << "ListenForScreenAvailability " << url->url; |
| 188 if (!delegate_) { | 186 if (!delegate_) { |
| 189 client_->OnScreenAvailabilityUpdated(url, false); | 187 client_->OnScreenAvailabilityUpdated(std::move(url), false); |
| 190 return; | 188 return; |
| 191 } | 189 } |
| 192 | 190 |
| 193 if (screen_availability_listeners_.count(url)) | 191 if (screen_availability_listeners_.count(url->url)) |
| 194 return; | 192 return; |
| 195 | 193 |
| 196 std::unique_ptr<ScreenAvailabilityListenerImpl> listener( | 194 std::unique_ptr<ScreenAvailabilityListenerImpl> listener( |
| 197 new ScreenAvailabilityListenerImpl(url, this)); | 195 new ScreenAvailabilityListenerImpl(url->url, this)); |
| 198 if (delegate_->AddScreenAvailabilityListener( | 196 if (delegate_->AddScreenAvailabilityListener( |
| 199 render_process_id_, | 197 render_process_id_, |
| 200 render_frame_id_, | 198 render_frame_id_, |
| 201 listener.get())) { | 199 listener.get())) { |
| 202 screen_availability_listeners_[url] = std::move(listener); | 200 screen_availability_listeners_[url->url] = std::move(listener); |
| 203 } else { | 201 } else { |
| 204 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; | 202 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; |
| 205 } | 203 } |
| 206 } | 204 } |
| 207 | 205 |
| 208 void PresentationServiceImpl::StopListeningForScreenAvailability( | 206 void PresentationServiceImpl::StopListeningForScreenAvailability( |
| 209 const std::string& url) { | 207 url::mojom::UrlPtr url) { |
| 210 DVLOG(2) << "StopListeningForScreenAvailability " << url; | 208 DVLOG(2) << "StopListeningForScreenAvailability " << url->url; |
| 211 if (!delegate_) | 209 if (!delegate_) |
| 212 return; | 210 return; |
| 213 | 211 |
| 214 auto listener_it = screen_availability_listeners_.find(url); | 212 auto listener_it = screen_availability_listeners_.find(url->url); |
| 215 if (listener_it == screen_availability_listeners_.end()) | 213 if (listener_it == screen_availability_listeners_.end()) |
| 216 return; | 214 return; |
| 217 | 215 |
| 218 delegate_->RemoveScreenAvailabilityListener( | 216 delegate_->RemoveScreenAvailabilityListener( |
| 219 render_process_id_, render_frame_id_, listener_it->second.get()); | 217 render_process_id_, render_frame_id_, listener_it->second.get()); |
| 220 screen_availability_listeners_.erase(listener_it); | 218 screen_availability_listeners_.erase(listener_it); |
| 221 } | 219 } |
| 222 | 220 |
| 223 void PresentationServiceImpl::StartSession(const std::string& presentation_url, | 221 void PresentationServiceImpl::StartSession( |
| 224 const NewSessionCallback& callback) { | 222 std::vector<url::mojom::UrlPtr> presentation_urls, |
| 223 const NewSessionCallback& callback) { | |
| 225 DVLOG(2) << "StartSession"; | 224 DVLOG(2) << "StartSession"; |
| 226 if (!delegate_) { | 225 if (!delegate_) { |
| 227 callback.Run( | 226 callback.Run( |
| 228 blink::mojom::PresentationSessionInfoPtr(), | 227 blink::mojom::PresentationSessionInfoPtr(), |
| 229 blink::mojom::PresentationError::From(PresentationError( | 228 blink::mojom::PresentationError::From(PresentationError( |
| 230 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found."))); | 229 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found."))); |
| 231 return; | 230 return; |
| 232 } | 231 } |
| 233 | 232 |
| 234 // There is a StartSession request in progress. To avoid queueing up | 233 // There is a StartSession request in progress. To avoid queueing up |
| 235 // requests, the incoming request is rejected. | 234 // requests, the incoming request is rejected. |
| 236 if (start_session_request_id_ != kInvalidRequestSessionId) { | 235 if (start_session_request_id_ != kInvalidRequestSessionId) { |
| 237 InvokeNewSessionCallbackWithError(callback); | 236 InvokeNewSessionCallbackWithError(callback); |
| 238 return; | 237 return; |
| 239 } | 238 } |
| 240 | 239 |
| 240 std::vector<std::string> presentation_urls_for_delegate( | |
| 241 presentation_urls.size()); | |
| 242 std::transform(presentation_urls.begin(), presentation_urls.end(), | |
| 243 presentation_urls_for_delegate.begin(), | |
| 244 [](const url::mojom::UrlPtr& url) { return url->url; }); | |
| 245 | |
| 241 start_session_request_id_ = GetNextRequestSessionId(); | 246 start_session_request_id_ = GetNextRequestSessionId(); |
| 242 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback)); | 247 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback)); |
| 243 delegate_->StartSession( | 248 delegate_->StartSession( |
| 244 render_process_id_, render_frame_id_, presentation_url, | 249 render_process_id_, render_frame_id_, presentation_urls_for_delegate, |
| 245 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded, | 250 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded, |
| 246 weak_factory_.GetWeakPtr(), start_session_request_id_), | 251 weak_factory_.GetWeakPtr(), start_session_request_id_), |
| 247 base::Bind(&PresentationServiceImpl::OnStartSessionError, | 252 base::Bind(&PresentationServiceImpl::OnStartSessionError, |
| 248 weak_factory_.GetWeakPtr(), start_session_request_id_)); | 253 weak_factory_.GetWeakPtr(), start_session_request_id_)); |
| 249 } | 254 } |
| 250 | 255 |
| 251 void PresentationServiceImpl::JoinSession( | 256 void PresentationServiceImpl::JoinSession( |
| 252 const std::string& presentation_url, | 257 std::vector<url::mojom::UrlPtr> presentation_urls, |
| 253 const base::Optional<std::string>& presentation_id, | 258 const base::Optional<std::string>& presentation_id, |
| 254 const NewSessionCallback& callback) { | 259 const NewSessionCallback& callback) { |
| 255 DVLOG(2) << "JoinSession"; | 260 DVLOG(2) << "JoinSession"; |
| 256 if (!delegate_) { | 261 if (!delegate_) { |
| 257 callback.Run(blink::mojom::PresentationSessionInfoPtr(), | 262 callback.Run(blink::mojom::PresentationSessionInfoPtr(), |
| 258 blink::mojom::PresentationError::From(PresentationError( | 263 blink::mojom::PresentationError::From(PresentationError( |
| 259 PRESENTATION_ERROR_NO_PRESENTATION_FOUND, | 264 PRESENTATION_ERROR_NO_PRESENTATION_FOUND, |
| 260 "Error joining route: No matching route"))); | 265 "Error joining route: No matching route"))); |
| 261 return; | 266 return; |
| 262 } | 267 } |
| 263 | 268 |
| 269 std::vector<std::string> presentation_urls_for_delegate( | |
| 270 presentation_urls.size()); | |
| 271 std::transform(presentation_urls.begin(), presentation_urls.end(), | |
| 272 presentation_urls_for_delegate.begin(), | |
| 273 [](const url::mojom::UrlPtr& url) { return url->url; }); | |
| 274 | |
| 264 int request_session_id = RegisterJoinSessionCallback(callback); | 275 int request_session_id = RegisterJoinSessionCallback(callback); |
| 265 if (request_session_id == kInvalidRequestSessionId) { | 276 if (request_session_id == kInvalidRequestSessionId) { |
| 266 InvokeNewSessionCallbackWithError(callback); | 277 InvokeNewSessionCallbackWithError(callback); |
| 267 return; | 278 return; |
| 268 } | 279 } |
| 269 delegate_->JoinSession( | 280 delegate_->JoinSession( |
| 270 render_process_id_, render_frame_id_, presentation_url, | 281 render_process_id_, render_frame_id_, presentation_urls_for_delegate, |
| 271 presentation_id.value_or(std::string()), | 282 presentation_id.value_or(std::string()), |
| 272 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded, | 283 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded, |
| 273 weak_factory_.GetWeakPtr(), request_session_id), | 284 weak_factory_.GetWeakPtr(), request_session_id), |
| 274 base::Bind(&PresentationServiceImpl::OnJoinSessionError, | 285 base::Bind(&PresentationServiceImpl::OnJoinSessionError, |
| 275 weak_factory_.GetWeakPtr(), request_session_id)); | 286 weak_factory_.GetWeakPtr(), request_session_id)); |
| 276 } | 287 } |
| 277 | 288 |
| 278 int PresentationServiceImpl::RegisterJoinSessionCallback( | 289 int PresentationServiceImpl::RegisterJoinSessionCallback( |
| 279 const NewSessionCallback& callback) { | 290 const NewSessionCallback& callback) { |
| 280 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests) | 291 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests) |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 350 auto it = pending_join_session_cbs_.find(request_session_id); | 361 auto it = pending_join_session_cbs_.find(request_session_id); |
| 351 if (it == pending_join_session_cbs_.end()) | 362 if (it == pending_join_session_cbs_.end()) |
| 352 return false; | 363 return false; |
| 353 | 364 |
| 354 DCHECK(it->second.get()); | 365 DCHECK(it->second.get()); |
| 355 it->second->Run(std::move(session), std::move(error)); | 366 it->second->Run(std::move(session), std::move(error)); |
| 356 pending_join_session_cbs_.erase(it); | 367 pending_join_session_cbs_.erase(it); |
| 357 return true; | 368 return true; |
| 358 } | 369 } |
| 359 | 370 |
| 360 void PresentationServiceImpl::SetDefaultPresentationURL( | 371 void PresentationServiceImpl::SetDefaultPresentationUrls( |
| 361 const std::string& url) { | 372 std::vector<url::mojom::UrlPtr> presentation_urls) { |
| 362 DVLOG(2) << "SetDefaultPresentationURL"; | 373 DVLOG(2) << "SetDefaultPresentationUrls"; |
| 363 if (!delegate_) | 374 if (!delegate_) |
| 364 return; | 375 return; |
| 365 | 376 |
| 366 if (default_presentation_url_ == url) | 377 std::vector<std::string> presentation_urls_for_delegate( |
| 378 presentation_urls.size()); | |
| 379 std::transform(presentation_urls.begin(), presentation_urls.end(), | |
| 380 presentation_urls_for_delegate.begin(), | |
| 381 [](const url::mojom::UrlPtr& url) { return url->url; }); | |
| 382 | |
| 383 if (default_presentation_urls_ == presentation_urls_for_delegate) | |
| 367 return; | 384 return; |
| 368 | 385 |
| 369 default_presentation_url_ = url; | 386 default_presentation_urls_ = presentation_urls_for_delegate; |
| 370 delegate_->SetDefaultPresentationUrl( | 387 delegate_->SetDefaultPresentationUrls( |
| 371 render_process_id_, render_frame_id_, url, | 388 render_process_id_, render_frame_id_, presentation_urls_for_delegate, |
| 372 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, | 389 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, |
| 373 weak_factory_.GetWeakPtr())); | 390 weak_factory_.GetWeakPtr())); |
| 374 } | 391 } |
| 375 | 392 |
| 376 void PresentationServiceImpl::SendSessionMessage( | 393 void PresentationServiceImpl::SendSessionMessage( |
| 377 blink::mojom::PresentationSessionInfoPtr session, | 394 blink::mojom::PresentationSessionInfoPtr session, |
| 378 blink::mojom::SessionMessagePtr session_message, | 395 blink::mojom::SessionMessagePtr session_message, |
| 379 const SendSessionMessageCallback& callback) { | 396 const SendSessionMessageCallback& callback) { |
| 380 DVLOG(2) << "SendSessionMessage"; | 397 DVLOG(2) << "SendSessionMessage"; |
| 381 DCHECK(!session_message.is_null()); | 398 DCHECK(!session_message.is_null()); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 398 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { | 415 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { |
| 399 // It is possible that Reset() is invoked before receiving this callback. | 416 // It is possible that Reset() is invoked before receiving this callback. |
| 400 // So, always check send_message_callback_ for non-null. | 417 // So, always check send_message_callback_ for non-null. |
| 401 if (send_message_callback_) { | 418 if (send_message_callback_) { |
| 402 send_message_callback_->Run(sent); | 419 send_message_callback_->Run(sent); |
| 403 send_message_callback_.reset(); | 420 send_message_callback_.reset(); |
| 404 } | 421 } |
| 405 } | 422 } |
| 406 | 423 |
| 407 void PresentationServiceImpl::CloseConnection( | 424 void PresentationServiceImpl::CloseConnection( |
| 408 const std::string& presentation_url, | 425 url::mojom::UrlPtr presentation_url, |
| 409 const std::string& presentation_id) { | 426 const std::string& presentation_id) { |
| 410 DVLOG(2) << "CloseConnection " << presentation_id; | 427 DVLOG(2) << "CloseConnection " << presentation_id; |
| 411 if (delegate_) | 428 if (delegate_) |
| 412 delegate_->CloseConnection(render_process_id_, render_frame_id_, | 429 delegate_->CloseConnection(render_process_id_, render_frame_id_, |
| 413 presentation_id); | 430 presentation_id); |
| 414 } | 431 } |
| 415 | 432 |
| 416 void PresentationServiceImpl::Terminate(const std::string& presentation_url, | 433 void PresentationServiceImpl::Terminate(url::mojom::UrlPtr presentation_url, |
| 417 const std::string& presentation_id) { | 434 const std::string& presentation_id) { |
| 418 DVLOG(2) << "Terminate " << presentation_id; | 435 DVLOG(2) << "Terminate " << presentation_id; |
| 419 if (delegate_) | 436 if (delegate_) |
| 420 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id); | 437 delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id); |
| 421 } | 438 } |
| 422 | 439 |
| 423 void PresentationServiceImpl::OnConnectionStateChanged( | 440 void PresentationServiceImpl::OnConnectionStateChanged( |
| 424 const PresentationSessionInfo& connection, | 441 const PresentationSessionInfo& connection, |
| 425 const PresentationConnectionStateChangeInfo& info) { | 442 const PresentationConnectionStateChangeInfo& info) { |
| 426 DCHECK(client_.get()); | 443 DCHECK(client_.get()); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 517 << "should've been deleted during RenderFrameDeleted()."; | 534 << "should've been deleted during RenderFrameDeleted()."; |
| 518 Reset(); | 535 Reset(); |
| 519 delete this; | 536 delete this; |
| 520 } | 537 } |
| 521 | 538 |
| 522 void PresentationServiceImpl::Reset() { | 539 void PresentationServiceImpl::Reset() { |
| 523 DVLOG(2) << "PresentationServiceImpl::Reset"; | 540 DVLOG(2) << "PresentationServiceImpl::Reset"; |
| 524 if (delegate_) | 541 if (delegate_) |
| 525 delegate_->Reset(render_process_id_, render_frame_id_); | 542 delegate_->Reset(render_process_id_, render_frame_id_); |
| 526 | 543 |
| 527 default_presentation_url_.clear(); | 544 default_presentation_urls_.clear(); |
| 528 | 545 |
| 529 screen_availability_listeners_.clear(); | 546 screen_availability_listeners_.clear(); |
| 530 | 547 |
| 531 start_session_request_id_ = kInvalidRequestSessionId; | 548 start_session_request_id_ = kInvalidRequestSessionId; |
| 532 pending_start_session_cb_.reset(); | 549 pending_start_session_cb_.reset(); |
| 533 | 550 |
| 534 pending_join_session_cbs_.clear(); | 551 pending_join_session_cbs_.clear(); |
| 535 | 552 |
| 536 if (on_session_messages_callback_.get()) { | 553 if (on_session_messages_callback_.get()) { |
| 537 on_session_messages_callback_->Run( | 554 on_session_messages_callback_->Run( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 575 ~ScreenAvailabilityListenerImpl() { | 592 ~ScreenAvailabilityListenerImpl() { |
| 576 } | 593 } |
| 577 | 594 |
| 578 std::string PresentationServiceImpl::ScreenAvailabilityListenerImpl | 595 std::string PresentationServiceImpl::ScreenAvailabilityListenerImpl |
| 579 ::GetAvailabilityUrl() const { | 596 ::GetAvailabilityUrl() const { |
| 580 return availability_url_; | 597 return availability_url_; |
| 581 } | 598 } |
| 582 | 599 |
| 583 void PresentationServiceImpl::ScreenAvailabilityListenerImpl | 600 void PresentationServiceImpl::ScreenAvailabilityListenerImpl |
| 584 ::OnScreenAvailabilityChanged(bool available) { | 601 ::OnScreenAvailabilityChanged(bool available) { |
| 585 service_->client_->OnScreenAvailabilityUpdated(availability_url_, available); | 602 url::mojom::UrlPtr url = url::mojom::Url::New(); |
| 603 url->url = availability_url_; | |
| 604 service_->client_->OnScreenAvailabilityUpdated(std::move(url), available); | |
|
dcheng
2016/08/26 18:13:33
Since this is typemapped, I think you should be ab
| |
| 586 } | 605 } |
| 587 | 606 |
| 588 void PresentationServiceImpl::ScreenAvailabilityListenerImpl | 607 void PresentationServiceImpl::ScreenAvailabilityListenerImpl |
| 589 ::OnScreenAvailabilityNotSupported() { | 608 ::OnScreenAvailabilityNotSupported() { |
| 590 service_->client_->OnScreenAvailabilityNotSupported(availability_url_); | 609 url::mojom::UrlPtr url = url::mojom::Url::New(); |
| 610 url->url = availability_url_; | |
| 611 service_->client_->OnScreenAvailabilityNotSupported(std::move(url)); | |
| 591 } | 612 } |
| 592 | 613 |
| 593 PresentationServiceImpl::NewSessionCallbackWrapper | 614 PresentationServiceImpl::NewSessionCallbackWrapper |
| 594 ::NewSessionCallbackWrapper(const NewSessionCallback& callback) | 615 ::NewSessionCallbackWrapper(const NewSessionCallback& callback) |
| 595 : callback_(callback) { | 616 : callback_(callback) { |
| 596 } | 617 } |
| 597 | 618 |
| 598 PresentationServiceImpl::NewSessionCallbackWrapper | 619 PresentationServiceImpl::NewSessionCallbackWrapper |
| 599 ::~NewSessionCallbackWrapper() { | 620 ::~NewSessionCallbackWrapper() { |
| 600 if (!callback_.is_null()) | 621 if (!callback_.is_null()) |
| 601 InvokeNewSessionCallbackWithError(callback_); | 622 InvokeNewSessionCallbackWithError(callback_); |
| 602 } | 623 } |
| 603 | 624 |
| 604 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( | 625 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( |
| 605 blink::mojom::PresentationSessionInfoPtr session, | 626 blink::mojom::PresentationSessionInfoPtr session, |
| 606 blink::mojom::PresentationErrorPtr error) { | 627 blink::mojom::PresentationErrorPtr error) { |
| 607 DCHECK(!callback_.is_null()); | 628 DCHECK(!callback_.is_null()); |
| 608 callback_.Run(std::move(session), std::move(error)); | 629 callback_.Run(std::move(session), std::move(error)); |
| 609 callback_.Reset(); | 630 callback_.Reset(); |
| 610 } | 631 } |
| 611 | 632 |
| 612 } // namespace content | 633 } // namespace content |
| OLD | NEW |