| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/offscreen_presentation_manager.h" | 5 #include "chrome/browser/media/router/offscreen_presentation_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "content/public/browser/render_frame_host.h" | 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 14 | 14 |
| 15 namespace media_router { | 15 namespace media_router { |
| 16 | 16 |
| 17 // OffscreenPresentationManager implementation. | 17 // OffscreenPresentationManager implementation. |
| 18 OffscreenPresentationManager::OffscreenPresentationManager() {} | 18 OffscreenPresentationManager::OffscreenPresentationManager() {} |
| 19 | 19 |
| 20 OffscreenPresentationManager::~OffscreenPresentationManager() {} | 20 OffscreenPresentationManager::~OffscreenPresentationManager() {} |
| 21 | 21 |
| 22 OffscreenPresentationManager::OffscreenPresentation* | 22 OffscreenPresentationManager::OffscreenPresentation* |
| 23 OffscreenPresentationManager::GetOrCreateOffscreenPresentation( | 23 OffscreenPresentationManager::GetOrCreateOffscreenPresentation( |
| 24 const std::string& presentation_id, | 24 const content::PresentationInfo& presentation_info) { |
| 25 const GURL& presentation_url) { | 25 auto it = offscreen_presentations_.find(presentation_info.presentation_id); |
| 26 auto it = offscreen_presentations_.find(presentation_id); | |
| 27 if (it == offscreen_presentations_.end()) { | 26 if (it == offscreen_presentations_.end()) { |
| 28 it = offscreen_presentations_ | 27 it = offscreen_presentations_ |
| 29 .insert(std::make_pair(presentation_id, | 28 .insert(std::make_pair( |
| 30 base::MakeUnique<OffscreenPresentation>( | 29 presentation_info.presentation_id, |
| 31 presentation_id, presentation_url))) | 30 base::MakeUnique<OffscreenPresentation>(presentation_info))) |
| 32 .first; | 31 .first; |
| 33 } | 32 } |
| 34 return it->second.get(); | 33 return it->second.get(); |
| 35 } | 34 } |
| 36 | 35 |
| 37 void OffscreenPresentationManager::RegisterOffscreenPresentationController( | 36 void OffscreenPresentationManager::RegisterOffscreenPresentationController( |
| 38 const std::string& presentation_id, | 37 const content::PresentationInfo& presentation_info, |
| 39 const GURL& presentation_url, | |
| 40 const RenderFrameHostId& render_frame_host_id, | 38 const RenderFrameHostId& render_frame_host_id, |
| 41 content::PresentationConnectionPtr controller_connection_ptr, | 39 content::PresentationConnectionPtr controller_connection_ptr, |
| 42 content::PresentationConnectionRequest receiver_connection_request, | 40 content::PresentationConnectionRequest receiver_connection_request, |
| 43 const MediaRoute& route) { | 41 const MediaRoute& route) { |
| 44 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id | 42 DVLOG(2) << __func__ |
| 43 << " [presentation_id]: " << presentation_info.presentation_id |
| 45 << ", [render_frame_host_id]: " << render_frame_host_id.second; | 44 << ", [render_frame_host_id]: " << render_frame_host_id.second; |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | 45 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 | 46 |
| 48 auto* presentation = | 47 auto* presentation = GetOrCreateOffscreenPresentation(presentation_info); |
| 49 GetOrCreateOffscreenPresentation(presentation_id, presentation_url); | |
| 50 presentation->RegisterController( | 48 presentation->RegisterController( |
| 51 render_frame_host_id, std::move(controller_connection_ptr), | 49 render_frame_host_id, std::move(controller_connection_ptr), |
| 52 std::move(receiver_connection_request), route); | 50 std::move(receiver_connection_request), route); |
| 53 } | 51 } |
| 54 | 52 |
| 55 void OffscreenPresentationManager::UnregisterOffscreenPresentationController( | 53 void OffscreenPresentationManager::UnregisterOffscreenPresentationController( |
| 56 const std::string& presentation_id, | 54 const std::string& presentation_id, |
| 57 const RenderFrameHostId& render_frame_host_id) { | 55 const RenderFrameHostId& render_frame_host_id) { |
| 58 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id | 56 DVLOG(2) << __func__ << " [presentation_id]: " << presentation_id |
| 59 << ", [render_frame_host_id]: " << render_frame_host_id.second; | 57 << ", [render_frame_host_id]: " << render_frame_host_id.second; |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | 58 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 | 59 |
| 62 auto it = offscreen_presentations_.find(presentation_id); | 60 auto it = offscreen_presentations_.find(presentation_id); |
| 63 if (it == offscreen_presentations_.end()) | 61 if (it == offscreen_presentations_.end()) |
| 64 return; | 62 return; |
| 65 | 63 |
| 66 // Remove presentation if no controller and receiver. | 64 // Remove presentation if no controller and receiver. |
| 67 it->second->UnregisterController(render_frame_host_id); | 65 it->second->UnregisterController(render_frame_host_id); |
| 68 if (!it->second->IsValid()) { | 66 if (!it->second->IsValid()) { |
| 69 DLOG(WARNING) << __func__ << " no receiver callback has been registered to " | 67 DLOG(WARNING) << __func__ << " no receiver callback has been registered to " |
| 70 << "[presentation_id]: " << presentation_id; | 68 << "[presentation_id]: " << presentation_id; |
| 71 offscreen_presentations_.erase(presentation_id); | 69 offscreen_presentations_.erase(presentation_id); |
| 72 } | 70 } |
| 73 } | 71 } |
| 74 | 72 |
| 75 void OffscreenPresentationManager::OnOffscreenPresentationReceiverCreated( | 73 void OffscreenPresentationManager::OnOffscreenPresentationReceiverCreated( |
| 76 const std::string& presentation_id, | 74 const content::PresentationInfo& presentation_info, |
| 77 const GURL& presentation_url, | |
| 78 const content::ReceiverConnectionAvailableCallback& receiver_callback) { | 75 const content::ReceiverConnectionAvailableCallback& receiver_callback) { |
| 79 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id; | 76 DVLOG(2) << __func__ |
| 77 << " [presentation_id]: " << presentation_info.presentation_id; |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); | 78 DCHECK(thread_checker_.CalledOnValidThread()); |
| 81 auto* presentation = | 79 auto* presentation = GetOrCreateOffscreenPresentation(presentation_info); |
| 82 GetOrCreateOffscreenPresentation(presentation_id, presentation_url); | |
| 83 presentation->RegisterReceiver(receiver_callback); | 80 presentation->RegisterReceiver(receiver_callback); |
| 84 } | 81 } |
| 85 | 82 |
| 86 void OffscreenPresentationManager::OnOffscreenPresentationReceiverTerminated( | 83 void OffscreenPresentationManager::OnOffscreenPresentationReceiverTerminated( |
| 87 const std::string& presentation_id) { | 84 const std::string& presentation_id) { |
| 88 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id; | 85 DVLOG(2) << __func__ << " [presentation_id]: " << presentation_id; |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 | 87 |
| 91 offscreen_presentations_.erase(presentation_id); | 88 offscreen_presentations_.erase(presentation_id); |
| 92 } | 89 } |
| 93 | 90 |
| 94 bool OffscreenPresentationManager::IsOffscreenPresentation( | 91 bool OffscreenPresentationManager::IsOffscreenPresentation( |
| 95 const std::string& presentation_id) { | 92 const std::string& presentation_id) { |
| 96 return base::ContainsKey(offscreen_presentations_, presentation_id); | 93 return base::ContainsKey(offscreen_presentations_, presentation_id); |
| 97 } | 94 } |
| 98 | 95 |
| 99 const MediaRoute* OffscreenPresentationManager::GetRoute( | 96 const MediaRoute* OffscreenPresentationManager::GetRoute( |
| 100 const std::string& presentation_id) { | 97 const std::string& presentation_id) { |
| 101 auto it = offscreen_presentations_.find(presentation_id); | 98 auto it = offscreen_presentations_.find(presentation_id); |
| 102 return (it != offscreen_presentations_.end() && | 99 return (it != offscreen_presentations_.end() && |
| 103 it->second->route_.has_value()) | 100 it->second->route_.has_value()) |
| 104 ? &(it->second->route_.value()) | 101 ? &(it->second->route_.value()) |
| 105 : nullptr; | 102 : nullptr; |
| 106 } | 103 } |
| 107 | 104 |
| 108 // OffscreenPresentation implementation. | 105 // OffscreenPresentation implementation. |
| 109 OffscreenPresentationManager::OffscreenPresentation::OffscreenPresentation( | 106 OffscreenPresentationManager::OffscreenPresentation::OffscreenPresentation( |
| 110 const std::string& presentation_id, | 107 const content::PresentationInfo& presentation_info) |
| 111 const GURL& presentation_url) | 108 : presentation_info_(presentation_info) {} |
| 112 : presentation_id_(presentation_id), presentation_url_(presentation_url) {} | |
| 113 | 109 |
| 114 OffscreenPresentationManager::OffscreenPresentation::~OffscreenPresentation() {} | 110 OffscreenPresentationManager::OffscreenPresentation::~OffscreenPresentation() {} |
| 115 | 111 |
| 116 void OffscreenPresentationManager::OffscreenPresentation::RegisterController( | 112 void OffscreenPresentationManager::OffscreenPresentation::RegisterController( |
| 117 const RenderFrameHostId& render_frame_host_id, | 113 const RenderFrameHostId& render_frame_host_id, |
| 118 content::PresentationConnectionPtr controller_connection_ptr, | 114 content::PresentationConnectionPtr controller_connection_ptr, |
| 119 content::PresentationConnectionRequest receiver_connection_request, | 115 content::PresentationConnectionRequest receiver_connection_request, |
| 120 const MediaRoute& route) { | 116 const MediaRoute& route) { |
| 121 if (!receiver_callback_.is_null()) { | 117 if (!receiver_callback_.is_null()) { |
| 122 receiver_callback_.Run( | 118 receiver_callback_.Run(presentation_info_, |
| 123 content::PresentationInfo(presentation_url_, presentation_id_), | 119 std::move(controller_connection_ptr), |
| 124 std::move(controller_connection_ptr), | 120 std::move(receiver_connection_request)); |
| 125 std::move(receiver_connection_request)); | |
| 126 } else { | 121 } else { |
| 127 pending_controllers_.insert(std::make_pair( | 122 pending_controllers_.insert(std::make_pair( |
| 128 render_frame_host_id, base::MakeUnique<ControllerConnection>( | 123 render_frame_host_id, base::MakeUnique<ControllerConnection>( |
| 129 std::move(controller_connection_ptr), | 124 std::move(controller_connection_ptr), |
| 130 std::move(receiver_connection_request)))); | 125 std::move(receiver_connection_request)))); |
| 131 } | 126 } |
| 132 | 127 |
| 133 route_ = route; | 128 route_ = route; |
| 134 } | 129 } |
| 135 | 130 |
| 136 void OffscreenPresentationManager::OffscreenPresentation::UnregisterController( | 131 void OffscreenPresentationManager::OffscreenPresentation::UnregisterController( |
| 137 const RenderFrameHostId& render_frame_host_id) { | 132 const RenderFrameHostId& render_frame_host_id) { |
| 138 pending_controllers_.erase(render_frame_host_id); | 133 pending_controllers_.erase(render_frame_host_id); |
| 139 } | 134 } |
| 140 | 135 |
| 141 void OffscreenPresentationManager::OffscreenPresentation::RegisterReceiver( | 136 void OffscreenPresentationManager::OffscreenPresentation::RegisterReceiver( |
| 142 const content::ReceiverConnectionAvailableCallback& receiver_callback) { | 137 const content::ReceiverConnectionAvailableCallback& receiver_callback) { |
| 143 DCHECK(receiver_callback_.is_null()); | 138 DCHECK(receiver_callback_.is_null()); |
| 144 | 139 |
| 145 for (auto& controller : pending_controllers_) { | 140 for (auto& controller : pending_controllers_) { |
| 146 receiver_callback.Run( | 141 receiver_callback.Run( |
| 147 content::PresentationInfo(presentation_url_, presentation_id_), | 142 presentation_info_, |
| 148 std::move(controller.second->controller_connection_ptr), | 143 std::move(controller.second->controller_connection_ptr), |
| 149 std::move(controller.second->receiver_connection_request)); | 144 std::move(controller.second->receiver_connection_request)); |
| 150 } | 145 } |
| 151 receiver_callback_ = receiver_callback; | 146 receiver_callback_ = receiver_callback; |
| 152 pending_controllers_.clear(); | 147 pending_controllers_.clear(); |
| 153 } | 148 } |
| 154 | 149 |
| 155 bool OffscreenPresentationManager::OffscreenPresentation::IsValid() const { | 150 bool OffscreenPresentationManager::OffscreenPresentation::IsValid() const { |
| 156 return !(pending_controllers_.empty() && receiver_callback_.is_null()); | 151 return !(pending_controllers_.empty() && receiver_callback_.is_null()); |
| 157 } | 152 } |
| 158 | 153 |
| 159 OffscreenPresentationManager::OffscreenPresentation::ControllerConnection:: | 154 OffscreenPresentationManager::OffscreenPresentation::ControllerConnection:: |
| 160 ControllerConnection( | 155 ControllerConnection( |
| 161 content::PresentationConnectionPtr controller_connection_ptr, | 156 content::PresentationConnectionPtr controller_connection_ptr, |
| 162 content::PresentationConnectionRequest receiver_connection_request) | 157 content::PresentationConnectionRequest receiver_connection_request) |
| 163 : controller_connection_ptr(std::move(controller_connection_ptr)), | 158 : controller_connection_ptr(std::move(controller_connection_ptr)), |
| 164 receiver_connection_request(std::move(receiver_connection_request)) {} | 159 receiver_connection_request(std::move(receiver_connection_request)) {} |
| 165 | 160 |
| 166 OffscreenPresentationManager::OffscreenPresentation::ControllerConnection:: | 161 OffscreenPresentationManager::OffscreenPresentation::ControllerConnection:: |
| 167 ~ControllerConnection() {} | 162 ~ControllerConnection() {} |
| 168 | 163 |
| 169 } // namespace media_router | 164 } // namespace media_router |
| OLD | NEW |