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

Side by Side Diff: chrome/browser/media/router/presentation_service_delegate_impl.cc

Issue 2477573002: [Presentation API] (3rd) (1-UA) Split PresentationServiceDelegateImpl(PSDImpl) (Closed)
Patch Set: resolve code review comments from Derek Created 4 years 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 "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 #include <unordered_map> 8 #include <unordered_map>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/containers/small_map.h" 12 #include "base/containers/small_map.h"
13 #include "base/guid.h" 13 #include "base/guid.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "chrome/browser/media/router/create_presentation_connection_request.h" 17 #include "chrome/browser/media/router/create_presentation_connection_request.h"
18 #include "chrome/browser/media/router/media_route.h" 18 #include "chrome/browser/media/router/media_route.h"
19 #include "chrome/browser/media/router/media_router.h" 19 #include "chrome/browser/media/router/media_router.h"
20 #include "chrome/browser/media/router/media_router_dialog_controller.h" 20 #include "chrome/browser/media/router/media_router_dialog_controller.h"
21 #include "chrome/browser/media/router/media_router_factory.h" 21 #include "chrome/browser/media/router/media_router_factory.h"
22 #include "chrome/browser/media/router/media_sink.h" 22 #include "chrome/browser/media/router/media_sink.h"
23 #include "chrome/browser/media/router/media_source_helper.h" 23 #include "chrome/browser/media/router/media_source_helper.h"
24 #include "chrome/browser/media/router/offscreen_presentation_manager.h"
25 #include "chrome/browser/media/router/offscreen_presentation_manager_factory.h"
24 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" 26 #include "chrome/browser/media/router/presentation_media_sinks_observer.h"
25 #include "chrome/browser/media/router/route_message.h" 27 #include "chrome/browser/media/router/route_message.h"
26 #include "chrome/browser/media/router/route_message_observer.h" 28 #include "chrome/browser/media/router/route_message_observer.h"
27 #include "chrome/browser/media/router/route_request_result.h" 29 #include "chrome/browser/media/router/route_request_result.h"
28 #include "chrome/browser/sessions/session_tab_helper.h" 30 #include "chrome/browser/sessions/session_tab_helper.h"
29 #include "content/public/browser/browser_context.h" 31 #include "content/public/browser/browser_context.h"
30 #include "content/public/browser/presentation_screen_availability_listener.h" 32 #include "content/public/browser/presentation_screen_availability_listener.h"
31 #include "content/public/browser/presentation_session.h" 33 #include "content/public/browser/presentation_session.h"
32 #include "content/public/browser/render_frame_host.h" 34 #include "content/public/browser/render_frame_host.h"
33 #include "content/public/browser/render_process_host.h" 35 #include "content/public/browser/render_process_host.h"
36 #include "url/gurl.h"
34 37
35 DEFINE_WEB_CONTENTS_USER_DATA_KEY( 38 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
36 media_router::PresentationServiceDelegateImpl); 39 media_router::PresentationServiceDelegateImpl);
37 40
38 using content::RenderFrameHost; 41 using content::RenderFrameHost;
39 42
40 namespace media_router { 43 namespace media_router {
41 44
42 namespace { 45 namespace {
43 46
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 void Reset(); 141 void Reset();
139 void RemoveConnection(const std::string& presentation_id, 142 void RemoveConnection(const std::string& presentation_id,
140 const MediaRoute::Id& route_id); 143 const MediaRoute::Id& route_id);
141 144
142 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; 145 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const;
143 const std::vector<MediaRoute::Id> GetRouteIds() const; 146 const std::vector<MediaRoute::Id> GetRouteIds() const;
144 147
145 void OnPresentationSessionStarted( 148 void OnPresentationSessionStarted(
146 const content::PresentationSessionInfo& session, 149 const content::PresentationSessionInfo& session,
147 const MediaRoute::Id& route_id); 150 const MediaRoute::Id& route_id);
148 void OnPresentationServiceDelegateDestroyed() const;
149
150 void set_delegate_observer(DelegateObserver* observer) {
151 delegate_observer_ = observer;
152 }
153 151
154 private: 152 private:
155 MediaSource GetMediaSourceFromListener( 153 MediaSource GetMediaSourceFromListener(
156 content::PresentationScreenAvailabilityListener* listener) const; 154 content::PresentationScreenAvailabilityListener* listener) const;
157 base::SmallMap<std::map<std::string, MediaRoute::Id>> 155 base::SmallMap<std::map<std::string, MediaRoute::Id>>
158 presentation_id_to_route_id_; 156 presentation_id_to_route_id_;
159 base::SmallMap< 157 base::SmallMap<
160 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> 158 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>>
161 url_to_sinks_observer_; 159 url_to_sinks_observer_;
162 std::unordered_map< 160 std::unordered_map<
163 MediaRoute::Id, 161 MediaRoute::Id,
164 std::unique_ptr<PresentationConnectionStateSubscription>> 162 std::unique_ptr<PresentationConnectionStateSubscription>>
165 connection_state_subscriptions_; 163 connection_state_subscriptions_;
166 std::unordered_map< 164 std::unordered_map<
167 MediaRoute::Id, 165 MediaRoute::Id,
168 std::unique_ptr<PresentationSessionMessagesObserver>> 166 std::unique_ptr<PresentationSessionMessagesObserver>>
169 session_messages_observers_; 167 session_messages_observers_;
170 168
171 RenderFrameHostId render_frame_host_id_; 169 RenderFrameHostId render_frame_host_id_;
172 170
173 // References to the owning WebContents, and the corresponding MediaRouter. 171 // References to the owning WebContents, and the corresponding MediaRouter.
174 const content::WebContents* web_contents_; 172 const content::WebContents* web_contents_;
175 MediaRouter* router_; 173 MediaRouter* router_;
176
177 DelegateObserver* delegate_observer_;
178 }; 174 };
179 175
180 PresentationFrame::PresentationFrame( 176 PresentationFrame::PresentationFrame(
181 const RenderFrameHostId& render_frame_host_id, 177 const RenderFrameHostId& render_frame_host_id,
182 content::WebContents* web_contents, 178 content::WebContents* web_contents,
183 MediaRouter* router) 179 MediaRouter* router)
184 : render_frame_host_id_(render_frame_host_id), 180 : render_frame_host_id_(render_frame_host_id),
185 web_contents_(web_contents), 181 web_contents_(web_contents),
186 router_(router), 182 router_(router) {
187 delegate_observer_(nullptr) {
188 DCHECK(web_contents_); 183 DCHECK(web_contents_);
189 DCHECK(router_); 184 DCHECK(router_);
190 } 185 }
191 186
192 PresentationFrame::~PresentationFrame() { 187 PresentationFrame::~PresentationFrame() {
193 } 188 }
194 189
195 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const {
196 if (delegate_observer_)
197 delegate_observer_->OnDelegateDestroyed();
198 }
199
200 void PresentationFrame::OnPresentationSessionStarted( 190 void PresentationFrame::OnPresentationSessionStarted(
201 const content::PresentationSessionInfo& session, 191 const content::PresentationSessionInfo& session,
202 const MediaRoute::Id& route_id) { 192 const MediaRoute::Id& route_id) {
203 presentation_id_to_route_id_[session.presentation_id] = route_id; 193 presentation_id_to_route_id_[session.presentation_id] = route_id;
204 } 194 }
205 195
206 const MediaRoute::Id PresentationFrame::GetRouteId( 196 const MediaRoute::Id PresentationFrame::GetRouteId(
207 const std::string& presentation_id) const { 197 const std::string& presentation_id) const {
208 auto it = presentation_id_to_route_id_.find(presentation_id); 198 auto it = presentation_id_to_route_id_.find(presentation_id);
209 return it != presentation_id_to_route_id_.end() ? it->second : ""; 199 return it != presentation_id_to_route_id_.end() ? it->second : "";
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 }; 430 };
441 431
442 PresentationFrameManager::PresentationFrameManager( 432 PresentationFrameManager::PresentationFrameManager(
443 content::WebContents* web_contents, 433 content::WebContents* web_contents,
444 MediaRouter* router) 434 MediaRouter* router)
445 : router_(router), web_contents_(web_contents) { 435 : router_(router), web_contents_(web_contents) {
446 DCHECK(web_contents_); 436 DCHECK(web_contents_);
447 DCHECK(router_); 437 DCHECK(router_);
448 } 438 }
449 439
450 PresentationFrameManager::~PresentationFrameManager() { 440 PresentationFrameManager::~PresentationFrameManager() {}
451 for (auto& frame : presentation_frames_)
452 frame.second->OnPresentationServiceDelegateDestroyed();
453 }
454 441
455 void PresentationFrameManager::OnPresentationSessionStarted( 442 void PresentationFrameManager::OnPresentationSessionStarted(
456 const RenderFrameHostId& render_frame_host_id, 443 const RenderFrameHostId& render_frame_host_id,
457 const content::PresentationSessionInfo& session, 444 const content::PresentationSessionInfo& session,
458 const MediaRoute::Id& route_id) { 445 const MediaRoute::Id& route_id) {
459 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); 446 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
460 presentation_frame->OnPresentationSessionStarted(session, route_id); 447 presentation_frame->OnPresentationSessionStarted(session, route_id);
461 } 448 }
462 449
463 void PresentationFrameManager::OnDefaultPresentationSessionStarted( 450 void PresentationFrameManager::OnDefaultPresentationSessionStarted(
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 DCHECK(!callback.is_null()); 538 DCHECK(!callback.is_null());
552 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); 539 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id));
553 PresentationRequest request(render_frame_host_id, 540 PresentationRequest request(render_frame_host_id,
554 std::vector<GURL>({default_presentation_url}), 541 std::vector<GURL>({default_presentation_url}),
555 frame_url); 542 frame_url);
556 default_presentation_started_callback_ = callback; 543 default_presentation_started_callback_ = callback;
557 SetDefaultPresentationRequest(request); 544 SetDefaultPresentationRequest(request);
558 } 545 }
559 } 546 }
560 547
561 void PresentationFrameManager::AddDelegateObserver(
562 const RenderFrameHostId& render_frame_host_id,
563 DelegateObserver* observer) {
564 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
565 presentation_frame->set_delegate_observer(observer);
566 }
567
568 void PresentationFrameManager::RemoveDelegateObserver(
569 const RenderFrameHostId& render_frame_host_id) {
570 const auto it = presentation_frames_.find(render_frame_host_id);
571 if (it != presentation_frames_.end()) {
572 it->second->set_delegate_observer(nullptr);
573 presentation_frames_.erase(it);
574 }
575 }
576
577 void PresentationFrameManager::AddDefaultPresentationRequestObserver( 548 void PresentationFrameManager::AddDefaultPresentationRequestObserver(
578 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* 549 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
579 observer) { 550 observer) {
580 default_presentation_request_observers_.AddObserver(observer); 551 default_presentation_request_observers_.AddObserver(observer);
581 } 552 }
582 553
583 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver( 554 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver(
584 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* 555 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
585 observer) { 556 observer) {
586 default_presentation_request_observers_.RemoveObserver(observer); 557 default_presentation_request_observers_.RemoveObserver(observer);
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 web_contents_->GetBrowserContext())), 638 web_contents_->GetBrowserContext())),
668 frame_manager_(new PresentationFrameManager(web_contents, router_)), 639 frame_manager_(new PresentationFrameManager(web_contents, router_)),
669 weak_factory_(this) { 640 weak_factory_(this) {
670 DCHECK(web_contents_); 641 DCHECK(web_contents_);
671 DCHECK(router_); 642 DCHECK(router_);
672 } 643 }
673 644
674 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() { 645 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() {
675 } 646 }
676 647
677 void PresentationServiceDelegateImpl::AddObserver(int render_process_id,
678 int render_frame_id,
679 DelegateObserver* observer) {
680 DCHECK(observer);
681 frame_manager_->AddDelegateObserver(
682 RenderFrameHostId(render_process_id, render_frame_id), observer);
683 }
684
685 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id,
686 int render_frame_id) {
687 frame_manager_->RemoveDelegateObserver(
688 RenderFrameHostId(render_process_id, render_frame_id));
689 }
690
691 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener( 648 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener(
692 int render_process_id, 649 int render_process_id,
693 int render_frame_id, 650 int render_frame_id,
694 content::PresentationScreenAvailabilityListener* listener) { 651 content::PresentationScreenAvailabilityListener* listener) {
695 DCHECK(listener); 652 DCHECK(listener);
696 return frame_manager_->SetScreenAvailabilityListener( 653 return frame_manager_->SetScreenAvailabilityListener(
697 RenderFrameHostId(render_process_id, render_frame_id), listener); 654 RenderFrameHostId(render_process_id, render_frame_id), listener);
698 } 655 }
699 656
700 void PresentationServiceDelegateImpl::RemoveScreenAvailabilityListener( 657 void PresentationServiceDelegateImpl::RemoveScreenAvailabilityListener(
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
911 int render_process_id, 868 int render_process_id,
912 int render_frame_id, 869 int render_frame_id,
913 const content::PresentationSessionInfo& connection, 870 const content::PresentationSessionInfo& connection,
914 const content::PresentationConnectionStateChangedCallback& 871 const content::PresentationConnectionStateChangedCallback&
915 state_changed_cb) { 872 state_changed_cb) {
916 frame_manager_->ListenForConnectionStateChange( 873 frame_manager_->ListenForConnectionStateChange(
917 RenderFrameHostId(render_process_id, render_frame_id), connection, 874 RenderFrameHostId(render_process_id, render_frame_id), connection,
918 state_changed_cb); 875 state_changed_cb);
919 } 876 }
920 877
878 void PresentationServiceDelegateImpl::ConnectToOffscreenPresentation(
879 int render_process_id,
880 int render_frame_id,
881 const content::PresentationSessionInfo& session,
882 content::PresentationConnectionPtr connection) {
883 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
884 auto* const offscreen_presentation_manager =
885 OffscreenPresentationManagerFactory::
886 GetOrCreateForControllerBrowserContext(web_contents_);
887 offscreen_presentation_manager->RegisterOffscreenPresentationController(
888 session.presentation_id, session.presentation_url, render_frame_host_id,
889 std::move(connection));
890 }
891
921 void PresentationServiceDelegateImpl::OnRouteResponse( 892 void PresentationServiceDelegateImpl::OnRouteResponse(
922 const PresentationRequest& presentation_request, 893 const PresentationRequest& presentation_request,
923 const RouteRequestResult& result) { 894 const RouteRequestResult& result) {
924 if (!result.route()) 895 if (!result.route())
925 return; 896 return;
926 897
927 content::PresentationSessionInfo session_info( 898 content::PresentationSessionInfo session_info(
928 presentation_request.presentation_url(), result.presentation_id()); 899 presentation_request.presentation_url(), result.presentation_id());
929 frame_manager_->OnDefaultPresentationSessionStarted( 900 frame_manager_->OnDefaultPresentationSessionStarted(
930 presentation_request, session_info, result.route()->media_route_id()); 901 presentation_request, session_info, result.route()->media_route_id());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( 935 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest(
965 int render_process_id, 936 int render_process_id,
966 int render_frame_id, 937 int render_frame_id,
967 const MediaSource::Id& source_id) const { 938 const MediaSource::Id& source_id) const {
968 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 939 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
969 return frame_manager_->HasScreenAvailabilityListenerForTest( 940 return frame_manager_->HasScreenAvailabilityListenerForTest(
970 render_frame_host_id, source_id); 941 render_frame_host_id, source_id);
971 } 942 }
972 943
973 } // namespace media_router 944 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698