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 #ifndef CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_ | |
6 #define CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/containers/scoped_ptr_map.h" | |
13 #include "base/macros.h" | |
14 #include "chrome/browser/media/router/render_frame_host_helper.h" | |
15 #include "components/keyed_service/core/keyed_service.h" | |
16 #include "content/public/browser/presentation_service_delegate.h" | |
17 | |
18 namespace media_router { | |
19 | |
20 // Acts as an intermediary between the party that hosts an offscreen | |
21 // presentation (AKA the receiver) and the frames that start/connect and | |
miu
2015/10/07 21:51:46
nit: s/frames/render frames/ for clarity
imcheng
2015/10/10 04:39:43
Done.
| |
22 // control the presentation (AKA the controller frames). Coordinates the | |
23 // (un)registration of receivers and controller frames in the context of | |
24 // an offscreen presentation to enable the exchange of messages and | |
25 // notification of state change to/from the other side. | |
26 // | |
27 // Example usage: | |
28 // | |
29 // Receiver is created to host the offscreen presentation and | |
30 // registers itself so that controller frames can connect to it: | |
31 // | |
32 // OffscreenPresentationManager* manager = | |
33 // OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext( | |
34 // context); | |
35 // manager->RegisterOffscreenPresentationReceiver(presentation_id, | |
36 // &base::Bind(&OnSessionAvailable)); | |
miu
2015/10/07 21:51:46
Delete the first ampersand.
imcheng
2015/10/10 04:39:43
Done.
| |
37 // ... | |
38 // void OnSessionAvailable( | |
39 // scoped_ptr<OffscreenPresentationSession> receiver_session) { | |
40 // [Calls methods in |receiver_session| to send/receive messages, etc.] | |
41 // } | |
42 // | |
43 // Controller frame establishes connection with the receiver side, resulting | |
44 // in a connection with the two endpoints being the controller session | |
45 // (given to controller frame) and the receiver session (given to receiver). | |
46 // Note calling this will trigger |OnSessionAvailable| on the receiver side. | |
47 // | |
48 // scoped_ptr<OffscreenPresentationSession> controller_session = | |
49 // manager->CreateOffscreenPresentationConnection( | |
50 // presentation_id, controller_frame_id); | |
51 // [Calls methods in |controller_session| to send/receive messages, etc.] | |
52 // | |
53 // A controller or receiver session leaves the offscreen presentation (e.g., | |
54 // due to navigation) by destroying the OffscreenPresentationSession object | |
55 // returned by this class. | |
56 // | |
57 // Receiver is no longer associated with an offscreen presentation. | |
58 // Receiver unregisters with OffscreenPresentationManager. Note that all | |
59 // sessions must be destroyed before unregistration. | |
miu
2015/10/07 21:51:46
formatting: More text fits on this line. Also, ma
imcheng
2015/10/10 04:39:43
Done.
| |
60 // This will prevent additional controllers from establishing a connection with | |
61 // the receiver: | |
62 // | |
63 // receiver_sessions.clear(); | |
64 // manager->UnregisterOffscreenPresentationReceiver(presentation_id); | |
65 // | |
66 // This class is not thread safe. All functions must be invoked on the UI | |
67 // thread. All callbacks passed into this class will also be invoked on UI | |
68 // thread. | |
69 class OffscreenPresentationManager : public KeyedService { | |
70 private: | |
71 // Forward declarations for OffscreenPresentationSession. | |
72 class OffscreenPresentationConnection; | |
73 class OffscreenPresentation; | |
74 | |
75 public: | |
76 // RAII representation of either the controller or receiver endpoint of an | |
77 // offscreen presentation connection. Roughly corresponds to a | |
78 // PresentationSession | |
79 // object created as a result of PresentationRequest.start/reconnect (for | |
80 // controller) or navigator.presentation.receiver APIs (for receiver). | |
81 // Two instances, one representing the controller and other representing the | |
82 // receiver, are created and returned by OffscreenPresentationManager when | |
83 // |CreateOffscreenPresentationConnection| is called. | |
84 // The controller or receiver session may disconnect itself by destroying the | |
85 // instance. | |
86 // Instances must not outlive the OffscreenPresentationManager instance that | |
87 // created it. | |
88 class OffscreenPresentationSession { | |
89 public: | |
90 ~OffscreenPresentationSession(); | |
91 | |
92 // Sends |message| to the other endpoint of the connection. | |
93 // |callback| will be invoked with whether the message was sent. | |
94 void SendMessage(scoped_ptr<content::PresentationSessionMessage> message, | |
95 const content::SendMessageCallback& callback); | |
96 // Listens for messages from the other endpoint of the connection. | |
97 // |callback| will be invoked with messages whenever there are messages. | |
98 void ListenForMessages( | |
99 const content::PresentationSessionMessageCallback& callback); | |
100 void ListenForStateChanges( | |
101 content::PresentationSessionStateListener* listener); | |
102 | |
103 private: | |
104 friend class OffscreenPresentation; | |
105 friend class OffscreenPresentationConnection; | |
106 | |
107 OffscreenPresentationSession(bool is_controller, | |
108 OffscreenPresentationConnection* connection); | |
109 void OnSessionStateChanged(content::PresentationSessionState state); | |
110 void OnMessageReceived(scoped_ptr<content::PresentationSessionMessage>); | |
111 | |
112 const bool is_controller_; | |
113 OffscreenPresentationConnection* connection_; | |
114 content::PresentationSessionMessageCallback messages_callback_; | |
115 content::PresentationSessionStateListener* state_change_listener_; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationSession); | |
118 }; | |
119 | |
120 ~OffscreenPresentationManager() override; | |
121 | |
122 using ReceiverSessionAvailableCallback = | |
123 base::Callback<void(scoped_ptr<OffscreenPresentationSession>)>; | |
124 | |
125 // Registers an offscreen presentation given by |presentation_id|. | |
126 // Enables controller frames to call |CreateOffscreenPresentationConnection| | |
127 // with the same presentation ID to establish a connection with the receiver | |
128 // side of the presentation. | |
129 // Whenever a connection is established, |receiver_available_callback| will be | |
130 // invoked with a OffscreenPresentationSession that represents the receiver | |
131 // side of the resulting connection. | |
132 void RegisterOffscreenPresentationReceiver( | |
133 const std::string& presentation_id, | |
134 const ReceiverSessionAvailableCallback& receiver_available_callback); | |
135 | |
136 // Unregisters a previously registered offscreen presentation. After this | |
137 // call, controller frames will no longer be able to establish connections | |
138 // to the presentation. The ReceiverSessionAvailableCallback previously | |
139 // registered will be removed and will no longer be invoked. | |
140 // NOTE: before this call can be made, all OffscreenPresentationSession | |
miu
2015/10/07 21:51:46
This "note" comment makes me wonder: Could you enf
imcheng
2015/10/10 04:39:43
That could work. I separate the two calls because
| |
141 // owned by the receiver must be destroyed first. | |
142 void UnregisterOffscreenPresentationReceiver( | |
143 const std::string& presentation_id); | |
144 | |
145 // Creates a connection to the receiver side of an offscreen presentation | |
146 // given by |presentation_id| registered with this instance, with the frame | |
147 // |controller_frame_id| as the controller side of the connection. | |
148 // Returns nullptr if the offscreen presentation is not already registered. | |
149 // Returns a OffscreenPresentationSession object representing the controller | |
150 // side of the resulting connection. In addition, | |
151 // |receiver_available_callback_| will be invoked with a | |
miu
2015/10/07 21:51:46
nit: To clarify, might I suggest this last sentenc
imcheng
2015/10/10 04:39:43
Thanks. I made some modification to your suggestio
| |
152 // OffscreenPresentationSession object representing the receiver side of the | |
153 // connection. | |
154 scoped_ptr<OffscreenPresentationSession> ConnectToOffscreenPresentation( | |
155 const std::string& presentation_id, | |
156 const RenderFrameHostId& controller_frame_id); | |
157 | |
158 private: | |
159 friend class OffscreenPresentationManagerFactory; | |
160 | |
161 // Represents a connection between a controller session and a receiver session | |
162 // of an offscreen presentation and coordinates the exchange of information | |
163 // between them. | |
164 // Note that each offscreen presentation may contain multiple connections. | |
165 class OffscreenPresentationConnection { | |
166 public: | |
167 ~OffscreenPresentationConnection(); | |
168 void RemoveControllerSession(); | |
169 void RemoveReceiverSession(); | |
170 void SendMessageToController( | |
171 scoped_ptr<content::PresentationSessionMessage> message, | |
172 const content::SendMessageCallback& callback); | |
173 void SendMessageToReceiver( | |
174 scoped_ptr<content::PresentationSessionMessage> message, | |
175 const content::SendMessageCallback& callback); | |
176 | |
177 private: | |
178 friend class OffscreenPresentation; | |
179 OffscreenPresentationConnection( | |
180 const RenderFrameHostId& controller_frame_id, | |
181 OffscreenPresentation* presentation); | |
182 void Init(OffscreenPresentationSession* controller, | |
183 OffscreenPresentationSession* receiver); | |
184 void RemoveSession(OffscreenPresentationSession* session); | |
185 void SendMessage(OffscreenPresentationSession* session, | |
186 scoped_ptr<content::PresentationSessionMessage> message, | |
187 const content::SendMessageCallback& callback); | |
188 | |
189 const RenderFrameHostId controller_frame_id_; | |
190 OffscreenPresentation* const presentation_; | |
191 | |
192 // Not owned by this class. | |
193 OffscreenPresentationSession* controller_; | |
194 OffscreenPresentationSession* receiver_; | |
195 | |
196 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationConnection); | |
197 }; | |
198 | |
199 // Represents an offscreen presentation registered with | |
200 // OffscreenPresentationManager. | |
201 // Maintains set of connections to the presentation. | |
202 // Contains callback to the receiver to inform it of new connections | |
203 // established from a controller. | |
204 class OffscreenPresentation { | |
205 public: | |
206 OffscreenPresentation( | |
207 const std::string& presentation_id, | |
208 const ReceiverSessionAvailableCallback& receiver_available_callback, | |
209 OffscreenPresentationManager* manager); | |
210 ~OffscreenPresentation(); | |
211 | |
212 scoped_ptr<OffscreenPresentationSession> AddConnection( | |
213 const RenderFrameHostId& controller_frame_id); | |
214 | |
215 private: | |
216 friend class OffscreenPresentationConnection; | |
217 | |
218 void RemoveConnection(const RenderFrameHostId& controller_frame_id); | |
219 | |
220 const std::string presentation_id_; | |
221 ReceiverSessionAvailableCallback receiver_available_callback_; | |
222 base::ScopedPtrMap<RenderFrameHostId, | |
223 scoped_ptr<OffscreenPresentationConnection>> | |
224 connections_; | |
225 OffscreenPresentationManager* const manager_; | |
226 | |
227 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentation); | |
228 }; | |
229 | |
230 // Used by OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext. | |
231 OffscreenPresentationManager(); | |
232 | |
233 // Maps from presentation ID to OffscreenPresentation. | |
234 base::ScopedPtrMap<std::string, scoped_ptr<OffscreenPresentation>> | |
235 offscreen_presentations_; | |
236 | |
237 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationManager); | |
238 }; | |
239 | |
240 } // namespace media_router | |
241 | |
242 #endif // CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_ | |
OLD | NEW |