1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_
#define CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_
#include <deque>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/containers/hash_tables.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "content/common/content_export.h"
#include "content/common/presentation/presentation_service.mojom.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/presentation_screen_availability_listener.h"
#include "content/public/browser/presentation_service_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/frame_navigate_params.h"
namespace content {
struct FrameNavigateParams;
struct LoadCommittedDetails;
struct PresentationSessionMessage;
class RenderFrameHost;
// Implementation of Mojo PresentationService.
// It handles Presentation API requests coming from Blink / renderer process
// and delegates the requests to the embedder's media router via
// PresentationServiceDelegate.
// An instance of this class tied to a RenderFrameHost and listens to events
// related to the RFH via implementing WebContentsObserver.
// This class is instantiated on-demand via Mojo's ConnectToRemoteService
// from the renderer when the first presentation API request is handled.
class CONTENT_EXPORT PresentationServiceImpl
: public NON_EXPORTED_BASE(presentation::PresentationService),
public mojo::ErrorHandler,
public WebContentsObserver,
public PresentationServiceDelegate::Observer {
public:
~PresentationServiceImpl() override;
// Static factory method to create an instance of PresentationServiceImpl.
// |render_frame_host|: The RFH the instance is associated with.
// |request|: The instance will be bound to this request. Used for Mojo setup.
static void CreateMojoService(
RenderFrameHost* render_frame_host,
mojo::InterfaceRequest<presentation::PresentationService> request);
private:
using ScreenAvailabilityMojoCallback =
mojo::Callback<void(mojo::String, bool)>;
using NewSessionMojoCallback =
mojo::Callback<void(presentation::PresentationSessionInfoPtr,
presentation::PresentationErrorPtr)>;
using DefaultSessionMojoCallback =
mojo::Callback<void(presentation::PresentationSessionInfoPtr)>;
using SessionStateCallback =
mojo::Callback<void(presentation::PresentationSessionInfoPtr,
presentation::PresentationSessionState)>;
using SessionMessagesCallback =
mojo::Callback<void(mojo::Array<presentation::SessionMessagePtr>)>;
// A helper data class used by PresentationServiceImpl to do bookkeeping
// of currently registered screen availability listeners.
// An instance of this class is a simple state machine that waits for both
// the available bit and the Mojo callback to become available.
// Once this happens, the Mojo callback will be invoked with the available
// bit, and the state machine will reset.
// The available bit is obtained from the embedder's media router.
// The callback is obtained from the renderer via PresentationServiceImpl's
// ListenForScreenAvailability().
class CONTENT_EXPORT ScreenAvailabilityContext
: public PresentationScreenAvailabilityListener {
public:
explicit ScreenAvailabilityContext(
const std::string& presentation_url);
~ScreenAvailabilityContext() override;
// If available bit exists, |callback| will be invoked with the bit and
// this state machine will reset.
// Otherwise |callback| is saved for later use.
// |callback|: Callback to the client of PresentationService
// (i.e. the renderer) that screen availability has changed, via Mojo.
void CallbackReceived(const ScreenAvailabilityMojoCallback& callback);
// PresentationScreenAvailabilityListener implementation.
std::string GetPresentationUrl() const override;
// If callback exists, it will be invoked with |available| and
// this state machine will reset.
// Otherwise |available| is saved for later use.
// |available|: New screen availability for the presentation URL.
void OnScreenAvailabilityChanged(bool available) override;
// Pass this context's queued callbacks to another context.
void PassPendingCallbacks(ScreenAvailabilityContext* other);
// Indicates if this context has any pending callbacks.
bool HasPendingCallbacks() const;
private:
std::string presentation_url_;
ScopedVector<ScreenAvailabilityMojoCallback> callbacks_;
scoped_ptr<bool> available_ptr_;
};
class CONTENT_EXPORT DefaultSessionStartContext {
public:
DefaultSessionStartContext();
~DefaultSessionStartContext();
// Adds a callback. May invoke the callback immediately if |session| using
// default presentation URL was already started.
void AddCallback(const DefaultSessionMojoCallback& callback);
// Sets the session info. Maybe invoke callbacks queued with AddCallback().
void set_session(const PresentationSessionInfo& session);
private:
// Flush all queued callbacks by invoking them with null
// PresentationSessionInfoPtr.
void Reset();
ScopedVector<DefaultSessionMojoCallback> callbacks_;
scoped_ptr<PresentationSessionInfo> session_;
};
// Context for a StartSession request.
class CONTENT_EXPORT StartSessionRequest {
public:
StartSessionRequest(const std::string& presentation_url,
const std::string& presentation_id,
const NewSessionMojoCallback& callback);
~StartSessionRequest();
// Retrieves the pending callback from this request, transferring ownership
// to the caller.
NewSessionMojoCallback PassCallback();
const std::string& presentation_url() const { return presentation_url_; }
const std::string& presentation_id() const { return presentation_id_; }
private:
const std::string presentation_url_;
const std::string presentation_id_;
NewSessionMojoCallback callback_;
};
friend class PresentationServiceImplTest;
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, Reset);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
DidNavigateThisFrame);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
DidNavigateNotThisFrame);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
ThisRenderFrameDeleted);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
NotThisRenderFrameDeleted);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
SetDefaultPresentationUrl);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
SetSameDefaultPresentationUrl);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
ClearDefaultPresentationUrl);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
ListenForDefaultSessionStart);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
ListenForDefaultSessionStartAfterSet);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
DefaultSessionStartReset);
FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest,
ReceiveSessionMessagesAfterReset);
// |render_frame_host|: The RFH this instance is associated with.
// |web_contents|: The WebContents to observe.
// |delegate|: Where Presentation API requests are delegated to. Not owned
// by this class.
PresentationServiceImpl(
RenderFrameHost* render_frame_host,
WebContents* web_contents,
PresentationServiceDelegate* delegate);
// PresentationService implementation.
void SetDefaultPresentationURL(
const mojo::String& presentation_url,
const mojo::String& presentation_id) override;
void ListenForScreenAvailability(
const mojo::String& presentation_url,
const ScreenAvailabilityMojoCallback& callback) override;
void RemoveScreenAvailabilityListener(
const mojo::String& presentation_url) override;
void ListenForDefaultSessionStart(
const DefaultSessionMojoCallback& callback) override;
void StartSession(
const mojo::String& presentation_url,
const mojo::String& presentation_id,
const NewSessionMojoCallback& callback) override;
void JoinSession(
const mojo::String& presentation_url,
const mojo::String& presentation_id,
const NewSessionMojoCallback& callback) override;
void CloseSession(
const mojo::String& presentation_url,
const mojo::String& presentation_id) override;
void ListenForSessionStateChange(
const SessionStateCallback& callback) override;
void ListenForSessionMessages(
const SessionMessagesCallback& callback) override;
// Creates a binding between this object and |request|.
void Bind(mojo::InterfaceRequest<presentation::PresentationService> request);
// mojo::ErrorHandler override.
// Note that this is called when the RenderFrameHost is deleted.
void OnConnectionError() override;
// WebContentsObserver override.
void DidNavigateAnyFrame(
content::RenderFrameHost* render_frame_host,
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) override;
void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
// PresentationServiceDelegate::Observer
void OnDelegateDestroyed() override;
void OnDefaultPresentationStarted(const PresentationSessionInfo& session)
override;
// Finds the callback from |pending_session_cbs_| using |request_session_id|.
// If it exists, invoke it with |session| and |error|, then erase it from
// |pending_session_cbs_|.
void RunAndEraseNewSessionMojoCallback(
int request_session_id,
presentation::PresentationSessionInfoPtr session,
presentation::PresentationErrorPtr error);
// Sets |default_presentation_url_| to |presentation_url| and informs the
// delegate of new default presentation URL and ID.
void DoSetDefaultPresentationUrl(
const std::string& presentation_url,
const std::string& presentation_id);
// Removes all listeners and resets default presentation URL on this instance
// and informs the PresentationServiceDelegate of such.
void Reset();
// These two functions are bound as base::Callbacks and passed to
// embedder's implementation of PresentationServiceDelegate for later
// invocation.
void OnStartOrJoinSessionSucceeded(
bool is_start_session,
int request_session_id,
const PresentationSessionInfo& session_info);
void OnStartOrJoinSessionError(
bool is_start_session,
int request_session_id,
const PresentationError& error);
// Requests delegate to start a session.
void DoStartSession(
const std::string& presentation_url,
const std::string& presentation_id,
const NewSessionMojoCallback& callback);
// Passed to embedder's implementation of PresentationServiceDelegate for
// later invocation when session messages arrive.
// For optimization purposes, this method will empty the messages
// passed to it.
void OnSessionMessages(
scoped_ptr<ScopedVector<PresentationSessionMessage>> messages);
// Removes the head of the queue (which represents the request that has just
// been processed).
// Checks if there are any queued StartSession requests and if so, executes
// the first one in the queue.
void HandleQueuedStartSessionRequests();
// Associates |callback| with a unique request ID and stores it in a map.
int RegisterNewSessionCallback(
const NewSessionMojoCallback& callback);
// Flushes all pending new session callbacks with error responses.
void FlushNewSessionCallbacks();
// Invokes |callback| with an error.
static void InvokeNewSessionMojoCallbackWithError(
const NewSessionMojoCallback& callback);
// Gets the ScreenAvailabilityContext for |presentation_url|, or creates one
// if it does not exist.
ScreenAvailabilityContext* GetOrCreateAvailabilityContext(
const std::string& presentation_url);
// Returns true if this object is associated with |render_frame_host|.
bool FrameMatches(content::RenderFrameHost* render_frame_host) const;
PresentationServiceDelegate* delegate_;
// Map from presentation URL to its ScreenAvailabilityContext state machine.
base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>>
availability_contexts_;
std::string default_presentation_url_;
std::string default_presentation_id_;
// We only allow one StartSession request to be processed at a time.
// StartSession requests are queued here. When a request has been processed,
// it is removed from head of the queue.
std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_;
// Indicates that a StartSession request is currently being processed.
bool is_start_session_pending_;
int next_request_session_id_;
base::hash_map<int, linked_ptr<NewSessionMojoCallback>> pending_session_cbs_;
scoped_ptr<DefaultSessionStartContext> default_session_start_context_;
// RAII binding of |this| to an Presentation interface request.
// The binding is removed when binding_ is cleared or goes out of scope.
scoped_ptr<mojo::Binding<presentation::PresentationService>> binding_;
scoped_ptr<SessionMessagesCallback> on_session_messages_callback_;
// ID of the RenderFrameHost this object is associated with.
int render_process_id_;
int render_frame_id_;
// NOTE: Weak pointers must be invalidated before all other member variables.
base::WeakPtrFactory<PresentationServiceImpl> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl);
};
} // namespace content
#endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_
|