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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 ~PresentationServiceImpl() override; | 48 ~PresentationServiceImpl() override; |
49 | 49 |
50 // Static factory method to create an instance of PresentationServiceImpl. | 50 // Static factory method to create an instance of PresentationServiceImpl. |
51 // |render_frame_host|: The RFH the instance is associated with. | 51 // |render_frame_host|: The RFH the instance is associated with. |
52 // |request|: The instance will be bound to this request. Used for Mojo setup. | 52 // |request|: The instance will be bound to this request. Used for Mojo setup. |
53 static void CreateMojoService( | 53 static void CreateMojoService( |
54 RenderFrameHost* render_frame_host, | 54 RenderFrameHost* render_frame_host, |
55 mojo::InterfaceRequest<presentation::PresentationService> request); | 55 mojo::InterfaceRequest<presentation::PresentationService> request); |
56 | 56 |
57 private: | 57 private: |
58 using ScreenAvailabilityMojoCallback = | 58 friend class PresentationServiceImplTest; |
59 mojo::Callback<void(mojo::String, bool)>; | 59 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, Reset); |
| 60 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, DidNavigateThisFrame); |
| 61 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 62 DidNavigateOtherFrame); |
| 63 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, ThisRenderFrameDeleted); |
| 64 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 65 OtherRenderFrameDeleted); |
| 66 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, DelegateFails); |
| 67 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 68 SetDefaultPresentationUrl); |
| 69 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 70 SetSameDefaultPresentationUrl); |
| 71 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 72 ClearDefaultPresentationUrl); |
| 73 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 74 ListenForDefaultSessionStart); |
| 75 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 76 ListenForDefaultSessionStartAfterSet); |
| 77 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 78 DefaultSessionStartReset); |
| 79 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 80 ReceiveSessionMessagesAfterReset); |
| 81 |
60 using NewSessionMojoCallback = | 82 using NewSessionMojoCallback = |
61 mojo::Callback<void(presentation::PresentationSessionInfoPtr, | 83 mojo::Callback<void(presentation::PresentationSessionInfoPtr, |
62 presentation::PresentationErrorPtr)>; | 84 presentation::PresentationErrorPtr)>; |
63 using DefaultSessionMojoCallback = | 85 using DefaultSessionMojoCallback = |
64 mojo::Callback<void(presentation::PresentationSessionInfoPtr)>; | 86 mojo::Callback<void(presentation::PresentationSessionInfoPtr)>; |
65 using SessionStateCallback = | 87 using SessionStateCallback = |
66 mojo::Callback<void(presentation::PresentationSessionInfoPtr, | 88 mojo::Callback<void(presentation::PresentationSessionInfoPtr, |
67 presentation::PresentationSessionState)>; | 89 presentation::PresentationSessionState)>; |
68 using SessionMessagesCallback = | 90 using SessionMessagesCallback = |
69 mojo::Callback<void(mojo::Array<presentation::SessionMessagePtr>)>; | 91 mojo::Callback<void(mojo::Array<presentation::SessionMessagePtr>)>; |
70 using SendMessageMojoCallback = mojo::Callback<void(bool)>; | 92 using SendMessageMojoCallback = mojo::Callback<void(bool)>; |
71 | 93 |
72 // A helper data class used by PresentationServiceImpl to do bookkeeping | 94 // Listener implementation owned by PresentationServiceImpl. An instance of |
73 // of currently registered screen availability listeners. | 95 // this is created when an |onavailablechange| handler is added. |
74 // An instance of this class is a simple state machine that waits for both | 96 // The instance receives screen availability results from the embedder and |
75 // the available bit and the Mojo callback to become available. | 97 // propagates results back to PresentationServiceImpl. |
76 // Once this happens, the Mojo callback will be invoked with the available | 98 class CONTENT_EXPORT ScreenAvailabilityListenerImpl |
77 // bit, and the state machine will reset. | |
78 // The available bit is obtained from the embedder's media router. | |
79 // The callback is obtained from the renderer via PresentationServiceImpl's | |
80 // ListenForScreenAvailability(). | |
81 class CONTENT_EXPORT ScreenAvailabilityContext | |
82 : public PresentationScreenAvailabilityListener { | 99 : public PresentationScreenAvailabilityListener { |
83 public: | 100 public: |
84 explicit ScreenAvailabilityContext( | 101 ScreenAvailabilityListenerImpl( |
85 const std::string& presentation_url); | 102 const std::string& presentation_url, |
86 ~ScreenAvailabilityContext() override; | 103 PresentationServiceImpl* service); |
87 | 104 ~ScreenAvailabilityListenerImpl() override; |
88 // If available bit exists, |callback| will be invoked with the bit and | |
89 // this state machine will reset. | |
90 // Otherwise |callback| is saved for later use. | |
91 // |callback|: Callback to the client of PresentationService | |
92 // (i.e. the renderer) that screen availability has changed, via Mojo. | |
93 void CallbackReceived(const ScreenAvailabilityMojoCallback& callback); | |
94 | 105 |
95 // PresentationScreenAvailabilityListener implementation. | 106 // PresentationScreenAvailabilityListener implementation. |
96 std::string GetPresentationUrl() const override; | 107 std::string GetPresentationUrl() const override; |
97 | |
98 // If callback exists, it will be invoked with |available| and | |
99 // this state machine will reset. | |
100 // Otherwise |available| is saved for later use. | |
101 // |available|: New screen availability for the presentation URL. | |
102 void OnScreenAvailabilityChanged(bool available) override; | 108 void OnScreenAvailabilityChanged(bool available) override; |
103 | 109 |
104 // Pass this context's queued callbacks to another context. | |
105 void PassPendingCallbacks(ScreenAvailabilityContext* other); | |
106 | |
107 // Indicates if this context has any pending callbacks. | |
108 bool HasPendingCallbacks() const; | |
109 | |
110 private: | 110 private: |
111 std::string presentation_url_; | 111 const std::string presentation_url_; |
112 ScopedVector<ScreenAvailabilityMojoCallback> callbacks_; | 112 PresentationServiceImpl* const service_; |
113 scoped_ptr<bool> available_ptr_; | |
114 }; | 113 }; |
115 | 114 |
116 class CONTENT_EXPORT DefaultSessionStartContext { | 115 class CONTENT_EXPORT DefaultSessionStartContext { |
117 public: | 116 public: |
118 DefaultSessionStartContext(); | 117 DefaultSessionStartContext(); |
119 ~DefaultSessionStartContext(); | 118 ~DefaultSessionStartContext(); |
120 | 119 |
121 // Adds a callback. May invoke the callback immediately if |session| using | 120 // Adds a callback. May invoke the callback immediately if |session| using |
122 // default presentation URL was already started. | 121 // default presentation URL was already started. |
123 void AddCallback(const DefaultSessionMojoCallback& callback); | 122 void AddCallback(const DefaultSessionMojoCallback& callback); |
(...skipping 24 matching lines...) Expand all Loading... |
148 | 147 |
149 const std::string& presentation_url() const { return presentation_url_; } | 148 const std::string& presentation_url() const { return presentation_url_; } |
150 const std::string& presentation_id() const { return presentation_id_; } | 149 const std::string& presentation_id() const { return presentation_id_; } |
151 | 150 |
152 private: | 151 private: |
153 const std::string presentation_url_; | 152 const std::string presentation_url_; |
154 const std::string presentation_id_; | 153 const std::string presentation_id_; |
155 NewSessionMojoCallback callback_; | 154 NewSessionMojoCallback callback_; |
156 }; | 155 }; |
157 | 156 |
158 friend class PresentationServiceImplTest; | |
159 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, Reset); | |
160 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
161 DidNavigateThisFrame); | |
162 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
163 DidNavigateNotThisFrame); | |
164 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
165 ThisRenderFrameDeleted); | |
166 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
167 NotThisRenderFrameDeleted); | |
168 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
169 SetDefaultPresentationUrl); | |
170 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
171 SetSameDefaultPresentationUrl); | |
172 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
173 ClearDefaultPresentationUrl); | |
174 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
175 ListenForDefaultSessionStart); | |
176 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
177 ListenForDefaultSessionStartAfterSet); | |
178 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
179 DefaultSessionStartReset); | |
180 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
181 ReceiveSessionMessagesAfterReset); | |
182 | |
183 // |render_frame_host|: The RFH this instance is associated with. | 157 // |render_frame_host|: The RFH this instance is associated with. |
184 // |web_contents|: The WebContents to observe. | 158 // |web_contents|: The WebContents to observe. |
185 // |delegate|: Where Presentation API requests are delegated to. Not owned | 159 // |delegate|: Where Presentation API requests are delegated to. Not owned |
186 // by this class. | 160 // by this class. |
187 PresentationServiceImpl( | 161 PresentationServiceImpl( |
188 RenderFrameHost* render_frame_host, | 162 RenderFrameHost* render_frame_host, |
189 WebContents* web_contents, | 163 WebContents* web_contents, |
190 PresentationServiceDelegate* delegate); | 164 PresentationServiceDelegate* delegate); |
191 | 165 |
192 // PresentationService implementation. | 166 // PresentationService implementation. |
193 void SetDefaultPresentationURL( | 167 void SetDefaultPresentationURL( |
194 const mojo::String& presentation_url, | 168 const mojo::String& presentation_url, |
195 const mojo::String& presentation_id) override; | 169 const mojo::String& presentation_id) override; |
196 void ListenForScreenAvailability( | 170 void SetClient(presentation::PresentationServiceClientPtr client) override; |
197 const mojo::String& presentation_url, | 171 void ListenForScreenAvailability() override; |
198 const ScreenAvailabilityMojoCallback& callback) override; | 172 void StopListeningForScreenAvailability() override; |
199 void RemoveScreenAvailabilityListener( | |
200 const mojo::String& presentation_url) override; | |
201 void ListenForDefaultSessionStart( | 173 void ListenForDefaultSessionStart( |
202 const DefaultSessionMojoCallback& callback) override; | 174 const DefaultSessionMojoCallback& callback) override; |
203 void StartSession( | 175 void StartSession( |
204 const mojo::String& presentation_url, | 176 const mojo::String& presentation_url, |
205 const mojo::String& presentation_id, | 177 const mojo::String& presentation_id, |
206 const NewSessionMojoCallback& callback) override; | 178 const NewSessionMojoCallback& callback) override; |
207 void JoinSession( | 179 void JoinSession( |
208 const mojo::String& presentation_url, | 180 const mojo::String& presentation_url, |
209 const mojo::String& presentation_id, | 181 const mojo::String& presentation_id, |
210 const NewSessionMojoCallback& callback) override; | 182 const NewSessionMojoCallback& callback) override; |
(...skipping 28 matching lines...) Expand all Loading... |
239 override; | 211 override; |
240 | 212 |
241 // Finds the callback from |pending_session_cbs_| using |request_session_id|. | 213 // Finds the callback from |pending_session_cbs_| using |request_session_id|. |
242 // If it exists, invoke it with |session| and |error|, then erase it from | 214 // If it exists, invoke it with |session| and |error|, then erase it from |
243 // |pending_session_cbs_|. | 215 // |pending_session_cbs_|. |
244 void RunAndEraseNewSessionMojoCallback( | 216 void RunAndEraseNewSessionMojoCallback( |
245 int request_session_id, | 217 int request_session_id, |
246 presentation::PresentationSessionInfoPtr session, | 218 presentation::PresentationSessionInfoPtr session, |
247 presentation::PresentationErrorPtr error); | 219 presentation::PresentationErrorPtr error); |
248 | 220 |
249 // Sets |default_presentation_url_| to |presentation_url| and informs the | 221 // Creates a new screen availability listener for |presentation_url| and |
250 // delegate of new default presentation URL and ID. | 222 // registers it with |delegate_|. Replaces the existing listener if any. |
251 void DoSetDefaultPresentationUrl( | 223 void ResetScreenAvailabilityListener(const std::string& presentation_url); |
252 const std::string& presentation_url, | |
253 const std::string& presentation_id); | |
254 | 224 |
255 // Removes all listeners and resets default presentation URL on this instance | 225 // Removes all listeners and resets default presentation URL on this instance |
256 // and informs the PresentationServiceDelegate of such. | 226 // and informs the PresentationServiceDelegate of such. |
257 void Reset(); | 227 void Reset(); |
258 | 228 |
259 // These functions are bound as base::Callbacks and passed to | 229 // These functions are bound as base::Callbacks and passed to |
260 // embedder's implementation of PresentationServiceDelegate for later | 230 // embedder's implementation of PresentationServiceDelegate for later |
261 // invocation. | 231 // invocation. |
262 void OnStartOrJoinSessionSucceeded( | 232 void OnStartOrJoinSessionSucceeded( |
263 bool is_start_session, | 233 bool is_start_session, |
(...skipping 28 matching lines...) Expand all Loading... |
292 int RegisterNewSessionCallback( | 262 int RegisterNewSessionCallback( |
293 const NewSessionMojoCallback& callback); | 263 const NewSessionMojoCallback& callback); |
294 | 264 |
295 // Flushes all pending new session callbacks with error responses. | 265 // Flushes all pending new session callbacks with error responses. |
296 void FlushNewSessionCallbacks(); | 266 void FlushNewSessionCallbacks(); |
297 | 267 |
298 // Invokes |callback| with an error. | 268 // Invokes |callback| with an error. |
299 static void InvokeNewSessionMojoCallbackWithError( | 269 static void InvokeNewSessionMojoCallbackWithError( |
300 const NewSessionMojoCallback& callback); | 270 const NewSessionMojoCallback& callback); |
301 | 271 |
302 // Gets the ScreenAvailabilityContext for |presentation_url|, or creates one | |
303 // if it does not exist. | |
304 ScreenAvailabilityContext* GetOrCreateAvailabilityContext( | |
305 const std::string& presentation_url); | |
306 | |
307 // Returns true if this object is associated with |render_frame_host|. | 272 // Returns true if this object is associated with |render_frame_host|. |
308 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; | 273 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; |
309 | 274 |
| 275 // Embedder-specific delegate to forward Presentation requests to. |
| 276 // May be null if embedder does not support Presentation API. |
310 PresentationServiceDelegate* delegate_; | 277 PresentationServiceDelegate* delegate_; |
311 | 278 |
312 // Map from presentation URL to its ScreenAvailabilityContext state machine. | 279 // Proxy to the PresentationServiceClient to send results (e.g., screen |
313 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> | 280 // availability) to. |
314 availability_contexts_; | 281 presentation::PresentationServiceClientPtr client_; |
315 | 282 |
316 std::string default_presentation_url_; | 283 std::string default_presentation_url_; |
317 std::string default_presentation_id_; | 284 std::string default_presentation_id_; |
318 | 285 |
| 286 scoped_ptr<ScreenAvailabilityListenerImpl> screen_availability_listener_; |
| 287 |
319 // We only allow one StartSession request to be processed at a time. | 288 // We only allow one StartSession request to be processed at a time. |
320 // StartSession requests are queued here. When a request has been processed, | 289 // StartSession requests are queued here. When a request has been processed, |
321 // it is removed from head of the queue. | 290 // it is removed from head of the queue. |
322 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_; | 291 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_; |
323 | 292 |
324 // Indicates that a StartSession request is currently being processed. | 293 // Indicates that a StartSession request is currently being processed. |
325 bool is_start_session_pending_; | 294 bool is_start_session_pending_; |
326 | 295 |
327 int next_request_session_id_; | 296 int next_request_session_id_; |
328 base::hash_map<int, linked_ptr<NewSessionMojoCallback>> pending_session_cbs_; | 297 base::hash_map<int, linked_ptr<NewSessionMojoCallback>> pending_session_cbs_; |
(...skipping 15 matching lines...) Expand all Loading... |
344 | 313 |
345 // NOTE: Weak pointers must be invalidated before all other member variables. | 314 // NOTE: Weak pointers must be invalidated before all other member variables. |
346 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; | 315 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; |
347 | 316 |
348 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 317 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
349 }; | 318 }; |
350 | 319 |
351 } // namespace content | 320 } // namespace content |
352 | 321 |
353 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 322 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
OLD | NEW |