| 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 <utility> | 10 #include <utility> |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 } | 174 } |
| 175 | 175 |
| 176 void PresentationServiceImpl::SetClient( | 176 void PresentationServiceImpl::SetClient( |
| 177 blink::mojom::PresentationServiceClientPtr client) { | 177 blink::mojom::PresentationServiceClientPtr client) { |
| 178 DCHECK(!client_.get()); | 178 DCHECK(!client_.get()); |
| 179 // TODO(imcheng): Set ErrorHandler to listen for errors. | 179 // TODO(imcheng): Set ErrorHandler to listen for errors. |
| 180 client_ = std::move(client); | 180 client_ = std::move(client); |
| 181 } | 181 } |
| 182 | 182 |
| 183 void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) { | 183 void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) { |
| 184 DVLOG(2) << "ListenForScreenAvailability " << url; | 184 DVLOG(2) << "ListenForScreenAvailability " << url.spec(); |
| 185 if (!delegate_) { | 185 if (!delegate_) { |
| 186 client_->OnScreenAvailabilityUpdated(url, false); | 186 client_->OnScreenAvailabilityUpdated(url, false); |
| 187 return; | 187 return; |
| 188 } | 188 } |
| 189 | 189 |
| 190 if (screen_availability_listeners_.count(url.spec())) | 190 if (screen_availability_listeners_.count(url)) |
| 191 return; | 191 return; |
| 192 | 192 |
| 193 std::unique_ptr<ScreenAvailabilityListenerImpl> listener( | 193 std::unique_ptr<ScreenAvailabilityListenerImpl> listener( |
| 194 new ScreenAvailabilityListenerImpl(url.spec(), this)); | 194 new ScreenAvailabilityListenerImpl(url, this)); |
| 195 if (delegate_->AddScreenAvailabilityListener( | 195 if (delegate_->AddScreenAvailabilityListener( |
| 196 render_process_id_, | 196 render_process_id_, |
| 197 render_frame_id_, | 197 render_frame_id_, |
| 198 listener.get())) { | 198 listener.get())) { |
| 199 screen_availability_listeners_[url.spec()] = std::move(listener); | 199 screen_availability_listeners_[url] = std::move(listener); |
| 200 } else { | 200 } else { |
| 201 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; | 201 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 | 204 |
| 205 void PresentationServiceImpl::StopListeningForScreenAvailability( | 205 void PresentationServiceImpl::StopListeningForScreenAvailability( |
| 206 const GURL& url) { | 206 const GURL& url) { |
| 207 DVLOG(2) << "StopListeningForScreenAvailability " << url.spec(); | 207 DVLOG(2) << "StopListeningForScreenAvailability " << url.spec(); |
| 208 if (!delegate_) | 208 if (!delegate_) |
| 209 return; | 209 return; |
| 210 | 210 |
| 211 auto listener_it = screen_availability_listeners_.find(url.spec()); | 211 auto listener_it = screen_availability_listeners_.find(url); |
| 212 if (listener_it == screen_availability_listeners_.end()) | 212 if (listener_it == screen_availability_listeners_.end()) |
| 213 return; | 213 return; |
| 214 | 214 |
| 215 delegate_->RemoveScreenAvailabilityListener( | 215 delegate_->RemoveScreenAvailabilityListener( |
| 216 render_process_id_, render_frame_id_, listener_it->second.get()); | 216 render_process_id_, render_frame_id_, listener_it->second.get()); |
| 217 screen_availability_listeners_.erase(listener_it); | 217 screen_availability_listeners_.erase(listener_it); |
| 218 } | 218 } |
| 219 | 219 |
| 220 void PresentationServiceImpl::StartSession( | 220 void PresentationServiceImpl::StartSession( |
| 221 const std::vector<GURL>& presentation_urls, | 221 const std::vector<GURL>& presentation_urls, |
| 222 const NewSessionCallback& callback) { | 222 const NewSessionCallback& callback) { |
| 223 DVLOG(2) << "StartSession"; | 223 DVLOG(2) << "StartSession"; |
| 224 if (!delegate_) { | 224 if (!delegate_) { |
| 225 callback.Run( | 225 callback.Run( |
| 226 blink::mojom::PresentationSessionInfoPtr(), | 226 blink::mojom::PresentationSessionInfoPtr(), |
| 227 blink::mojom::PresentationError::From(PresentationError( | 227 blink::mojom::PresentationError::From(PresentationError( |
| 228 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found."))); | 228 PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, "No screens found."))); |
| 229 return; | 229 return; |
| 230 } | 230 } |
| 231 | 231 |
| 232 // There is a StartSession request in progress. To avoid queueing up | 232 // There is a StartSession request in progress. To avoid queueing up |
| 233 // requests, the incoming request is rejected. | 233 // requests, the incoming request is rejected. |
| 234 if (start_session_request_id_ != kInvalidRequestSessionId) { | 234 if (start_session_request_id_ != kInvalidRequestSessionId) { |
| 235 InvokeNewSessionCallbackWithError(callback); | 235 InvokeNewSessionCallbackWithError(callback); |
| 236 return; | 236 return; |
| 237 } | 237 } |
| 238 | 238 |
| 239 std::vector<std::string> presentation_urls_for_delegate( | |
| 240 presentation_urls.size()); | |
| 241 std::transform(presentation_urls.begin(), presentation_urls.end(), | |
| 242 presentation_urls_for_delegate.begin(), | |
| 243 [](const GURL& url) { return url.spec(); }); | |
| 244 | |
| 245 start_session_request_id_ = GetNextRequestSessionId(); | 239 start_session_request_id_ = GetNextRequestSessionId(); |
| 246 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback)); | 240 pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback)); |
| 247 delegate_->StartSession( | 241 delegate_->StartSession( |
| 248 render_process_id_, render_frame_id_, presentation_urls_for_delegate, | 242 render_process_id_, render_frame_id_, presentation_urls, |
| 249 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded, | 243 base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded, |
| 250 weak_factory_.GetWeakPtr(), start_session_request_id_), | 244 weak_factory_.GetWeakPtr(), start_session_request_id_), |
| 251 base::Bind(&PresentationServiceImpl::OnStartSessionError, | 245 base::Bind(&PresentationServiceImpl::OnStartSessionError, |
| 252 weak_factory_.GetWeakPtr(), start_session_request_id_)); | 246 weak_factory_.GetWeakPtr(), start_session_request_id_)); |
| 253 } | 247 } |
| 254 | 248 |
| 255 void PresentationServiceImpl::JoinSession( | 249 void PresentationServiceImpl::JoinSession( |
| 256 const std::vector<GURL>& presentation_urls, | 250 const std::vector<GURL>& presentation_urls, |
| 257 const base::Optional<std::string>& presentation_id, | 251 const base::Optional<std::string>& presentation_id, |
| 258 const NewSessionCallback& callback) { | 252 const NewSessionCallback& callback) { |
| 259 DVLOG(2) << "JoinSession"; | 253 DVLOG(2) << "JoinSession"; |
| 260 if (!delegate_) { | 254 if (!delegate_) { |
| 261 callback.Run(blink::mojom::PresentationSessionInfoPtr(), | 255 callback.Run(blink::mojom::PresentationSessionInfoPtr(), |
| 262 blink::mojom::PresentationError::From(PresentationError( | 256 blink::mojom::PresentationError::From(PresentationError( |
| 263 PRESENTATION_ERROR_NO_PRESENTATION_FOUND, | 257 PRESENTATION_ERROR_NO_PRESENTATION_FOUND, |
| 264 "Error joining route: No matching route"))); | 258 "Error joining route: No matching route"))); |
| 265 return; | 259 return; |
| 266 } | 260 } |
| 267 | 261 |
| 268 std::vector<std::string> presentation_urls_for_delegate( | |
| 269 presentation_urls.size()); | |
| 270 std::transform(presentation_urls.begin(), presentation_urls.end(), | |
| 271 presentation_urls_for_delegate.begin(), | |
| 272 [](const GURL& url) { return url.spec(); }); | |
| 273 | |
| 274 int request_session_id = RegisterJoinSessionCallback(callback); | 262 int request_session_id = RegisterJoinSessionCallback(callback); |
| 275 if (request_session_id == kInvalidRequestSessionId) { | 263 if (request_session_id == kInvalidRequestSessionId) { |
| 276 InvokeNewSessionCallbackWithError(callback); | 264 InvokeNewSessionCallbackWithError(callback); |
| 277 return; | 265 return; |
| 278 } | 266 } |
| 279 delegate_->JoinSession( | 267 delegate_->JoinSession( |
| 280 render_process_id_, render_frame_id_, presentation_urls_for_delegate, | 268 render_process_id_, render_frame_id_, presentation_urls, |
| 281 presentation_id.value_or(std::string()), | 269 presentation_id.value_or(std::string()), |
| 282 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded, | 270 base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded, |
| 283 weak_factory_.GetWeakPtr(), request_session_id), | 271 weak_factory_.GetWeakPtr(), request_session_id), |
| 284 base::Bind(&PresentationServiceImpl::OnJoinSessionError, | 272 base::Bind(&PresentationServiceImpl::OnJoinSessionError, |
| 285 weak_factory_.GetWeakPtr(), request_session_id)); | 273 weak_factory_.GetWeakPtr(), request_session_id)); |
| 286 } | 274 } |
| 287 | 275 |
| 288 int PresentationServiceImpl::RegisterJoinSessionCallback( | 276 int PresentationServiceImpl::RegisterJoinSessionCallback( |
| 289 const NewSessionCallback& callback) { | 277 const NewSessionCallback& callback) { |
| 290 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests) | 278 if (pending_join_session_cbs_.size() >= kMaxNumQueuedSessionRequests) |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 pending_join_session_cbs_.erase(it); | 354 pending_join_session_cbs_.erase(it); |
| 367 return true; | 355 return true; |
| 368 } | 356 } |
| 369 | 357 |
| 370 void PresentationServiceImpl::SetDefaultPresentationUrls( | 358 void PresentationServiceImpl::SetDefaultPresentationUrls( |
| 371 const std::vector<GURL>& presentation_urls) { | 359 const std::vector<GURL>& presentation_urls) { |
| 372 DVLOG(2) << "SetDefaultPresentationUrls"; | 360 DVLOG(2) << "SetDefaultPresentationUrls"; |
| 373 if (!delegate_) | 361 if (!delegate_) |
| 374 return; | 362 return; |
| 375 | 363 |
| 376 std::vector<std::string> presentation_urls_for_delegate( | 364 if (default_presentation_urls_ == presentation_urls) |
| 377 presentation_urls.size()); | |
| 378 std::transform(presentation_urls.begin(), presentation_urls.end(), | |
| 379 presentation_urls_for_delegate.begin(), | |
| 380 [](const GURL& url) { return url.spec(); }); | |
| 381 | |
| 382 if (default_presentation_urls_ == presentation_urls_for_delegate) | |
| 383 return; | 365 return; |
| 384 | 366 |
| 385 default_presentation_urls_ = presentation_urls_for_delegate; | 367 default_presentation_urls_ = presentation_urls; |
| 386 delegate_->SetDefaultPresentationUrls( | 368 delegate_->SetDefaultPresentationUrls( |
| 387 render_process_id_, render_frame_id_, presentation_urls_for_delegate, | 369 render_process_id_, render_frame_id_, presentation_urls, |
| 388 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, | 370 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, |
| 389 weak_factory_.GetWeakPtr())); | 371 weak_factory_.GetWeakPtr())); |
| 390 } | 372 } |
| 391 | 373 |
| 392 void PresentationServiceImpl::SendSessionMessage( | 374 void PresentationServiceImpl::SendSessionMessage( |
| 393 blink::mojom::PresentationSessionInfoPtr session, | 375 blink::mojom::PresentationSessionInfoPtr session, |
| 394 blink::mojom::SessionMessagePtr session_message, | 376 blink::mojom::SessionMessagePtr session_message, |
| 395 const SendSessionMessageCallback& callback) { | 377 const SendSessionMessageCallback& callback) { |
| 396 DVLOG(2) << "SendSessionMessage"; | 378 DVLOG(2) << "SendSessionMessage"; |
| 397 DCHECK(!session_message.is_null()); | 379 DCHECK(!session_message.is_null()); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 } | 552 } |
| 571 | 553 |
| 572 void PresentationServiceImpl::OnDefaultPresentationStarted( | 554 void PresentationServiceImpl::OnDefaultPresentationStarted( |
| 573 const PresentationSessionInfo& connection) { | 555 const PresentationSessionInfo& connection) { |
| 574 DCHECK(client_.get()); | 556 DCHECK(client_.get()); |
| 575 client_->OnDefaultSessionStarted( | 557 client_->OnDefaultSessionStarted( |
| 576 blink::mojom::PresentationSessionInfo::From(connection)); | 558 blink::mojom::PresentationSessionInfo::From(connection)); |
| 577 ListenForConnectionStateChange(connection); | 559 ListenForConnectionStateChange(connection); |
| 578 } | 560 } |
| 579 | 561 |
| 580 PresentationServiceImpl::ScreenAvailabilityListenerImpl | 562 PresentationServiceImpl::ScreenAvailabilityListenerImpl:: |
| 581 ::ScreenAvailabilityListenerImpl( | 563 ScreenAvailabilityListenerImpl(const GURL& availability_url, |
| 582 const std::string& availability_url, | 564 PresentationServiceImpl* service) |
| 583 PresentationServiceImpl* service) | 565 : availability_url_(availability_url), service_(service) { |
| 584 : availability_url_(availability_url), | |
| 585 service_(service) { | |
| 586 DCHECK(service_); | 566 DCHECK(service_); |
| 587 DCHECK(service_->client_.get()); | 567 DCHECK(service_->client_.get()); |
| 588 } | 568 } |
| 589 | 569 |
| 590 PresentationServiceImpl::ScreenAvailabilityListenerImpl:: | 570 PresentationServiceImpl::ScreenAvailabilityListenerImpl:: |
| 591 ~ScreenAvailabilityListenerImpl() { | 571 ~ScreenAvailabilityListenerImpl() { |
| 592 } | 572 } |
| 593 | 573 |
| 594 std::string PresentationServiceImpl::ScreenAvailabilityListenerImpl | 574 GURL PresentationServiceImpl::ScreenAvailabilityListenerImpl:: |
| 595 ::GetAvailabilityUrl() const { | 575 GetAvailabilityUrl() const { |
| 596 return availability_url_; | 576 return availability_url_; |
| 597 } | 577 } |
| 598 | 578 |
| 599 void PresentationServiceImpl::ScreenAvailabilityListenerImpl | 579 void PresentationServiceImpl::ScreenAvailabilityListenerImpl |
| 600 ::OnScreenAvailabilityChanged(bool available) { | 580 ::OnScreenAvailabilityChanged(bool available) { |
| 601 service_->client_->OnScreenAvailabilityUpdated(GURL(availability_url_), | 581 service_->client_->OnScreenAvailabilityUpdated(availability_url_, available); |
| 602 available); | |
| 603 } | 582 } |
| 604 | 583 |
| 605 void PresentationServiceImpl::ScreenAvailabilityListenerImpl | 584 void PresentationServiceImpl::ScreenAvailabilityListenerImpl |
| 606 ::OnScreenAvailabilityNotSupported() { | 585 ::OnScreenAvailabilityNotSupported() { |
| 607 service_->client_->OnScreenAvailabilityNotSupported(GURL(availability_url_)); | 586 service_->client_->OnScreenAvailabilityNotSupported(availability_url_); |
| 608 } | 587 } |
| 609 | 588 |
| 610 PresentationServiceImpl::NewSessionCallbackWrapper | 589 PresentationServiceImpl::NewSessionCallbackWrapper |
| 611 ::NewSessionCallbackWrapper(const NewSessionCallback& callback) | 590 ::NewSessionCallbackWrapper(const NewSessionCallback& callback) |
| 612 : callback_(callback) { | 591 : callback_(callback) { |
| 613 } | 592 } |
| 614 | 593 |
| 615 PresentationServiceImpl::NewSessionCallbackWrapper | 594 PresentationServiceImpl::NewSessionCallbackWrapper |
| 616 ::~NewSessionCallbackWrapper() { | 595 ::~NewSessionCallbackWrapper() { |
| 617 if (!callback_.is_null()) | 596 if (!callback_.is_null()) |
| 618 InvokeNewSessionCallbackWithError(callback_); | 597 InvokeNewSessionCallbackWithError(callback_); |
| 619 } | 598 } |
| 620 | 599 |
| 621 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( | 600 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( |
| 622 blink::mojom::PresentationSessionInfoPtr session, | 601 blink::mojom::PresentationSessionInfoPtr session, |
| 623 blink::mojom::PresentationErrorPtr error) { | 602 blink::mojom::PresentationErrorPtr error) { |
| 624 DCHECK(!callback_.is_null()); | 603 DCHECK(!callback_.is_null()); |
| 625 callback_.Run(std::move(session), std::move(error)); | 604 callback_.Run(std::move(session), std::move(error)); |
| 626 callback_.Reset(); | 605 callback_.Reset(); |
| 627 } | 606 } |
| 628 | 607 |
| 629 } // namespace content | 608 } // namespace content |
| OLD | NEW |