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 | 130 |
| 113 // A helper data class used by PresentationServiceImpl to do bookkeeping | 131 // A helper data class used by PresentationServiceImpl to do bookkeeping |
| 114 // of currently registered screen availability listeners. | 132 // of currently registered screen availability listeners. |
| 115 // 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 |
| 116 // the available bit and the Mojo callback to become available. | 134 // the available bit and the Mojo callback to become available. |
| 117 // 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 |
| 118 // bit, and the state machine will reset. | 136 // bit, and the state machine will reset. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 137 void Reset(); | 155 void Reset(); |
| 138 | 156 |
| 139 // PresentationScreenAvailabilityListener implementation. | 157 // PresentationScreenAvailabilityListener implementation. |
| 140 std::string GetPresentationUrl() const override; | 158 std::string GetPresentationUrl() const override; |
| 141 // If callback exists, it will be invoked with |available| and | 159 // If callback exists, it will be invoked with |available| and |
| 142 // this state machine will reset. | 160 // this state machine will reset. |
| 143 // Otherwise |available| is saved for later use. | 161 // Otherwise |available| is saved for later use. |
| 144 // |available|: New screen availability for the presentation URL. | 162 // |available|: New screen availability for the presentation URL. |
| 145 void OnScreenAvailabilityChanged(bool available) override; | 163 void OnScreenAvailabilityChanged(bool available) override; |
| 146 | 164 |
| 165 // Gets the callback as a raw pointer. | |
| 166 ScreenAvailabilityMojoCallback* callback() const { | |
| 167 return callback_ptr_.get(); | |
| 168 } | |
| 169 | |
| 147 private: | 170 private: |
| 148 std::string presentation_url_; | 171 std::string presentation_url_; |
| 149 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; | 172 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; |
| 150 scoped_ptr<bool> available_ptr_; | 173 scoped_ptr<bool> available_ptr_; |
| 151 }; | 174 }; |
| 152 | 175 |
| 153 // Map from presentation URL to its ScreenAvailabilityContext state machine. | 176 // Map from presentation URL to its ScreenAvailabilityContext state machine. |
| 154 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> | 177 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> |
| 155 availability_contexts_; | 178 availability_contexts_; |
| 156 | 179 |
| 157 std::string default_presentation_url_; | 180 std::string default_presentation_url_; |
| 158 | 181 |
| 182 // This is set when there is a StartSession/JoinSession in progress. | |
| 183 // Additional StartSession/JoinSession calls are ignored while this is | |
| 184 // set. | |
|
mark a. foltz
2015/03/11 01:08:19
Normally I wouldn't expect overlapping calls, but
imcheng
2015/03/12 20:03:41
For JoinSession, we should be able to let them ove
| |
| 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 | |
| 159 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 192 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
| 160 }; | 193 }; |
| 161 | 194 |
| 162 } // namespace content | 195 } // namespace content |
| 163 | 196 |
| 164 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 197 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| OLD | NEW |