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 #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 render frames that start/connect and | |
| 22 // control the presentation (AKA the controller frames). Coordinates the | |
| 23 // (un)registration of receivers and controller frames in the context of an | |
| 24 // offscreen presentation to enable the exchange of messages and notification | |
| 25 // of state change to/from the other side. | |
| 26 // | |
| 27 // Example usage: | |
| 28 // | |
| 29 // Receiver is created to host the offscreen presentation and registers itself | |
| 30 // 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)); | |
| 37 // ... | |
| 38 // void OnSessionAvailable( | |
| 39 // scoped_ptr<OffscreenPresentationSession> receiver_session) { | |
| 40 // [Calls methods in |receiver_session| to send/receive messages, etc.] | |
|
mark a. foltz
2015/10/12 21:56:10
Nitpick: I wouldn't expect this to happen in this
imcheng
2015/10/17 01:00:23
Done.
| |
| 41 // } | |
| 42 // | |
| 43 // Controller frame establishes connection with the receiver side, resulting | |
| 44 // in a connection with the two endpoints being the controller session (given | |
| 45 // to controller frame) and the receiver session (given to receiver). Note | |
|
mark a. foltz
2015/10/12 21:56:10
To be clear, each controller <--> receiver relatio
imcheng
2015/10/17 01:00:23
Yes that's correct.
| |
| 46 // 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 // When the receiver is no longer associated with an offscreen presentation, it | |
|
mark a. foltz
2015/10/12 21:56:10
Which object is "the receiver" in this context? I
imcheng
2015/10/17 01:00:23
The receiver is the party that called RegisterOffs
| |
| 58 // shall destroy all outstanding sessions and then unregister itself with | |
| 59 // OffscreenPresentationManager. Unregistration will prevent additional | |
| 60 // controllers from establishing a connection with the receiver: | |
| 61 // | |
| 62 // receiver_sessions.clear(); | |
|
mark a. foltz
2015/10/12 21:56:10
Where does the receiver get |receiver_sessions|?
imcheng
2015/10/17 01:00:23
Yes. I added an example above using |receiver_sess
| |
| 63 // manager->UnregisterOffscreenPresentationReceiver(presentation_id); | |
| 64 // | |
| 65 // This class is not thread safe. All functions must be invoked on the UI | |
| 66 // thread. All callbacks passed into this class will also be invoked on UI | |
| 67 // thread. | |
| 68 class OffscreenPresentationManager : public KeyedService { | |
| 69 private: | |
| 70 // Forward declarations for OffscreenPresentationSession. | |
|
whywhat
2015/10/13 15:21:30
nit: I think this comment is redundant.
imcheng
2015/10/17 01:00:23
I added it to justify the extra 'private:' portion
| |
| 71 class OffscreenPresentationConnection; | |
| 72 class OffscreenPresentation; | |
| 73 | |
| 74 public: | |
| 75 // RAII representation of either the controller or receiver endpoint of an | |
| 76 // offscreen presentation connection. Roughly corresponds to a | |
| 77 // PresentationSession object created as a result of | |
| 78 // PresentationRequest.start/reconnect (for controller) or | |
| 79 // navigator.presentation.receiver APIs (for receiver). | |
| 80 // Two instances, one representing the controller and other representing the | |
| 81 // receiver, are created and returned by OffscreenPresentationManager when | |
| 82 // |CreateOffscreenPresentationConnection| is called. | |
| 83 // The controller or receiver session may disconnect itself by destroying the | |
| 84 // instance. | |
| 85 // Instances must not outlive the OffscreenPresentationManager instance that | |
| 86 // created it. | |
| 87 class OffscreenPresentationSession { | |
| 88 public: | |
| 89 ~OffscreenPresentationSession(); | |
| 90 | |
| 91 // Sends |message| to the other endpoint of the connection. | |
| 92 // |callback| will be invoked with whether the message was sent. | |
| 93 void SendMessage(scoped_ptr<content::PresentationSessionMessage> message, | |
| 94 const content::SendMessageCallback& callback); | |
| 95 // Listens for messages from the other endpoint of the connection. | |
| 96 // |callback| will be invoked with messages whenever there are messages. | |
| 97 void ListenForMessages( | |
| 98 const content::PresentationSessionMessageCallback& callback); | |
| 99 void ListenForStateChanges( | |
|
whywhat
2015/10/13 15:21:30
nit: missed a comment for this method?
imcheng
2015/10/17 01:00:23
Done.
| |
| 100 content::PresentationSessionStateListener* listener); | |
| 101 | |
| 102 private: | |
| 103 friend class OffscreenPresentation; | |
| 104 friend class OffscreenPresentationConnection; | |
| 105 | |
| 106 explicit OffscreenPresentationSession( | |
| 107 OffscreenPresentationConnection* connection); | |
| 108 void OnSessionStateChanged(content::PresentationSessionState state); | |
| 109 void OnMessageReceived(scoped_ptr<content::PresentationSessionMessage>); | |
| 110 | |
| 111 OffscreenPresentationConnection* connection_; | |
| 112 content::PresentationSessionMessageCallback messages_callback_; | |
| 113 content::PresentationSessionStateListener* state_change_listener_; | |
|
whywhat
2015/10/13 15:21:30
nit: make it const pointer?
imcheng
2015/10/17 01:00:23
It cannot be const since state_change_listener_ is
| |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationSession); | |
| 116 }; | |
| 117 | |
| 118 ~OffscreenPresentationManager() override; | |
| 119 | |
| 120 using ReceiverSessionAvailableCallback = | |
| 121 base::Callback<void(scoped_ptr<OffscreenPresentationSession>)>; | |
| 122 | |
| 123 // Registers an offscreen presentation given by |presentation_id|. | |
| 124 // Enables controller frames to call |CreateOffscreenPresentationConnection| | |
| 125 // with the same presentation ID to establish a connection with the receiver | |
| 126 // side of the presentation. | |
| 127 // Whenever a connection is established, |receiver_available_callback| will be | |
| 128 // invoked with a OffscreenPresentationSession that represents the receiver | |
| 129 // side of the resulting connection. | |
| 130 void RegisterOffscreenPresentationReceiver( | |
| 131 const std::string& presentation_id, | |
| 132 const ReceiverSessionAvailableCallback& receiver_available_callback); | |
| 133 | |
| 134 // Unregisters a previously registered offscreen presentation. After this | |
| 135 // call, controller frames will no longer be able to establish connections | |
| 136 // to the presentation. The ReceiverSessionAvailableCallback previously | |
| 137 // registered will be removed and will no longer be invoked. | |
| 138 // NOTE: before this call can be made, all OffscreenPresentationSession | |
| 139 // owned by the receiver must be destroyed first. | |
| 140 void UnregisterOffscreenPresentationReceiver( | |
| 141 const std::string& presentation_id); | |
| 142 | |
| 143 // Creates a connection to the receiver side of an offscreen presentation | |
| 144 // given by |presentation_id| registered with this instance, with the frame | |
| 145 // |controller_frame_id| as the controller side of the connection. | |
| 146 // Returns nullptr if the offscreen presentation is not already registered. | |
| 147 // Returns a OffscreenPresentationSession object representing the controller | |
| 148 // side of the resulting connection. In addition, the entity that has | |
| 149 // registered the receiver will have its ReceiverSessionAvailableCallback run | |
| 150 // to notify it that a new connection is now available. | |
| 151 scoped_ptr<OffscreenPresentationSession> ConnectToOffscreenPresentation( | |
| 152 const std::string& presentation_id, | |
| 153 const RenderFrameHostId& controller_frame_id); | |
| 154 | |
| 155 private: | |
| 156 friend class OffscreenPresentationManagerFactory; | |
| 157 | |
| 158 // Represents a connection between a controller session and a receiver session | |
| 159 // of an offscreen presentation and coordinates the exchange of information | |
| 160 // between them. | |
| 161 // Note that each offscreen presentation may contain multiple connections. | |
| 162 class OffscreenPresentationConnection { | |
| 163 public: | |
| 164 ~OffscreenPresentationConnection(); | |
| 165 void RemoveSession(OffscreenPresentationSession* session); | |
| 166 void SendMessageFrom( | |
| 167 OffscreenPresentationSession* session, | |
| 168 scoped_ptr<content::PresentationSessionMessage> message, | |
| 169 const content::SendMessageCallback& callback); | |
| 170 | |
| 171 private: | |
| 172 friend class OffscreenPresentation; | |
| 173 OffscreenPresentationConnection( | |
| 174 const RenderFrameHostId& controller_frame_id, | |
| 175 OffscreenPresentation* presentation); | |
| 176 void Init(OffscreenPresentationSession* controller, | |
| 177 OffscreenPresentationSession* receiver); | |
| 178 | |
| 179 const RenderFrameHostId controller_frame_id_; | |
| 180 OffscreenPresentation* const presentation_; | |
| 181 | |
| 182 // Not owned by this class. | |
|
whywhat
2015/10/13 15:21:30
nit: Should this comment relate to the pointer abo
whywhat
2015/10/13 15:21:30
nit: Should this comment relate to the pointer abo
imcheng
2015/10/17 01:00:23
Done.
imcheng
2015/10/17 01:00:23
Done.
| |
| 183 OffscreenPresentationSession* controller_; | |
|
whywhat
2015/10/13 15:21:31
nit: const pointers?
imcheng
2015/10/17 01:00:23
They are non-const because they are set during Ini
| |
| 184 OffscreenPresentationSession* receiver_; | |
| 185 | |
| 186 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationConnection); | |
| 187 }; | |
| 188 | |
| 189 // Represents an offscreen presentation registered with | |
| 190 // OffscreenPresentationManager. | |
| 191 // Maintains set of connections to the presentation. | |
| 192 // Contains callback to the receiver to inform it of new connections | |
| 193 // established from a controller. | |
| 194 class OffscreenPresentation { | |
| 195 public: | |
| 196 OffscreenPresentation( | |
| 197 const std::string& presentation_id, | |
| 198 const ReceiverSessionAvailableCallback& receiver_available_callback, | |
| 199 OffscreenPresentationManager* manager); | |
| 200 ~OffscreenPresentation(); | |
| 201 | |
| 202 scoped_ptr<OffscreenPresentationSession> AddConnection( | |
| 203 const RenderFrameHostId& controller_frame_id); | |
| 204 | |
| 205 private: | |
| 206 friend class OffscreenPresentationConnection; | |
| 207 | |
| 208 void RemoveConnection(const RenderFrameHostId& controller_frame_id); | |
| 209 | |
| 210 const std::string presentation_id_; | |
| 211 ReceiverSessionAvailableCallback receiver_available_callback_; | |
| 212 base::ScopedPtrMap<RenderFrameHostId, | |
| 213 scoped_ptr<OffscreenPresentationConnection>> | |
| 214 connections_; | |
| 215 OffscreenPresentationManager* const manager_; | |
| 216 | |
| 217 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentation); | |
| 218 }; | |
| 219 | |
| 220 // Used by OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext. | |
| 221 OffscreenPresentationManager(); | |
| 222 | |
| 223 // Maps from presentation ID to OffscreenPresentation. | |
| 224 base::ScopedPtrMap<std::string, scoped_ptr<OffscreenPresentation>> | |
| 225 offscreen_presentations_; | |
| 226 | |
| 227 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationManager); | |
| 228 }; | |
| 229 | |
| 230 } // namespace media_router | |
|
whywhat
2015/10/13 15:21:31
nit: I know this is kind of the current style of t
imcheng
2015/10/17 01:00:23
Done. LGTY?
| |
| 231 | |
| 232 #endif // CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_ | |
| OLD | NEW |