| 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 "chrome/browser/media/router/presentation_service_delegate_impl.h" | 5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/containers/scoped_ptr_hash_map.h" | 9 #include "base/containers/scoped_ptr_hash_map.h" |
| 10 #include "base/containers/small_map.h" | 10 #include "base/containers/small_map.h" |
| 11 #include "base/guid.h" | 11 #include "base/guid.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "chrome/browser/media/router/create_session_request.h" | 14 #include "chrome/browser/media/router/create_presentation_session_request.h" |
| 15 #include "chrome/browser/media/router/media_route.h" | 15 #include "chrome/browser/media/router/media_route.h" |
| 16 #include "chrome/browser/media/router/media_router.h" | 16 #include "chrome/browser/media/router/media_router.h" |
| 17 #include "chrome/browser/media/router/media_router_mojo_impl.h" | 17 #include "chrome/browser/media/router/media_router_mojo_impl.h" |
| 18 #include "chrome/browser/media/router/media_router_mojo_impl_factory.h" | 18 #include "chrome/browser/media/router/media_router_mojo_impl_factory.h" |
| 19 #include "chrome/browser/media/router/media_sink.h" | 19 #include "chrome/browser/media/router/media_sink.h" |
| 20 #include "chrome/browser/media/router/media_source_helper.h" | 20 #include "chrome/browser/media/router/media_source_helper.h" |
| 21 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" | 21 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" |
| 22 #include "chrome/browser/media/router/presentation_session_state_observer.h" | 22 #include "chrome/browser/media/router/presentation_session_state_observer.h" |
| 23 #include "chrome/browser/sessions/session_tab_helper.h" | 23 #include "chrome/browser/sessions/session_tab_helper.h" |
| 24 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller.h" | 24 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller.h" |
| (...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 OnDefaultMediaSourceChanged(default_source_, default_frame_url_)); | 536 OnDefaultMediaSourceChanged(default_source_, default_frame_url_)); |
| 537 } | 537 } |
| 538 } | 538 } |
| 539 | 539 |
| 540 void PresentationServiceDelegateImpl::OnJoinRouteResponse( | 540 void PresentationServiceDelegateImpl::OnJoinRouteResponse( |
| 541 int render_process_id, | 541 int render_process_id, |
| 542 int render_frame_id, | 542 int render_frame_id, |
| 543 const content::PresentationSessionInfo& session, | 543 const content::PresentationSessionInfo& session, |
| 544 const PresentationSessionSuccessCallback& success_cb, | 544 const PresentationSessionSuccessCallback& success_cb, |
| 545 const PresentationSessionErrorCallback& error_cb, | 545 const PresentationSessionErrorCallback& error_cb, |
| 546 scoped_ptr<MediaRoute> route, | 546 const MediaRoute* route, |
| 547 const std::string& error_text) { | 547 const std::string& error_text) { |
| 548 if (!route.get()) { | 548 if (!route) { |
| 549 error_cb.Run(content::PresentationError( | 549 error_cb.Run(content::PresentationError( |
| 550 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text)); | 550 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text)); |
| 551 } else { | 551 } else { |
| 552 DVLOG(1) << "OnJoinRouteResponse: " | 552 DVLOG(1) << "OnJoinRouteResponse: " |
| 553 << "route_id: " << route->media_route_id() | 553 << "route_id: " << route->media_route_id() |
| 554 << ", presentation URL: " << session.presentation_url | 554 << ", presentation URL: " << session.presentation_url |
| 555 << ", presentation ID: " << session.presentation_id; | 555 << ", presentation ID: " << session.presentation_id; |
| 556 frame_manager_->OnPresentationSessionStarted( | 556 frame_manager_->OnPresentationSessionStarted( |
| 557 RenderFrameHostId(render_process_id, render_frame_id), false, session, | 557 RenderFrameHostId(render_process_id, render_frame_id), false, session, |
| 558 route->media_route_id()); | 558 route->media_route_id()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 589 } | 589 } |
| 590 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 590 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 591 | 591 |
| 592 // TODO(mlamouri,avayvod): don't use the default presentation id provided by | 592 // TODO(mlamouri,avayvod): don't use the default presentation id provided by |
| 593 // the frame when we implement the new default presentation model. | 593 // the frame when we implement the new default presentation model. |
| 594 std::string presentation_id = | 594 std::string presentation_id = |
| 595 frame_manager_->GetDefaultPresentationId(render_frame_host_id); | 595 frame_manager_->GetDefaultPresentationId(render_frame_host_id); |
| 596 if (presentation_id.empty()) | 596 if (presentation_id.empty()) |
| 597 presentation_id = base::GenerateGUID(); | 597 presentation_id = base::GenerateGUID(); |
| 598 | 598 |
| 599 scoped_ptr<CreateSessionRequest> context(new CreateSessionRequest( | 599 scoped_ptr<CreatePresentationSessionRequest> context( |
| 600 presentation_url, presentation_id, | 600 new CreatePresentationSessionRequest( |
| 601 GetLastCommittedURLForFrame(render_frame_host_id), | 601 presentation_url, presentation_id, |
| 602 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, | 602 GetLastCommittedURLForFrame(render_frame_host_id), |
| 603 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, | 603 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, |
| 604 success_cb), | 604 weak_factory_.GetWeakPtr(), render_process_id, |
| 605 error_cb)); | 605 render_frame_id, success_cb), |
| 606 error_cb)); |
| 606 // NOTE: Currently this request is ignored if a dialog is already open, e.g. | 607 // NOTE: Currently this request is ignored if a dialog is already open, e.g. |
| 607 // via browser action. In practice, this should rarely happen, but log | 608 // via browser action. In practice, this should rarely happen, but log |
| 608 // an error message in case it does. | 609 // an error message in case it does. |
| 609 MediaRouterDialogController::CreateForWebContents(web_contents_); | 610 MediaRouterDialogController::CreateForWebContents(web_contents_); |
| 610 MediaRouterDialogController* controller = | 611 MediaRouterDialogController* controller = |
| 611 MediaRouterDialogController::FromWebContents(web_contents_); | 612 MediaRouterDialogController::FromWebContents(web_contents_); |
| 612 | 613 |
| 613 if (!controller->ShowMediaRouterDialogForPresentation(context.Pass())) { | 614 if (!controller->ShowMediaRouterDialogForPresentation(context.Pass())) { |
| 614 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; | 615 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; |
| 615 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 616 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 616 "Unable to create dialog.")); | 617 "Unable to create dialog.")); |
| 617 return; | 618 return; |
| 618 } | 619 } |
| 619 } | 620 } |
| 620 | 621 |
| 621 void PresentationServiceDelegateImpl::JoinSession( | 622 void PresentationServiceDelegateImpl::JoinSession( |
| 622 int render_process_id, | 623 int render_process_id, |
| 623 int render_frame_id, | 624 int render_frame_id, |
| 624 const std::string& presentation_url, | 625 const std::string& presentation_url, |
| 625 const std::string& presentation_id, | 626 const std::string& presentation_id, |
| 626 const PresentationSessionSuccessCallback& success_cb, | 627 const PresentationSessionSuccessCallback& success_cb, |
| 627 const PresentationSessionErrorCallback& error_cb) { | 628 const PresentationSessionErrorCallback& error_cb) { |
| 629 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 630 route_response_callbacks.push_back(base::Bind( |
| 631 &PresentationServiceDelegateImpl::OnJoinRouteResponse, |
| 632 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, |
| 633 content::PresentationSessionInfo(presentation_url, presentation_id), |
| 634 success_cb, error_cb)); |
| 628 router_->JoinRoute( | 635 router_->JoinRoute( |
| 629 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, | 636 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, |
| 630 GetLastCommittedURLForFrame( | 637 GetLastCommittedURLForFrame( |
| 631 RenderFrameHostId(render_process_id, render_frame_id)).GetOrigin(), | 638 RenderFrameHostId(render_process_id, render_frame_id)) |
| 632 SessionTabHelper::IdForTab(web_contents_), | 639 .GetOrigin(), |
| 633 base::Bind( | 640 SessionTabHelper::IdForTab(web_contents_), route_response_callbacks); |
| 634 &PresentationServiceDelegateImpl::OnJoinRouteResponse, | |
| 635 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, | |
| 636 content::PresentationSessionInfo(presentation_url, presentation_id), | |
| 637 success_cb, error_cb)); | |
| 638 } | 641 } |
| 639 | 642 |
| 640 void PresentationServiceDelegateImpl::CloseSession( | 643 void PresentationServiceDelegateImpl::CloseSession( |
| 641 int render_process_id, | 644 int render_process_id, |
| 642 int render_frame_id, | 645 int render_frame_id, |
| 643 const std::string& presentation_id) { | 646 const std::string& presentation_id) { |
| 644 const MediaRoute::Id& route_id = frame_manager_->GetRouteId( | 647 const MediaRoute::Id& route_id = frame_manager_->GetRouteId( |
| 645 RenderFrameHostId(render_process_id, render_frame_id), presentation_id); | 648 RenderFrameHostId(render_process_id, render_frame_id), presentation_id); |
| 646 if (route_id.empty()) { | 649 if (route_id.empty()) { |
| 647 DVLOG(1) << "No active route for: " << presentation_id; | 650 DVLOG(1) << "No active route for: " << presentation_id; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 } | 692 } |
| 690 | 693 |
| 691 void PresentationServiceDelegateImpl::ListenForSessionStateChange( | 694 void PresentationServiceDelegateImpl::ListenForSessionStateChange( |
| 692 int render_process_id, | 695 int render_process_id, |
| 693 int render_frame_id, | 696 int render_frame_id, |
| 694 const content::SessionStateChangedCallback& state_changed_cb) { | 697 const content::SessionStateChangedCallback& state_changed_cb) { |
| 695 frame_manager_->ListenForSessionStateChange( | 698 frame_manager_->ListenForSessionStateChange( |
| 696 RenderFrameHostId(render_process_id, render_frame_id), state_changed_cb); | 699 RenderFrameHostId(render_process_id, render_frame_id), state_changed_cb); |
| 697 } | 700 } |
| 698 | 701 |
| 699 void PresentationServiceDelegateImpl::OnRouteCreated(const MediaRoute& route) { | 702 void PresentationServiceDelegateImpl::OnRouteResponse( |
| 700 const MediaSource& source = route.media_source(); | 703 const MediaRoute* route, |
| 704 const std::string& error) { |
| 705 if (!route) |
| 706 return; |
| 707 const MediaSource& source = route->media_source(); |
| 701 DCHECK(!source.Empty()); | 708 DCHECK(!source.Empty()); |
| 702 if (!default_source_.Equals(source)) | 709 if (!default_source_.Equals(source)) |
| 703 return; | 710 return; |
| 704 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); | 711 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); |
| 705 if (!main_frame) | 712 if (!main_frame) |
| 706 return; | 713 return; |
| 707 RenderFrameHostId render_frame_host_id(GetRenderFrameHostId(main_frame)); | 714 RenderFrameHostId render_frame_host_id(GetRenderFrameHostId(main_frame)); |
| 708 std::string presentation_id = | 715 std::string presentation_id = |
| 709 GetPresentationIdAndUrl(route.media_route_id()).first; | 716 GetPresentationIdAndUrl(route->media_route_id()).first; |
| 710 frame_manager_->OnPresentationSessionStarted( | 717 frame_manager_->OnPresentationSessionStarted( |
| 711 render_frame_host_id, true, | 718 render_frame_host_id, true, |
| 712 content::PresentationSessionInfo(PresentationUrlFromMediaSource(source), | 719 content::PresentationSessionInfo(PresentationUrlFromMediaSource(source), |
| 713 presentation_id), | 720 presentation_id), |
| 714 route.media_route_id()); | 721 route->media_route_id()); |
| 715 } | 722 } |
| 716 | 723 |
| 717 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver( | 724 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver( |
| 718 DefaultMediaSourceObserver* observer) { | 725 DefaultMediaSourceObserver* observer) { |
| 719 default_media_source_observers_.AddObserver(observer); | 726 default_media_source_observers_.AddObserver(observer); |
| 720 } | 727 } |
| 721 | 728 |
| 722 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver( | 729 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver( |
| 723 DefaultMediaSourceObserver* observer) { | 730 DefaultMediaSourceObserver* observer) { |
| 724 default_media_source_observers_.RemoveObserver(observer); | 731 default_media_source_observers_.RemoveObserver(observer); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 738 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 745 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
| 739 int render_process_id, | 746 int render_process_id, |
| 740 int render_frame_id, | 747 int render_frame_id, |
| 741 const MediaSource::Id& source_id) const { | 748 const MediaSource::Id& source_id) const { |
| 742 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 749 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 743 return frame_manager_->HasScreenAvailabilityListenerForTest( | 750 return frame_manager_->HasScreenAvailabilityListenerForTest( |
| 744 render_frame_host_id, source_id); | 751 render_frame_host_id, source_id); |
| 745 } | 752 } |
| 746 | 753 |
| 747 } // namespace media_router | 754 } // namespace media_router |
| OLD | NEW |