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