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.
5 #ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_
6 #define CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/macros.h"
15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/scoped_vector.h"
18 #include "base/memory/weak_ptr.h"
19 #include "content/common/content_export.h"
20 #include "content/common/presentation/presentation_service.mojom.h"
21 #include "content/public/browser/navigation_details.h"
22 #include "content/public/browser/presentation_screen_availability_listener.h"
23 #include "content/public/browser/presentation_service_delegate.h"
24 #include "content/public/browser/web_contents_observer.h"
25 #include "content/public/common/frame_navigate_params.h"
29 struct FrameNavigateParams
;
30 struct LoadCommittedDetails
;
31 struct PresentationSessionMessage
;
32 class RenderFrameHost
;
34 // Implementation of Mojo PresentationService.
35 // It handles Presentation API requests coming from Blink / renderer process
36 // and delegates the requests to the embedder's media router via
37 // PresentationServiceDelegate.
38 // An instance of this class tied to a RenderFrameHost and listens to events
39 // related to the RFH via implementing WebContentsObserver.
40 // This class is instantiated on-demand via Mojo's ConnectToRemoteService
41 // from the renderer when the first presentation API request is handled.
42 class CONTENT_EXPORT PresentationServiceImpl
43 : public NON_EXPORTED_BASE(presentation::PresentationService
),
44 public mojo::ErrorHandler
,
45 public WebContentsObserver
,
46 public PresentationServiceDelegate::Observer
{
48 ~PresentationServiceImpl() override
;
50 // Static factory method to create an instance of PresentationServiceImpl.
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.
53 static void CreateMojoService(
54 RenderFrameHost
* render_frame_host
,
55 mojo::InterfaceRequest
<presentation::PresentationService
> request
);
58 friend class PresentationServiceImplTest
;
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
);
82 using NewSessionMojoCallback
=
83 mojo::Callback
<void(presentation::PresentationSessionInfoPtr
,
84 presentation::PresentationErrorPtr
)>;
85 using DefaultSessionMojoCallback
=
86 mojo::Callback
<void(presentation::PresentationSessionInfoPtr
)>;
87 using SessionStateCallback
=
88 mojo::Callback
<void(presentation::PresentationSessionInfoPtr
,
89 presentation::PresentationSessionState
)>;
90 using SessionMessagesCallback
=
91 mojo::Callback
<void(mojo::Array
<presentation::SessionMessagePtr
>)>;
92 using SendMessageMojoCallback
= mojo::Callback
<void(bool)>;
94 // Listener implementation owned by PresentationServiceImpl. An instance of
95 // this is created when an |onavailablechange| handler is added.
96 // The instance receives screen availability results from the embedder and
97 // propagates results back to PresentationServiceImpl.
98 class CONTENT_EXPORT ScreenAvailabilityListenerImpl
99 : public PresentationScreenAvailabilityListener
{
101 ScreenAvailabilityListenerImpl(
102 const std::string
& presentation_url
,
103 PresentationServiceImpl
* service
);
104 ~ScreenAvailabilityListenerImpl() override
;
106 // PresentationScreenAvailabilityListener implementation.
107 std::string
GetPresentationUrl() const override
;
108 void OnScreenAvailabilityChanged(bool available
) override
;
111 const std::string presentation_url_
;
112 PresentationServiceImpl
* const service_
;
115 class CONTENT_EXPORT DefaultSessionStartContext
{
117 DefaultSessionStartContext();
118 ~DefaultSessionStartContext();
120 // Adds a callback. May invoke the callback immediately if |session| using
121 // default presentation URL was already started.
122 void AddCallback(const DefaultSessionMojoCallback
& callback
);
124 // Sets the session info. Maybe invoke callbacks queued with AddCallback().
125 void set_session(const PresentationSessionInfo
& session
);
128 // Flush all queued callbacks by invoking them with null
129 // PresentationSessionInfoPtr.
132 ScopedVector
<DefaultSessionMojoCallback
> callbacks_
;
133 scoped_ptr
<PresentationSessionInfo
> session_
;
136 // Context for a StartSession request.
137 class CONTENT_EXPORT StartSessionRequest
{
139 StartSessionRequest(const std::string
& presentation_url
,
140 const std::string
& presentation_id
,
141 const NewSessionMojoCallback
& callback
);
142 ~StartSessionRequest();
144 // Retrieves the pending callback from this request, transferring ownership
146 NewSessionMojoCallback
PassCallback();
148 const std::string
& presentation_url() const { return presentation_url_
; }
149 const std::string
& presentation_id() const { return presentation_id_
; }
152 const std::string presentation_url_
;
153 const std::string presentation_id_
;
154 NewSessionMojoCallback callback_
;
157 // |render_frame_host|: The RFH this instance is associated with.
158 // |web_contents|: The WebContents to observe.
159 // |delegate|: Where Presentation API requests are delegated to. Not owned
161 PresentationServiceImpl(
162 RenderFrameHost
* render_frame_host
,
163 WebContents
* web_contents
,
164 PresentationServiceDelegate
* delegate
);
166 // PresentationService implementation.
167 void SetDefaultPresentationURL(
168 const mojo::String
& presentation_url
,
169 const mojo::String
& presentation_id
) override
;
170 void SetClient(presentation::PresentationServiceClientPtr client
) override
;
171 void ListenForScreenAvailability() override
;
172 void StopListeningForScreenAvailability() override
;
173 void ListenForDefaultSessionStart(
174 const DefaultSessionMojoCallback
& callback
) override
;
176 const mojo::String
& presentation_url
,
177 const mojo::String
& presentation_id
,
178 const NewSessionMojoCallback
& callback
) override
;
180 const mojo::String
& presentation_url
,
181 const mojo::String
& presentation_id
,
182 const NewSessionMojoCallback
& callback
) override
;
183 void SendSessionMessage(
184 presentation::SessionMessagePtr session_message
,
185 const SendMessageMojoCallback
& callback
) override
;
187 const mojo::String
& presentation_url
,
188 const mojo::String
& presentation_id
) override
;
189 void ListenForSessionStateChange(
190 const SessionStateCallback
& callback
) override
;
191 void ListenForSessionMessages(
192 const SessionMessagesCallback
& callback
) override
;
194 // Creates a binding between this object and |request|.
195 void Bind(mojo::InterfaceRequest
<presentation::PresentationService
> request
);
197 // mojo::ErrorHandler override.
198 // Note that this is called when the RenderFrameHost is deleted.
199 void OnConnectionError() override
;
201 // WebContentsObserver override.
202 void DidNavigateAnyFrame(
203 content::RenderFrameHost
* render_frame_host
,
204 const content::LoadCommittedDetails
& details
,
205 const content::FrameNavigateParams
& params
) override
;
206 void RenderFrameDeleted(content::RenderFrameHost
* render_frame_host
) override
;
208 // PresentationServiceDelegate::Observer
209 void OnDelegateDestroyed() override
;
210 void OnDefaultPresentationStarted(const PresentationSessionInfo
& session
)
213 // Finds the callback from |pending_session_cbs_| using |request_session_id|.
214 // If it exists, invoke it with |session| and |error|, then erase it from
215 // |pending_session_cbs_|.
216 void RunAndEraseNewSessionMojoCallback(
217 int request_session_id
,
218 presentation::PresentationSessionInfoPtr session
,
219 presentation::PresentationErrorPtr error
);
221 // Creates a new screen availability listener for |presentation_url| and
222 // registers it with |delegate_|. Replaces the existing listener if any.
223 void ResetScreenAvailabilityListener(const std::string
& presentation_url
);
225 // Removes all listeners and resets default presentation URL on this instance
226 // and informs the PresentationServiceDelegate of such.
229 // These functions are bound as base::Callbacks and passed to
230 // embedder's implementation of PresentationServiceDelegate for later
232 void OnStartOrJoinSessionSucceeded(
233 bool is_start_session
,
234 int request_session_id
,
235 const PresentationSessionInfo
& session_info
);
236 void OnStartOrJoinSessionError(
237 bool is_start_session
,
238 int request_session_id
,
239 const PresentationError
& error
);
240 void OnSendMessageCallback();
242 // Requests delegate to start a session.
244 const std::string
& presentation_url
,
245 const std::string
& presentation_id
,
246 const NewSessionMojoCallback
& callback
);
248 // Passed to embedder's implementation of PresentationServiceDelegate for
249 // later invocation when session messages arrive.
250 // For optimization purposes, this method will empty the messages
252 void OnSessionMessages(
253 scoped_ptr
<ScopedVector
<PresentationSessionMessage
>> messages
);
255 // Removes the head of the queue (which represents the request that has just
257 // Checks if there are any queued StartSession requests and if so, executes
258 // the first one in the queue.
259 void HandleQueuedStartSessionRequests();
261 // Associates |callback| with a unique request ID and stores it in a map.
262 int RegisterNewSessionCallback(
263 const NewSessionMojoCallback
& callback
);
265 // Flushes all pending new session callbacks with error responses.
266 void FlushNewSessionCallbacks();
268 // Invokes |callback| with an error.
269 static void InvokeNewSessionMojoCallbackWithError(
270 const NewSessionMojoCallback
& callback
);
272 // Returns true if this object is associated with |render_frame_host|.
273 bool FrameMatches(content::RenderFrameHost
* render_frame_host
) const;
275 // Embedder-specific delegate to forward Presentation requests to.
276 // May be null if embedder does not support Presentation API.
277 PresentationServiceDelegate
* delegate_
;
279 // Proxy to the PresentationServiceClient to send results (e.g., screen
281 presentation::PresentationServiceClientPtr client_
;
283 std::string default_presentation_url_
;
284 std::string default_presentation_id_
;
286 scoped_ptr
<ScreenAvailabilityListenerImpl
> screen_availability_listener_
;
288 // We only allow one StartSession request to be processed at a time.
289 // StartSession requests are queued here. When a request has been processed,
290 // it is removed from head of the queue.
291 std::deque
<linked_ptr
<StartSessionRequest
>> queued_start_session_requests_
;
293 // Indicates that a StartSession request is currently being processed.
294 bool is_start_session_pending_
;
296 int next_request_session_id_
;
297 base::hash_map
<int, linked_ptr
<NewSessionMojoCallback
>> pending_session_cbs_
;
299 scoped_ptr
<DefaultSessionStartContext
> default_session_start_context_
;
301 // RAII binding of |this| to an Presentation interface request.
302 // The binding is removed when binding_ is cleared or goes out of scope.
303 scoped_ptr
<mojo::Binding
<presentation::PresentationService
>> binding_
;
305 // There can be only one send message request at a time.
306 scoped_ptr
<SendMessageMojoCallback
> send_message_callback_
;
308 scoped_ptr
<SessionMessagesCallback
> on_session_messages_callback_
;
310 // ID of the RenderFrameHost this object is associated with.
311 int render_process_id_
;
312 int render_frame_id_
;
314 // NOTE: Weak pointers must be invalidated before all other member variables.
315 base::WeakPtrFactory
<PresentationServiceImpl
> weak_factory_
;
317 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl
);
320 } // namespace content
322 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_