Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/media/router/offscreen_presentation_manager.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "content/public/browser/render_frame_host.h" | |
| 9 #include "content/public/browser/render_process_host.h" | |
| 10 #include "content/public/browser/web_contents.h" | |
| 11 | |
| 12 namespace media_router { | |
| 13 | |
| 14 // OffscreenPresentationManager implementation. //////////////////////////////// | |
| 15 OffscreenPresentationManager::OffscreenPresentationManager() {} | |
| 16 OffscreenPresentationManager::~OffscreenPresentationManager() {} | |
| 17 | |
| 18 void OffscreenPresentationManager::RegisterOffscreenPresentationController( | |
| 19 const std::string& presentation_id, | |
| 20 content::OffscreenPresentationClient* controller) { | |
| 21 DVLOG(2) | |
| 22 << "OffscreenPresentationManager::RegisterOffscreenPresentationController" | |
| 23 << "[id]: " << presentation_id; | |
| 24 | |
| 25 // Create a new presentation. | |
| 26 if (!base::ContainsKey(offscreen_presentations_, presentation_id)) | |
| 27 offscreen_presentations_.insert(std::make_pair( | |
| 28 presentation_id, | |
| 29 base::WrapUnique(new OffscreenPresentation(presentation_id)))); | |
|
imcheng
2016/09/19 23:01:17
base::MakeUnique<OffscreenPresentation>(presentati
zhaobin
2016/09/23 17:18:18
Done.
| |
| 30 | |
| 31 auto it = offscreen_presentations_.find(presentation_id); | |
|
imcheng
2016/09/19 23:01:17
The entry should already exist in offscreen_presen
zhaobin
2016/09/23 17:18:18
Done.
| |
| 32 if (it != offscreen_presentations_.end()) | |
| 33 it->second->RegisterController(controller); | |
| 34 } | |
| 35 | |
| 36 void OffscreenPresentationManager::UnregisterOffscreenPresentationController( | |
| 37 const std::string& presentation_id, | |
| 38 content::OffscreenPresentationClient* controller) { | |
| 39 DVLOG(2) << "OffscreenPresentationManager::" | |
| 40 "UnRegisterOffscreenPresentationController"; | |
| 41 | |
| 42 auto it = offscreen_presentations_.find(presentation_id); | |
| 43 if (it == offscreen_presentations_.end()) | |
| 44 return; | |
| 45 | |
| 46 // Remove presentation if no controller. | |
| 47 it->second->UnregisterController(controller); | |
| 48 if (it->second->controllers_.size() == 0) | |
| 49 UnregisterOffscreenPresentationReceiver(presentation_id); | |
| 50 } | |
| 51 | |
| 52 void OffscreenPresentationManager::RegisterOffscreenPresentationReceiver( | |
| 53 const std::string& presentation_id, | |
| 54 content::OffscreenPresentationClient* receiver) { | |
| 55 DVLOG(2) | |
| 56 << "OffscreenPresentationManager::RegisterOffscreenPresentationReceiver" | |
| 57 << "[id]: " << presentation_id; | |
| 58 | |
| 59 auto it = offscreen_presentations_.find(presentation_id); | |
| 60 if (it != offscreen_presentations_.end()) | |
| 61 it->second->RegisterReceiver(receiver); | |
| 62 } | |
| 63 | |
| 64 void OffscreenPresentationManager::UnregisterOffscreenPresentationReceiver( | |
| 65 const std::string& presentation_id) { | |
| 66 DVLOG(2) << "OffscreenPresentationManager::" | |
| 67 "UnRegisterOffscreenPresentationReceiver"; | |
| 68 | |
| 69 auto it = offscreen_presentations_.find(presentation_id); | |
| 70 if (it != offscreen_presentations_.end()) | |
| 71 it->second->UnregisterReceiver(); | |
| 72 | |
| 73 offscreen_presentations_.erase(presentation_id); | |
| 74 } | |
| 75 | |
| 76 // OffscreenPresentation implementation. /////////////////////////////////////// | |
| 77 OffscreenPresentation::OffscreenPresentation(const std::string& presentation_id) | |
| 78 : presentation_id_(presentation_id), receiver_(nullptr) {} | |
| 79 | |
| 80 OffscreenPresentation::~OffscreenPresentation() {} | |
| 81 | |
| 82 void OffscreenPresentation::RegisterController( | |
| 83 content::OffscreenPresentationClient* controller) { | |
| 84 // Connect controller PSImpl and receiver PSImpl. | |
| 85 if (receiver_) | |
| 86 AddOffscreenPresentationObserver(controller, receiver_); | |
| 87 controllers_.insert(controller); | |
| 88 } | |
| 89 | |
| 90 void OffscreenPresentation::UnregisterController( | |
| 91 content::OffscreenPresentationClient* controller) { | |
| 92 if (receiver_) | |
| 93 RemoveOffscreenPresentationObserver(controller, receiver_); | |
| 94 controllers_.erase(controller); | |
| 95 } | |
| 96 | |
| 97 void OffscreenPresentation::RegisterReceiver( | |
| 98 content::OffscreenPresentationClient* receiver) { | |
| 99 for (const auto& controller : controllers_) | |
| 100 AddOffscreenPresentationObserver(controller, receiver); | |
| 101 receiver_ = receiver; | |
| 102 } | |
| 103 | |
| 104 void OffscreenPresentation::UnregisterReceiver() { | |
| 105 if (!receiver_) | |
| 106 return; | |
| 107 | |
| 108 for (const auto& iter : controllers_) | |
| 109 RemoveOffscreenPresentationObserver(iter, receiver_); | |
| 110 receiver_ = nullptr; | |
| 111 } | |
| 112 | |
| 113 void OffscreenPresentation::AddOffscreenPresentationObserver( | |
| 114 content::OffscreenPresentationClient* controller, | |
| 115 content::OffscreenPresentationClient* receiver) { | |
| 116 // Connect controller PSImpl and receiver PSImpl. | |
| 117 controller->AddOffscreenPresentationObserver(receiver); | |
| 118 receiver->AddOffscreenPresentationObserver(controller); | |
| 119 | |
| 120 // Build a connection between controller and receiver and | |
| 121 // invoke on OnReceiverConnectionAvailable event. | |
| 122 receiver->OnReceiverConnectionAvailable( | |
| 123 content::PresentationSessionInfo(presentation_url_, presentation_id_)); | |
| 124 } | |
| 125 | |
| 126 void OffscreenPresentation::RemoveOffscreenPresentationObserver( | |
| 127 content::OffscreenPresentationClient* controller, | |
| 128 content::OffscreenPresentationClient* receiver) { | |
| 129 // Connect controller PSImpl and receiver PSImpl. | |
| 130 controller->RemoveOffscreenPresentationObserver(receiver); | |
| 131 receiver->RemoveOffscreenPresentationObserver(controller); | |
| 132 } | |
| 133 | |
| 134 } // namespace media_router | |
| OLD | NEW |