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 if (connection_) { | |
28 if (is_controller_) | |
miu
2015/10/07 21:51:46
Consider getting rid of the |is_controller_| bool
imcheng
2015/10/10 04:39:43
Done.
| |
29 connection_->RemoveControllerSession(); | |
30 else | |
31 connection_->RemoveReceiverSession(); | |
32 } | |
33 } | |
34 | |
35 void OffscreenPresentationManager::OffscreenPresentationSession::SendMessage( | |
36 scoped_ptr<content::PresentationSessionMessage> message, | |
37 const content::SendMessageCallback& callback) { | |
38 if (!connection_) { | |
39 callback.Run(false); | |
40 return; | |
41 } | |
42 if (is_controller_) | |
miu
2015/10/07 21:51:46
Without the |is_controller_| bool, you might chang
imcheng
2015/10/10 04:39:43
Done.
| |
43 connection_->SendMessageToReceiver(message.Pass(), callback); | |
44 else | |
45 connection_->SendMessageToController(message.Pass(), callback); | |
46 } | |
47 | |
48 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
49 ListenForMessages( | |
50 const content::PresentationSessionMessageCallback& callback) { | |
51 messages_callback_ = callback; | |
miu
2015/10/07 21:51:46
Since there can only be one listener, should you D
imcheng
2015/10/10 04:39:43
Done.
| |
52 } | |
53 | |
54 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
55 ListenForStateChanges(content::PresentationSessionStateListener* listener) { | |
56 state_change_listener_ = listener; | |
57 } | |
58 | |
59 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
60 OnSessionStateChanged(content::PresentationSessionState state) { | |
61 if (state_change_listener_) | |
62 state_change_listener_->OnSessionStateChanged(state); | |
63 | |
64 if (state == content::PRESENTATION_SESSION_STATE_DISCONNECTED) | |
65 connection_ = nullptr; | |
66 } | |
67 | |
68 void OffscreenPresentationManager::OffscreenPresentationSession:: | |
69 OnMessageReceived(scoped_ptr<content::PresentationSessionMessage> message) { | |
70 if (!messages_callback_.is_null()) { | |
71 ScopedVector<content::PresentationSessionMessage> messages; | |
72 messages.push_back(message.release()); | |
73 messages_callback_.Run(messages.Pass(), true); | |
74 } | |
75 } | |
76 | |
77 OffscreenPresentationManager::~OffscreenPresentationManager() {} | |
78 | |
79 void OffscreenPresentationManager::RegisterOffscreenPresentationReceiver( | |
80 const std::string& presentation_id, | |
81 const ReceiverSessionAvailableCallback& receiver_available_callback) { | |
82 DCHECK(!ContainsKey(offscreen_presentations_, presentation_id)); | |
83 | |
84 offscreen_presentations_.insert( | |
85 presentation_id, | |
86 make_scoped_ptr(new OffscreenPresentation( | |
87 presentation_id, receiver_available_callback, this))); | |
88 } | |
89 | |
90 void OffscreenPresentationManager::UnregisterOffscreenPresentationReceiver( | |
91 const std::string& presentation_id) { | |
92 DCHECK(ContainsKey(offscreen_presentations_, presentation_id)); | |
93 offscreen_presentations_.erase(presentation_id); | |
94 } | |
95 | |
96 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
97 OffscreenPresentationManager::ConnectToOffscreenPresentation( | |
98 const std::string& presentation_id, | |
99 const RenderFrameHostId& controller_frame_id) { | |
100 auto it = offscreen_presentations_.find(presentation_id); | |
101 if (it == offscreen_presentations_.end()) | |
102 return scoped_ptr< | |
103 OffscreenPresentationManager::OffscreenPresentationSession>(); | |
miu
2015/10/07 21:51:46
nit: OffscreenPresentationSession is an inner clas
imcheng
2015/10/10 04:39:43
Done.
| |
104 | |
105 return it->second->AddConnection(controller_frame_id); | |
106 } | |
107 | |
108 OffscreenPresentationManager::OffscreenPresentationManager() {} | |
109 | |
110 OffscreenPresentationManager::OffscreenPresentationConnection:: | |
111 OffscreenPresentationConnection( | |
112 const RenderFrameHostId& controller_frame_id, | |
113 OffscreenPresentation* presentation) | |
114 : presentation_(presentation), | |
miu
2015/10/07 21:51:46
Please be consistent everywhere with ordering of v
imcheng
2015/10/10 04:39:43
Done.
| |
115 controller_frame_id_(controller_frame_id), | |
116 controller_(nullptr), | |
117 receiver_(nullptr) { | |
118 DCHECK(presentation_); | |
119 } | |
120 | |
121 void OffscreenPresentationManager::OffscreenPresentationConnection::Init( | |
122 OffscreenPresentationManager::OffscreenPresentationSession* controller, | |
123 OffscreenPresentationManager::OffscreenPresentationSession* receiver) { | |
124 DCHECK(!controller_); | |
125 DCHECK(!receiver_); | |
126 DCHECK(controller); | |
127 DCHECK(receiver); | |
128 controller_ = controller; | |
129 receiver_ = receiver; | |
130 } | |
131 | |
132 OffscreenPresentationManager::OffscreenPresentationConnection:: | |
133 ~OffscreenPresentationConnection() { | |
134 DCHECK(!controller_ || !receiver_); | |
135 } | |
136 | |
137 void OffscreenPresentationManager::OffscreenPresentationConnection:: | |
138 RemoveControllerSession() { | |
139 RemoveSession(controller_); | |
140 } | |
141 | |
142 void OffscreenPresentationManager::OffscreenPresentationConnection:: | |
143 RemoveReceiverSession() { | |
144 RemoveSession(receiver_); | |
145 } | |
146 | |
147 void OffscreenPresentationManager::OffscreenPresentationConnection:: | |
148 RemoveSession(OffscreenPresentationSession* session) { | |
149 DCHECK(session); | |
150 OffscreenPresentationManager::OffscreenPresentationSession* other_session = | |
151 nullptr; | |
152 if (session == controller_) { | |
153 controller_ = nullptr; | |
154 other_session = receiver_; | |
155 } else { | |
156 DCHECK(session == receiver_); | |
157 receiver_ = nullptr; | |
158 other_session = controller_; | |
159 } | |
160 | |
161 DCHECK(other_session); | |
162 other_session->OnSessionStateChanged( | |
163 content::PRESENTATION_SESSION_STATE_DISCONNECTED); | |
164 presentation_->RemoveConnection(controller_frame_id_); | |
165 // |this| is deleted beyond this point. | |
166 } | |
167 | |
168 void OffscreenPresentationManager::OffscreenPresentationConnection:: | |
169 SendMessageToController( | |
170 scoped_ptr<content::PresentationSessionMessage> message, | |
171 const content::SendMessageCallback& callback) { | |
172 SendMessage(controller_, message.Pass(), callback); | |
173 } | |
174 | |
175 void OffscreenPresentationManager::OffscreenPresentationConnection:: | |
176 SendMessageToReceiver( | |
177 scoped_ptr<content::PresentationSessionMessage> message, | |
178 const content::SendMessageCallback& callback) { | |
179 SendMessage(receiver_, message.Pass(), callback); | |
180 } | |
181 | |
182 void OffscreenPresentationManager::OffscreenPresentationConnection::SendMessage( | |
183 OffscreenPresentationSession* session, | |
184 scoped_ptr<content::PresentationSessionMessage> message, | |
185 const content::SendMessageCallback& callback) { | |
186 // TODO(imcheng): Implement message batching. | |
187 session->OnMessageReceived(message.Pass()); | |
188 callback.Run(true); | |
189 } | |
190 | |
191 OffscreenPresentationManager::OffscreenPresentation::OffscreenPresentation( | |
192 const std::string& presentation_id, | |
193 const ReceiverSessionAvailableCallback& receiver_available_callback, | |
194 OffscreenPresentationManager* manager) | |
195 : presentation_id_(presentation_id), | |
196 receiver_available_callback_(receiver_available_callback), | |
197 manager_(manager) { | |
198 DCHECK(!receiver_available_callback_.is_null()); | |
199 DCHECK(manager_); | |
200 } | |
201 | |
202 OffscreenPresentationManager::OffscreenPresentation::~OffscreenPresentation() { | |
203 // The receiver must have destroyed all connections before unregistration. | |
204 DCHECK(connections_.empty()); | |
205 } | |
206 | |
207 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
208 OffscreenPresentationManager::OffscreenPresentation::AddConnection( | |
209 const RenderFrameHostId& controller_frame_id) { | |
210 DCHECK(!ContainsKey(connections_, controller_frame_id)); | |
211 | |
212 scoped_ptr<OffscreenPresentationConnection> connection( | |
213 new OffscreenPresentationManager::OffscreenPresentationConnection( | |
214 controller_frame_id, this)); | |
215 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
216 controller_session( | |
217 new OffscreenPresentationManager::OffscreenPresentationSession( | |
218 true, connection.get())); | |
219 scoped_ptr<OffscreenPresentationManager::OffscreenPresentationSession> | |
220 receiver_session( | |
221 new OffscreenPresentationManager::OffscreenPresentationSession( | |
222 false, connection.get())); | |
223 | |
224 connection->Init(controller_session.get(), receiver_session.get()); | |
225 connections_.insert(controller_frame_id, connection.Pass()); | |
226 receiver_available_callback_.Run(receiver_session.Pass()); | |
227 return controller_session.Pass(); | |
228 } | |
229 | |
230 void OffscreenPresentationManager::OffscreenPresentation::RemoveConnection( | |
231 const RenderFrameHostId& controller_frame_id) { | |
232 DCHECK(ContainsKey(connections_, controller_frame_id)); | |
233 connections_.erase(controller_frame_id); | |
234 } | |
235 | |
236 } // namespace media_router | |
OLD | NEW |