OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
6 #define CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 6 #define CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 #include "content/public/browser/web_contents_observer.h" | 24 #include "content/public/browser/web_contents_observer.h" |
25 #include "content/public/common/frame_navigate_params.h" | 25 #include "content/public/common/frame_navigate_params.h" |
26 | 26 |
27 namespace content { | 27 namespace content { |
28 | 28 |
29 struct FrameNavigateParams; | 29 struct FrameNavigateParams; |
30 struct LoadCommittedDetails; | 30 struct LoadCommittedDetails; |
31 struct PresentationSessionMessage; | 31 struct PresentationSessionMessage; |
32 class RenderFrameHost; | 32 class RenderFrameHost; |
33 | 33 |
34 using NewSessionMojoCallback = mojo::Callback< | |
35 void(presentation::PresentationSessionInfoPtr, | |
36 presentation::PresentationErrorPtr)>; | |
37 | |
34 // Implementation of Mojo PresentationService. | 38 // Implementation of Mojo PresentationService. |
35 // It handles Presentation API requests coming from Blink / renderer process | 39 // It handles Presentation API requests coming from Blink / renderer process |
36 // and delegates the requests to the embedder's media router via | 40 // and delegates the requests to the embedder's media router via |
37 // PresentationServiceDelegate. | 41 // PresentationServiceDelegate. |
38 // An instance of this class tied to a RenderFrameHost and listens to events | 42 // An instance of this class tied to a RenderFrameHost and listens to events |
39 // related to the RFH via implementing WebContentsObserver. | 43 // related to the RFH via implementing WebContentsObserver. |
40 // This class is instantiated on-demand via Mojo's ConnectToRemoteService | 44 // This class is instantiated on-demand via Mojo's ConnectToRemoteService |
41 // from the renderer when the first presentation API request is handled. | 45 // from the renderer when the first presentation API request is handled. |
42 class CONTENT_EXPORT PresentationServiceImpl | 46 class CONTENT_EXPORT PresentationServiceImpl |
43 : public NON_EXPORTED_BASE(presentation::PresentationService), | 47 : public NON_EXPORTED_BASE(presentation::PresentationService), |
(...skipping 27 matching lines...) Expand all Loading... | |
71 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 75 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
72 ClearDefaultPresentationUrl); | 76 ClearDefaultPresentationUrl); |
73 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 77 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
74 ListenForDefaultSessionStart); | 78 ListenForDefaultSessionStart); |
75 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 79 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
76 ListenForDefaultSessionStartAfterSet); | 80 ListenForDefaultSessionStartAfterSet); |
77 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 81 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
78 DefaultSessionStartReset); | 82 DefaultSessionStartReset); |
79 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 83 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
80 ReceiveSessionMessagesAfterReset); | 84 ReceiveSessionMessagesAfterReset); |
85 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
86 MaxPendingStartSessionRequests); | |
87 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
88 MaxPendingJoinSessionRequests); | |
81 | 89 |
82 using NewSessionMojoCallback = | 90 // Maximum number of queued StartSession or JoinSession requests. |
83 mojo::Callback<void(presentation::PresentationSessionInfoPtr, | 91 static const int kMaxNumQueuedSessionRequests = 10; |
84 presentation::PresentationErrorPtr)>; | 92 |
85 using DefaultSessionMojoCallback = | 93 using DefaultSessionMojoCallback = |
86 mojo::Callback<void(presentation::PresentationSessionInfoPtr)>; | 94 mojo::Callback<void(presentation::PresentationSessionInfoPtr)>; |
87 using SessionStateCallback = | 95 using SessionStateCallback = |
88 mojo::Callback<void(presentation::PresentationSessionInfoPtr, | 96 mojo::Callback<void(presentation::PresentationSessionInfoPtr, |
89 presentation::PresentationSessionState)>; | 97 presentation::PresentationSessionState)>; |
90 using SessionMessagesCallback = | 98 using SessionMessagesCallback = |
91 mojo::Callback<void(mojo::Array<presentation::SessionMessagePtr>)>; | 99 mojo::Callback<void(mojo::Array<presentation::SessionMessagePtr>)>; |
92 using SendMessageMojoCallback = mojo::Callback<void(bool)>; | 100 using SendMessageMojoCallback = mojo::Callback<void(bool)>; |
93 | 101 |
94 // Listener implementation owned by PresentationServiceImpl. An instance of | 102 // Listener implementation owned by PresentationServiceImpl. An instance of |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 | 134 |
127 private: | 135 private: |
128 // Flush all queued callbacks by invoking them with null | 136 // Flush all queued callbacks by invoking them with null |
129 // PresentationSessionInfoPtr. | 137 // PresentationSessionInfoPtr. |
130 void Reset(); | 138 void Reset(); |
131 | 139 |
132 ScopedVector<DefaultSessionMojoCallback> callbacks_; | 140 ScopedVector<DefaultSessionMojoCallback> callbacks_; |
133 scoped_ptr<PresentationSessionInfo> session_; | 141 scoped_ptr<PresentationSessionInfo> session_; |
134 }; | 142 }; |
135 | 143 |
136 // Context for a StartSession request. | 144 // Ensures the provided NewSessionMojoCallback is invoked exactly once |
145 // before it goes out of scope. | |
146 class NewSessionMojoCallbackWrapper { | |
147 public: | |
148 explicit NewSessionMojoCallbackWrapper( | |
149 const NewSessionMojoCallback& callback); | |
150 ~NewSessionMojoCallbackWrapper(); | |
151 | |
152 void Run(presentation::PresentationSessionInfoPtr session, | |
153 presentation::PresentationErrorPtr error); | |
154 | |
155 private: | |
156 NewSessionMojoCallback callback_; | |
157 | |
158 DISALLOW_COPY_AND_ASSIGN(NewSessionMojoCallbackWrapper); | |
159 }; | |
160 | |
161 // Context for a queued StartSession request. | |
137 class CONTENT_EXPORT StartSessionRequest { | 162 class CONTENT_EXPORT StartSessionRequest { |
138 public: | 163 public: |
139 StartSessionRequest(const std::string& presentation_url, | 164 StartSessionRequest(const std::string& presentation_url, |
140 const std::string& presentation_id, | 165 const std::string& presentation_id, |
141 const NewSessionMojoCallback& callback); | 166 const NewSessionMojoCallback& callback); |
142 ~StartSessionRequest(); | 167 ~StartSessionRequest(); |
143 | 168 |
144 // Retrieves the pending callback from this request, transferring ownership | 169 scoped_ptr<NewSessionMojoCallbackWrapper> PassCallback(); |
145 // to the caller. | |
146 NewSessionMojoCallback PassCallback(); | |
147 | 170 |
148 const std::string& presentation_url() const { return presentation_url_; } | 171 const std::string& presentation_url() const { return presentation_url_; } |
149 const std::string& presentation_id() const { return presentation_id_; } | 172 const std::string& presentation_id() const { return presentation_id_; } |
150 | 173 |
151 private: | 174 private: |
152 const std::string presentation_url_; | 175 const std::string presentation_url_; |
153 const std::string presentation_id_; | 176 const std::string presentation_id_; |
154 NewSessionMojoCallback callback_; | 177 scoped_ptr<NewSessionMojoCallbackWrapper> callback_wrapper_; |
178 | |
179 DISALLOW_COPY_AND_ASSIGN(StartSessionRequest); | |
155 }; | 180 }; |
156 | 181 |
157 // |render_frame_host|: The RFH this instance is associated with. | 182 // |render_frame_host|: The RFH this instance is associated with. |
158 // |web_contents|: The WebContents to observe. | 183 // |web_contents|: The WebContents to observe. |
159 // |delegate|: Where Presentation API requests are delegated to. Not owned | 184 // |delegate|: Where Presentation API requests are delegated to. Not owned |
160 // by this class. | 185 // by this class. |
161 PresentationServiceImpl( | 186 PresentationServiceImpl( |
162 RenderFrameHost* render_frame_host, | 187 RenderFrameHost* render_frame_host, |
163 WebContents* web_contents, | 188 WebContents* web_contents, |
164 PresentationServiceDelegate* delegate); | 189 PresentationServiceDelegate* delegate); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 content::RenderFrameHost* render_frame_host, | 228 content::RenderFrameHost* render_frame_host, |
204 const content::LoadCommittedDetails& details, | 229 const content::LoadCommittedDetails& details, |
205 const content::FrameNavigateParams& params) override; | 230 const content::FrameNavigateParams& params) override; |
206 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; | 231 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; |
207 | 232 |
208 // PresentationServiceDelegate::Observer | 233 // PresentationServiceDelegate::Observer |
209 void OnDelegateDestroyed() override; | 234 void OnDelegateDestroyed() override; |
210 void OnDefaultPresentationStarted(const PresentationSessionInfo& session) | 235 void OnDefaultPresentationStarted(const PresentationSessionInfo& session) |
211 override; | 236 override; |
212 | 237 |
213 // Finds the callback from |pending_session_cbs_| using |request_session_id|. | 238 // Finds the callback from |pending_join_session_cbs_| using |
239 // |request_session_id|. | |
214 // If it exists, invoke it with |session| and |error|, then erase it from | 240 // If it exists, invoke it with |session| and |error|, then erase it from |
215 // |pending_session_cbs_|. | 241 // |pending_join_session_cbs_|. |
216 void RunAndEraseNewSessionMojoCallback( | 242 void RunAndEraseJoinSessionMojoCallback( |
217 int request_session_id, | 243 int request_session_id, |
218 presentation::PresentationSessionInfoPtr session, | 244 presentation::PresentationSessionInfoPtr session, |
219 presentation::PresentationErrorPtr error); | 245 presentation::PresentationErrorPtr error); |
220 | 246 |
221 // Creates a new screen availability listener for |presentation_url| and | 247 // Creates a new screen availability listener for |presentation_url| and |
222 // registers it with |delegate_|. Replaces the existing listener if any. | 248 // registers it with |delegate_|. Replaces the existing listener if any. |
223 void ResetScreenAvailabilityListener(const std::string& presentation_url); | 249 void ResetScreenAvailabilityListener(const std::string& presentation_url); |
224 | 250 |
225 // Removes all listeners and resets default presentation URL on this instance | 251 // Removes all listeners and resets default presentation URL on this instance |
226 // and informs the PresentationServiceDelegate of such. | 252 // and informs the PresentationServiceDelegate of such. |
227 void Reset(); | 253 void Reset(); |
228 | 254 |
229 // These functions are bound as base::Callbacks and passed to | 255 // These functions are bound as base::Callbacks and passed to |
230 // embedder's implementation of PresentationServiceDelegate for later | 256 // embedder's implementation of PresentationServiceDelegate for later |
231 // invocation. | 257 // invocation. |
232 void OnStartOrJoinSessionSucceeded( | 258 void OnStartSessionSucceeded( |
233 bool is_start_session, | |
234 int request_session_id, | 259 int request_session_id, |
235 const PresentationSessionInfo& session_info); | 260 const PresentationSessionInfo& session_info); |
236 void OnStartOrJoinSessionError( | 261 void OnStartSessionError( |
237 bool is_start_session, | 262 int request_session_id, |
263 const PresentationError& error); | |
264 void OnJoinSessionSucceeded( | |
265 int request_session_id, | |
266 const PresentationSessionInfo& session_info); | |
267 void OnJoinSessionError( | |
238 int request_session_id, | 268 int request_session_id, |
239 const PresentationError& error); | 269 const PresentationError& error); |
240 void OnSendMessageCallback(); | 270 void OnSendMessageCallback(); |
241 | 271 |
242 // Requests delegate to start a session. | 272 // Requests delegate to start a session. |
243 void DoStartSession( | 273 void DoStartSession(scoped_ptr<StartSessionRequest> request); |
244 const std::string& presentation_url, | |
245 const std::string& presentation_id, | |
246 const NewSessionMojoCallback& callback); | |
247 | 274 |
248 // Passed to embedder's implementation of PresentationServiceDelegate for | 275 // Passed to embedder's implementation of PresentationServiceDelegate for |
249 // later invocation when session messages arrive. | 276 // later invocation when session messages arrive. |
250 // For optimization purposes, this method will empty the messages | 277 // For optimization purposes, this method will empty the messages |
251 // passed to it. | 278 // passed to it. |
252 void OnSessionMessages( | 279 void OnSessionMessages( |
253 scoped_ptr<ScopedVector<PresentationSessionMessage>> messages); | 280 scoped_ptr<ScopedVector<PresentationSessionMessage>> messages); |
254 | 281 |
255 // Removes the head of the queue (which represents the request that has just | 282 // Removes the head of the queue (which represents the request that has just |
256 // been processed). | 283 // been processed). |
257 // Checks if there are any queued StartSession requests and if so, executes | 284 // Checks if there are any queued StartSession requests and if so, executes |
258 // the first one in the queue. | 285 // the first one in the queue. |
259 void HandleQueuedStartSessionRequests(); | 286 void HandleQueuedStartSessionRequests(); |
260 | 287 |
261 // Associates |callback| with a unique request ID and stores it in a map. | 288 // Generates and returns an ID to use for a pending StartSession/JoinSession |
262 int RegisterNewSessionCallback( | 289 // request. |
263 const NewSessionMojoCallback& callback); | 290 int GetNextRequestSessionId(); |
291 | |
292 // Associates a JoinSession |callback| with a unique request ID and | |
293 // stores it in a map. | |
294 // Returns a positive value on success. | |
295 int RegisterJoinSessionCallback(const NewSessionMojoCallback& callback); | |
264 | 296 |
265 // Flushes all pending new session callbacks with error responses. | 297 // Flushes all pending new session callbacks with error responses. |
266 void FlushNewSessionCallbacks(); | 298 void FlushNewSessionCallbacks(); |
267 | 299 |
268 // Invokes |callback| with an error. | |
269 static void InvokeNewSessionMojoCallbackWithError( | |
270 const NewSessionMojoCallback& callback); | |
271 | |
272 // Returns true if this object is associated with |render_frame_host|. | 300 // Returns true if this object is associated with |render_frame_host|. |
273 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; | 301 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; |
274 | 302 |
275 // Embedder-specific delegate to forward Presentation requests to. | 303 // Embedder-specific delegate to forward Presentation requests to. |
276 // May be null if embedder does not support Presentation API. | 304 // May be null if embedder does not support Presentation API. |
277 PresentationServiceDelegate* delegate_; | 305 PresentationServiceDelegate* delegate_; |
278 | 306 |
279 // Proxy to the PresentationServiceClient to send results (e.g., screen | 307 // Proxy to the PresentationServiceClient to send results (e.g., screen |
280 // availability) to. | 308 // availability) to. |
281 presentation::PresentationServiceClientPtr client_; | 309 presentation::PresentationServiceClientPtr client_; |
282 | 310 |
283 std::string default_presentation_url_; | 311 std::string default_presentation_url_; |
284 std::string default_presentation_id_; | 312 std::string default_presentation_id_; |
285 | 313 |
286 scoped_ptr<ScreenAvailabilityListenerImpl> screen_availability_listener_; | 314 scoped_ptr<ScreenAvailabilityListenerImpl> screen_availability_listener_; |
287 | 315 |
288 // We only allow one StartSession request to be processed at a time. | 316 // We only allow one StartSession request to be processed at a time. |
289 // StartSession requests are queued here. When a request has been processed, | 317 // StartSession requests are queued here. When a request has been processed, |
290 // it is removed from head of the queue. | 318 // it is removed from head of the queue. |
291 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_; | 319 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_; |
292 | 320 |
293 // Indicates that a StartSession request is currently being processed. | 321 // Indicates that a StartSession request is currently being processed. |
whywhat
2015/05/13 11:11:33
nit: update comment?
imcheng (use chromium acct)
2015/05/13 17:07:45
Done.
| |
294 bool is_start_session_pending_; | 322 int start_session_request_id_; |
323 scoped_ptr<NewSessionMojoCallbackWrapper> pending_start_session_cb_; | |
295 | 324 |
325 // For JoinSession requests. | |
326 base::hash_map<int, linked_ptr<NewSessionMojoCallbackWrapper>> | |
327 pending_join_session_cbs_; | |
328 | |
329 // Request ID counter for use in StartSession/JoinSession. | |
296 int next_request_session_id_; | 330 int next_request_session_id_; |
whywhat
2015/05/13 11:11:33
not clear from the comment how this is used vs sta
imcheng (use chromium acct)
2015/05/13 17:07:45
Updated comment.
| |
297 base::hash_map<int, linked_ptr<NewSessionMojoCallback>> pending_session_cbs_; | |
298 | 331 |
299 scoped_ptr<DefaultSessionStartContext> default_session_start_context_; | 332 scoped_ptr<DefaultSessionStartContext> default_session_start_context_; |
300 | 333 |
301 // RAII binding of |this| to an Presentation interface request. | 334 // RAII binding of |this| to an Presentation interface request. |
302 // The binding is removed when binding_ is cleared or goes out of scope. | 335 // The binding is removed when binding_ is cleared or goes out of scope. |
303 scoped_ptr<mojo::Binding<presentation::PresentationService>> binding_; | 336 scoped_ptr<mojo::Binding<presentation::PresentationService>> binding_; |
304 | 337 |
305 // There can be only one send message request at a time. | 338 // There can be only one send message request at a time. |
306 scoped_ptr<SendMessageMojoCallback> send_message_callback_; | 339 scoped_ptr<SendMessageMojoCallback> send_message_callback_; |
307 | 340 |
308 scoped_ptr<SessionMessagesCallback> on_session_messages_callback_; | 341 scoped_ptr<SessionMessagesCallback> on_session_messages_callback_; |
309 | 342 |
310 // ID of the RenderFrameHost this object is associated with. | 343 // ID of the RenderFrameHost this object is associated with. |
311 int render_process_id_; | 344 int render_process_id_; |
312 int render_frame_id_; | 345 int render_frame_id_; |
313 | 346 |
314 // NOTE: Weak pointers must be invalidated before all other member variables. | 347 // NOTE: Weak pointers must be invalidated before all other member variables. |
315 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; | 348 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; |
316 | 349 |
317 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 350 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
318 }; | 351 }; |
319 | 352 |
320 } // namespace content | 353 } // namespace content |
321 | 354 |
322 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 355 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
OLD | NEW |