OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
6 #define CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 6 #define CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 | 12 |
| 13 #include "base/callback.h" |
13 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
14 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/linked_ptr.h" | 18 #include "base/memory/linked_ptr.h" |
18 #include "base/memory/scoped_vector.h" | 19 #include "base/memory/scoped_vector.h" |
19 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
20 #include "content/common/content_export.h" | 21 #include "content/common/content_export.h" |
21 #include "content/public/browser/navigation_details.h" | 22 #include "content/public/browser/navigation_details.h" |
22 #include "content/public/browser/presentation_screen_availability_listener.h" | 23 #include "content/public/browser/presentation_screen_availability_listener.h" |
23 #include "content/public/browser/presentation_service_delegate.h" | 24 #include "content/public/browser/presentation_service_delegate.h" |
24 #include "content/public/browser/web_contents_observer.h" | 25 #include "content/public/browser/web_contents_observer.h" |
25 #include "content/public/common/frame_navigate_params.h" | 26 #include "content/public/common/frame_navigate_params.h" |
26 #include "mojo/public/cpp/bindings/binding.h" | 27 #include "mojo/public/cpp/bindings/binding.h" |
27 #include "third_party/WebKit/public/platform/modules/presentation/presentation.m
ojom.h" | 28 #include "third_party/WebKit/public/platform/modules/presentation/presentation.m
ojom.h" |
28 | 29 |
29 namespace content { | 30 namespace content { |
30 | 31 |
31 struct FrameNavigateParams; | 32 struct FrameNavigateParams; |
32 struct LoadCommittedDetails; | 33 struct LoadCommittedDetails; |
33 struct PresentationSessionMessage; | 34 struct PresentationSessionMessage; |
34 class RenderFrameHost; | 35 class RenderFrameHost; |
35 | 36 |
36 using NewSessionMojoCallback = | |
37 mojo::Callback<void(blink::mojom::PresentationSessionInfoPtr, | |
38 blink::mojom::PresentationErrorPtr)>; | |
39 | |
40 // Implementation of Mojo PresentationService. | 37 // Implementation of Mojo PresentationService. |
41 // It handles Presentation API requests coming from Blink / renderer process | 38 // It handles Presentation API requests coming from Blink / renderer process |
42 // and delegates the requests to the embedder's media router via | 39 // and delegates the requests to the embedder's media router via |
43 // PresentationServiceDelegate. | 40 // PresentationServiceDelegate. |
44 // An instance of this class tied to a RenderFrameHost and listens to events | 41 // An instance of this class tied to a RenderFrameHost and listens to events |
45 // related to the RFH via implementing WebContentsObserver. | 42 // related to the RFH via implementing WebContentsObserver. |
46 // This class is instantiated on-demand via Mojo's ConnectToRemoteService | 43 // This class is instantiated on-demand via Mojo's ConnectToRemoteService |
47 // from the renderer when the first presentation API request is handled. | 44 // from the renderer when the first presentation API request is handled. |
48 class CONTENT_EXPORT PresentationServiceImpl | 45 class CONTENT_EXPORT PresentationServiceImpl |
49 : public NON_EXPORTED_BASE(blink::mojom::PresentationService), | 46 : public NON_EXPORTED_BASE(blink::mojom::PresentationService), |
50 public WebContentsObserver, | 47 public WebContentsObserver, |
51 public PresentationServiceDelegate::Observer { | 48 public PresentationServiceDelegate::Observer { |
52 public: | 49 public: |
| 50 using NewSessionCallback = |
| 51 base::Callback<void(blink::mojom::PresentationSessionInfoPtr, |
| 52 blink::mojom::PresentationErrorPtr)>; |
| 53 |
53 ~PresentationServiceImpl() override; | 54 ~PresentationServiceImpl() override; |
54 | 55 |
55 // Static factory method to create an instance of PresentationServiceImpl. | 56 // Static factory method to create an instance of PresentationServiceImpl. |
56 // |render_frame_host|: The RFH the instance is associated with. | 57 // |render_frame_host|: The RFH the instance is associated with. |
57 // |request|: The instance will be bound to this request. Used for Mojo setup. | 58 // |request|: The instance will be bound to this request. Used for Mojo setup. |
58 static void CreateMojoService( | 59 static void CreateMojoService( |
59 RenderFrameHost* render_frame_host, | 60 RenderFrameHost* render_frame_host, |
60 mojo::InterfaceRequest<blink::mojom::PresentationService> request); | 61 mojo::InterfaceRequest<blink::mojom::PresentationService> request); |
61 | 62 |
62 private: | 63 private: |
(...skipping 25 matching lines...) Expand all Loading... |
88 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 89 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
89 MaxPendingJoinSessionRequests); | 90 MaxPendingJoinSessionRequests); |
90 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 91 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
91 ListenForConnectionStateChange); | 92 ListenForConnectionStateChange); |
92 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 93 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
93 ListenForConnectionClose); | 94 ListenForConnectionClose); |
94 | 95 |
95 // Maximum number of pending JoinSession requests at any given time. | 96 // Maximum number of pending JoinSession requests at any given time. |
96 static const int kMaxNumQueuedSessionRequests = 10; | 97 static const int kMaxNumQueuedSessionRequests = 10; |
97 | 98 |
98 using PresentationSessionMojoCallback = | |
99 mojo::Callback<void(blink::mojom::PresentationSessionInfoPtr)>; | |
100 using SessionMessagesCallback = | 99 using SessionMessagesCallback = |
101 mojo::Callback<void(mojo::Array<blink::mojom::SessionMessagePtr>)>; | 100 base::Callback<void(mojo::Array<blink::mojom::SessionMessagePtr>)>; |
102 using SendMessageMojoCallback = mojo::Callback<void(bool)>; | 101 using SendSessionMessageCallback = base::Callback<void(bool)>; |
103 | 102 |
104 // Listener implementation owned by PresentationServiceImpl. An instance of | 103 // Listener implementation owned by PresentationServiceImpl. An instance of |
105 // this is created when PresentationRequest.getAvailability() is resolved. | 104 // this is created when PresentationRequest.getAvailability() is resolved. |
106 // The instance receives screen availability results from the embedder and | 105 // The instance receives screen availability results from the embedder and |
107 // propagates results back to PresentationServiceImpl. | 106 // propagates results back to PresentationServiceImpl. |
108 class CONTENT_EXPORT ScreenAvailabilityListenerImpl | 107 class CONTENT_EXPORT ScreenAvailabilityListenerImpl |
109 : public PresentationScreenAvailabilityListener { | 108 : public PresentationScreenAvailabilityListener { |
110 public: | 109 public: |
111 ScreenAvailabilityListenerImpl( | 110 ScreenAvailabilityListenerImpl( |
112 const std::string& availability_url, | 111 const std::string& availability_url, |
113 PresentationServiceImpl* service); | 112 PresentationServiceImpl* service); |
114 ~ScreenAvailabilityListenerImpl() override; | 113 ~ScreenAvailabilityListenerImpl() override; |
115 | 114 |
116 // PresentationScreenAvailabilityListener implementation. | 115 // PresentationScreenAvailabilityListener implementation. |
117 std::string GetAvailabilityUrl() const override; | 116 std::string GetAvailabilityUrl() const override; |
118 void OnScreenAvailabilityChanged(bool available) override; | 117 void OnScreenAvailabilityChanged(bool available) override; |
119 void OnScreenAvailabilityNotSupported() override; | 118 void OnScreenAvailabilityNotSupported() override; |
120 | 119 |
121 private: | 120 private: |
122 const std::string availability_url_; | 121 const std::string availability_url_; |
123 PresentationServiceImpl* const service_; | 122 PresentationServiceImpl* const service_; |
124 }; | 123 }; |
125 | 124 |
126 // Ensures the provided NewSessionMojoCallback is invoked exactly once | 125 // Ensures the provided NewSessionCallback is invoked exactly once |
127 // before it goes out of scope. | 126 // before it goes out of scope. |
128 class NewSessionMojoCallbackWrapper { | 127 class NewSessionCallbackWrapper { |
129 public: | 128 public: |
130 explicit NewSessionMojoCallbackWrapper( | 129 explicit NewSessionCallbackWrapper( |
131 const NewSessionMojoCallback& callback); | 130 const NewSessionCallback& callback); |
132 ~NewSessionMojoCallbackWrapper(); | 131 ~NewSessionCallbackWrapper(); |
133 | 132 |
134 void Run(blink::mojom::PresentationSessionInfoPtr session, | 133 void Run(blink::mojom::PresentationSessionInfoPtr session, |
135 blink::mojom::PresentationErrorPtr error); | 134 blink::mojom::PresentationErrorPtr error); |
136 | 135 |
137 private: | 136 private: |
138 NewSessionMojoCallback callback_; | 137 NewSessionCallback callback_; |
139 | 138 |
140 DISALLOW_COPY_AND_ASSIGN(NewSessionMojoCallbackWrapper); | 139 DISALLOW_COPY_AND_ASSIGN(NewSessionCallbackWrapper); |
141 }; | 140 }; |
142 | 141 |
143 // |render_frame_host|: The RFH this instance is associated with. | 142 // |render_frame_host|: The RFH this instance is associated with. |
144 // |web_contents|: The WebContents to observe. | 143 // |web_contents|: The WebContents to observe. |
145 // |delegate|: Where Presentation API requests are delegated to. Not owned | 144 // |delegate|: Where Presentation API requests are delegated to. Not owned |
146 // by this class. | 145 // by this class. |
147 PresentationServiceImpl( | 146 PresentationServiceImpl( |
148 RenderFrameHost* render_frame_host, | 147 RenderFrameHost* render_frame_host, |
149 WebContents* web_contents, | 148 WebContents* web_contents, |
150 PresentationServiceDelegate* delegate); | 149 PresentationServiceDelegate* delegate); |
151 | 150 |
152 // PresentationService implementation. | 151 // PresentationService implementation. |
153 void SetDefaultPresentationURL(const mojo::String& url) override; | 152 void SetDefaultPresentationURL(const mojo::String& url) override; |
154 void SetClient(blink::mojom::PresentationServiceClientPtr client) override; | 153 void SetClient(blink::mojom::PresentationServiceClientPtr client) override; |
155 void ListenForScreenAvailability(const mojo::String& url) override; | 154 void ListenForScreenAvailability(const mojo::String& url) override; |
156 void StopListeningForScreenAvailability(const mojo::String& url) override; | 155 void StopListeningForScreenAvailability(const mojo::String& url) override; |
157 void StartSession( | 156 void StartSession( |
158 const mojo::String& presentation_url, | 157 const mojo::String& presentation_url, |
159 const NewSessionMojoCallback& callback) override; | 158 const NewSessionCallback& callback) override; |
160 void JoinSession( | 159 void JoinSession( |
161 const mojo::String& presentation_url, | 160 const mojo::String& presentation_url, |
162 const mojo::String& presentation_id, | 161 const mojo::String& presentation_id, |
163 const NewSessionMojoCallback& callback) override; | 162 const NewSessionCallback& callback) override; |
164 void SendSessionMessage(blink::mojom::PresentationSessionInfoPtr session_info, | 163 void SendSessionMessage(blink::mojom::PresentationSessionInfoPtr session_info, |
165 blink::mojom::SessionMessagePtr session_message, | 164 blink::mojom::SessionMessagePtr session_message, |
166 const SendMessageMojoCallback& callback) override; | 165 const SendSessionMessageCallback& callback) override; |
167 void CloseConnection(const mojo::String& presentation_url, | 166 void CloseConnection(const mojo::String& presentation_url, |
168 const mojo::String& presentation_id) override; | 167 const mojo::String& presentation_id) override; |
169 void Terminate(const mojo::String& presentation_url, | 168 void Terminate(const mojo::String& presentation_url, |
170 const mojo::String& presentation_id) override; | 169 const mojo::String& presentation_id) override; |
171 void ListenForSessionMessages( | 170 void ListenForSessionMessages( |
172 blink::mojom::PresentationSessionInfoPtr session) override; | 171 blink::mojom::PresentationSessionInfoPtr session) override; |
173 | 172 |
174 // Creates a binding between this object and |request|. | 173 // Creates a binding between this object and |request|. |
175 void Bind(mojo::InterfaceRequest<blink::mojom::PresentationService> request); | 174 void Bind(mojo::InterfaceRequest<blink::mojom::PresentationService> request); |
176 | 175 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 // Passed to embedder's implementation of PresentationServiceDelegate for | 228 // Passed to embedder's implementation of PresentationServiceDelegate for |
230 // later invocation when session messages arrive. | 229 // later invocation when session messages arrive. |
231 void OnSessionMessages( | 230 void OnSessionMessages( |
232 const content::PresentationSessionInfo& session, | 231 const content::PresentationSessionInfo& session, |
233 const ScopedVector<PresentationSessionMessage>& messages, | 232 const ScopedVector<PresentationSessionMessage>& messages, |
234 bool pass_ownership); | 233 bool pass_ownership); |
235 | 234 |
236 // Associates a JoinSession |callback| with a unique request ID and | 235 // Associates a JoinSession |callback| with a unique request ID and |
237 // stores it in a map. | 236 // stores it in a map. |
238 // Returns a positive value on success. | 237 // Returns a positive value on success. |
239 int RegisterJoinSessionCallback(const NewSessionMojoCallback& callback); | 238 int RegisterJoinSessionCallback(const NewSessionCallback& callback); |
240 | 239 |
241 // Invoked by the embedder's PresentationServiceDelegate when a | 240 // Invoked by the embedder's PresentationServiceDelegate when a |
242 // PresentationConnection's state has changed. | 241 // PresentationConnection's state has changed. |
243 void OnConnectionStateChanged( | 242 void OnConnectionStateChanged( |
244 const PresentationSessionInfo& connection, | 243 const PresentationSessionInfo& connection, |
245 const PresentationConnectionStateChangeInfo& info); | 244 const PresentationConnectionStateChangeInfo& info); |
246 | 245 |
247 // Returns true if this object is associated with |render_frame_host|. | 246 // Returns true if this object is associated with |render_frame_host|. |
248 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; | 247 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; |
249 | 248 |
250 // Embedder-specific delegate to forward Presentation requests to. | 249 // Embedder-specific delegate to forward Presentation requests to. |
251 // May be null if embedder does not support Presentation API. | 250 // May be null if embedder does not support Presentation API. |
252 PresentationServiceDelegate* delegate_; | 251 PresentationServiceDelegate* delegate_; |
253 | 252 |
254 // Proxy to the PresentationServiceClient to send results (e.g., screen | 253 // Proxy to the PresentationServiceClient to send results (e.g., screen |
255 // availability) to. | 254 // availability) to. |
256 blink::mojom::PresentationServiceClientPtr client_; | 255 blink::mojom::PresentationServiceClientPtr client_; |
257 | 256 |
258 std::string default_presentation_url_; | 257 std::string default_presentation_url_; |
259 | 258 |
260 using ScreenAvailabilityListenerMap = | 259 using ScreenAvailabilityListenerMap = |
261 std::map<std::string, std::unique_ptr<ScreenAvailabilityListenerImpl>>; | 260 std::map<std::string, std::unique_ptr<ScreenAvailabilityListenerImpl>>; |
262 ScreenAvailabilityListenerMap screen_availability_listeners_; | 261 ScreenAvailabilityListenerMap screen_availability_listeners_; |
263 | 262 |
264 // For StartSession requests. | 263 // For StartSession requests. |
265 // Set to a positive value when a StartSession request is being processed. | 264 // Set to a positive value when a StartSession request is being processed. |
266 int start_session_request_id_; | 265 int start_session_request_id_; |
267 std::unique_ptr<NewSessionMojoCallbackWrapper> pending_start_session_cb_; | 266 std::unique_ptr<NewSessionCallbackWrapper> pending_start_session_cb_; |
268 | 267 |
269 // For JoinSession requests. | 268 // For JoinSession requests. |
270 base::hash_map<int, linked_ptr<NewSessionMojoCallbackWrapper>> | 269 base::hash_map<int, linked_ptr<NewSessionCallbackWrapper>> |
271 pending_join_session_cbs_; | 270 pending_join_session_cbs_; |
272 | 271 |
273 // RAII binding of |this| to an Presentation interface request. | 272 // RAII binding of |this| to an Presentation interface request. |
274 // The binding is removed when binding_ is cleared or goes out of scope. | 273 // The binding is removed when binding_ is cleared or goes out of scope. |
275 std::unique_ptr<mojo::Binding<blink::mojom::PresentationService>> binding_; | 274 std::unique_ptr<mojo::Binding<blink::mojom::PresentationService>> binding_; |
276 | 275 |
277 // There can be only one send message request at a time. | 276 // There can be only one send message request at a time. |
278 std::unique_ptr<SendMessageMojoCallback> send_message_callback_; | 277 std::unique_ptr<SendSessionMessageCallback> send_message_callback_; |
279 | 278 |
280 std::unique_ptr<SessionMessagesCallback> on_session_messages_callback_; | 279 std::unique_ptr<SessionMessagesCallback> on_session_messages_callback_; |
281 | 280 |
282 // ID of the RenderFrameHost this object is associated with. | 281 // ID of the RenderFrameHost this object is associated with. |
283 int render_process_id_; | 282 int render_process_id_; |
284 int render_frame_id_; | 283 int render_frame_id_; |
285 | 284 |
286 // NOTE: Weak pointers must be invalidated before all other member variables. | 285 // NOTE: Weak pointers must be invalidated before all other member variables. |
287 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; | 286 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; |
288 | 287 |
289 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 288 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
290 }; | 289 }; |
291 | 290 |
292 } // namespace content | 291 } // namespace content |
293 | 292 |
294 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 293 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
OLD | NEW |