Android Chromoting: Remove exit-fullscreen button.
[chromium-blink-merge.git] / content / browser / presentation / presentation_service_impl.h
blob135320da4093519005f1588f49c2d2a71dff5454
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_
8 #include <deque>
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"
27 namespace content {
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(
43 mojo::InterfaceImpl<presentation::PresentationService>),
44 public WebContentsObserver,
45 public PresentationServiceDelegate::Observer {
46 public:
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);
56 private:
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 {
79 public:
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;
106 private:
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
144 // by this class.
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;
161 void StartSession(
162 const mojo::String& presentation_url,
163 const mojo::String& presentation_id,
164 const NewSessionMojoCallback& callback) override;
165 void JoinSession(
166 const mojo::String& presentation_url,
167 const mojo::String& presentation_id,
168 const NewSessionMojoCallback& callback) override;
169 void CloseSession(
170 const mojo::String& presentation_url,
171 const mojo::String& presentation_id) override;
172 void ListenForSessionStateChange(
173 const SessionStateCallback& callback) override;
175 // mojo::InterfaceImpl override.
176 // Note that this is called when the RenderFrameHost is deleted.
177 void OnConnectionError() override;
179 // WebContentsObserver override.
180 void DidNavigateAnyFrame(
181 content::RenderFrameHost* render_frame_host,
182 const content::LoadCommittedDetails& details,
183 const content::FrameNavigateParams& params) override;
184 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
186 // PresentationServiceDelegate::Observer
187 void OnDelegateDestroyed() override;
189 // Finds the callback from |pending_session_cbs_| using |request_session_id|.
190 // If it exists, invoke it with |session| and |error|, then erase it from
191 // |pending_session_cbs_|.
192 void RunAndEraseNewSessionMojoCallback(
193 int request_session_id,
194 presentation::PresentationSessionInfoPtr session,
195 presentation::PresentationErrorPtr error);
197 // Sets |default_presentation_url_| to |presentation_url| and informs the
198 // delegate of new default presentation URL and ID.
199 void DoSetDefaultPresentationUrl(
200 const std::string& presentation_url,
201 const std::string& presentation_id);
203 // Removes all listeners and resets default presentation URL on this instance
204 // and informs the PresentationServiceDelegate of such.
205 void Reset();
207 // These two functions are bound as base::Callbacks and passed to
208 // embedder's implementation of PresentationServiceDelegate for later
209 // invocation.
210 void OnStartOrJoinSessionSucceeded(
211 bool is_start_session,
212 int request_session_id,
213 const PresentationSessionInfo& session_info);
214 void OnStartOrJoinSessionError(
215 bool is_start_session,
216 int request_session_id,
217 const PresentationError& error);
219 // Requests delegate to start a session.
220 void DoStartSession(
221 const std::string& presentation_url,
222 const std::string& presentation_id,
223 const NewSessionMojoCallback& callback);
225 // Removes the head of the queue (which represents the request that has just
226 // been processed).
227 // Checks if there are any queued StartSession requests and if so, executes
228 // the first one in the queue.
229 void HandleQueuedStartSessionRequests();
231 // Associates |callback| with a unique request ID and stores it in a map.
232 int RegisterNewSessionCallback(
233 const NewSessionMojoCallback& callback);
235 // Invokes |callback| with an error.
236 void InvokeNewSessionMojoCallbackWithError(
237 const NewSessionMojoCallback& callback);
239 // Gets the ScreenAvailabilityContext for |presentation_url|, or creates one
240 // if it does not exist.
241 ScreenAvailabilityContext* GetOrCreateAvailabilityContext(
242 const std::string& presentation_url);
244 RenderFrameHost* render_frame_host_;
245 PresentationServiceDelegate* delegate_;
247 // Map from presentation URL to its ScreenAvailabilityContext state machine.
248 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>>
249 availability_contexts_;
251 std::string default_presentation_url_;
252 std::string default_presentation_id_;
254 // We only allow one StartSession request to be processed at a time.
255 // StartSession requests are queued here. When a request has been processed,
256 // it is removed from head of the queue.
257 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_;
259 int next_request_session_id_;
260 base::hash_map<int, linked_ptr<NewSessionMojoCallback>> pending_session_cbs_;
262 // NOTE: Weak pointers must be invalidated before all other member variables.
263 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_;
265 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl);
268 } // namespace content
270 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_