Chromium Code Reviews| 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->RegisterOffscreenPresentationReceiver(presentation_id, | |
|
imcheng
2016/11/01 17:20:29
please update comments, or update the method name
zhaobin
2016/11/02 03:55:47
Done.
| |
| 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->UnregisterOffscreenPresentationReceiver(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 PSImpl to presentation with |presentation_id|, | |
| 102 // |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 void OnOffscreenPresentationReceiverTerminated( | |
|
imcheng
2016/11/01 17:20:29
Please add documentation
zhaobin
2016/11/02 03:55:47
Done.
| |
| 115 const std::string& presentation_id); | |
| 116 | |
| 117 private: | |
| 118 // Represents an offscreen presentation registered with | |
| 119 // OffscreenPresentationManager. | |
| 120 // Contains callback to the receiver to inform it of new connections | |
| 121 // established from a controller. | |
| 122 // Contains set of controllers registered to OffscreenPresentationManager | |
| 123 // before corresponding receiver. | |
| 124 class OffscreenPresentation { | |
| 125 public: | |
| 126 OffscreenPresentation(const std::string& presentation_id, | |
| 127 const GURL& presentation_url); | |
| 128 ~OffscreenPresentation(); | |
| 129 | |
| 130 void RegisterController(const RenderFrameHostId& render_frame_id, | |
| 131 content::PresentationConnectionPtr controller); | |
| 132 | |
| 133 void UnregisterController(const RenderFrameHostId& render_frame_id); | |
| 134 | |
| 135 void RegisterReceiver( | |
| 136 const content::ReceiverConnectionAvailableCallback& receiver_callback); | |
| 137 | |
| 138 private: | |
| 139 friend class OffscreenPresentationManagerTest; | |
| 140 friend class OffscreenPresentationManager; | |
| 141 | |
| 142 // presentation_id for current presentation. | |
| 143 const std::string presentation_id_; | |
| 144 | |
| 145 // URL for current presentation. | |
| 146 const GURL presentation_url_; | |
| 147 | |
| 148 // Bind to receiver frame's | |
| 149 // PresentationServiceImpl::OnReceiverConnectionAvailable. | |
| 150 content::ReceiverConnectionAvailableCallback receiver_callback_; | |
| 151 | |
| 152 // proxy to controller PSImpl | |
| 153 // It only contains controllers registered before receiver_callback_ | |
| 154 // is set. This map will be cleared in RegisterReceiver(). | |
| 155 std::unordered_map<RenderFrameHostId, | |
| 156 content::PresentationConnectionPtr, | |
| 157 RenderFrameHostIdHasher> | |
| 158 pending_controllers_; | |
| 159 | |
| 160 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentation); | |
| 161 }; | |
| 162 | |
| 163 private: | |
| 164 friend class OffscreenPresentationManagerFactory; | |
| 165 friend class OffscreenPresentationManagerTest; | |
| 166 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest, | |
| 167 ConnectToOffscreenPresentation); | |
| 168 | |
| 169 // Used by OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext. | |
| 170 OffscreenPresentationManager(); | |
| 171 | |
| 172 using OffscreenPresentationMap = | |
| 173 std::map<std::string, std::unique_ptr<OffscreenPresentation>>; | |
| 174 // Creates an offscreen presentation with | |
| 175 // |presentation_id| and |presentation_url|. | |
| 176 OffscreenPresentationMap::iterator GetOrCreateOffscreenPresentation( | |
| 177 const std::string& presentation_id, | |
| 178 const GURL& presentation_url); | |
| 179 | |
| 180 // Maps from presentation ID to OffscreenPresentation. | |
| 181 OffscreenPresentationMap offscreen_presentations_; | |
| 182 | |
| 183 base::ThreadChecker thread_checker_; | |
| 184 | |
| 185 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationManager); | |
| 186 }; | |
| 187 | |
| 188 } // namespace media_router | |
| 189 | |
| 190 #endif // CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_ | |
| OLD | NEW |