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 "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | |
| 14 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
| 15 #include "content/common/presentation/presentation_service.mojom.h" | 16 #include "content/common/presentation/presentation_service.mojom.h" |
| 16 #include "content/common/presentation/presentation_session.mojom.h" | 17 #include "content/common/presentation/presentation_session.mojom.h" |
| 17 #include "content/public/browser/navigation_details.h" | 18 #include "content/public/browser/navigation_details.h" |
| 18 #include "content/public/browser/presentation_screen_availability_listener.h" | 19 #include "content/public/browser/presentation_screen_availability_listener.h" |
| 19 #include "content/public/browser/presentation_service_delegate.h" | 20 #include "content/public/browser/presentation_service_delegate.h" |
| 20 #include "content/public/browser/web_contents_observer.h" | 21 #include "content/public/browser/web_contents_observer.h" |
| 21 #include "content/public/common/frame_navigate_params.h" | 22 #include "content/public/common/frame_navigate_params.h" |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 49 RenderFrameHost* render_frame_host, | 50 RenderFrameHost* render_frame_host, |
| 50 mojo::InterfaceRequest<presentation::PresentationService> request); | 51 mojo::InterfaceRequest<presentation::PresentationService> request); |
| 51 | 52 |
| 52 private: | 53 private: |
| 53 using ScreenAvailabilityMojoCallback = mojo::Callback<void(bool)>; | 54 using ScreenAvailabilityMojoCallback = mojo::Callback<void(bool)>; |
| 54 using NewSessionMojoCallback = | 55 using NewSessionMojoCallback = |
| 55 mojo::Callback<void(presentation::PresentationSessionInfoPtr, | 56 mojo::Callback<void(presentation::PresentationSessionInfoPtr, |
| 56 presentation::PresentationErrorPtr)>; | 57 presentation::PresentationErrorPtr)>; |
| 57 | 58 |
| 58 friend class PresentationServiceImplTest; | 59 friend class PresentationServiceImplTest; |
| 59 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, RemoveAllListeners); | 60 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, Reset); |
| 60 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 61 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 61 DidNavigateThisFrame); | 62 DidNavigateThisFrame); |
| 62 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 63 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 63 DidNavigateNotThisFrame); | 64 DidNavigateNotThisFrame); |
| 64 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 65 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 65 ThisRenderFrameDeleted); | 66 ThisRenderFrameDeleted); |
| 66 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 67 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 67 NotThisRenderFrameDeleted); | 68 NotThisRenderFrameDeleted); |
| 69 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 70 SetDefaultPresentationUrl); | |
| 71 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 72 SetSameDefaultPresentationUrl); | |
| 73 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 74 ClearDefaultPresentationUrl); | |
| 68 | 75 |
| 69 // |render_frame_host|: The RFH this instance is associated with. | 76 // |render_frame_host|: The RFH this instance is associated with. |
| 70 // |web_contents|: The WebContents to observe. | 77 // |web_contents|: The WebContents to observe. |
| 71 // |delegate|: Where Presentation API requests are delegated to. Not owned | 78 // |delegate|: Where Presentation API requests are delegated to. Not owned |
| 72 // by this class. | 79 // by this class. |
| 73 PresentationServiceImpl( | 80 PresentationServiceImpl( |
| 74 RenderFrameHost* render_frame_host, | 81 RenderFrameHost* render_frame_host, |
| 75 WebContents* web_contents, | 82 WebContents* web_contents, |
| 76 PresentationServiceDelegate* delegate); | 83 PresentationServiceDelegate* delegate); |
| 77 | 84 |
| 78 // PresentationService implementation. | 85 // PresentationService implementation. |
| 79 void GetScreenAvailability( | 86 void GetScreenAvailability( |
| 80 const mojo::String& presentation_url, | 87 const mojo::String& presentation_url, |
| 81 const ScreenAvailabilityMojoCallback& callback) override; | 88 const ScreenAvailabilityMojoCallback& callback) override; |
| 82 void OnScreenAvailabilityListenerRemoved() override; | 89 void OnScreenAvailabilityListenerRemoved() override; |
| 83 void StartSession( | 90 void StartSession( |
| 84 const mojo::String& presentation_url, | 91 const mojo::String& presentation_url, |
| 85 const mojo::String& presentation_id, | 92 const mojo::String& presentation_id, |
| 86 const NewSessionMojoCallback& callback) override; | 93 const NewSessionMojoCallback& callback) override; |
| 87 void JoinSession( | 94 void JoinSession( |
| 88 const mojo::String& presentation_url, | 95 const mojo::String& presentation_url, |
| 89 const mojo::String& presentation_id, | 96 const mojo::String& presentation_id, |
| 90 const NewSessionMojoCallback& callback) override; | 97 const NewSessionMojoCallback& callback) override; |
| 98 void SetDefaultPresentationUrl( | |
| 99 const mojo::String& presentation_url) override; | |
| 91 | 100 |
| 92 // mojo::InterfaceImpl override. | 101 // mojo::InterfaceImpl override. |
| 93 // Note that this is called when the RenderFrameHost is deleted. | 102 // Note that this is called when the RenderFrameHost is deleted. |
| 94 void OnConnectionError() override; | 103 void OnConnectionError() override; |
| 95 | 104 |
| 96 // WebContentsObserver override. | 105 // WebContentsObserver override. |
| 97 void DidNavigateAnyFrame( | 106 void DidNavigateAnyFrame( |
| 98 content::RenderFrameHost* render_frame_host, | 107 content::RenderFrameHost* render_frame_host, |
| 99 const content::LoadCommittedDetails& details, | 108 const content::LoadCommittedDetails& details, |
| 100 const content::FrameNavigateParams& params) override; | 109 const content::FrameNavigateParams& params) override; |
| 101 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; | 110 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; |
| 102 | 111 |
| 103 // PresentationServiceDelegate::Observer | 112 // PresentationServiceDelegate::Observer |
| 104 void OnDelegateDestroyed() override; | 113 void OnDelegateDestroyed() override; |
| 105 | 114 |
| 106 // Removes all listeners on this instance and informs the | 115 // Sets |default_presentation_url_| to |presentation_url| and informs the |
| 107 // PresentationServiceDelegate of such. | 116 // delegate of such. |
| 108 void RemoveAllListeners(); | 117 void DoSetDefaultPresentationUrl(const std::string& presentation_url); |
| 118 | |
| 119 // Removes all listeners and resets default presentation URL on this instance | |
| 120 // and informs the PresentationServiceDelegate of such. | |
| 121 void Reset(); | |
| 122 | |
| 123 // Callbacks from the embedder as a result of StartSession/JoinSession. | |
| 124 void OnStartOrJoinSessionSucceeded( | |
| 125 const PresentationSessionInfo& session_info); | |
| 126 void OnStartOrJoinSessionError(const PresentationError& error); | |
| 109 | 127 |
| 110 RenderFrameHost* render_frame_host_; | 128 RenderFrameHost* render_frame_host_; |
| 111 PresentationServiceDelegate* delegate_; | 129 PresentationServiceDelegate* delegate_; |
| 112 | |
| 113 // A helper data class used by PresentationServiceImpl to do bookkeeping | |
| 114 // of currently registered screen availability listeners. | |
| 115 // An instance of this class is a simple state machine that waits for both | |
| 116 // the available bit and the Mojo callback to become available. | |
| 117 // Once this happens, the Mojo callback will be invoked with the available | |
| 118 // bit, and the state machine will reset. | |
| 119 // The available bit is obtained from the embedder's media router. | |
| 120 // The callback is obtained from the renderer via PresentationServiceImpl's | |
| 121 // GetScreenAvailability(). | |
| 122 class ScreenAvailabilityContext | |
| 123 : public PresentationScreenAvailabilityListener { | |
| 124 public: | |
| 125 explicit ScreenAvailabilityContext( | |
| 126 const std::string& presentation_url); | |
| 127 ~ScreenAvailabilityContext() override; | |
| 128 | |
| 129 // If available bit exists, |callback| will be invoked with the bit and | |
| 130 // this state machine will reset. | |
| 131 // Otherwise |callback| is saved for later use. | |
| 132 // |callback|: Callback to the client of PresentationService | |
| 133 // (i.e. the renderer) that screen availability has changed, via Mojo. | |
| 134 void CallbackReceived(const ScreenAvailabilityMojoCallback& callback); | |
| 135 | |
| 136 // Clears both callback and available bit. | |
| 137 void Reset(); | |
| 138 | |
| 139 // PresentationScreenAvailabilityListener implementation. | |
| 140 std::string GetPresentationUrl() const override; | |
| 141 // If callback exists, it will be invoked with |available| and | |
| 142 // this state machine will reset. | |
| 143 // Otherwise |available| is saved for later use. | |
| 144 // |available|: New screen availability for the presentation URL. | |
| 145 void OnScreenAvailabilityChanged(bool available) override; | |
| 146 | |
| 147 private: | |
| 148 std::string presentation_url_; | |
| 149 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; | |
| 150 scoped_ptr<bool> available_ptr_; | |
| 151 }; | |
| 152 | |
| 153 // Map from presentation URL to its ScreenAvailabilityContext state machine. | |
| 154 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> | |
| 155 availability_contexts_; | |
| 156 | |
| 157 std::string default_presentation_url_; | |
| 158 | |
| 159 // mojo::InterfaceImpl override. | |
| 160 // Note that this is called when the RenderFrameHost is deleted. | |
| 161 void OnConnectionError() override; | |
| 162 | |
| 163 // WebContentsObserver override. | |
| 164 void DidNavigateAnyFrame( | |
| 165 content::RenderFrameHost* render_frame_host, | |
| 166 const content::LoadCommittedDetails& details, | |
| 167 const content::FrameNavigateParams& params) override; | |
| 168 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; | |
| 169 | |
| 170 // PresentationServiceDelegate::Observer | |
| 171 void OnDelegateDestroyed() override; | |
| 172 | |
| 173 // Removes all listeners on this instance and informs the | |
| 174 // PresentationServiceDelegate of such. | |
| 175 void RemoveAllListeners(); | |
| 176 | |
| 177 RenderFrameHost* render_frame_host_; | |
| 178 PresentationServiceDelegate* delegate_; | |
| 179 | 130 |
| 180 // A helper data class used by PresentationServiceImpl to do bookkeeping | 131 // A helper data class used by PresentationServiceImpl to do bookkeeping |
| 181 // of currently registered screen availability listeners. | 132 // of currently registered screen availability listeners. |
| 182 // An instance of this class is a simple state machine that waits for both | 133 // An instance of this class is a simple state machine that waits for both |
| 183 // the available bit and the Mojo callback to become available. | 134 // the available bit and the Mojo callback to become available. |
| 184 // Once this happens, the Mojo callback will be invoked with the available | 135 // Once this happens, the Mojo callback will be invoked with the available |
| 185 // bit, and the state machine will reset. | 136 // bit, and the state machine will reset. |
| 186 // The available bit is obtained from the embedder's media router. | 137 // The available bit is obtained from the embedder's media router. |
| 187 // The callback is obtained from the renderer via PresentationServiceImpl's | 138 // The callback is obtained from the renderer via PresentationServiceImpl's |
| 188 // GetScreenAvailability(). | 139 // GetScreenAvailability(). |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 204 void Reset(); | 155 void Reset(); |
| 205 | 156 |
| 206 // PresentationScreenAvailabilityListener implementation. | 157 // PresentationScreenAvailabilityListener implementation. |
| 207 std::string GetPresentationUrl() const override; | 158 std::string GetPresentationUrl() const override; |
| 208 // If callback exists, it will be invoked with |available| and | 159 // If callback exists, it will be invoked with |available| and |
| 209 // this state machine will reset. | 160 // this state machine will reset. |
| 210 // Otherwise |available| is saved for later use. | 161 // Otherwise |available| is saved for later use. |
| 211 // |available|: New screen availability for the presentation URL. | 162 // |available|: New screen availability for the presentation URL. |
| 212 void OnScreenAvailabilityChanged(bool available) override; | 163 void OnScreenAvailabilityChanged(bool available) override; |
| 213 | 164 |
| 165 // Gets the callback as a raw pointer. | |
| 166 ScreenAvailabilityMojoCallback* callback() const { | |
| 167 return callback_ptr_.get(); | |
| 168 } | |
| 169 | |
| 214 private: | 170 private: |
| 215 std::string presentation_url_; | 171 std::string presentation_url_; |
| 216 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; | 172 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; |
| 217 scoped_ptr<bool> available_ptr_; | 173 scoped_ptr<bool> available_ptr_; |
| 218 }; | 174 }; |
| 219 | 175 |
| 220 // Map from presentation URL to its ScreenAvailabilityContext state machine. | 176 // Map from presentation URL to its ScreenAvailabilityContext state machine. |
| 221 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> | 177 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> |
| 222 availability_contexts_; | 178 availability_contexts_; |
| 223 | 179 |
| 224 std::string default_presentation_url_; | 180 std::string default_presentation_url_; |
| 225 | 181 |
| 182 // This is set when there is a StartSession/JoinSession in progress. | |
| 183 // Additional StartSession/JoinSession calls are ignored while this is | |
|
whywhat
2015/03/10 17:05:04
Is this a temporary limitation? What if the frame
imcheng
2015/03/10 18:31:15
For StartSession, we can only process one at a tim
| |
| 184 // set. | |
| 185 // When embedder returns with a result, this callback will be invoked and then | |
| 186 // reset. | |
| 187 scoped_ptr<NewSessionMojoCallback> start_or_join_session_cb_; | |
| 188 | |
| 189 // NOTE: Weak pointers must be invalidated before all other member variables. | |
| 190 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; | |
| 191 | |
| 226 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 192 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
| 227 }; | 193 }; |
| 228 | 194 |
| 229 } // namespace content | 195 } // namespace content |
| 230 | 196 |
| 231 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 197 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| OLD | NEW |