Chromium Code Reviews| 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" | 19 #include "content/common/presentation/presentation_session.mojom.h" |
| 17 #include "content/public/browser/navigation_details.h" | 20 #include "content/public/browser/navigation_details.h" |
| 18 #include "content/public/browser/presentation_screen_availability_listener.h" | 21 #include "content/public/browser/presentation_screen_availability_listener.h" |
| 19 #include "content/public/browser/presentation_service_delegate.h" | 22 #include "content/public/browser/presentation_service_delegate.h" |
| 20 #include "content/public/browser/web_contents_observer.h" | 23 #include "content/public/browser/web_contents_observer.h" |
| 21 #include "content/public/common/frame_navigate_params.h" | 24 #include "content/public/common/frame_navigate_params.h" |
| 22 | 25 |
| 23 namespace content { | 26 namespace content { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 43 ~PresentationServiceImpl() override; | 46 ~PresentationServiceImpl() override; |
| 44 | 47 |
| 45 // Static factory method to create an instance of PresentationServiceImpl. | 48 // Static factory method to create an instance of PresentationServiceImpl. |
| 46 // |render_frame_host|: The RFH the instance is associated with. | 49 // |render_frame_host|: The RFH the instance is associated with. |
| 47 // |request|: The instance will be bound to this request. Used for Mojo setup. | 50 // |request|: The instance will be bound to this request. Used for Mojo setup. |
| 48 static void CreateMojoService( | 51 static void CreateMojoService( |
| 49 RenderFrameHost* render_frame_host, | 52 RenderFrameHost* render_frame_host, |
| 50 mojo::InterfaceRequest<presentation::PresentationService> request); | 53 mojo::InterfaceRequest<presentation::PresentationService> request); |
| 51 | 54 |
| 52 private: | 55 private: |
| 53 using ScreenAvailabilityMojoCallback = mojo::Callback<void(bool)>; | 56 using ScreenAvailabilityMojoCallback = |
| 57 mojo::Callback<void(mojo::String, bool)>; | |
| 54 using NewSessionMojoCallback = | 58 using NewSessionMojoCallback = |
| 55 mojo::Callback<void(presentation::PresentationSessionInfoPtr, | 59 mojo::Callback<void(presentation::PresentationSessionInfoPtr, |
| 56 presentation::PresentationErrorPtr)>; | 60 presentation::PresentationErrorPtr)>; |
| 57 using DefaultSessionMojoCallback = | 61 using DefaultSessionMojoCallback = |
| 58 mojo::Callback<void(presentation::PresentationSessionInfoPtr)>; | 62 mojo::Callback<void(presentation::PresentationSessionInfoPtr)>; |
| 59 | 63 |
| 64 // A helper data class used by PresentationServiceImpl to do bookkeeping | |
| 65 // of currently registered screen availability listeners. | |
| 66 // An instance of this class is a simple state machine that waits for both | |
| 67 // the available bit and the Mojo callback to become available. | |
| 68 // Once this happens, the Mojo callback will be invoked with the available | |
| 69 // bit, and the state machine will reset. | |
| 70 // The available bit is obtained from the embedder's media router. | |
| 71 // The callback is obtained from the renderer via PresentationServiceImpl's | |
| 72 // GetScreenAvailability(). | |
| 73 class CONTENT_EXPORT ScreenAvailabilityContext | |
|
mark a. foltz
2015/03/18 00:10:58
Is CONTENT_EXPORT only for unit testing?
It is od
imcheng
2015/03/18 00:56:53
Yeah unfortunately this is needed otherwise unit t
| |
| 74 : public PresentationScreenAvailabilityListener { | |
| 75 public: | |
| 76 explicit ScreenAvailabilityContext( | |
| 77 const std::string& presentation_url); | |
| 78 ~ScreenAvailabilityContext() override; | |
| 79 | |
| 80 // If available bit exists, |callback| will be invoked with the bit and | |
| 81 // this state machine will reset. | |
| 82 // Otherwise |callback| is saved for later use. | |
| 83 // |callback|: Callback to the client of PresentationService | |
| 84 // (i.e. the renderer) that screen availability has changed, via Mojo. | |
| 85 void CallbackReceived(const ScreenAvailabilityMojoCallback& callback); | |
| 86 | |
| 87 // Clears both callback and available bit. | |
| 88 void Reset(); | |
| 89 | |
| 90 // PresentationScreenAvailabilityListener implementation. | |
| 91 std::string GetPresentationUrl() const override; | |
| 92 // If callback exists, it will be invoked with |available| and | |
|
mark a. foltz
2015/03/18 00:10:58
Add a blank line before to help readability of the
imcheng
2015/03/18 00:56:53
Done.
| |
| 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 { | |
|
mark a. foltz
2015/03/18 00:10:58
Similar comment about CONTENT_EXPORT.
imcheng
2015/03/18 00:56:53
Ditto
| |
| 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 // Callbacks from the embedder as a result of StartSession/JoinSession. | |
|
mark a. foltz
2015/03/18 00:10:58
I don't quite follow this comment - this is in con
imcheng
2015/03/18 00:56:53
I clarified the comments here.
What I mean is tha
| |
| 190 void OnStartOrJoinSessionSucceeded( | |
| 191 bool is_start_session, | |
| 192 const NewSessionMojoCallback& callback, | |
| 193 const PresentationSessionInfo& session_info); | |
| 194 void OnStartOrJoinSessionError( | |
| 195 bool is_start_session, | |
| 196 const NewSessionMojoCallback& callback, | |
| 197 const PresentationError& error); | |
| 198 | |
| 199 // Requests delegate to start a session. | |
| 200 void DoStartSession( | |
| 201 const std::string& presentation_url, | |
| 202 const std::string& presentation_id, | |
| 203 const NewSessionMojoCallback& callback); | |
| 204 | |
| 205 // Removes the head of the queue (which represents the request that has just | |
| 206 // been processed). | |
| 207 // Checks if there are any queued StartSession requests and if so, executes | |
| 208 // the first one in the queue. | |
| 209 void HandleQueuedStartSessionRequests(); | |
| 210 | |
| 211 // Gets the ScreenAvailabilityContext for |presentation_url|, or creates one | |
| 212 // if it does not exist. | |
| 213 ScreenAvailabilityContext* GetOrCreateAvailabilityContext( | |
| 214 const std::string& presentation_url); | |
| 117 | 215 |
| 118 RenderFrameHost* render_frame_host_; | 216 RenderFrameHost* render_frame_host_; |
| 119 PresentationServiceDelegate* delegate_; | 217 PresentationServiceDelegate* delegate_; |
| 120 | 218 |
| 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. | 219 // Map from presentation URL to its ScreenAvailabilityContext state machine. |
| 162 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> | 220 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> |
| 163 availability_contexts_; | 221 availability_contexts_; |
| 164 | 222 |
| 165 std::string default_presentation_url_; | 223 std::string default_presentation_url_; |
| 224 std::string default_presentation_id_; | |
| 225 | |
| 226 // We only allow one StartSession request to be processed at a time. | |
| 227 // StartSession requests are queued here. When a request has been processed, | |
| 228 // it is removed from head of the queue. | |
| 229 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_; | |
| 230 | |
| 231 // NOTE: Weak pointers must be invalidated before all other member variables. | |
| 232 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; | |
| 166 | 233 |
| 167 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 234 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
| 168 }; | 235 }; |
| 169 | 236 |
| 170 } // namespace content | 237 } // namespace content |
| 171 | 238 |
| 172 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 239 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| OLD | NEW |