OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_ |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 #include <string> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/threading/thread_checker.h" |
| 14 #include "chrome/browser/media/router/render_frame_host_id.h" |
| 15 #include "components/keyed_service/core/keyed_service.h" |
| 16 #include "content/public/browser/presentation_service_delegate.h" |
| 17 |
| 18 class GURL; |
| 19 |
| 20 namespace media_router { |
| 21 |
| 22 // Instances of this class manages all offscreen presentations started in the |
| 23 // associated Profile and facilitates communication between the controllers and |
| 24 // the receiver of an offscreen presentation. |
| 25 // |
| 26 // Example usage: |
| 27 // |
| 28 // (PresentationConnectionPtr - blink::mojom::PresentationConnectionPtr, mojo |
| 29 // handler for blink::PresentationConnection object in render process) |
| 30 // |
| 31 // Receiver is created to host the offscreen presentation and registers itself |
| 32 // so that controller frames can connect to it: |
| 33 // |
| 34 // OffscreenPresentationManager* manager = |
| 35 // OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext( |
| 36 // web_contents_->GetBrowserContext()); |
| 37 // manager->OnOffscreenPresentationReceiverCreated(presentation_id, |
| 38 // base::Bind(&PresentationServiceImpl::OnReceiverConnectionAvailable)); |
| 39 // ... |
| 40 // Invoked on receiver's PresentationServiceImpl when controller connection is |
| 41 // established. |
| 42 // |
| 43 // void PresentationServiceImpl::OnReceiverConnectionAvailable( |
| 44 // const content::PresentationSessionInfo& session, |
| 45 // PresentationConnectionPtr&& controller) { |
| 46 // [Create a PresentationConnection on receiver page. |
| 47 // Connect receiver PresentationConnectionPtr with controller |
| 48 // PresentationConnectionPtr] |
| 49 // client_->OnReceiverConnectionAvailable(); |
| 50 // } |
| 51 // |
| 52 // Controller frame establishes connection with the receiver side, resulting |
| 53 // in a connection with the two endpoints being the controller |
| 54 // PresentationConnectionPtr and receiver PresentationConnectionPtr. |
| 55 // Note calling this will trigger receiver frame's |
| 56 // PresentationServiceImpl::OnReceiverConnectionAvailable. |
| 57 // |
| 58 // manager->RegisterOffscreenPresentationController( |
| 59 // presentation_id, controller_frame_id, controller_ptr); |
| 60 // |
| 61 // Send message from controller/receiver to receiver/controller: |
| 62 // |
| 63 // In controller's (or receiver's) PresentationConnctionPtr::SendString() { |
| 64 // ... |
| 65 // target_connection_->OnSessionMessageReceived(); |
| 66 // } |
| 67 // |
| 68 // A controller or receiver leaves the offscreen presentation (e.g., |
| 69 // due to navigation) by unregistering themselves from |
| 70 // OffscreenPresentationConnection object. |
| 71 // |
| 72 // When the receiver is no longer associated with an offscreen presentation, it |
| 73 // shall remove itself from associated controllers and then unregister itself |
| 74 // with OffscreenPresentationManager. Unregistration will prevent additional |
| 75 // controllers from establishing a connection with the receiver: |
| 76 // |
| 77 // In receiver's PSImpl::Reset() { |
| 78 // for (controller : offscreen_presentation_observers_) |
| 79 // controller->RemoveOffscreenPresentationClient(receiver); |
| 80 // } |
| 81 // manager->OnOffscreenPresentationReceiverTerminated(presentation_id); |
| 82 // |
| 83 // This class is not thread safe. All functions must be invoked on the UI |
| 84 // thread. All callbacks passed into this class will also be invoked on UI |
| 85 // thread. |
| 86 class OffscreenPresentationManager : public KeyedService { |
| 87 public: |
| 88 ~OffscreenPresentationManager() override; |
| 89 |
| 90 // Registers controller PresentationConnectionPtr to presentation |
| 91 // with |presentation_id|, |render_frame_id|. |
| 92 // Creates a new presentation if no presentation with |presentation_id| |
| 93 // exists. |
| 94 // |controller| Not owned by this class. |
| 95 void RegisterOffscreenPresentationController( |
| 96 const std::string& presentation_id, |
| 97 const GURL& presentation_url, |
| 98 const RenderFrameHostId& render_frame_id, |
| 99 content::PresentationConnectionPtr controller); |
| 100 |
| 101 // Unregisters controller PresentationConnectionPtr to presentation with |
| 102 // |presentation_id|, |render_frame_id|. |
| 103 void UnregisterOffscreenPresentationController( |
| 104 const std::string& presentation_id, |
| 105 const RenderFrameHostId& render_frame_id); |
| 106 |
| 107 // Registers receiverConnectionAvailablecallback to presentation |
| 108 // with |presentation_id|. |
| 109 void OnOffscreenPresentationReceiverCreated( |
| 110 const std::string& presentation_id, |
| 111 const GURL& presentation_url, |
| 112 const content::ReceiverConnectionAvailableCallback& receiver_callback); |
| 113 |
| 114 // Unregister receiverConnectionAvailablecallback from |
| 115 // OffscreenPresentationManager. |
| 116 void OnOffscreenPresentationReceiverTerminated( |
| 117 const std::string& presentation_id); |
| 118 |
| 119 private: |
| 120 // Represents an offscreen presentation registered with |
| 121 // OffscreenPresentationManager. |
| 122 // Contains callback to the receiver to inform it of new connections |
| 123 // established from a controller. |
| 124 // Contains set of controllers registered to OffscreenPresentationManager |
| 125 // before corresponding receiver. |
| 126 class OffscreenPresentation { |
| 127 public: |
| 128 OffscreenPresentation(const std::string& presentation_id, |
| 129 const GURL& presentation_url); |
| 130 ~OffscreenPresentation(); |
| 131 |
| 132 void RegisterController(const RenderFrameHostId& render_frame_id, |
| 133 content::PresentationConnectionPtr controller); |
| 134 |
| 135 void UnregisterController(const RenderFrameHostId& render_frame_id); |
| 136 |
| 137 void RegisterReceiver( |
| 138 const content::ReceiverConnectionAvailableCallback& receiver_callback); |
| 139 |
| 140 private: |
| 141 friend class OffscreenPresentationManagerTest; |
| 142 friend class OffscreenPresentationManager; |
| 143 |
| 144 // Returns false if receiver_callback_ is null and no pending controllers. |
| 145 bool IsValid(); |
| 146 |
| 147 // presentation_id for current presentation. |
| 148 const std::string presentation_id_; |
| 149 |
| 150 // URL for current presentation. |
| 151 const GURL presentation_url_; |
| 152 |
| 153 // Bind to receiver frame's |
| 154 // PresentationServiceImpl::OnReceiverConnectionAvailable. |
| 155 content::ReceiverConnectionAvailableCallback receiver_callback_; |
| 156 |
| 157 // proxy to controller PSImpl |
| 158 // It only contains controllers registered before receiver_callback_ |
| 159 // is set. This map will be cleared in RegisterReceiver(). |
| 160 std::unordered_map<RenderFrameHostId, |
| 161 content::PresentationConnectionPtr, |
| 162 RenderFrameHostIdHasher> |
| 163 pending_controllers_; |
| 164 |
| 165 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentation); |
| 166 }; |
| 167 |
| 168 private: |
| 169 friend class OffscreenPresentationManagerFactory; |
| 170 friend class OffscreenPresentationManagerTest; |
| 171 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest, |
| 172 ConnectToOffscreenPresentation); |
| 173 |
| 174 // Used by OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext. |
| 175 OffscreenPresentationManager(); |
| 176 |
| 177 using OffscreenPresentationMap = |
| 178 std::map<std::string, std::unique_ptr<OffscreenPresentation>>; |
| 179 // Creates an offscreen presentation with |
| 180 // |presentation_id| and |presentation_url|. |
| 181 OffscreenPresentation* GetOrCreateOffscreenPresentation( |
| 182 const std::string& presentation_id, |
| 183 const GURL& presentation_url); |
| 184 |
| 185 // Maps from presentation ID to OffscreenPresentation. |
| 186 OffscreenPresentationMap offscreen_presentations_; |
| 187 |
| 188 base::ThreadChecker thread_checker_; |
| 189 |
| 190 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationManager); |
| 191 }; |
| 192 |
| 193 } // namespace media_router |
| 194 |
| 195 #endif // CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_ |
OLD | NEW |