| 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 <set> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/callback.h" | 15 #include "base/callback.h" |
| 15 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 16 #include "base/containers/hash_tables.h" | 17 #include "base/containers/hash_tables.h" |
| 17 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
| 18 #include "base/macros.h" | 19 #include "base/macros.h" |
| 19 #include "base/memory/linked_ptr.h" | 20 #include "base/memory/linked_ptr.h" |
| 20 #include "base/memory/scoped_vector.h" | 21 #include "base/memory/scoped_vector.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 41 // It handles Presentation API requests coming from Blink / renderer process | 42 // It handles Presentation API requests coming from Blink / renderer process |
| 42 // and delegates the requests to the embedder's media router via | 43 // and delegates the requests to the embedder's media router via |
| 43 // PresentationServiceDelegate. | 44 // PresentationServiceDelegate. |
| 44 // An instance of this class tied to a RenderFrameHost and listens to events | 45 // An instance of this class tied to a RenderFrameHost and listens to events |
| 45 // related to the RFH via implementing WebContentsObserver. | 46 // related to the RFH via implementing WebContentsObserver. |
| 46 // This class is instantiated on-demand via Mojo's ConnectToRemoteService | 47 // This class is instantiated on-demand via Mojo's ConnectToRemoteService |
| 47 // from the renderer when the first presentation API request is handled. | 48 // from the renderer when the first presentation API request is handled. |
| 48 class CONTENT_EXPORT PresentationServiceImpl | 49 class CONTENT_EXPORT PresentationServiceImpl |
| 49 : public NON_EXPORTED_BASE(blink::mojom::PresentationService), | 50 : public NON_EXPORTED_BASE(blink::mojom::PresentationService), |
| 50 public WebContentsObserver, | 51 public WebContentsObserver, |
| 51 public PresentationServiceDelegate::Observer { | 52 public PresentationServiceDelegateBase::Observer { |
| 52 public: | 53 public: |
| 53 using NewSessionCallback = | 54 using NewSessionCallback = |
| 54 base::Callback<void(blink::mojom::PresentationSessionInfoPtr, | 55 base::Callback<void(blink::mojom::PresentationSessionInfoPtr, |
| 55 blink::mojom::PresentationErrorPtr)>; | 56 blink::mojom::PresentationErrorPtr)>; |
| 56 | 57 |
| 57 ~PresentationServiceImpl() override; | 58 ~PresentationServiceImpl() override; |
| 58 | 59 |
| 59 // Static factory method to create an instance of PresentationServiceImpl. | 60 // Static factory method to create an instance of PresentationServiceImpl. |
| 60 // |render_frame_host|: The RFH the instance is associated with. | 61 // |render_frame_host|: The RFH the instance is associated with. |
| 61 // |request|: The instance will be bound to this request. Used for Mojo setup. | 62 // |request|: The instance will be bound to this request. Used for Mojo setup. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 88 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 89 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 89 ReceiveSessionMessagesAfterReset); | 90 ReceiveSessionMessagesAfterReset); |
| 90 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 91 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 91 MaxPendingStartSessionRequests); | 92 MaxPendingStartSessionRequests); |
| 92 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 93 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 93 MaxPendingJoinSessionRequests); | 94 MaxPendingJoinSessionRequests); |
| 94 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 95 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 95 ListenForConnectionStateChange); | 96 ListenForConnectionStateChange); |
| 96 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 97 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 97 ListenForConnectionClose); | 98 ListenForConnectionClose); |
| 99 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 100 SetPresentationConnection); |
| 101 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 102 ReceiverPresentationServiceDelegate); |
| 98 | 103 |
| 99 // Maximum number of pending JoinSession requests at any given time. | 104 // Maximum number of pending JoinSession requests at any given time. |
| 100 static const int kMaxNumQueuedSessionRequests = 10; | 105 static const int kMaxNumQueuedSessionRequests = 10; |
| 101 | 106 |
| 102 using SessionMessagesCallback = | 107 using SessionMessagesCallback = |
| 103 base::Callback<void(mojo::Array<blink::mojom::SessionMessagePtr>)>; | 108 base::Callback<void(mojo::Array<blink::mojom::SessionMessagePtr>)>; |
| 104 using SendSessionMessageCallback = base::Callback<void(bool)>; | 109 using SendSessionMessageCallback = base::Callback<void(bool)>; |
| 105 | 110 |
| 106 // Listener implementation owned by PresentationServiceImpl. An instance of | 111 // Listener implementation owned by PresentationServiceImpl. An instance of |
| 107 // this is created when PresentationRequest.getAvailability() is resolved. | 112 // this is created when PresentationRequest.getAvailability() is resolved. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 136 blink::mojom::PresentationErrorPtr error); | 141 blink::mojom::PresentationErrorPtr error); |
| 137 | 142 |
| 138 private: | 143 private: |
| 139 NewSessionCallback callback_; | 144 NewSessionCallback callback_; |
| 140 | 145 |
| 141 DISALLOW_COPY_AND_ASSIGN(NewSessionCallbackWrapper); | 146 DISALLOW_COPY_AND_ASSIGN(NewSessionCallbackWrapper); |
| 142 }; | 147 }; |
| 143 | 148 |
| 144 // |render_frame_host|: The RFH this instance is associated with. | 149 // |render_frame_host|: The RFH this instance is associated with. |
| 145 // |web_contents|: The WebContents to observe. | 150 // |web_contents|: The WebContents to observe. |
| 146 // |delegate|: Where Presentation API requests are delegated to. Not owned | 151 // |controller_delegate|: Where Presentation API requests are delegated to in |
| 152 // controller frame. Set to null if current frame is receiver frame. Not owned |
| 153 // by this class. |
| 154 // |receiver_delegate|: Where Presentation API requests are delegated to in |
| 155 // receiver frame. Set to null if current frame is controller frame. Not owned |
| 147 // by this class. | 156 // by this class. |
| 148 PresentationServiceImpl( | 157 PresentationServiceImpl( |
| 149 RenderFrameHost* render_frame_host, | 158 RenderFrameHost* render_frame_host, |
| 150 WebContents* web_contents, | 159 WebContents* web_contents, |
| 151 PresentationServiceDelegate* delegate); | 160 ControllerPresentationServiceDelegate* controller_delegate, |
| 161 ReceiverPresentationServiceDelegate* receiver_delegate); |
| 152 | 162 |
| 153 // PresentationService implementation. | 163 // PresentationService implementation. |
| 154 void SetDefaultPresentationUrls( | 164 void SetDefaultPresentationUrls( |
| 155 const std::vector<GURL>& presentation_urls) override; | 165 const std::vector<GURL>& presentation_urls) override; |
| 156 void SetClient(blink::mojom::PresentationServiceClientPtr client) override; | 166 void SetClient(blink::mojom::PresentationServiceClientPtr client) override; |
| 157 void ListenForScreenAvailability(const GURL& url) override; | 167 void ListenForScreenAvailability(const GURL& url) override; |
| 158 void StopListeningForScreenAvailability(const GURL& url) override; | 168 void StopListeningForScreenAvailability(const GURL& url) override; |
| 159 void StartSession(const std::vector<GURL>& presentation_urls, | 169 void StartSession(const std::vector<GURL>& presentation_urls, |
| 160 const NewSessionCallback& callback) override; | 170 const NewSessionCallback& callback) override; |
| 161 void JoinSession(const std::vector<GURL>& presentation_urls, | 171 void JoinSession(const std::vector<GURL>& presentation_urls, |
| 162 const base::Optional<std::string>& presentation_id, | 172 const base::Optional<std::string>& presentation_id, |
| 163 const NewSessionCallback& callback) override; | 173 const NewSessionCallback& callback) override; |
| 164 void SendSessionMessage(blink::mojom::PresentationSessionInfoPtr session_info, | 174 void SendSessionMessage(blink::mojom::PresentationSessionInfoPtr session_info, |
| 165 blink::mojom::SessionMessagePtr session_message, | 175 blink::mojom::SessionMessagePtr session_message, |
| 166 const SendSessionMessageCallback& callback) override; | 176 const SendSessionMessageCallback& callback) override; |
| 167 void CloseConnection(const GURL& presentation_url, | 177 void CloseConnection(const GURL& presentation_url, |
| 168 const std::string& presentation_id) override; | 178 const std::string& presentation_id) override; |
| 169 void Terminate(const GURL& presentation_url, | 179 void Terminate(const GURL& presentation_url, |
| 170 const std::string& presentation_id) override; | 180 const std::string& presentation_id) override; |
| 171 void ListenForSessionMessages( | 181 void ListenForSessionMessages( |
| 172 blink::mojom::PresentationSessionInfoPtr session) override; | 182 blink::mojom::PresentationSessionInfoPtr session) override; |
| 183 void SetPresentationConnection( |
| 184 blink::mojom::PresentationSessionInfoPtr session, |
| 185 blink::mojom::PresentationConnectionPtr connection) override; |
| 173 | 186 |
| 174 // Creates a binding between this object and |request|. | 187 // Creates a binding between this object and |request|. |
| 175 void Bind(mojo::InterfaceRequest<blink::mojom::PresentationService> request); | 188 void Bind(mojo::InterfaceRequest<blink::mojom::PresentationService> request); |
| 176 | 189 |
| 177 // WebContentsObserver override. | 190 // WebContentsObserver override. |
| 178 void DidNavigateAnyFrame( | 191 void DidNavigateAnyFrame( |
| 179 content::RenderFrameHost* render_frame_host, | 192 content::RenderFrameHost* render_frame_host, |
| 180 const content::LoadCommittedDetails& details, | 193 const content::LoadCommittedDetails& details, |
| 181 const content::FrameNavigateParams& params) override; | 194 const content::FrameNavigateParams& params) override; |
| 182 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; | 195 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 void OnJoinSessionError( | 232 void OnJoinSessionError( |
| 220 int request_session_id, | 233 int request_session_id, |
| 221 const PresentationError& error); | 234 const PresentationError& error); |
| 222 void OnSendMessageCallback(bool sent); | 235 void OnSendMessageCallback(bool sent); |
| 223 | 236 |
| 224 // Calls to |delegate_| to start listening for state changes for |connection|. | 237 // Calls to |delegate_| to start listening for state changes for |connection|. |
| 225 // State changes will be returned via |OnConnectionStateChanged|. | 238 // State changes will be returned via |OnConnectionStateChanged|. |
| 226 void ListenForConnectionStateChange( | 239 void ListenForConnectionStateChange( |
| 227 const PresentationSessionInfo& connection); | 240 const PresentationSessionInfo& connection); |
| 228 | 241 |
| 229 // Passed to embedder's implementation of PresentationServiceDelegate for | |
| 230 // later invocation when session messages arrive. | |
| 231 void OnSessionMessages( | 242 void OnSessionMessages( |
| 232 const content::PresentationSessionInfo& session, | 243 const content::PresentationSessionInfo& session, |
| 233 const ScopedVector<PresentationSessionMessage>& messages, | 244 const ScopedVector<PresentationSessionMessage>& messages, |
| 234 bool pass_ownership); | 245 bool pass_ownership); |
| 235 | 246 |
| 247 // A callback registered to OffscreenPresentationManager when |
| 248 // receiver PSImpl inits. Calls |client_| to create a new |
| 249 // PresentationConnection on receiver page. |
| 250 void OnReceiverConnectionAvailable( |
| 251 const content::PresentationSessionInfo& session_info, |
| 252 PresentationConnectionPtr&& controller_connection); |
| 253 |
| 236 // Associates a JoinSession |callback| with a unique request ID and | 254 // Associates a JoinSession |callback| with a unique request ID and |
| 237 // stores it in a map. | 255 // stores it in a map. |
| 238 // Returns a positive value on success. | 256 // Returns a positive value on success. |
| 239 int RegisterJoinSessionCallback(const NewSessionCallback& callback); | 257 int RegisterJoinSessionCallback(const NewSessionCallback& callback); |
| 240 | 258 |
| 241 // Invoked by the embedder's PresentationServiceDelegate when a | 259 // Invoked by the embedder's PresentationServiceDelegate when a |
| 242 // PresentationConnection's state has changed. | 260 // PresentationConnection's state has changed. |
| 243 void OnConnectionStateChanged( | 261 void OnConnectionStateChanged( |
| 244 const PresentationSessionInfo& connection, | 262 const PresentationSessionInfo& connection, |
| 245 const PresentationConnectionStateChangeInfo& info); | 263 const PresentationConnectionStateChangeInfo& info); |
| 246 | 264 |
| 247 // Returns true if this object is associated with |render_frame_host|. | 265 // Returns true if this object is associated with |render_frame_host|. |
| 248 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; | 266 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; |
| 249 | 267 |
| 250 // Embedder-specific delegate to forward Presentation requests to. | 268 // Returns |controller_delegate| if current frame is controller frame; Returns |
| 251 // May be null if embedder does not support Presentation API. | 269 // |receiver_delegate| if current frame is receiver frame. |
| 252 PresentationServiceDelegate* delegate_; | 270 PresentationServiceDelegateBase* GetPresentationServiceDelegate(); |
| 271 |
| 272 // Embedder-specific delegate for controller to forward Presentation requests |
| 273 // to. Must be nullptr if current page is receiver page or |
| 274 // embedder does not support Presentation API . |
| 275 ControllerPresentationServiceDelegate* controller_delegate_; |
| 276 |
| 277 // Embedder-specific delegate for receiver to forward Presentation requests |
| 278 // to. Must be nullptr if current page is receiver page or |
| 279 // embedder does not support Presentation API. |
| 280 ReceiverPresentationServiceDelegate* receiver_delegate_; |
| 253 | 281 |
| 254 // Proxy to the PresentationServiceClient to send results (e.g., screen | 282 // Proxy to the PresentationServiceClient to send results (e.g., screen |
| 255 // availability) to. | 283 // availability) to. |
| 256 blink::mojom::PresentationServiceClientPtr client_; | 284 blink::mojom::PresentationServiceClientPtr client_; |
| 257 | 285 |
| 258 std::vector<GURL> default_presentation_urls_; | 286 std::vector<GURL> default_presentation_urls_; |
| 259 | 287 |
| 260 using ScreenAvailabilityListenerMap = | 288 using ScreenAvailabilityListenerMap = |
| 261 std::map<GURL, std::unique_ptr<ScreenAvailabilityListenerImpl>>; | 289 std::map<GURL, std::unique_ptr<ScreenAvailabilityListenerImpl>>; |
| 262 ScreenAvailabilityListenerMap screen_availability_listeners_; | 290 ScreenAvailabilityListenerMap screen_availability_listeners_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 285 | 313 |
| 286 // NOTE: Weak pointers must be invalidated before all other member variables. | 314 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 287 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; | 315 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; |
| 288 | 316 |
| 289 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 317 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
| 290 }; | 318 }; |
| 291 | 319 |
| 292 } // namespace content | 320 } // namespace content |
| 293 | 321 |
| 294 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 322 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| OLD | NEW |