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_id.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 // [Retains |receiver_session| to send/receive messages in response to |
| 41 // send()] |
| 42 // receiver_sessions.push_back(receiver_session.Pass()); |
| 43 // } |
| 44 // |
| 45 // Controller frame establishes connection with the receiver side, resulting |
| 46 // in a connection with the two endpoints being the controller session (given |
| 47 // to controller frame) and the receiver session (given to receiver). Note |
| 48 // calling this will trigger |OnSessionAvailable| on the receiver side. |
| 49 // |
| 50 // scoped_ptr<OffscreenPresentationSession> controller_session = |
| 51 // manager->CreateOffscreenPresentationConnection( |
| 52 // presentation_id, controller_frame_id); |
| 53 // [Calls methods in |controller_session| to send/receive messages, etc.] |
| 54 // |
| 55 // A controller or receiver session leaves the offscreen presentation (e.g., |
| 56 // due to navigation) by destroying the OffscreenPresentationSession object |
| 57 // returned by this class. |
| 58 // |
| 59 // When the receiver is no longer associated with an offscreen presentation, it |
| 60 // shall destroy all outstanding sessions and then unregister itself with |
| 61 // OffscreenPresentationManager. Unregistration will prevent additional |
| 62 // controllers from establishing a connection with the receiver: |
| 63 // |
| 64 // receiver_sessions.clear(); |
| 65 // manager->UnregisterOffscreenPresentationReceiver(presentation_id); |
| 66 // |
| 67 // This class is not thread safe. All functions must be invoked on the UI |
| 68 // thread. All callbacks passed into this class will also be invoked on UI |
| 69 // thread. |
| 70 class OffscreenPresentationManager : public KeyedService { |
| 71 private: |
| 72 // Forward declarations for OffscreenPresentationSession. |
| 73 class OffscreenPresentationConnection; |
| 74 class OffscreenPresentation; |
| 75 |
| 76 public: |
| 77 // RAII representation of either the controller or receiver endpoint of an |
| 78 // offscreen presentation connection. Roughly corresponds to a |
| 79 // PresentationSession object created as a result of |
| 80 // PresentationRequest.start/reconnect (for controller) or |
| 81 // navigator.presentation.receiver APIs (for receiver). |
| 82 // Two instances, one representing the controller and other representing the |
| 83 // receiver, are created and returned by OffscreenPresentationManager when |
| 84 // |CreateOffscreenPresentationConnection| is called. |
| 85 // The controller or receiver session may disconnect itself by destroying the |
| 86 // instance. |
| 87 // Instances must not outlive the OffscreenPresentationManager instance that |
| 88 // created it. |
| 89 class OffscreenPresentationSession { |
| 90 public: |
| 91 ~OffscreenPresentationSession(); |
| 92 |
| 93 // Sends |message| to the other endpoint of the connection. |
| 94 // |callback| will be invoked with whether the message was sent. |
| 95 void SendMessage(scoped_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 session. When a state change occurs, |
| 104 // |listener| will be notified. |
| 105 void ListenForStateChanges( |
| 106 content::PresentationSessionStateListener* listener); |
| 107 |
| 108 private: |
| 109 friend class OffscreenPresentation; |
| 110 friend class OffscreenPresentationConnection; |
| 111 |
| 112 explicit OffscreenPresentationSession( |
| 113 OffscreenPresentationConnection* connection); |
| 114 void OnSessionStateChanged(content::PresentationSessionState state); |
| 115 void OnMessageReceived(scoped_ptr<content::PresentationSessionMessage>); |
| 116 |
| 117 // Reference to the connection which this session is an endpoint of. |
| 118 // When the other endpoint of the connection is destroyed, this will be |
| 119 // set to nullptr. |
| 120 // Not owned by this class. |
| 121 OffscreenPresentationConnection* connection_; |
| 122 |
| 123 content::PresentationSessionMessageCallback messages_callback_; |
| 124 |
| 125 // Not owned by this class. |
| 126 content::PresentationSessionStateListener* state_change_listener_; |
| 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationSession); |
| 129 }; |
| 130 |
| 131 ~OffscreenPresentationManager() override; |
| 132 |
| 133 using ReceiverSessionAvailableCallback = |
| 134 base::Callback<void(scoped_ptr<OffscreenPresentationSession>)>; |
| 135 |
| 136 // Registers an offscreen presentation given by |presentation_id|. |
| 137 // Enables controller frames to call |CreateOffscreenPresentationConnection| |
| 138 // with the same presentation ID to establish a connection with the receiver |
| 139 // side of the presentation. |
| 140 // Whenever a connection is established, |receiver_available_callback| will be |
| 141 // invoked with a OffscreenPresentationSession that represents the receiver |
| 142 // side of the resulting connection. |
| 143 void RegisterOffscreenPresentationReceiver( |
| 144 const std::string& presentation_id, |
| 145 const ReceiverSessionAvailableCallback& receiver_available_callback); |
| 146 |
| 147 // Unregisters a previously registered offscreen presentation. After this |
| 148 // call, controller frames will no longer be able to establish connections |
| 149 // to the presentation. The ReceiverSessionAvailableCallback previously |
| 150 // registered will be removed and will no longer be invoked. |
| 151 // NOTE: before this call can be made, all OffscreenPresentationSession |
| 152 // owned by the receiver must be destroyed first. |
| 153 void UnregisterOffscreenPresentationReceiver( |
| 154 const std::string& presentation_id); |
| 155 |
| 156 // Creates a connection to the receiver side of an offscreen presentation |
| 157 // given by |presentation_id| registered with this instance, with the frame |
| 158 // |controller_frame_id| as the controller side of the connection. |
| 159 // Returns nullptr if the offscreen presentation is not already registered. |
| 160 // Returns a OffscreenPresentationSession object representing the controller |
| 161 // side of the resulting connection. In addition, the entity that has |
| 162 // registered the receiver will have its ReceiverSessionAvailableCallback run |
| 163 // to notify it that a new connection is now available. |
| 164 scoped_ptr<OffscreenPresentationSession> ConnectToOffscreenPresentation( |
| 165 const std::string& presentation_id, |
| 166 const RenderFrameHostId& controller_frame_id); |
| 167 |
| 168 private: |
| 169 friend class OffscreenPresentationManagerFactory; |
| 170 |
| 171 // Represents a connection between a controller session and a receiver session |
| 172 // of an offscreen presentation and coordinates the exchange of information |
| 173 // between them. |
| 174 // Note that each offscreen presentation may contain multiple connections. |
| 175 class OffscreenPresentationConnection { |
| 176 public: |
| 177 ~OffscreenPresentationConnection(); |
| 178 void RemoveSession(OffscreenPresentationSession* session); |
| 179 void SendMessageFrom( |
| 180 OffscreenPresentationSession* session, |
| 181 scoped_ptr<content::PresentationSessionMessage> message, |
| 182 const content::SendMessageCallback& callback); |
| 183 |
| 184 private: |
| 185 friend class OffscreenPresentation; |
| 186 OffscreenPresentationConnection( |
| 187 const RenderFrameHostId& controller_frame_id, |
| 188 OffscreenPresentation* presentation); |
| 189 void Init(OffscreenPresentationSession* controller, |
| 190 OffscreenPresentationSession* receiver); |
| 191 |
| 192 const RenderFrameHostId controller_frame_id_; |
| 193 |
| 194 // Not owned by this class. |
| 195 OffscreenPresentation* const presentation_; |
| 196 |
| 197 // Not owned by this class. |
| 198 OffscreenPresentationSession* controller_; |
| 199 OffscreenPresentationSession* receiver_; |
| 200 |
| 201 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationConnection); |
| 202 }; |
| 203 |
| 204 // Represents an offscreen presentation registered with |
| 205 // OffscreenPresentationManager. |
| 206 // Maintains set of connections to the presentation. |
| 207 // Contains callback to the receiver to inform it of new connections |
| 208 // established from a controller. |
| 209 class OffscreenPresentation { |
| 210 public: |
| 211 OffscreenPresentation( |
| 212 const std::string& presentation_id, |
| 213 const ReceiverSessionAvailableCallback& receiver_available_callback, |
| 214 OffscreenPresentationManager* manager); |
| 215 ~OffscreenPresentation(); |
| 216 |
| 217 scoped_ptr<OffscreenPresentationSession> AddConnection( |
| 218 const RenderFrameHostId& controller_frame_id); |
| 219 |
| 220 private: |
| 221 friend class OffscreenPresentationConnection; |
| 222 |
| 223 void RemoveConnection(const RenderFrameHostId& controller_frame_id); |
| 224 |
| 225 const std::string presentation_id_; |
| 226 ReceiverSessionAvailableCallback receiver_available_callback_; |
| 227 base::ScopedPtrMap<RenderFrameHostId, |
| 228 scoped_ptr<OffscreenPresentationConnection>> |
| 229 connections_; |
| 230 |
| 231 // Not owned by this class. |
| 232 OffscreenPresentationManager* const manager_; |
| 233 |
| 234 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentation); |
| 235 }; |
| 236 |
| 237 // Used by OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext. |
| 238 OffscreenPresentationManager(); |
| 239 |
| 240 // Maps from presentation ID to OffscreenPresentation. |
| 241 base::ScopedPtrMap<std::string, scoped_ptr<OffscreenPresentation>> |
| 242 offscreen_presentations_; |
| 243 |
| 244 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationManager); |
| 245 }; |
| 246 |
| 247 } // namespace media_router |
| 248 |
| 249 #endif // CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_ |
OLD | NEW |