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 | 61 |
| 58 friend class PresentationServiceImplTest; | 62 friend class PresentationServiceImplTest; |
| 59 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, RemoveAllListeners); | 63 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, Reset); |
| 60 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 64 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 61 DidNavigateThisFrame); | 65 DidNavigateThisFrame); |
| 62 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 66 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 63 DidNavigateNotThisFrame); | 67 DidNavigateNotThisFrame); |
| 64 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 68 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 65 ThisRenderFrameDeleted); | 69 ThisRenderFrameDeleted); |
| 66 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | 70 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, |
| 67 NotThisRenderFrameDeleted); | 71 NotThisRenderFrameDeleted); |
| 72 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 73 SetDefaultPresentationUrl); | |
| 74 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 75 SetSameDefaultPresentationUrl); | |
| 76 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 77 ClearDefaultPresentationUrl); | |
| 68 | 78 |
| 69 // |render_frame_host|: The RFH this instance is associated with. | 79 // |render_frame_host|: The RFH this instance is associated with. |
| 70 // |web_contents|: The WebContents to observe. | 80 // |web_contents|: The WebContents to observe. |
| 71 // |delegate|: Where Presentation API requests are delegated to. Not owned | 81 // |delegate|: Where Presentation API requests are delegated to. Not owned |
| 72 // by this class. | 82 // by this class. |
| 73 PresentationServiceImpl( | 83 PresentationServiceImpl( |
| 74 RenderFrameHost* render_frame_host, | 84 RenderFrameHost* render_frame_host, |
| 75 WebContents* web_contents, | 85 WebContents* web_contents, |
| 76 PresentationServiceDelegate* delegate); | 86 PresentationServiceDelegate* delegate); |
| 77 | 87 |
| 78 // PresentationService implementation. | 88 // PresentationService implementation. |
| 79 void GetScreenAvailability( | 89 void GetScreenAvailability( |
| 80 const mojo::String& presentation_url, | 90 const mojo::String& presentation_url, |
| 81 const ScreenAvailabilityMojoCallback& callback) override; | 91 const ScreenAvailabilityMojoCallback& callback) override; |
| 82 void OnScreenAvailabilityListenerRemoved() override; | 92 void OnScreenAvailabilityListenerRemoved( |
| 93 const mojo::String& presentation_url) override; | |
| 83 void StartSession( | 94 void StartSession( |
| 84 const mojo::String& presentation_url, | 95 const mojo::String& presentation_url, |
| 85 const mojo::String& presentation_id, | 96 const mojo::String& presentation_id, |
| 86 const NewSessionMojoCallback& callback) override; | 97 const NewSessionMojoCallback& callback) override; |
| 87 void JoinSession( | 98 void JoinSession( |
| 88 const mojo::String& presentation_url, | 99 const mojo::String& presentation_url, |
| 89 const mojo::String& presentation_id, | 100 const mojo::String& presentation_id, |
| 90 const NewSessionMojoCallback& callback) override; | 101 const NewSessionMojoCallback& callback) override; |
| 102 void SetDefaultPresentationUrl( | |
| 103 const mojo::String& default_presentation_url, | |
| 104 const mojo::String& default_presentation_id) override; | |
| 91 | 105 |
| 92 // mojo::InterfaceImpl override. | 106 // mojo::InterfaceImpl override. |
| 93 // Note that this is called when the RenderFrameHost is deleted. | 107 // Note that this is called when the RenderFrameHost is deleted. |
| 94 void OnConnectionError() override; | 108 void OnConnectionError() override; |
| 95 | 109 |
| 96 // WebContentsObserver override. | 110 // WebContentsObserver override. |
| 97 void DidNavigateAnyFrame( | 111 void DidNavigateAnyFrame( |
| 98 content::RenderFrameHost* render_frame_host, | 112 content::RenderFrameHost* render_frame_host, |
| 99 const content::LoadCommittedDetails& details, | 113 const content::LoadCommittedDetails& details, |
| 100 const content::FrameNavigateParams& params) override; | 114 const content::FrameNavigateParams& params) override; |
| 101 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; | 115 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; |
| 102 | 116 |
| 103 // PresentationServiceDelegate::Observer | 117 // PresentationServiceDelegate::Observer |
| 104 void OnDelegateDestroyed() override; | 118 void OnDelegateDestroyed() override; |
| 105 | 119 |
| 106 // Removes all listeners on this instance and informs the | 120 // Sets |default_presentation_url_| to |presentation_url| and informs the |
| 107 // PresentationServiceDelegate of such. | 121 // delegate of new default presentation URL and ID. |
| 108 void RemoveAllListeners(); | 122 void DoSetDefaultPresentationUrl( |
| 123 const std::string& presentation_url, | |
| 124 const std::string& presentation_id); | |
| 125 | |
| 126 // Removes all listeners and resets default presentation URL on this instance | |
| 127 // and informs the PresentationServiceDelegate of such. | |
| 128 void Reset(); | |
| 129 | |
| 130 // Callbacks from the embedder as a result of StartSession/JoinSession. | |
| 131 void OnStartOrJoinSessionSucceeded( | |
| 132 bool is_start_session, | |
| 133 const NewSessionMojoCallback& callback, | |
| 134 const PresentationSessionInfo& session_info); | |
| 135 void OnStartOrJoinSessionError( | |
| 136 bool is_start_session, | |
| 137 const NewSessionMojoCallback& callback, | |
| 138 const PresentationError& error); | |
| 139 | |
| 140 // Requests delegate to start a session. | |
| 141 void DoStartSession( | |
| 142 const std::string& presentation_url, | |
| 143 const std::string& presentation_id, | |
| 144 const NewSessionMojoCallback& callback); | |
| 145 | |
| 146 // Checks if there are any queued StartSession requests and if so, executes | |
| 147 // the first one in the queue and remove it from the queue. | |
|
whywhat
2015/03/13 17:44:35
nit: s/remove/removes
imcheng
2015/03/13 23:36:46
Done.
| |
| 148 void HandleQueuedStartSessionRequests(); | |
| 109 | 149 |
| 110 RenderFrameHost* render_frame_host_; | 150 RenderFrameHost* render_frame_host_; |
| 111 PresentationServiceDelegate* delegate_; | 151 PresentationServiceDelegate* delegate_; |
| 112 | 152 |
| 113 // A helper data class used by PresentationServiceImpl to do bookkeeping | 153 // A helper data class used by PresentationServiceImpl to do bookkeeping |
| 114 // of currently registered screen availability listeners. | 154 // of currently registered screen availability listeners. |
| 115 // An instance of this class is a simple state machine that waits for both | 155 // 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. | 156 // the available bit and the Mojo callback to become available. |
| 117 // Once this happens, the Mojo callback will be invoked with the available | 157 // Once this happens, the Mojo callback will be invoked with the available |
| 118 // bit, and the state machine will reset. | 158 // bit, and the state machine will reset. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 137 void Reset(); | 177 void Reset(); |
| 138 | 178 |
| 139 // PresentationScreenAvailabilityListener implementation. | 179 // PresentationScreenAvailabilityListener implementation. |
| 140 std::string GetPresentationUrl() const override; | 180 std::string GetPresentationUrl() const override; |
| 141 // If callback exists, it will be invoked with |available| and | 181 // If callback exists, it will be invoked with |available| and |
| 142 // this state machine will reset. | 182 // this state machine will reset. |
| 143 // Otherwise |available| is saved for later use. | 183 // Otherwise |available| is saved for later use. |
| 144 // |available|: New screen availability for the presentation URL. | 184 // |available|: New screen availability for the presentation URL. |
| 145 void OnScreenAvailabilityChanged(bool available) override; | 185 void OnScreenAvailabilityChanged(bool available) override; |
| 146 | 186 |
| 187 // Gets the callback as a raw pointer. | |
| 188 ScreenAvailabilityMojoCallback* callback() const { | |
|
whywhat
2015/03/13 17:44:35
nit: I think since it's not a plain and simple "re
imcheng
2015/03/13 23:36:46
Done.
| |
| 189 return callback_ptr_.get(); | |
| 190 } | |
| 191 | |
| 147 private: | 192 private: |
| 148 std::string presentation_url_; | 193 std::string presentation_url_; |
| 149 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; | 194 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; |
| 150 scoped_ptr<bool> available_ptr_; | 195 scoped_ptr<bool> available_ptr_; |
| 151 }; | 196 }; |
| 152 | 197 |
| 198 // Context for a StartSession request. | |
| 199 struct StartSessionRequest { | |
| 200 StartSessionRequest(const std::string& presentation_url, | |
| 201 const std::string& presentation_id, | |
| 202 const NewSessionMojoCallback& callback); | |
| 203 ~StartSessionRequest(); | |
| 204 | |
| 205 const std::string presentation_url; | |
| 206 const std::string presentation_id; | |
| 207 const NewSessionMojoCallback callback; | |
| 208 }; | |
| 209 | |
| 153 // Map from presentation URL to its ScreenAvailabilityContext state machine. | 210 // Map from presentation URL to its ScreenAvailabilityContext state machine. |
| 154 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> | 211 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> |
| 155 availability_contexts_; | 212 availability_contexts_; |
| 156 | 213 |
| 157 std::string default_presentation_url_; | 214 std::string default_presentation_url_; |
| 215 std::string default_presentation_id_; | |
| 216 | |
| 217 // This is set when there is a StartSession in progress. | |
| 218 // Additional StartSession requests while one is in progress are queued for | |
| 219 // later execution. | |
| 220 bool start_session_in_progress_; | |
| 221 std::deque<linked_ptr<StartSessionRequest>> queued_start_session_requests_; | |
| 222 | |
| 223 // NOTE: Weak pointers must be invalidated before all other member variables. | |
| 224 base::WeakPtrFactory<PresentationServiceImpl> weak_factory_; | |
| 158 | 225 |
| 159 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 226 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
| 160 }; | 227 }; |
| 161 | 228 |
| 162 } // namespace content | 229 } // namespace content |
| 163 | 230 |
| 164 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 231 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| OLD | NEW |