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 OffscreenPresentationManager::OffscreenPresentationSession:: | |
15 OffscreenPresentationSession( | |
16 bool is_controller, | |
17 OffscreenPresentationManager::OffscreenPresentationConnection* | |
18 connection) | |
19 : is_controller_(is_controller), | |
20 state_change_listener_(nullptr), | |
21 connection_(connection) { | |
22 DCHECK(connection_); | |
23 } | |
24 | |
25 OffscreenPresentationManager::OffscreenPresentationSession:: | |
26 ~OffscreenPresentationSession() { | |
27 connection_->RemoveSession(is_controller_); | |
28 } | |
29 | |
30 void OffscreenPresentationManager::OffscreenPresentationSession::SendMessage( | |
31 scoped_ptr<content::PresentationSessionMessage> message, | |
32 const content::SendMessageCallback& callback) { | |
33 connection_->SendMessage(is_controller_, message.Pass(), callback); | |
34 } | |
35 | |
36 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
37 ListenForMessages( | |
38 const content::PresentationSessionMessageCallback& callback) { | |
39 messages_callback_ = callback; | |
40 } | |
41 | |
42 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
43 ListenForStateChanges(content::PresentationSessionStateListener* listener) { | |
44 state_change_listener_ = listener; | |
45 } | |
46 | |
47 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
48 OnSessionStateChanged(content::PresentationSessionState state) { | |
49 if (state_change_listener_) | |
50 state_change_listener_->OnSessionStateChanged(state); | |
51 } | |
52 | |
53 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
54 OnMessageReceived(scoped_ptr<content::PresentationSessionMessage> message) { | |
mark a. foltz
2015/10/01 18:39:12
IIUC the PresentationService gets batches of messa
imcheng
2015/10/06 00:59:14
Yes. I added a TODO here. I want to make sure it w
| |
55 if (!messages_callback_.is_null()) { | |
56 ScopedVector<content::PresentationSessionMessage> messages; | |
57 messages.push_back(message.release()); | |
58 messages_callback_.Run(messages.Pass(), true); | |
59 } | |
60 } | |
61 | |
62 OffscreenPresentationManager::~OffscreenPresentationManager() {} | |
63 | |
64 void OffscreenPresentationManager::RegisterOffscreenPresentationReceiver( | |
65 const std::string& presentation_id, | |
66 const ReceiverSessionAvailableCallback& receiver_available_callback) { | |
67 DCHECK(!ContainsKey(offscreen_presentations_, presentation_id)); | |
mark a. foltz
2015/10/01 18:39:12
Well this will be a no-op in release builds. Mayb
imcheng
2015/10/06 00:59:13
insert() will also be no-op if there is already an
| |
68 | |
69 offscreen_presentations_.insert( | |
70 presentation_id, | |
71 make_scoped_ptr(new OffscreenPresentation( | |
72 presentation_id, receiver_available_callback, this))); | |
73 } | |
74 | |
75 void OffscreenPresentationManager::UnregisterOffscreenPresentationReceiver( | |
76 const std::string& presentation_id) { | |
77 auto it = offscreen_presentations_.find(presentation_id); | |
78 DCHECK(it != offscreen_presentations_.end()); | |
mark a. foltz
2015/10/01 18:39:12
Similar comment here - it would be better to DLOG
imcheng
2015/10/06 00:59:13
We are always deleting the OffscreenPresentation h
| |
79 it->second->OnReceiverDetached(); | |
80 } | |
81 | |
82 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
83 OffscreenPresentationManager::CreateOffscreenPresentationConnection( | |
84 const std::string& presentation_id, | |
85 const RenderFrameHostId& controller_frame_id) { | |
86 auto it = offscreen_presentations_.find(presentation_id); | |
87 if (it == offscreen_presentations_.end()) | |
88 return scoped_ptr< | |
89 OffscreenPresentationManager::OffscreenPresentationSession>(); | |
90 | |
91 return it->second->AddConnectionAndNotifyReceiver(controller_frame_id); | |
92 } | |
93 | |
94 OffscreenPresentationManager::OffscreenPresentationManager() {} | |
95 | |
96 void OffscreenPresentationManager::RemovePresentation( | |
mark a. foltz
2015/10/01 18:39:12
This must only be called on presentations that are
imcheng
2015/10/06 00:59:14
This function is removed. But that is correct - I
| |
97 const std::string& presentation_id) { | |
98 offscreen_presentations_.erase(presentation_id); | |
99 } | |
100 | |
101 OffscreenPresentationManager::OffscreenPresentationConnection:: | |
102 OffscreenPresentationConnection( | |
103 const RenderFrameHostId& controller_frame_id, | |
104 OffscreenPresentation* presentation) | |
105 : presentation_(presentation), | |
106 controller_frame_id_(controller_frame_id), | |
107 controller_(nullptr), | |
mark a. foltz
2015/10/01 18:39:12
This class would be simpler by accepting a non-nul
imcheng
2015/10/06 00:59:13
The issue is that OffscreenPresentationConnection
| |
108 receiver_(nullptr) { | |
109 DCHECK(presentation_); | |
110 } | |
111 | |
112 void OffscreenPresentationManager::OffscreenPresentationConnection::Init( | |
113 OffscreenPresentationManager::OffscreenPresentationSession* controller, | |
114 OffscreenPresentationManager::OffscreenPresentationSession* receiver) { | |
115 DCHECK(!controller_); | |
116 DCHECK(!receiver_); | |
117 DCHECK(controller); | |
118 DCHECK(receiver); | |
119 controller_ = controller; | |
120 receiver_ = receiver; | |
121 } | |
122 | |
123 OffscreenPresentationManager::OffscreenPresentationConnection:: | |
124 ~OffscreenPresentationConnection() { | |
125 DCHECK(!controller_); | |
126 DCHECK(!receiver_); | |
127 } | |
128 | |
129 void OffscreenPresentationManager::OffscreenPresentationConnection:: | |
130 RemoveSession(bool is_controller) { | |
131 OffscreenPresentationManager::OffscreenPresentationSession* other_session = | |
132 nullptr; | |
133 if (is_controller) { | |
mark a. foltz
2015/10/01 18:39:12
This would seem simpler as:
if (is_controller &&
imcheng
2015/10/06 00:59:14
Ack
| |
134 DCHECK(controller_); | |
135 controller_ = nullptr; | |
136 other_session = receiver_; | |
137 } else { | |
138 DCHECK(receiver_); | |
139 receiver_ = nullptr; | |
140 other_session = controller_; | |
141 } | |
142 | |
143 if (other_session) { | |
144 other_session->OnSessionStateChanged( | |
145 content::PRESENTATION_SESSION_STATE_DISCONNECTED); | |
146 } else { | |
147 presentation_->RemoveConnection(controller_frame_id_); | |
148 // |this| is deleted beyond this point. | |
149 } | |
150 } | |
151 | |
152 void OffscreenPresentationManager::OffscreenPresentationConnection::SendMessage( | |
153 bool is_controller, | |
154 scoped_ptr<content::PresentationSessionMessage> message, | |
155 const content::SendMessageCallback& callback) { | |
156 OffscreenPresentationManager::OffscreenPresentationSession* other_session = | |
mark a. foltz
2015/10/01 18:39:12
Help me understand - the connection will always ha
imcheng
2015/10/06 00:59:13
Went with your other comment in the .h - SendMessa
| |
157 is_controller ? receiver_ : controller_; | |
158 if (!other_session) { | |
159 callback.Run(false); | |
160 return; | |
161 } else { | |
162 other_session->OnMessageReceived(message.Pass()); | |
163 callback.Run(true); | |
164 } | |
165 } | |
166 | |
167 OffscreenPresentationManager::OffscreenPresentation::OffscreenPresentation( | |
168 const std::string& presentation_id, | |
169 const ReceiverSessionAvailableCallback& receiver_available_callback, | |
170 OffscreenPresentationManager* manager) | |
171 : presentation_id_(presentation_id), | |
172 receiver_available_callback_(receiver_available_callback), | |
173 manager_(manager) { | |
174 DCHECK(!receiver_available_callback_.is_null()); | |
175 DCHECK(manager_); | |
176 } | |
177 | |
178 OffscreenPresentationManager::OffscreenPresentation::~OffscreenPresentation() {} | |
179 | |
180 void OffscreenPresentationManager::OffscreenPresentation::OnReceiverDetached() { | |
181 receiver_available_callback_.Reset(); | |
182 MaybeSelfDestruct(); | |
183 } | |
184 | |
185 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
186 OffscreenPresentationManager::OffscreenPresentation:: | |
187 AddConnectionAndNotifyReceiver( | |
188 const RenderFrameHostId& controller_frame_id) { | |
189 DCHECK(!ContainsKey(connections_, controller_frame_id)); | |
190 | |
191 if (IsReceiverGone()) { | |
192 return scoped_ptr< | |
193 OffscreenPresentationManager::OffscreenPresentationSession>(); | |
194 } | |
195 | |
196 scoped_ptr<OffscreenPresentationConnection> connection( | |
197 new OffscreenPresentationManager::OffscreenPresentationConnection( | |
198 controller_frame_id, this)); | |
mark a. foltz
2015/10/01 18:39:12
ISTM the connection should create and own the rece
imcheng
2015/10/06 00:59:14
Per comment above, I could new these Session objec
| |
199 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
200 controller_session( | |
201 new OffscreenPresentationManager::OffscreenPresentationSession( | |
202 true, connection.get())); | |
203 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
204 receiver_session( | |
205 new OffscreenPresentationManager::OffscreenPresentationSession( | |
206 false, connection.get())); | |
207 | |
208 connection->Init(controller_session.get(), receiver_session.get()); | |
209 connections_.insert(controller_frame_id, connection.Pass()); | |
210 receiver_available_callback_.Run(receiver_session.Pass()); | |
211 return controller_session.Pass(); | |
212 } | |
213 | |
214 void OffscreenPresentationManager::OffscreenPresentation::RemoveConnection( | |
mark a. foltz
2015/10/01 18:39:12
What guarantees that the two sessions are in the d
imcheng
2015/10/06 00:59:13
In the new patchset, this is only called from Offs
| |
215 const RenderFrameHostId& controller_frame_id) { | |
216 DCHECK(ContainsKey(connections_, controller_frame_id)); | |
217 | |
218 connections_.erase(controller_frame_id); | |
219 MaybeSelfDestruct(); | |
220 } | |
221 | |
222 void OffscreenPresentationManager::OffscreenPresentation::MaybeSelfDestruct() { | |
223 if (IsReceiverGone() && connections_.empty()) | |
224 manager_->RemovePresentation(presentation_id_); | |
225 // |this| will be deleted beyond this point. | |
226 } | |
227 | |
228 } // namespace media_router | |
OLD | NEW |