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

Side by Side Diff: content/browser/presentation/presentation_service_impl.h

Issue 1131053005: [Presentation API] Convert screen availability API into Client style. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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
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
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 DidNavigateNotThisFrame);
whywhat 2015/05/11 17:58:19 s/NotThis/Other?
imcheng (use chromium acct) 2015/05/11 22:01:54 Done.
63 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, ThisRenderFrameDeleted);
64 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
65 NotThisRenderFrameDeleted);
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 92
71 // A helper data class used by PresentationServiceImpl to do bookkeeping 93 // Listener implementation owned by PresentationServiceImpl. An instance of
72 // of currently registered screen availability listeners. 94 // this is created when an |onavailablechange| handler is added.
73 // An instance of this class is a simple state machine that waits for both 95 // This class receive screen availability results from the embedder and
mark a. foltz 2015/05/09 07:01:32 s/This class receive/The instance receives/
imcheng (use chromium acct) 2015/05/11 22:01:54 Done.
74 // the available bit and the Mojo callback to become available. 96 // propagates results back to PresentationServiceImpl.
75 // Once this happens, the Mojo callback will be invoked with the available 97 class CONTENT_EXPORT ScreenAvailabilityListenerImpl
76 // bit, and the state machine will reset.
77 // The available bit is obtained from the embedder's media router.
78 // The callback is obtained from the renderer via PresentationServiceImpl's
79 // ListenForScreenAvailability().
80 class CONTENT_EXPORT ScreenAvailabilityContext
81 : public PresentationScreenAvailabilityListener { 98 : public PresentationScreenAvailabilityListener {
82 public: 99 public:
83 explicit ScreenAvailabilityContext( 100 ScreenAvailabilityListenerImpl(
84 const std::string& presentation_url); 101 const std::string& presentation_url,
85 ~ScreenAvailabilityContext() override; 102 PresentationServiceImpl* service);
86 103 ~ScreenAvailabilityListenerImpl() override;
87 // If available bit exists, |callback| will be invoked with the bit and
88 // this state machine will reset.
89 // Otherwise |callback| is saved for later use.
90 // |callback|: Callback to the client of PresentationService
91 // (i.e. the renderer) that screen availability has changed, via Mojo.
92 void CallbackReceived(const ScreenAvailabilityMojoCallback& callback);
93 104
94 // PresentationScreenAvailabilityListener implementation. 105 // PresentationScreenAvailabilityListener implementation.
95 std::string GetPresentationUrl() const override; 106 std::string GetPresentationUrl() const override;
96
97 // If callback exists, it will be invoked with |available| and
98 // this state machine will reset.
99 // Otherwise |available| is saved for later use.
100 // |available|: New screen availability for the presentation URL.
101 void OnScreenAvailabilityChanged(bool available) override; 107 void OnScreenAvailabilityChanged(bool available) override;
102 108
103 // Pass this context's queued callbacks to another context.
104 void PassPendingCallbacks(ScreenAvailabilityContext* other);
105
106 // Indicates if this context has any pending callbacks.
107 bool HasPendingCallbacks() const;
108
109 private: 109 private:
110 std::string presentation_url_; 110 const std::string presentation_url_;
111 ScopedVector<ScreenAvailabilityMojoCallback> callbacks_; 111 PresentationServiceImpl* const service_;
112 scoped_ptr<bool> available_ptr_;
113 }; 112 };
114 113
115 class CONTENT_EXPORT DefaultSessionStartContext { 114 class CONTENT_EXPORT DefaultSessionStartContext {
116 public: 115 public:
117 DefaultSessionStartContext(); 116 DefaultSessionStartContext();
118 ~DefaultSessionStartContext(); 117 ~DefaultSessionStartContext();
119 118
120 // Adds a callback. May invoke the callback immediately if |session| using 119 // Adds a callback. May invoke the callback immediately if |session| using
121 // default presentation URL was already started. 120 // default presentation URL was already started.
122 void AddCallback(const DefaultSessionMojoCallback& callback); 121 void AddCallback(const DefaultSessionMojoCallback& callback);
(...skipping 24 matching lines...) Expand all
147 146
148 const std::string& presentation_url() const { return presentation_url_; } 147 const std::string& presentation_url() const { return presentation_url_; }
149 const std::string& presentation_id() const { return presentation_id_; } 148 const std::string& presentation_id() const { return presentation_id_; }
150 149
151 private: 150 private:
152 const std::string presentation_url_; 151 const std::string presentation_url_;
153 const std::string presentation_id_; 152 const std::string presentation_id_;
154 NewSessionMojoCallback callback_; 153 NewSessionMojoCallback callback_;
155 }; 154 };
156 155
157 friend class PresentationServiceImplTest;
158 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, Reset);
159 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
160 DidNavigateThisFrame);
161 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
162 DidNavigateNotThisFrame);
163 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
164 ThisRenderFrameDeleted);
165 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
166 NotThisRenderFrameDeleted);
167 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
168 SetDefaultPresentationUrl);
169 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
170 SetSameDefaultPresentationUrl);
171 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
172 ClearDefaultPresentationUrl);
173 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
174 ListenForDefaultSessionStart);
175 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
176 ListenForDefaultSessionStartAfterSet);
177 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
178 DefaultSessionStartReset);
179 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
180 ReceiveSessionMessagesAfterReset);
181
182 // |render_frame_host|: The RFH this instance is associated with. 156 // |render_frame_host|: The RFH this instance is associated with.
183 // |web_contents|: The WebContents to observe. 157 // |web_contents|: The WebContents to observe.
184 // |delegate|: Where Presentation API requests are delegated to. Not owned 158 // |delegate|: Where Presentation API requests are delegated to. Not owned
185 // by this class. 159 // by this class.
186 PresentationServiceImpl( 160 PresentationServiceImpl(
187 RenderFrameHost* render_frame_host, 161 RenderFrameHost* render_frame_host,
188 WebContents* web_contents, 162 WebContents* web_contents,
189 PresentationServiceDelegate* delegate); 163 PresentationServiceDelegate* delegate);
190 164
191 // PresentationService implementation. 165 // PresentationService implementation.
192 void SetDefaultPresentationURL( 166 void SetDefaultPresentationURL(
193 const mojo::String& presentation_url, 167 const mojo::String& presentation_url,
194 const mojo::String& presentation_id) override; 168 const mojo::String& presentation_id) override;
195 void ListenForScreenAvailability( 169 void SetClient(presentation::PresentationServiceClientPtr client) override;
196 const mojo::String& presentation_url, 170 void ListenForScreenAvailability() override;
mark a. foltz 2015/05/09 07:01:32 Does this need to be part of the public API, or ca
imcheng (use chromium acct) 2015/05/11 22:01:54 Not sure if I understood your comment correctly. T
mark a. foltz 2015/05/11 23:48:38 Okay, I understand better now. This triggers the
197 const ScreenAvailabilityMojoCallback& callback) override; 171 void StopListeningForScreenAvailability() override;
mark a. foltz 2015/05/09 07:01:32 Parallel comment to the one above
imcheng (use chromium acct) 2015/05/11 22:01:54 Ditto
198 void RemoveScreenAvailabilityListener(
199 const mojo::String& presentation_url) override;
200 void ListenForDefaultSessionStart( 172 void ListenForDefaultSessionStart(
201 const DefaultSessionMojoCallback& callback) override; 173 const DefaultSessionMojoCallback& callback) override;
202 void StartSession( 174 void StartSession(
203 const mojo::String& presentation_url, 175 const mojo::String& presentation_url,
204 const mojo::String& presentation_id, 176 const mojo::String& presentation_id,
205 const NewSessionMojoCallback& callback) override; 177 const NewSessionMojoCallback& callback) override;
206 void JoinSession( 178 void JoinSession(
207 const mojo::String& presentation_url, 179 const mojo::String& presentation_url,
208 const mojo::String& presentation_id, 180 const mojo::String& presentation_id,
209 const NewSessionMojoCallback& callback) override; 181 const NewSessionMojoCallback& callback) override;
(...skipping 25 matching lines...) Expand all
235 override; 207 override;
236 208
237 // Finds the callback from |pending_session_cbs_| using |request_session_id|. 209 // Finds the callback from |pending_session_cbs_| using |request_session_id|.
238 // If it exists, invoke it with |session| and |error|, then erase it from 210 // If it exists, invoke it with |session| and |error|, then erase it from
239 // |pending_session_cbs_|. 211 // |pending_session_cbs_|.
240 void RunAndEraseNewSessionMojoCallback( 212 void RunAndEraseNewSessionMojoCallback(
241 int request_session_id, 213 int request_session_id,
242 presentation::PresentationSessionInfoPtr session, 214 presentation::PresentationSessionInfoPtr session,
243 presentation::PresentationErrorPtr error); 215 presentation::PresentationErrorPtr error);
244 216
245 // Sets |default_presentation_url_| to |presentation_url| and informs the 217 // Creates a new screen availability listener for |presentation_url| and
246 // delegate of new default presentation URL and ID. 218 // registers it with |delegate_|. Replaces the existing listener if any.
mark a. foltz 2015/05/09 07:01:32 Since the listener is not returned to the caller d
imcheng (use chromium acct) 2015/05/11 22:01:54 A listener is bound to a URL, so if we were to sta
mark a. foltz 2015/05/11 23:48:38 SGTM
247 void DoSetDefaultPresentationUrl( 219 void CreateScreenAvailabilityListener(const std::string& presentation_url);
whywhat 2015/05/11 17:58:18 s/Create/[Re]Set? s/Create/Register? I'd expect a
imcheng (use chromium acct) 2015/05/11 22:01:54 Renamed to ResetScreenAvailabilityListener.
248 const std::string& presentation_url,
249 const std::string& presentation_id);
250 220
251 // Removes all listeners and resets default presentation URL on this instance 221 // Removes all listeners and resets default presentation URL on this instance
252 // and informs the PresentationServiceDelegate of such. 222 // and informs the PresentationServiceDelegate of such.
253 void Reset(); 223 void Reset();
254 224
255 // These two functions are bound as base::Callbacks and passed to 225 // These two functions are bound as base::Callbacks and passed to
256 // embedder's implementation of PresentationServiceDelegate for later 226 // embedder's implementation of PresentationServiceDelegate for later
257 // invocation. 227 // invocation.
258 void OnStartOrJoinSessionSucceeded( 228 void OnStartOrJoinSessionSucceeded(
259 bool is_start_session, 229 bool is_start_session,
(...skipping 27 matching lines...) Expand all
287 int RegisterNewSessionCallback( 257 int RegisterNewSessionCallback(
288 const NewSessionMojoCallback& callback); 258 const NewSessionMojoCallback& callback);
289 259
290 // Flushes all pending new session callbacks with error responses. 260 // Flushes all pending new session callbacks with error responses.
291 void FlushNewSessionCallbacks(); 261 void FlushNewSessionCallbacks();
292 262
293 // Invokes |callback| with an error. 263 // Invokes |callback| with an error.
294 static void InvokeNewSessionMojoCallbackWithError( 264 static void InvokeNewSessionMojoCallbackWithError(
295 const NewSessionMojoCallback& callback); 265 const NewSessionMojoCallback& callback);
296 266
297 // Gets the ScreenAvailabilityContext for |presentation_url|, or creates one
298 // if it does not exist.
299 ScreenAvailabilityContext* GetOrCreateAvailabilityContext(
300 const std::string& presentation_url);
301
302 // Returns true if this object is associated with |render_frame_host|. 267 // Returns true if this object is associated with |render_frame_host|.
303 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; 268 bool FrameMatches(content::RenderFrameHost* render_frame_host) const;
304 269
270 // Embedder-specific delegate to forward Presentation requests to.
271 // May be null if embedder does not support Presentation API.
305 PresentationServiceDelegate* delegate_; 272 PresentationServiceDelegate* delegate_;
306 273
307 // Map from presentation URL to its ScreenAvailabilityContext state machine. 274 // Proxy to the PresentationServiceClient to send results (e.g., screen
308 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> 275 // availability) to.
309 availability_contexts_; 276 presentation::PresentationServiceClientPtr client_;
310 277
311 std::string default_presentation_url_; 278 std::string default_presentation_url_;
312 std::string default_presentation_id_; 279 std::string default_presentation_id_;
313 280
281 scoped_ptr<ScreenAvailabilityListenerImpl> screen_availability_listener_;
282
314 // We only allow one StartSession request to be processed at a time. 283 // We only allow one StartSession request to be processed at a time.
315 // StartSession requests are queued here. When a request has been processed, 284 // StartSession requests are queued here. When a request has been processed,
316 // it is removed from head of the queue. 285 // it is removed from head of the queue.
317 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_; 286 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_;
318 287
319 // Indicates that a StartSession request is currently being processed. 288 // Indicates that a StartSession request is currently being processed.
320 bool is_start_session_pending_; 289 bool is_start_session_pending_;
321 290
322 int next_request_session_id_; 291 int next_request_session_id_;
323 base::hash_map<int, linked_ptr<NewSessionMojoCallback>> pending_session_cbs_; 292 base::hash_map<int, linked_ptr<NewSessionMojoCallback>> pending_session_cbs_;
(...skipping 12 matching lines...) Expand all
336 305
337 // NOTE: Weak pointers must be invalidated before all other member variables. 306 // NOTE: Weak pointers must be invalidated before all other member variables.
338 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; 307 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_;
339 308
340 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); 309 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl);
341 }; 310 };
342 311
343 } // namespace content 312 } // namespace content
344 313
345 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ 314 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698