Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: chrome/browser/media/router/offscreen_presentation_manager.h

Issue 1314413005: [Presentation API] 1-UA presentation support + presenter APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/macros.h"
13 #include "chrome/browser/media/router/render_frame_host_id.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "content/public/browser/presentation_service_delegate.h"
16
17 namespace media_router {
18
19 // Acts as an intermediary between the party that hosts an offscreen
20 // presentation (AKA the receiver) and the render frames that start/connect and
21 // control the presentation (AKA the controller frames). Coordinates the
22 // (un)registration of receivers and controller frames in the context of an
23 // offscreen presentation to enable the exchange of messages and notification
24 // of state change to/from the other side.
25 //
26 // Example usage:
27 //
28 // Receiver is created to host the offscreen presentation and registers itself
29 // so that controller frames can connect to it:
30 //
31 // OffscreenPresentationManager* manager =
32 // OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext(
33 // context);
34 // manager->RegisterOffscreenPresentationReceiver(presentation_id,
35 // base::Bind(&OnConnectionAvailable));
36 // ...
37 // void OnConnectionAvailable(
38 // std::unique_ptr<OffscreenPresentationConnection> receiver_connection) {
39 // [Retains |receiver_connection| to send/receive messages in response to
40 // send()]
41 // receiver_connection.push_back(std::move(receiver_connection));
42 // }
43 //
44 // Controller frame establishes connection with the receiver side, resulting
45 // in a session with the two endpoints being the controller connection (given
46 // to controller frame) and the receiver connection (given to receiver). Note
47 // calling this will trigger |OnConnectionAvailable| on the receiver side.
48 //
49 // std::unique_ptr<OffscreenPresentationConnection> controller_connection =
50 // manager->ConnectToOffscreenPresentation(
51 // presentation_id, controller_frame_id);
52 // [Calls methods in |controller_connection| to send/receive messages, etc.]
53 //
54 // A controller or receiver connection leaves the offscreen presentation (e.g.,
55 // due to navigation) by destroying the OffscreenPresentationConnection object
56 // returned by this class.
57 //
58 // When the receiver is no longer associated with an offscreen presentation, it
59 // shall destroy all outstanding connections and then unregister itself with
60 // OffscreenPresentationManager. Unregistration will prevent additional
61 // controllers from establishing a connection with the receiver:
62 //
63 // receiver_connections.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 OffscreenPresentationConnection.
72 class OffscreenPresentationSession;
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 // PresentationConnection object created as a result of
79 // PresentationRequest.start/reconnect (for controller) or
80 // 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 // |ConnectToOffscreenPresentation| is called.
84 // The controller or receiver connection may disconnect itself by destroying
85 // the instance.
86 // Instances must not outlive the OffscreenPresentationManager instance that
87 // created it.
88 class OffscreenPresentationConnection {
89 public:
90 ~OffscreenPresentationConnection();
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(
95 std::unique_ptr<content::PresentationSessionMessage> message,
96 const content::SendMessageCallback& callback);
97
98 // Listens for messages from the other endpoint of the connection.
99 // |callback| will be invoked with messages whenever there are messages.
100 void ListenForMessages(
101 const content::PresentationSessionMessageCallback& callback);
102
103 // Listen for state changes to this connection. When a state change occurs,
104 // |callback| will be invoked with the new state.
105 void ListenForStateChange(
106 const content::PresentationConnectionStateChangedCallback& callback);
107
108 // If |session_| is not nullptr, removes itself from |session_|, and
109 // notifies the other endpoint of state change with |remove_reason|.
110 // |remove_reason|'s state must be CLOSED or TERMINATED.
111 // |session_| will be nullptr after this method is called.
112 // If |session_| is not nullptr when this object is destroyed, then this
113 // method will be called with {state = CLOSED, reason = WENT_AWAY}.
114 void RemoveFromPresentation(
115 const content::PresentationConnectionStateChangeInfo& remove_reason);
116
117 private:
118 friend class OffscreenPresentation;
119 friend class OffscreenPresentationSession;
120
121 explicit OffscreenPresentationConnection(
122 OffscreenPresentationSession* session);
123
124 // If this method is invoked with state = CLOSED or TERMINATED, then
125 // |session_| will be set to nullptr.
126 void OnConnectionStateChanged(
127 const content::PresentationConnectionStateChangeInfo& info);
128 void OnMessageReceived(
129 std::unique_ptr<content::PresentationSessionMessage>);
130
131 // Reference to the session which this connection is an endpoint of.
132 // Set to nullptr either this or the other endpoint has been removed from
133 // the presentation.
134 // Not owned by this class.
135 OffscreenPresentationSession* session_;
136
137 content::PresentationSessionMessageCallback messages_callback_;
138 content::PresentationConnectionStateChangedCallback state_change_callback_;
139
140 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationConnection);
141 };
142
143 ~OffscreenPresentationManager() override;
144
145 using ReceiverConnectionAvailableCallback =
146 base::Callback<void(std::unique_ptr<OffscreenPresentationConnection>)>;
147
148 // Registers an offscreen presentation given by |presentation_id|.
149 // Enables controller frames to call |ConnectToOffscreenPresentation|
150 // with the same presentation ID to establish a connection with the receiver
151 // side of the presentation.
152 // Whenever a connection is established, |receiver_available_callback| will be
153 // invoked with a OffscreenPresentationConnection that represents the receiver
154 // side of the resulting connection.
155 void RegisterOffscreenPresentationReceiver(
156 const std::string& presentation_id,
157 const ReceiverConnectionAvailableCallback& receiver_available_callback);
158
159 // Unregisters a previously registered offscreen presentation. After this
160 // call, controller frames will no longer be able to establish connections
161 // to the presentation. The ReceiverConnectionAvailableCallback previously
162 // registered will be removed and will no longer be invoked.
163 // NOTE: before this call can be made, all OffscreenPresentationConnection
164 // owned by the receiver must be destroyed first.
165 void UnregisterOffscreenPresentationReceiver(
166 const std::string& presentation_id);
167
168 // Creates a connection to the receiver side of an offscreen presentation
169 // given by |presentation_id| registered with this instance, with the frame
170 // |controller_frame_id| as the controller side of the connection.
171 // Returns nullptr if the offscreen presentation is not already registered.
172 // Returns a OffscreenPresentationConnection object representing the
173 // controller
174 // side of the resulting connection. In addition, the entity that has
175 // registered the receiver will have its ReceiverConnectionAvailableCallback
176 // run
177 // to notify it that a new connection is now available.
178 std::unique_ptr<OffscreenPresentationConnection>
179 ConnectToOffscreenPresentation(const std::string& presentation_id,
180 const RenderFrameHostId& controller_frame_id);
181
182 private:
183 friend class OffscreenPresentationManagerFactory;
184 friend class OffscreenPresentationManagerTest;
185
186 // Represents a session consisting of a controller endpoint and a receiver
187 // endpoint of an offscreen presentation and coordinates the exchange of
188 // information between them.
189 // Note that each offscreen presentation may contain multiple sessions.
190 class OffscreenPresentationSession {
191 public:
192 ~OffscreenPresentationSession();
193 void RemoveConnection(
194 OffscreenPresentationConnection* connection,
195 const content::PresentationConnectionStateChangeInfo& remove_reason);
196 void SendMessageFrom(
197 OffscreenPresentationConnection* connection,
198 std::unique_ptr<content::PresentationSessionMessage> message,
199 const content::SendMessageCallback& callback);
200
201 private:
202 friend class OffscreenPresentation;
203 OffscreenPresentationSession(const RenderFrameHostId& controller_frame_id,
204 OffscreenPresentation* presentation);
205 void Init(OffscreenPresentationConnection* controller,
206 OffscreenPresentationConnection* receiver);
207
208 const RenderFrameHostId controller_frame_id_;
209
210 // Not owned by this class.
211 OffscreenPresentation* const presentation_;
212
213 // Not owned by this class.
214 OffscreenPresentationConnection* controller_;
215 OffscreenPresentationConnection* receiver_;
216
217 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationSession);
218 };
219
220 // Represents an offscreen presentation registered with
221 // OffscreenPresentationManager.
222 // Maintains set of sessions to the presentation.
223 // Contains callback to the receiver to inform it of new sessions
224 // established from a controller.
225 class OffscreenPresentation {
226 public:
227 OffscreenPresentation(
228 const std::string& presentation_id,
229 const ReceiverConnectionAvailableCallback& receiver_available_callback,
230 OffscreenPresentationManager* manager);
231 ~OffscreenPresentation();
232
233 std::unique_ptr<OffscreenPresentationConnection> AddSession(
234 const RenderFrameHostId& controller_frame_id);
235
236 private:
237 friend class OffscreenPresentationSession;
238
239 void RemoveSession(const RenderFrameHostId& controller_frame_id);
240
241 const std::string presentation_id_;
242 ReceiverConnectionAvailableCallback receiver_available_callback_;
243 std::map<RenderFrameHostId, std::unique_ptr<OffscreenPresentationSession>>
244 sessions_;
245
246 // Not owned by this class.
247 OffscreenPresentationManager* const manager_;
248
249 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentation);
250 };
251
252 // Used by OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext.
253 OffscreenPresentationManager();
254
255 // Maps from presentation ID to OffscreenPresentation.
256 std::map<std::string, std::unique_ptr<OffscreenPresentation>>
257 offscreen_presentations_;
258
259 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationManager);
260 };
261
262 } // namespace media_router
263
264 #endif // CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698