Chromium Code Reviews| 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 "content/public/browser/presentation_session_state_listener.h" | |
| 8 #include "content/public/browser/render_frame_host.h" | |
| 9 #include "content/public/browser/render_process_host.h" | |
| 10 #include "content/public/browser/web_contents.h" | |
| 11 | |
| 12 namespace media_router { | |
| 13 | |
| 14 // OffscreenPresentationSession implementation. //////////////////////////////// | |
| 15 | |
| 16 OffscreenPresentationManager::OffscreenPresentationSession:: | |
| 17 OffscreenPresentationSession( | |
| 18 OffscreenPresentationManager::OffscreenPresentationConnection* | |
| 19 connection) | |
| 20 : state_change_listener_(nullptr), connection_(connection) { | |
| 21 DCHECK(connection_); | |
| 22 } | |
| 23 | |
| 24 OffscreenPresentationManager::OffscreenPresentationSession:: | |
| 25 ~OffscreenPresentationSession() { | |
| 26 if (connection_) | |
| 27 connection_->RemoveSession(this); | |
| 28 } | |
| 29 | |
| 30 void OffscreenPresentationManager::OffscreenPresentationSession::SendMessage( | |
| 31 scoped_ptr<content::PresentationSessionMessage> message, | |
| 32 const content::SendMessageCallback& callback) { | |
| 33 if (!connection_) { | |
| 34 callback.Run(false); | |
| 35 return; | |
| 36 } | |
| 37 | |
| 38 connection_->SendMessageFrom(this, message.Pass(), callback); | |
| 39 } | |
| 40 | |
| 41 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
| 42 ListenForMessages( | |
| 43 const content::PresentationSessionMessageCallback& callback) { | |
| 44 DCHECK(messages_callback_.is_null()); | |
| 45 DCHECK(!callback.is_null()); | |
| 46 messages_callback_ = callback; | |
| 47 } | |
| 48 | |
| 49 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
| 50 ListenForStateChanges(content::PresentationSessionStateListener* listener) { | |
| 51 DCHECK(!state_change_listener_); | |
| 52 DCHECK(listener); | |
| 53 state_change_listener_ = listener; | |
| 54 } | |
| 55 | |
| 56 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
| 57 OnSessionStateChanged(content::PresentationSessionState state) { | |
| 58 if (state_change_listener_) | |
| 59 state_change_listener_->OnSessionStateChanged(state); | |
| 60 | |
| 61 if (state == content::PRESENTATION_SESSION_STATE_DISCONNECTED) | |
| 62 connection_ = nullptr; | |
| 63 } | |
| 64 | |
| 65 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
| 66 OnMessageReceived(scoped_ptr<content::PresentationSessionMessage> message) { | |
| 67 if (messages_callback_.is_null()) | |
| 68 return; | |
| 69 | |
| 70 // TODO(imcheng): Implement message queueing/batching. | |
| 71 ScopedVector<content::PresentationSessionMessage> messages; | |
| 72 messages.push_back(message.release()); | |
| 73 messages_callback_.Run(messages.Pass(), true); | |
| 74 } | |
| 75 | |
| 76 | |
| 77 // OffscreenPresentationManager implementation. //////////////////////////////// | |
| 78 | |
| 79 OffscreenPresentationManager::~OffscreenPresentationManager() {} | |
| 80 | |
| 81 void OffscreenPresentationManager::RegisterOffscreenPresentationReceiver( | |
| 82 const std::string& presentation_id, | |
| 83 const ReceiverSessionAvailableCallback& receiver_available_callback) { | |
| 84 DCHECK(!ContainsKey(offscreen_presentations_, presentation_id)); | |
| 85 | |
| 86 offscreen_presentations_.insert( | |
| 87 presentation_id, | |
| 88 make_scoped_ptr(new OffscreenPresentation( | |
| 89 presentation_id, receiver_available_callback, this))); | |
| 90 } | |
| 91 | |
| 92 void OffscreenPresentationManager::UnregisterOffscreenPresentationReceiver( | |
| 93 const std::string& presentation_id) { | |
| 94 DCHECK(ContainsKey(offscreen_presentations_, presentation_id)); | |
| 95 offscreen_presentations_.erase(presentation_id); | |
| 96 } | |
| 97 | |
| 98 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
| 99 OffscreenPresentationManager::ConnectToOffscreenPresentation( | |
| 100 const std::string& presentation_id, | |
| 101 const RenderFrameHostId& controller_frame_id) { | |
| 102 auto it = offscreen_presentations_.find(presentation_id); | |
| 103 if (it == offscreen_presentations_.end()) | |
| 104 return scoped_ptr<OffscreenPresentationSession>(); | |
| 105 | |
| 106 return it->second->AddConnection(controller_frame_id); | |
| 107 } | |
| 108 | |
| 109 OffscreenPresentationManager::OffscreenPresentationManager() {} | |
| 110 | |
| 111 | |
| 112 // OffscreenPresentationConnection implementation. ///////////////////////////// | |
| 113 | |
| 114 OffscreenPresentationManager::OffscreenPresentationConnection:: | |
| 115 OffscreenPresentationConnection( | |
| 116 const RenderFrameHostId& controller_frame_id, | |
| 117 OffscreenPresentation* presentation) | |
| 118 : controller_frame_id_(controller_frame_id), | |
| 119 presentation_(presentation), | |
| 120 controller_(nullptr), | |
| 121 receiver_(nullptr) { | |
| 122 DCHECK(presentation_); | |
| 123 } | |
| 124 | |
| 125 void OffscreenPresentationManager::OffscreenPresentationConnection::Init( | |
| 126 OffscreenPresentationManager::OffscreenPresentationSession* controller, | |
| 127 OffscreenPresentationManager::OffscreenPresentationSession* receiver) { | |
| 128 DCHECK(!controller_); | |
| 129 DCHECK(!receiver_); | |
| 130 DCHECK(controller); | |
| 131 DCHECK(receiver); | |
| 132 controller_ = controller; | |
| 133 receiver_ = receiver; | |
| 134 } | |
| 135 | |
| 136 OffscreenPresentationManager::OffscreenPresentationConnection:: | |
| 137 ~OffscreenPresentationConnection() { | |
| 138 DCHECK(!controller_ || !receiver_); | |
| 139 } | |
| 140 | |
| 141 void OffscreenPresentationManager::OffscreenPresentationConnection:: | |
| 142 RemoveSession(OffscreenPresentationSession* session) { | |
| 143 DCHECK(session); | |
| 144 OffscreenPresentationSession* other_session = nullptr; | |
| 145 if (session == controller_) { | |
| 146 controller_ = nullptr; | |
| 147 other_session = receiver_; | |
| 148 } else { | |
| 149 DCHECK(session == receiver_); | |
| 150 receiver_ = nullptr; | |
| 151 other_session = controller_; | |
| 152 } | |
| 153 | |
| 154 DCHECK(other_session); | |
| 155 other_session->OnSessionStateChanged( | |
| 156 content::PRESENTATION_SESSION_STATE_DISCONNECTED); | |
| 157 presentation_->RemoveConnection(controller_frame_id_); | |
| 158 // |this| is deleted beyond this point. | |
| 159 } | |
| 160 | |
| 161 void OffscreenPresentationManager::OffscreenPresentationConnection:: | |
| 162 SendMessageFrom(OffscreenPresentationSession* session, | |
| 163 scoped_ptr<content::PresentationSessionMessage> message, | |
| 164 const content::SendMessageCallback& callback) { | |
| 165 OffscreenPresentationSession* other_session = | |
| 166 session == controller_ ? receiver_ : controller_; | |
| 167 DCHECK(other_session); | |
| 168 | |
| 169 other_session->OnMessageReceived(message.Pass()); | |
| 170 callback.Run(true); | |
| 171 } | |
| 172 | |
| 173 | |
| 174 // OffscreenPresentation implementation. /////////////////////////////////////// | |
| 175 | |
| 176 OffscreenPresentationManager::OffscreenPresentation::OffscreenPresentation( | |
| 177 const std::string& presentation_id, | |
| 178 const ReceiverSessionAvailableCallback& receiver_available_callback, | |
| 179 OffscreenPresentationManager* manager) | |
| 180 : presentation_id_(presentation_id), | |
| 181 receiver_available_callback_(receiver_available_callback), | |
| 182 manager_(manager) { | |
| 183 DCHECK(!receiver_available_callback_.is_null()); | |
| 184 DCHECK(manager_); | |
| 185 } | |
| 186 | |
| 187 OffscreenPresentationManager::OffscreenPresentation::~OffscreenPresentation() { | |
| 188 // The receiver must have destroyed all connections before unregistration. | |
| 189 DCHECK(connections_.empty()); | |
| 190 } | |
| 191 | |
| 192 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
| 193 OffscreenPresentationManager::OffscreenPresentation::AddConnection( | |
| 194 const RenderFrameHostId& controller_frame_id) { | |
| 195 if (ContainsKey(connections_, controller_frame_id)) { | |
| 196 LOG(ERROR) << "Frame " << controller_frame_id.first << ", " | |
|
miu
2015/10/20 00:56:50
nit: LOG(DFATAL) instead?
imcheng
2016/06/13 22:30:01
Changed to DLOG(ERROR). We probably do not need th
| |
| 197 << controller_frame_id.second << " already registered as a " | |
| 198 << "controller for presentation " << presentation_id_; | |
| 199 return scoped_ptr<OffscreenPresentationSession>(); | |
| 200 } | |
| 201 | |
| 202 scoped_ptr<OffscreenPresentationConnection> connection( | |
| 203 new OffscreenPresentationConnection(controller_frame_id, this)); | |
| 204 scoped_ptr<OffscreenPresentationSession> controller_session( | |
| 205 new OffscreenPresentationSession(connection.get())); | |
| 206 scoped_ptr<OffscreenPresentationSession> receiver_session( | |
| 207 new OffscreenPresentationSession(connection.get())); | |
| 208 | |
| 209 connection->Init(controller_session.get(), receiver_session.get()); | |
| 210 connections_.insert(controller_frame_id, connection.Pass()); | |
| 211 receiver_available_callback_.Run(receiver_session.Pass()); | |
| 212 return controller_session.Pass(); | |
| 213 } | |
| 214 | |
| 215 void OffscreenPresentationManager::OffscreenPresentation::RemoveConnection( | |
| 216 const RenderFrameHostId& controller_frame_id) { | |
| 217 DCHECK(ContainsKey(connections_, controller_frame_id)); | |
| 218 connections_.erase(controller_frame_id); | |
| 219 } | |
| 220 | |
| 221 } // namespace media_router | |
| OLD | NEW |