OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/media/router/offscreen_presentation_manager.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace media_router { |
| 16 |
| 17 // OffscreenPresentationManager implementation. |
| 18 OffscreenPresentationManager::OffscreenPresentationManager() {} |
| 19 |
| 20 OffscreenPresentationManager::~OffscreenPresentationManager() {} |
| 21 |
| 22 OffscreenPresentationManager::OffscreenPresentation* |
| 23 OffscreenPresentationManager::GetOrCreateOffscreenPresentation( |
| 24 const std::string& presentation_id, |
| 25 const GURL& presentation_url) { |
| 26 auto it = offscreen_presentations_.find(presentation_id); |
| 27 if (it == offscreen_presentations_.end()) { |
| 28 it = offscreen_presentations_ |
| 29 .insert(std::make_pair(presentation_id, |
| 30 base::WrapUnique(new OffscreenPresentation( |
| 31 presentation_id, presentation_url)))) |
| 32 .first; |
| 33 } |
| 34 return it->second.get(); |
| 35 } |
| 36 |
| 37 void OffscreenPresentationManager::RegisterOffscreenPresentationController( |
| 38 const std::string& presentation_id, |
| 39 const GURL& presentation_url, |
| 40 const RenderFrameHostId& render_frame_host_id, |
| 41 content::PresentationConnectionPtr controller) { |
| 42 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id |
| 43 << ", [render_frame_host_id]: " << render_frame_host_id.second; |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 |
| 46 auto presentation = |
| 47 GetOrCreateOffscreenPresentation(presentation_id, presentation_url); |
| 48 presentation->RegisterController(render_frame_host_id, std::move(controller)); |
| 49 } |
| 50 |
| 51 void OffscreenPresentationManager::UnregisterOffscreenPresentationController( |
| 52 const std::string& presentation_id, |
| 53 const RenderFrameHostId& render_frame_host_id) { |
| 54 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id |
| 55 << ", [render_frame_host_id]: " << render_frame_host_id.second; |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 57 |
| 58 auto it = offscreen_presentations_.find(presentation_id); |
| 59 if (it == offscreen_presentations_.end()) |
| 60 return; |
| 61 |
| 62 // Remove presentation if no controller and receiver. |
| 63 it->second->UnregisterController(render_frame_host_id); |
| 64 if (it->second->pending_controllers_.size() == 0 && |
| 65 it->second->receiver_callback_.is_null()) |
| 66 offscreen_presentations_.erase(presentation_id); |
| 67 } |
| 68 |
| 69 void OffscreenPresentationManager::OnOffscreenPresentationReceiverCreated( |
| 70 const std::string& presentation_id, |
| 71 const GURL& presentation_url, |
| 72 const content::ReceiverConnectionAvailableCallback& receiver_callback) { |
| 73 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id; |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); |
| 75 auto presentation = |
| 76 GetOrCreateOffscreenPresentation(presentation_id, presentation_url); |
| 77 presentation->RegisterReceiver(receiver_callback); |
| 78 } |
| 79 |
| 80 void OffscreenPresentationManager::OnOffscreenPresentationReceiverTerminated( |
| 81 const std::string& presentation_id) { |
| 82 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id; |
| 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 84 |
| 85 offscreen_presentations_.erase(presentation_id); |
| 86 } |
| 87 |
| 88 // OffscreenPresentation implementation. |
| 89 OffscreenPresentationManager::OffscreenPresentation::OffscreenPresentation( |
| 90 const std::string& presentation_id, |
| 91 const GURL& presentation_url) |
| 92 : presentation_id_(presentation_id), presentation_url_(presentation_url) {} |
| 93 |
| 94 OffscreenPresentationManager::OffscreenPresentation::~OffscreenPresentation() {} |
| 95 |
| 96 void OffscreenPresentationManager::OffscreenPresentation::RegisterController( |
| 97 const RenderFrameHostId& render_frame_host_id, |
| 98 content::PresentationConnectionPtr controller) { |
| 99 if (!receiver_callback_.is_null()) { |
| 100 receiver_callback_.Run( |
| 101 content::PresentationSessionInfo(presentation_url_, presentation_id_), |
| 102 std::move(controller)); |
| 103 } else { |
| 104 pending_controllers_.insert( |
| 105 std::make_pair(render_frame_host_id, std::move(controller))); |
| 106 } |
| 107 } |
| 108 |
| 109 void OffscreenPresentationManager::OffscreenPresentation::UnregisterController( |
| 110 const RenderFrameHostId& render_frame_host_id) { |
| 111 pending_controllers_.erase(render_frame_host_id); |
| 112 } |
| 113 |
| 114 void OffscreenPresentationManager::OffscreenPresentation::RegisterReceiver( |
| 115 const content::ReceiverConnectionAvailableCallback& receiver_callback) { |
| 116 DCHECK(receiver_callback_.is_null()); |
| 117 |
| 118 for (auto& controller : pending_controllers_) { |
| 119 receiver_callback.Run( |
| 120 content::PresentationSessionInfo(presentation_url_, presentation_id_), |
| 121 std::move(controller.second)); |
| 122 } |
| 123 receiver_callback_ = receiver_callback; |
| 124 pending_controllers_.clear(); |
| 125 } |
| 126 |
| 127 bool OffscreenPresentationManager::OffscreenPresentation::IsValid() { |
| 128 return !(pending_controllers_.size() == 0 && receiver_callback_.is_null()); |
| 129 } |
| 130 |
| 131 } // namespace media_router |
OLD | NEW |