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 struct CONTENT_EXPORT StartSessionRequest
{
114 StartSessionRequest(const std::string
& presentation_url
,
115 const std::string
& presentation_id
,
116 const NewSessionMojoCallback
& callback
);
117 ~StartSessionRequest();
119 const std::string presentation_url
;
120 const std::string presentation_id
;
121 const NewSessionMojoCallback callback
;
124 friend class PresentationServiceImplTest
;
125 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
, Reset
);
126 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
127 DidNavigateThisFrame
);
128 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
129 DidNavigateNotThisFrame
);
130 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
131 ThisRenderFrameDeleted
);
132 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
133 NotThisRenderFrameDeleted
);
134 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
135 SetDefaultPresentationUrl
);
136 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
137 SetSameDefaultPresentationUrl
);
138 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
139 ClearDefaultPresentationUrl
);
141 // |render_frame_host|: The RFH this instance is associated with.
142 // |web_contents|: The WebContents to observe.
143 // |delegate|: Where Presentation API requests are delegated to. Not owned
145 PresentationServiceImpl(
146 RenderFrameHost
* render_frame_host
,
147 WebContents
* web_contents
,
148 PresentationServiceDelegate
* delegate
);
150 // PresentationService implementation.
151 void SetDefaultPresentationURL(
152 const mojo::String
& presentation_url
,
153 const mojo::String
& presentation_id
) override
;
154 void ListenForScreenAvailability(
155 const mojo::String
& presentation_url
,
156 const ScreenAvailabilityMojoCallback
& callback
) override
;
157 void RemoveScreenAvailabilityListener(
158 const mojo::String
& presentation_url
) override
;
159 void ListenForDefaultSessionStart(
160 const DefaultSessionMojoCallback
& callback
) override
;
162 const mojo::String
& presentation_url
,
163 const mojo::String
& presentation_id
,
164 const NewSessionMojoCallback
& callback
) override
;
166 const mojo::String
& presentation_url
,
167 const mojo::String
& presentation_id
,
168 const NewSessionMojoCallback
& callback
) override
;
170 const mojo::String
& presentation_url
,
171 const mojo::String
& presentation_id
) override
;
172 void ListenForSessionStateChange(
173 const SessionStateCallback
& callback
) override
;
175 // Creates a binding between this object and |request|.
176 void Bind(mojo::InterfaceRequest
<presentation::PresentationService
> request
);
178 // mojo::ErrorHandler override.
179 // Note that this is called when the RenderFrameHost is deleted.
180 void OnConnectionError() override
;
182 // WebContentsObserver override.
183 void DidNavigateAnyFrame(
184 content::RenderFrameHost
* render_frame_host
,
185 const content::LoadCommittedDetails
& details
,
186 const content::FrameNavigateParams
& params
) override
;
187 void RenderFrameDeleted(content::RenderFrameHost
* render_frame_host
) override
;
189 // PresentationServiceDelegate::Observer
190 void OnDelegateDestroyed() override
;
192 // Finds the callback from |pending_session_cbs_| using |request_session_id|.
193 // If it exists, invoke it with |session| and |error|, then erase it from
194 // |pending_session_cbs_|.
195 void RunAndEraseNewSessionMojoCallback(
196 int request_session_id
,
197 presentation::PresentationSessionInfoPtr session
,
198 presentation::PresentationErrorPtr error
);
200 // Sets |default_presentation_url_| to |presentation_url| and informs the
201 // delegate of new default presentation URL and ID.
202 void DoSetDefaultPresentationUrl(
203 const std::string
& presentation_url
,
204 const std::string
& presentation_id
);
206 // Removes all listeners and resets default presentation URL on this instance
207 // and informs the PresentationServiceDelegate of such.
210 // These two functions are bound as base::Callbacks and passed to
211 // embedder's implementation of PresentationServiceDelegate for later
213 void OnStartOrJoinSessionSucceeded(
214 bool is_start_session
,
215 int request_session_id
,
216 const PresentationSessionInfo
& session_info
);
217 void OnStartOrJoinSessionError(
218 bool is_start_session
,
219 int request_session_id
,
220 const PresentationError
& error
);
222 // Requests delegate to start a session.
224 const std::string
& presentation_url
,
225 const std::string
& presentation_id
,
226 const NewSessionMojoCallback
& callback
);
228 // Removes the head of the queue (which represents the request that has just
230 // Checks if there are any queued StartSession requests and if so, executes
231 // the first one in the queue.
232 void HandleQueuedStartSessionRequests();
234 // Associates |callback| with a unique request ID and stores it in a map.
235 int RegisterNewSessionCallback(
236 const NewSessionMojoCallback
& callback
);
238 // Invokes |callback| with an error.
239 void InvokeNewSessionMojoCallbackWithError(
240 const NewSessionMojoCallback
& callback
);
242 // Gets the ScreenAvailabilityContext for |presentation_url|, or creates one
243 // if it does not exist.
244 ScreenAvailabilityContext
* GetOrCreateAvailabilityContext(
245 const std::string
& presentation_url
);
247 RenderFrameHost
* render_frame_host_
;
248 PresentationServiceDelegate
* delegate_
;
250 // Map from presentation URL to its ScreenAvailabilityContext state machine.
251 base::hash_map
<std::string
, linked_ptr
<ScreenAvailabilityContext
>>
252 availability_contexts_
;
254 std::string default_presentation_url_
;
255 std::string default_presentation_id_
;
257 // We only allow one StartSession request to be processed at a time.
258 // StartSession requests are queued here. When a request has been processed,
259 // it is removed from head of the queue.
260 std::deque
<linked_ptr
<StartSessionRequest
>> queued_start_session_requests_
;
262 int next_request_session_id_
;
263 base::hash_map
<int, linked_ptr
<NewSessionMojoCallback
>> pending_session_cbs_
;
265 // RAII binding of |this| to an Presentation interface request.
266 // The binding is removed when binding_ is cleared or goes out of scope.
267 scoped_ptr
<mojo::Binding
<presentation::PresentationService
>> binding_
;
269 // NOTE: Weak pointers must be invalidated before all other member variables.
270 base::WeakPtrFactory
<PresentationServiceImpl
> weak_factory_
;
272 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl
);
275 } // namespace content
277 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_