| 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/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/linked_ptr.h" | 17 #include "base/memory/linked_ptr.h" |
| 18 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
| 19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 20 #include "content/common/content_export.h" | 20 #include "content/common/content_export.h" |
| 21 #include "content/common/presentation/presentation_service.mojom.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" |
| 27 #include "third_party/WebKit/public/platform/modules/presentation/presentation.m
ojom.h" |
| 28 | 28 |
| 29 namespace content { | 29 namespace content { |
| 30 | 30 |
| 31 struct FrameNavigateParams; | 31 struct FrameNavigateParams; |
| 32 struct LoadCommittedDetails; | 32 struct LoadCommittedDetails; |
| 33 struct PresentationSessionMessage; | 33 struct PresentationSessionMessage; |
| 34 class RenderFrameHost; | 34 class RenderFrameHost; |
| 35 | 35 |
| 36 using NewSessionMojoCallback = | 36 using NewSessionMojoCallback = |
| 37 mojo::Callback<void(mojom::PresentationSessionInfoPtr, | 37 mojo::Callback<void(blink::mojom::PresentationSessionInfoPtr, |
| 38 mojom::PresentationErrorPtr)>; | 38 blink::mojom::PresentationErrorPtr)>; |
| 39 | 39 |
| 40 // Implementation of Mojo PresentationService. | 40 // Implementation of Mojo PresentationService. |
| 41 // It handles Presentation API requests coming from Blink / renderer process | 41 // It handles Presentation API requests coming from Blink / renderer process |
| 42 // and delegates the requests to the embedder's media router via | 42 // and delegates the requests to the embedder's media router via |
| 43 // PresentationServiceDelegate. | 43 // PresentationServiceDelegate. |
| 44 // 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 |
| 45 // related to the RFH via implementing WebContentsObserver. | 45 // related to the RFH via implementing WebContentsObserver. |
| 46 // This class is instantiated on-demand via Mojo's ConnectToRemoteService | 46 // This class is instantiated on-demand via Mojo's ConnectToRemoteService |
| 47 // from the renderer when the first presentation API request is handled. | 47 // from the renderer when the first presentation API request is handled. |
| 48 class CONTENT_EXPORT PresentationServiceImpl | 48 class CONTENT_EXPORT PresentationServiceImpl |
| 49 : public NON_EXPORTED_BASE(mojom::PresentationService), | 49 : public NON_EXPORTED_BASE(blink::mojom::PresentationService), |
| 50 public WebContentsObserver, | 50 public WebContentsObserver, |
| 51 public PresentationServiceDelegate::Observer { | 51 public PresentationServiceDelegate::Observer { |
| 52 public: | 52 public: |
| 53 ~PresentationServiceImpl() override; | 53 ~PresentationServiceImpl() override; |
| 54 | 54 |
| 55 // Static factory method to create an instance of PresentationServiceImpl. | 55 // Static factory method to create an instance of PresentationServiceImpl. |
| 56 // |render_frame_host|: The RFH the instance is associated with. | 56 // |render_frame_host|: The RFH the instance is associated with. |
| 57 // |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. |
| 58 static void CreateMojoService( | 58 static void CreateMojoService( |
| 59 RenderFrameHost* render_frame_host, | 59 RenderFrameHost* render_frame_host, |
| 60 mojo::InterfaceRequest<mojom::PresentationService> request); | 60 mojo::InterfaceRequest<blink::mojom::PresentationService> request); |
| 61 | 61 |
| 62 private: | 62 private: |
| 63 friend class PresentationServiceImplTest; | 63 friend class PresentationServiceImplTest; |
| 64 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, Reset); | 64 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, Reset); |
| 65 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, DidNavigateThisFrame); | 65 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, DidNavigateThisFrame); |
| 66 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 66 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 67 DidNavigateOtherFrame); | 67 DidNavigateOtherFrame); |
| 68 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, ThisRenderFrameDeleted); | 68 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, ThisRenderFrameDeleted); |
| 69 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 69 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 70 OtherRenderFrameDeleted); | 70 OtherRenderFrameDeleted); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 89 MaxPendingJoinSessionRequests); | 89 MaxPendingJoinSessionRequests); |
| 90 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 90 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 91 ListenForConnectionStateChange); | 91 ListenForConnectionStateChange); |
| 92 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 92 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 93 ListenForConnectionClose); | 93 ListenForConnectionClose); |
| 94 | 94 |
| 95 // Maximum number of pending JoinSession requests at any given time. | 95 // Maximum number of pending JoinSession requests at any given time. |
| 96 static const int kMaxNumQueuedSessionRequests = 10; | 96 static const int kMaxNumQueuedSessionRequests = 10; |
| 97 | 97 |
| 98 using PresentationSessionMojoCallback = | 98 using PresentationSessionMojoCallback = |
| 99 mojo::Callback<void(mojom::PresentationSessionInfoPtr)>; | 99 mojo::Callback<void(blink::mojom::PresentationSessionInfoPtr)>; |
| 100 using SessionMessagesCallback = | 100 using SessionMessagesCallback = |
| 101 mojo::Callback<void(mojo::Array<mojom::SessionMessagePtr>)>; | 101 mojo::Callback<void(mojo::Array<blink::mojom::SessionMessagePtr>)>; |
| 102 using SendMessageMojoCallback = mojo::Callback<void(bool)>; | 102 using SendMessageMojoCallback = mojo::Callback<void(bool)>; |
| 103 | 103 |
| 104 // Listener implementation owned by PresentationServiceImpl. An instance of | 104 // Listener implementation owned by PresentationServiceImpl. An instance of |
| 105 // this is created when PresentationRequest.getAvailability() is resolved. | 105 // this is created when PresentationRequest.getAvailability() is resolved. |
| 106 // The instance receives screen availability results from the embedder and | 106 // The instance receives screen availability results from the embedder and |
| 107 // propagates results back to PresentationServiceImpl. | 107 // propagates results back to PresentationServiceImpl. |
| 108 class CONTENT_EXPORT ScreenAvailabilityListenerImpl | 108 class CONTENT_EXPORT ScreenAvailabilityListenerImpl |
| 109 : public PresentationScreenAvailabilityListener { | 109 : public PresentationScreenAvailabilityListener { |
| 110 public: | 110 public: |
| 111 ScreenAvailabilityListenerImpl( | 111 ScreenAvailabilityListenerImpl( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 124 }; | 124 }; |
| 125 | 125 |
| 126 // Ensures the provided NewSessionMojoCallback is invoked exactly once | 126 // Ensures the provided NewSessionMojoCallback is invoked exactly once |
| 127 // before it goes out of scope. | 127 // before it goes out of scope. |
| 128 class NewSessionMojoCallbackWrapper { | 128 class NewSessionMojoCallbackWrapper { |
| 129 public: | 129 public: |
| 130 explicit NewSessionMojoCallbackWrapper( | 130 explicit NewSessionMojoCallbackWrapper( |
| 131 const NewSessionMojoCallback& callback); | 131 const NewSessionMojoCallback& callback); |
| 132 ~NewSessionMojoCallbackWrapper(); | 132 ~NewSessionMojoCallbackWrapper(); |
| 133 | 133 |
| 134 void Run(mojom::PresentationSessionInfoPtr session, | 134 void Run(blink::mojom::PresentationSessionInfoPtr session, |
| 135 mojom::PresentationErrorPtr error); | 135 blink::mojom::PresentationErrorPtr error); |
| 136 | 136 |
| 137 private: | 137 private: |
| 138 NewSessionMojoCallback callback_; | 138 NewSessionMojoCallback callback_; |
| 139 | 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(NewSessionMojoCallbackWrapper); | 140 DISALLOW_COPY_AND_ASSIGN(NewSessionMojoCallbackWrapper); |
| 141 }; | 141 }; |
| 142 | 142 |
| 143 // |render_frame_host|: The RFH this instance is associated with. | 143 // |render_frame_host|: The RFH this instance is associated with. |
| 144 // |web_contents|: The WebContents to observe. | 144 // |web_contents|: The WebContents to observe. |
| 145 // |delegate|: Where Presentation API requests are delegated to. Not owned | 145 // |delegate|: Where Presentation API requests are delegated to. Not owned |
| 146 // by this class. | 146 // by this class. |
| 147 PresentationServiceImpl( | 147 PresentationServiceImpl( |
| 148 RenderFrameHost* render_frame_host, | 148 RenderFrameHost* render_frame_host, |
| 149 WebContents* web_contents, | 149 WebContents* web_contents, |
| 150 PresentationServiceDelegate* delegate); | 150 PresentationServiceDelegate* delegate); |
| 151 | 151 |
| 152 // PresentationService implementation. | 152 // PresentationService implementation. |
| 153 void SetDefaultPresentationURL(const mojo::String& url) override; | 153 void SetDefaultPresentationURL(const mojo::String& url) override; |
| 154 void SetClient(mojom::PresentationServiceClientPtr client) override; | 154 void SetClient(blink::mojom::PresentationServiceClientPtr client) override; |
| 155 void ListenForScreenAvailability(const mojo::String& url) override; | 155 void ListenForScreenAvailability(const mojo::String& url) override; |
| 156 void StopListeningForScreenAvailability(const mojo::String& url) override; | 156 void StopListeningForScreenAvailability(const mojo::String& url) override; |
| 157 void StartSession( | 157 void StartSession( |
| 158 const mojo::String& presentation_url, | 158 const mojo::String& presentation_url, |
| 159 const NewSessionMojoCallback& callback) override; | 159 const NewSessionMojoCallback& callback) override; |
| 160 void JoinSession( | 160 void JoinSession( |
| 161 const mojo::String& presentation_url, | 161 const mojo::String& presentation_url, |
| 162 const mojo::String& presentation_id, | 162 const mojo::String& presentation_id, |
| 163 const NewSessionMojoCallback& callback) override; | 163 const NewSessionMojoCallback& callback) override; |
| 164 void SendSessionMessage(mojom::PresentationSessionInfoPtr session_info, | 164 void SendSessionMessage(blink::mojom::PresentationSessionInfoPtr session_info, |
| 165 mojom::SessionMessagePtr session_message, | 165 blink::mojom::SessionMessagePtr session_message, |
| 166 const SendMessageMojoCallback& callback) override; | 166 const SendMessageMojoCallback& callback) override; |
| 167 void CloseConnection(const mojo::String& presentation_url, | 167 void CloseConnection(const mojo::String& presentation_url, |
| 168 const mojo::String& presentation_id) override; | 168 const mojo::String& presentation_id) override; |
| 169 void Terminate(const mojo::String& presentation_url, | 169 void Terminate(const mojo::String& presentation_url, |
| 170 const mojo::String& presentation_id) override; | 170 const mojo::String& presentation_id) override; |
| 171 void ListenForSessionMessages( | 171 void ListenForSessionMessages( |
| 172 mojom::PresentationSessionInfoPtr session) override; | 172 blink::mojom::PresentationSessionInfoPtr session) override; |
| 173 | 173 |
| 174 // Creates a binding between this object and |request|. | 174 // Creates a binding between this object and |request|. |
| 175 void Bind(mojo::InterfaceRequest<mojom::PresentationService> request); | 175 void Bind(mojo::InterfaceRequest<blink::mojom::PresentationService> request); |
| 176 | 176 |
| 177 // WebContentsObserver override. | 177 // WebContentsObserver override. |
| 178 void DidNavigateAnyFrame( | 178 void DidNavigateAnyFrame( |
| 179 content::RenderFrameHost* render_frame_host, | 179 content::RenderFrameHost* render_frame_host, |
| 180 const content::LoadCommittedDetails& details, | 180 const content::LoadCommittedDetails& details, |
| 181 const content::FrameNavigateParams& params) override; | 181 const content::FrameNavigateParams& params) override; |
| 182 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; | 182 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; |
| 183 void WebContentsDestroyed() override; | 183 void WebContentsDestroyed() override; |
| 184 | 184 |
| 185 // PresentationServiceDelegate::Observer | 185 // PresentationServiceDelegate::Observer |
| 186 void OnDelegateDestroyed() override; | 186 void OnDelegateDestroyed() override; |
| 187 | 187 |
| 188 // Passed to embedder's implementation of PresentationServiceDelegate for | 188 // Passed to embedder's implementation of PresentationServiceDelegate for |
| 189 // later invocation when default presentation has started. | 189 // later invocation when default presentation has started. |
| 190 void OnDefaultPresentationStarted( | 190 void OnDefaultPresentationStarted( |
| 191 const PresentationSessionInfo& session_info); | 191 const PresentationSessionInfo& session_info); |
| 192 | 192 |
| 193 // Finds the callback from |pending_join_session_cbs_| using | 193 // Finds the callback from |pending_join_session_cbs_| using |
| 194 // |request_session_id|. | 194 // |request_session_id|. |
| 195 // If it exists, invoke it with |session| and |error|, then erase it from | 195 // If it exists, invoke it with |session| and |error|, then erase it from |
| 196 // |pending_join_session_cbs_|. | 196 // |pending_join_session_cbs_|. |
| 197 // Returns true if the callback was found. | 197 // Returns true if the callback was found. |
| 198 bool RunAndEraseJoinSessionMojoCallback( | 198 bool RunAndEraseJoinSessionMojoCallback( |
| 199 int request_session_id, | 199 int request_session_id, |
| 200 mojom::PresentationSessionInfoPtr session, | 200 blink::mojom::PresentationSessionInfoPtr session, |
| 201 mojom::PresentationErrorPtr error); | 201 blink::mojom::PresentationErrorPtr error); |
| 202 | 202 |
| 203 // Removes all listeners and resets default presentation URL on this instance | 203 // Removes all listeners and resets default presentation URL on this instance |
| 204 // and informs the PresentationServiceDelegate of such. | 204 // and informs the PresentationServiceDelegate of such. |
| 205 void Reset(); | 205 void Reset(); |
| 206 | 206 |
| 207 // These functions are bound as base::Callbacks and passed to | 207 // These functions are bound as base::Callbacks and passed to |
| 208 // embedder's implementation of PresentationServiceDelegate for later | 208 // embedder's implementation of PresentationServiceDelegate for later |
| 209 // invocation. | 209 // invocation. |
| 210 void OnStartSessionSucceeded( | 210 void OnStartSessionSucceeded( |
| 211 int request_session_id, | 211 int request_session_id, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 | 246 |
| 247 // Returns true if this object is associated with |render_frame_host|. | 247 // Returns true if this object is associated with |render_frame_host|. |
| 248 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; | 248 bool FrameMatches(content::RenderFrameHost* render_frame_host) const; |
| 249 | 249 |
| 250 // Embedder-specific delegate to forward Presentation requests to. | 250 // Embedder-specific delegate to forward Presentation requests to. |
| 251 // May be null if embedder does not support Presentation API. | 251 // May be null if embedder does not support Presentation API. |
| 252 PresentationServiceDelegate* delegate_; | 252 PresentationServiceDelegate* delegate_; |
| 253 | 253 |
| 254 // Proxy to the PresentationServiceClient to send results (e.g., screen | 254 // Proxy to the PresentationServiceClient to send results (e.g., screen |
| 255 // availability) to. | 255 // availability) to. |
| 256 mojom::PresentationServiceClientPtr client_; | 256 blink::mojom::PresentationServiceClientPtr client_; |
| 257 | 257 |
| 258 std::string default_presentation_url_; | 258 std::string default_presentation_url_; |
| 259 | 259 |
| 260 using ScreenAvailabilityListenerMap = | 260 using ScreenAvailabilityListenerMap = |
| 261 std::map<std::string, std::unique_ptr<ScreenAvailabilityListenerImpl>>; | 261 std::map<std::string, std::unique_ptr<ScreenAvailabilityListenerImpl>>; |
| 262 ScreenAvailabilityListenerMap screen_availability_listeners_; | 262 ScreenAvailabilityListenerMap screen_availability_listeners_; |
| 263 | 263 |
| 264 // For StartSession requests. | 264 // For StartSession requests. |
| 265 // 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. |
| 266 int start_session_request_id_; | 266 int start_session_request_id_; |
| 267 std::unique_ptr<NewSessionMojoCallbackWrapper> pending_start_session_cb_; | 267 std::unique_ptr<NewSessionMojoCallbackWrapper> pending_start_session_cb_; |
| 268 | 268 |
| 269 // For JoinSession requests. | 269 // For JoinSession requests. |
| 270 base::hash_map<int, linked_ptr<NewSessionMojoCallbackWrapper>> | 270 base::hash_map<int, linked_ptr<NewSessionMojoCallbackWrapper>> |
| 271 pending_join_session_cbs_; | 271 pending_join_session_cbs_; |
| 272 | 272 |
| 273 // RAII binding of |this| to an Presentation interface request. | 273 // RAII binding of |this| to an Presentation interface request. |
| 274 // 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. |
| 275 std::unique_ptr<mojo::Binding<mojom::PresentationService>> binding_; | 275 std::unique_ptr<mojo::Binding<blink::mojom::PresentationService>> binding_; |
| 276 | 276 |
| 277 // There can be only one send message request at a time. | 277 // There can be only one send message request at a time. |
| 278 std::unique_ptr<SendMessageMojoCallback> send_message_callback_; | 278 std::unique_ptr<SendMessageMojoCallback> send_message_callback_; |
| 279 | 279 |
| 280 std::unique_ptr<SessionMessagesCallback> on_session_messages_callback_; | 280 std::unique_ptr<SessionMessagesCallback> on_session_messages_callback_; |
| 281 | 281 |
| 282 // ID of the RenderFrameHost this object is associated with. | 282 // ID of the RenderFrameHost this object is associated with. |
| 283 int render_process_id_; | 283 int render_process_id_; |
| 284 int render_frame_id_; | 284 int render_frame_id_; |
| 285 | 285 |
| 286 // NOTE: Weak pointers must be invalidated before all other member variables. | 286 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 287 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; | 287 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; |
| 288 | 288 |
| 289 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 289 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
| 290 }; | 290 }; |
| 291 | 291 |
| 292 } // namespace content | 292 } // namespace content |
| 293 | 293 |
| 294 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 294 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| OLD | NEW |