| 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> |
| 9 |
| 8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 10 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 11 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" | 14 #include "base/macros.h" |
| 13 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" |
| 14 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 15 #include "content/common/presentation/presentation_service.mojom.h" | 18 #include "content/common/presentation/presentation_service.mojom.h" |
| 16 #include "content/common/presentation/presentation_session.mojom.h" | |
| 17 #include "content/public/browser/navigation_details.h" | 19 #include "content/public/browser/navigation_details.h" |
| 18 #include "content/public/browser/presentation_screen_availability_listener.h" | 20 #include "content/public/browser/presentation_screen_availability_listener.h" |
| 19 #include "content/public/browser/presentation_service_delegate.h" | 21 #include "content/public/browser/presentation_service_delegate.h" |
| 20 #include "content/public/browser/web_contents_observer.h" | 22 #include "content/public/browser/web_contents_observer.h" |
| 21 #include "content/public/common/frame_navigate_params.h" | 23 #include "content/public/common/frame_navigate_params.h" |
| 22 | 24 |
| 23 namespace content { | 25 namespace content { |
| 24 | 26 |
| 25 struct FrameNavigateParams; | 27 struct FrameNavigateParams; |
| 26 struct LoadCommittedDetails; | 28 struct LoadCommittedDetails; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 43 ~PresentationServiceImpl() override; | 45 ~PresentationServiceImpl() override; |
| 44 | 46 |
| 45 // Static factory method to create an instance of PresentationServiceImpl. | 47 // Static factory method to create an instance of PresentationServiceImpl. |
| 46 // |render_frame_host|: The RFH the instance is associated with. | 48 // |render_frame_host|: The RFH the instance is associated with. |
| 47 // |request|: The instance will be bound to this request. Used for Mojo setup. | 49 // |request|: The instance will be bound to this request. Used for Mojo setup. |
| 48 static void CreateMojoService( | 50 static void CreateMojoService( |
| 49 RenderFrameHost* render_frame_host, | 51 RenderFrameHost* render_frame_host, |
| 50 mojo::InterfaceRequest<presentation::PresentationService> request); | 52 mojo::InterfaceRequest<presentation::PresentationService> request); |
| 51 | 53 |
| 52 private: | 54 private: |
| 53 using ScreenAvailabilityMojoCallback = mojo::Callback<void(bool)>; | 55 using ScreenAvailabilityMojoCallback = |
| 56 mojo::Callback<void(mojo::String, bool)>; |
| 54 using NewSessionMojoCallback = | 57 using NewSessionMojoCallback = |
| 55 mojo::Callback<void(presentation::PresentationSessionInfoPtr, | 58 mojo::Callback<void(presentation::PresentationSessionInfoPtr, |
| 56 presentation::PresentationErrorPtr)>; | 59 presentation::PresentationErrorPtr)>; |
| 57 using DefaultSessionMojoCallback = | 60 using DefaultSessionMojoCallback = |
| 58 mojo::Callback<void(presentation::PresentationSessionInfoPtr)>; | 61 mojo::Callback<void(presentation::PresentationSessionInfoPtr)>; |
| 59 | 62 |
| 63 // A helper data class used by PresentationServiceImpl to do bookkeeping |
| 64 // of currently registered screen availability listeners. |
| 65 // An instance of this class is a simple state machine that waits for both |
| 66 // the available bit and the Mojo callback to become available. |
| 67 // Once this happens, the Mojo callback will be invoked with the available |
| 68 // bit, and the state machine will reset. |
| 69 // The available bit is obtained from the embedder's media router. |
| 70 // The callback is obtained from the renderer via PresentationServiceImpl's |
| 71 // GetScreenAvailability(). |
| 72 class CONTENT_EXPORT ScreenAvailabilityContext |
| 73 : public PresentationScreenAvailabilityListener { |
| 74 public: |
| 75 explicit ScreenAvailabilityContext( |
| 76 const std::string& presentation_url); |
| 77 ~ScreenAvailabilityContext() override; |
| 78 |
| 79 // If available bit exists, |callback| will be invoked with the bit and |
| 80 // this state machine will reset. |
| 81 // Otherwise |callback| is saved for later use. |
| 82 // |callback|: Callback to the client of PresentationService |
| 83 // (i.e. the renderer) that screen availability has changed, via Mojo. |
| 84 void CallbackReceived(const ScreenAvailabilityMojoCallback& callback); |
| 85 |
| 86 // Clears both callback and available bit. |
| 87 void Reset(); |
| 88 |
| 89 // PresentationScreenAvailabilityListener implementation. |
| 90 std::string GetPresentationUrl() const override; |
| 91 |
| 92 // If callback exists, it will be invoked with |available| and |
| 93 // this state machine will reset. |
| 94 // Otherwise |available| is saved for later use. |
| 95 // |available|: New screen availability for the presentation URL. |
| 96 void OnScreenAvailabilityChanged(bool available) override; |
| 97 |
| 98 // Gets the callback as a raw pointer. |
| 99 ScreenAvailabilityMojoCallback* GetCallback() const; |
| 100 |
| 101 private: |
| 102 std::string presentation_url_; |
| 103 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; |
| 104 scoped_ptr<bool> available_ptr_; |
| 105 }; |
| 106 |
| 107 // Context for a StartSession request. |
| 108 struct CONTENT_EXPORT StartSessionRequest { |
| 109 StartSessionRequest(const std::string& presentation_url, |
| 110 const std::string& presentation_id, |
| 111 const NewSessionMojoCallback& callback); |
| 112 ~StartSessionRequest(); |
| 113 |
| 114 const std::string presentation_url; |
| 115 const std::string presentation_id; |
| 116 const NewSessionMojoCallback callback; |
| 117 }; |
| 118 |
| 60 friend class PresentationServiceImplTest; | 119 friend class PresentationServiceImplTest; |
| 61 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, RemoveAllListeners); | 120 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, Reset); |
| 62 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 121 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 63 DidNavigateThisFrame); | 122 DidNavigateThisFrame); |
| 64 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 123 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 65 DidNavigateNotThisFrame); | 124 DidNavigateNotThisFrame); |
| 66 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 125 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 67 ThisRenderFrameDeleted); | 126 ThisRenderFrameDeleted); |
| 68 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 127 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 69 NotThisRenderFrameDeleted); | 128 NotThisRenderFrameDeleted); |
| 129 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 130 SetDefaultPresentationUrl); |
| 131 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 132 SetSameDefaultPresentationUrl); |
| 133 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 134 ClearDefaultPresentationUrl); |
| 70 | 135 |
| 71 // |render_frame_host|: The RFH this instance is associated with. | 136 // |render_frame_host|: The RFH this instance is associated with. |
| 72 // |web_contents|: The WebContents to observe. | 137 // |web_contents|: The WebContents to observe. |
| 73 // |delegate|: Where Presentation API requests are delegated to. Not owned | 138 // |delegate|: Where Presentation API requests are delegated to. Not owned |
| 74 // by this class. | 139 // by this class. |
| 75 PresentationServiceImpl( | 140 PresentationServiceImpl( |
| 76 RenderFrameHost* render_frame_host, | 141 RenderFrameHost* render_frame_host, |
| 77 WebContents* web_contents, | 142 WebContents* web_contents, |
| 78 PresentationServiceDelegate* delegate); | 143 PresentationServiceDelegate* delegate); |
| 79 | 144 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 104 // WebContentsObserver override. | 169 // WebContentsObserver override. |
| 105 void DidNavigateAnyFrame( | 170 void DidNavigateAnyFrame( |
| 106 content::RenderFrameHost* render_frame_host, | 171 content::RenderFrameHost* render_frame_host, |
| 107 const content::LoadCommittedDetails& details, | 172 const content::LoadCommittedDetails& details, |
| 108 const content::FrameNavigateParams& params) override; | 173 const content::FrameNavigateParams& params) override; |
| 109 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; | 174 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; |
| 110 | 175 |
| 111 // PresentationServiceDelegate::Observer | 176 // PresentationServiceDelegate::Observer |
| 112 void OnDelegateDestroyed() override; | 177 void OnDelegateDestroyed() override; |
| 113 | 178 |
| 114 // Removes all listeners on this instance and informs the | 179 // Sets |default_presentation_url_| to |presentation_url| and informs the |
| 115 // PresentationServiceDelegate of such. | 180 // delegate of new default presentation URL and ID. |
| 116 void RemoveAllListeners(); | 181 void DoSetDefaultPresentationUrl( |
| 182 const std::string& presentation_url, |
| 183 const std::string& presentation_id); |
| 184 |
| 185 // Removes all listeners and resets default presentation URL on this instance |
| 186 // and informs the PresentationServiceDelegate of such. |
| 187 void Reset(); |
| 188 |
| 189 // These two functions are bound as base::Callbacks and passed to |
| 190 // embedder's implementation of PresentationServiceDelegate for later |
| 191 // invocation. |
| 192 void OnStartOrJoinSessionSucceeded( |
| 193 bool is_start_session, |
| 194 const NewSessionMojoCallback& callback, |
| 195 const PresentationSessionInfo& session_info); |
| 196 void OnStartOrJoinSessionError( |
| 197 bool is_start_session, |
| 198 const NewSessionMojoCallback& callback, |
| 199 const PresentationError& error); |
| 200 |
| 201 // Requests delegate to start a session. |
| 202 void DoStartSession( |
| 203 const std::string& presentation_url, |
| 204 const std::string& presentation_id, |
| 205 const NewSessionMojoCallback& callback); |
| 206 |
| 207 // Removes the head of the queue (which represents the request that has just |
| 208 // been processed). |
| 209 // Checks if there are any queued StartSession requests and if so, executes |
| 210 // the first one in the queue. |
| 211 void HandleQueuedStartSessionRequests(); |
| 212 |
| 213 // Gets the ScreenAvailabilityContext for |presentation_url|, or creates one |
| 214 // if it does not exist. |
| 215 ScreenAvailabilityContext* GetOrCreateAvailabilityContext( |
| 216 const std::string& presentation_url); |
| 117 | 217 |
| 118 RenderFrameHost* render_frame_host_; | 218 RenderFrameHost* render_frame_host_; |
| 119 PresentationServiceDelegate* delegate_; | 219 PresentationServiceDelegate* delegate_; |
| 120 | 220 |
| 121 // A helper data class used by PresentationServiceImpl to do bookkeeping | |
| 122 // of currently registered screen availability listeners. | |
| 123 // An instance of this class is a simple state machine that waits for both | |
| 124 // the available bit and the Mojo callback to become available. | |
| 125 // Once this happens, the Mojo callback will be invoked with the available | |
| 126 // bit, and the state machine will reset. | |
| 127 // The available bit is obtained from the embedder's media router. | |
| 128 // The callback is obtained from the renderer via PresentationServiceImpl's | |
| 129 // GetScreenAvailability(). | |
| 130 class ScreenAvailabilityContext | |
| 131 : public PresentationScreenAvailabilityListener { | |
| 132 public: | |
| 133 explicit ScreenAvailabilityContext( | |
| 134 const std::string& presentation_url); | |
| 135 ~ScreenAvailabilityContext() override; | |
| 136 | |
| 137 // If available bit exists, |callback| will be invoked with the bit and | |
| 138 // this state machine will reset. | |
| 139 // Otherwise |callback| is saved for later use. | |
| 140 // |callback|: Callback to the client of PresentationService | |
| 141 // (i.e. the renderer) that screen availability has changed, via Mojo. | |
| 142 void CallbackReceived(const ScreenAvailabilityMojoCallback& callback); | |
| 143 | |
| 144 // Clears both callback and available bit. | |
| 145 void Reset(); | |
| 146 | |
| 147 // PresentationScreenAvailabilityListener implementation. | |
| 148 std::string GetPresentationUrl() const override; | |
| 149 // If callback exists, it will be invoked with |available| and | |
| 150 // this state machine will reset. | |
| 151 // Otherwise |available| is saved for later use. | |
| 152 // |available|: New screen availability for the presentation URL. | |
| 153 void OnScreenAvailabilityChanged(bool available) override; | |
| 154 | |
| 155 private: | |
| 156 std::string presentation_url_; | |
| 157 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; | |
| 158 scoped_ptr<bool> available_ptr_; | |
| 159 }; | |
| 160 | |
| 161 // Map from presentation URL to its ScreenAvailabilityContext state machine. | 221 // Map from presentation URL to its ScreenAvailabilityContext state machine. |
| 162 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> | 222 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> |
| 163 availability_contexts_; | 223 availability_contexts_; |
| 164 | 224 |
| 165 std::string default_presentation_url_; | 225 std::string default_presentation_url_; |
| 226 std::string default_presentation_id_; |
| 227 |
| 228 // We only allow one StartSession request to be processed at a time. |
| 229 // StartSession requests are queued here. When a request has been processed, |
| 230 // it is removed from head of the queue. |
| 231 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_; |
| 232 |
| 233 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 234 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; |
| 166 | 235 |
| 167 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 236 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
| 168 }; | 237 }; |
| 169 | 238 |
| 170 } // namespace content | 239 } // namespace content |
| 171 | 240 |
| 172 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 241 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| OLD | NEW |