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 class RenderFrameHost
;
33 // Implementation of Mojo PresentationService.
34 // It handles Presentation API requests coming from Blink / renderer process
35 // and delegates the requests to the embedder's media router via
36 // PresentationServiceDelegate.
37 // An instance of this class tied to a RenderFrameHost and listens to events
38 // related to the RFH via implementing WebContentsObserver.
39 // This class is instantiated on-demand via Mojo's ConnectToRemoteService
40 // from the renderer when the first presentation API request is handled.
41 class CONTENT_EXPORT PresentationServiceImpl
42 : public NON_EXPORTED_BASE(presentation::PresentationService
),
43 public mojo::ErrorHandler
,
44 public WebContentsObserver
,
45 public PresentationServiceDelegate::Observer
{
47 ~PresentationServiceImpl() override
;
49 // Static factory method to create an instance of PresentationServiceImpl.
50 // |render_frame_host|: The RFH the instance is associated with.
51 // |request|: The instance will be bound to this request. Used for Mojo setup.
52 static void CreateMojoService(
53 RenderFrameHost
* render_frame_host
,
54 mojo::InterfaceRequest
<presentation::PresentationService
> request
);
57 using ScreenAvailabilityMojoCallback
=
58 mojo::Callback
<void(mojo::String
, bool)>;
59 using NewSessionMojoCallback
=
60 mojo::Callback
<void(presentation::PresentationSessionInfoPtr
,
61 presentation::PresentationErrorPtr
)>;
62 using DefaultSessionMojoCallback
=
63 mojo::Callback
<void(presentation::PresentationSessionInfoPtr
)>;
64 using SessionStateCallback
=
65 mojo::Callback
<void(presentation::PresentationSessionInfoPtr
,
66 presentation::PresentationSessionState
)>;
68 // A helper data class used by PresentationServiceImpl to do bookkeeping
69 // of currently registered screen availability listeners.
70 // An instance of this class is a simple state machine that waits for both
71 // the available bit and the Mojo callback to become available.
72 // Once this happens, the Mojo callback will be invoked with the available
73 // bit, and the state machine will reset.
74 // The available bit is obtained from the embedder's media router.
75 // The callback is obtained from the renderer via PresentationServiceImpl's
76 // ListenForScreenAvailability().
77 class CONTENT_EXPORT ScreenAvailabilityContext
78 : public PresentationScreenAvailabilityListener
{
80 explicit ScreenAvailabilityContext(
81 const std::string
& presentation_url
);
82 ~ScreenAvailabilityContext() override
;
84 // If available bit exists, |callback| will be invoked with the bit and
85 // this state machine will reset.
86 // Otherwise |callback| is saved for later use.
87 // |callback|: Callback to the client of PresentationService
88 // (i.e. the renderer) that screen availability has changed, via Mojo.
89 void CallbackReceived(const ScreenAvailabilityMojoCallback
& callback
);
91 // PresentationScreenAvailabilityListener implementation.
92 std::string
GetPresentationUrl() const override
;
94 // If callback exists, it will be invoked with |available| and
95 // this state machine will reset.
96 // Otherwise |available| is saved for later use.
97 // |available|: New screen availability for the presentation URL.
98 void OnScreenAvailabilityChanged(bool available
) override
;
100 // Pass this context's queued callbacks to another context.
101 void PassPendingCallbacks(ScreenAvailabilityContext
* other
);
103 // Indicates if this context has any pending callbacks.
104 bool HasPendingCallbacks() const;
107 std::string presentation_url_
;
108 ScopedVector
<ScreenAvailabilityMojoCallback
> callbacks_
;
109 scoped_ptr
<bool> available_ptr_
;
112 // Context for a StartSession request.
113 class CONTENT_EXPORT StartSessionRequest
{
115 StartSessionRequest(const std::string
& presentation_url
,
116 const std::string
& presentation_id
,
117 const NewSessionMojoCallback
& callback
);
118 ~StartSessionRequest();
120 // Retrieves the pending callback from this request, transferring ownership
122 NewSessionMojoCallback
PassCallback();
124 const std::string
& presentation_url() const { return presentation_url_
; }
125 const std::string
& presentation_id() const { return presentation_id_
; }
128 const std::string presentation_url_
;
129 const std::string presentation_id_
;
130 NewSessionMojoCallback callback_
;
133 friend class PresentationServiceImplTest
;
134 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
, Reset
);
135 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
136 DidNavigateThisFrame
);
137 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
138 DidNavigateNotThisFrame
);
139 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
140 ThisRenderFrameDeleted
);
141 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
142 NotThisRenderFrameDeleted
);
143 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
144 SetDefaultPresentationUrl
);
145 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
146 SetSameDefaultPresentationUrl
);
147 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
148 ClearDefaultPresentationUrl
);
150 // |render_frame_host|: The RFH this instance is associated with.
151 // |web_contents|: The WebContents to observe.
152 // |delegate|: Where Presentation API requests are delegated to. Not owned
154 PresentationServiceImpl(
155 RenderFrameHost
* render_frame_host
,
156 WebContents
* web_contents
,
157 PresentationServiceDelegate
* delegate
);
159 // PresentationService implementation.
160 void SetDefaultPresentationURL(
161 const mojo::String
& presentation_url
,
162 const mojo::String
& presentation_id
) override
;
163 void ListenForScreenAvailability(
164 const mojo::String
& presentation_url
,
165 const ScreenAvailabilityMojoCallback
& callback
) override
;
166 void RemoveScreenAvailabilityListener(
167 const mojo::String
& presentation_url
) override
;
168 void ListenForDefaultSessionStart(
169 const DefaultSessionMojoCallback
& callback
) override
;
171 const mojo::String
& presentation_url
,
172 const mojo::String
& presentation_id
,
173 const NewSessionMojoCallback
& callback
) override
;
175 const mojo::String
& presentation_url
,
176 const mojo::String
& presentation_id
,
177 const NewSessionMojoCallback
& callback
) override
;
179 const mojo::String
& presentation_url
,
180 const mojo::String
& presentation_id
) override
;
181 void ListenForSessionStateChange(
182 const SessionStateCallback
& callback
) override
;
184 // Creates a binding between this object and |request|.
185 void Bind(mojo::InterfaceRequest
<presentation::PresentationService
> request
);
187 // mojo::ErrorHandler override.
188 // Note that this is called when the RenderFrameHost is deleted.
189 void OnConnectionError() override
;
191 // WebContentsObserver override.
192 void DidNavigateAnyFrame(
193 content::RenderFrameHost
* render_frame_host
,
194 const content::LoadCommittedDetails
& details
,
195 const content::FrameNavigateParams
& params
) override
;
196 void RenderFrameDeleted(content::RenderFrameHost
* render_frame_host
) override
;
198 // PresentationServiceDelegate::Observer
199 void OnDelegateDestroyed() override
;
201 // Finds the callback from |pending_session_cbs_| using |request_session_id|.
202 // If it exists, invoke it with |session| and |error|, then erase it from
203 // |pending_session_cbs_|.
204 void RunAndEraseNewSessionMojoCallback(
205 int request_session_id
,
206 presentation::PresentationSessionInfoPtr session
,
207 presentation::PresentationErrorPtr error
);
209 // Sets |default_presentation_url_| to |presentation_url| and informs the
210 // delegate of new default presentation URL and ID.
211 void DoSetDefaultPresentationUrl(
212 const std::string
& presentation_url
,
213 const std::string
& presentation_id
);
215 // Removes all listeners and resets default presentation URL on this instance
216 // and informs the PresentationServiceDelegate of such.
219 // These two functions are bound as base::Callbacks and passed to
220 // embedder's implementation of PresentationServiceDelegate for later
222 void OnStartOrJoinSessionSucceeded(
223 bool is_start_session
,
224 int request_session_id
,
225 const PresentationSessionInfo
& session_info
);
226 void OnStartOrJoinSessionError(
227 bool is_start_session
,
228 int request_session_id
,
229 const PresentationError
& error
);
231 // Requests delegate to start a session.
233 const std::string
& presentation_url
,
234 const std::string
& presentation_id
,
235 const NewSessionMojoCallback
& callback
);
237 // Removes the head of the queue (which represents the request that has just
239 // Checks if there are any queued StartSession requests and if so, executes
240 // the first one in the queue.
241 void HandleQueuedStartSessionRequests();
243 // Associates |callback| with a unique request ID and stores it in a map.
244 int RegisterNewSessionCallback(
245 const NewSessionMojoCallback
& callback
);
247 // Flushes all pending new session callbacks with error responses.
248 void FlushNewSessionCallbacks();
250 // Invokes |callback| with an error.
251 static void InvokeNewSessionMojoCallbackWithError(
252 const NewSessionMojoCallback
& callback
);
254 // Gets the ScreenAvailabilityContext for |presentation_url|, or creates one
255 // if it does not exist.
256 ScreenAvailabilityContext
* GetOrCreateAvailabilityContext(
257 const std::string
& presentation_url
);
259 RenderFrameHost
* render_frame_host_
;
260 PresentationServiceDelegate
* delegate_
;
262 // Map from presentation URL to its ScreenAvailabilityContext state machine.
263 base::hash_map
<std::string
, linked_ptr
<ScreenAvailabilityContext
>>
264 availability_contexts_
;
266 std::string default_presentation_url_
;
267 std::string default_presentation_id_
;
269 // We only allow one StartSession request to be processed at a time.
270 // StartSession requests are queued here. When a request has been processed,
271 // it is removed from head of the queue.
272 std::deque
<linked_ptr
<StartSessionRequest
>> queued_start_session_requests_
;
274 // Indicates that a StartSession request is currently being processed.
275 bool is_start_session_pending_
;
277 int next_request_session_id_
;
278 base::hash_map
<int, linked_ptr
<NewSessionMojoCallback
>> pending_session_cbs_
;
280 // RAII binding of |this| to an Presentation interface request.
281 // The binding is removed when binding_ is cleared or goes out of scope.
282 scoped_ptr
<mojo::Binding
<presentation::PresentationService
>> binding_
;
284 // NOTE: Weak pointers must be invalidated before all other member variables.
285 base::WeakPtrFactory
<PresentationServiceImpl
> weak_factory_
;
287 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl
);
290 } // namespace content
292 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_