Chromium Code Reviews| Index: chrome/browser/media/router/offscreen_presentation_manager.cc |
| diff --git a/chrome/browser/media/router/offscreen_presentation_manager.cc b/chrome/browser/media/router/offscreen_presentation_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5ccf31f143a39d0b2e83e7de33260d2ee26da2da |
| --- /dev/null |
| +++ b/chrome/browser/media/router/offscreen_presentation_manager.cc |
| @@ -0,0 +1,107 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/media/router/offscreen_presentation_manager.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "content/public/browser/render_frame_host.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace media_router { |
| + |
| +// OffscreenPresentationManager implementation. //////////////////////////////// |
| +OffscreenPresentationManager::OffscreenPresentationManager() {} |
| + |
| +OffscreenPresentationManager::~OffscreenPresentationManager() {} |
| + |
| +void OffscreenPresentationManager::RegisterOffscreenPresentationController( |
| + const std::string& presentation_id, |
| + int render_frame_id, |
| + content::OffscreenPresentationClient* controller) { |
| + DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id |
| + << ", [render_frame_id]: " << render_frame_id; |
| + |
| + auto it = offscreen_presentations_.find(presentation_id); |
| + // Create a new presentation. |
| + if (it == offscreen_presentations_.end()) { |
| + it = offscreen_presentations_ |
| + .insert(std::make_pair( |
| + presentation_id, |
| + base::MakeUnique<OffscreenPresentation>(presentation_id))) |
| + .first; |
| + } |
| + it->second->RegisterController(render_frame_id, controller); |
| +} |
| + |
| +void OffscreenPresentationManager::UnregisterOffscreenPresentationController( |
| + const std::string& presentation_id, |
| + int render_frame_id) { |
| + DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id |
| + << ", [render_frame_id]: " << render_frame_id; |
| + |
| + auto it = offscreen_presentations_.find(presentation_id); |
| + if (it == offscreen_presentations_.end()) |
| + return; |
| + |
| + // Remove presentation if no controller and receiver. |
| + it->second->UnregisterController(render_frame_id); |
| + if (it->second->controllers_.size() == 0 && |
| + it->second->receiver_callback_.is_null()) |
| + offscreen_presentations_.erase(presentation_id); |
| +} |
| + |
| +void OffscreenPresentationManager::RegisterOffscreenPresentationReceiver( |
| + const std::string& presentation_id, |
| + const content::ReceiverConnectionAvailableCallback& receiver_callback) { |
| + DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id; |
| + |
| + auto it = offscreen_presentations_.find(presentation_id); |
| + if (it != offscreen_presentations_.end()) |
|
imcheng
2016/09/28 07:28:35
Does the first RegisterOffscreenPresentationContro
zhaobin
2016/09/29 17:20:43
They are async so they can occur in any order. Gen
|
| + it->second->RegisterReceiver(receiver_callback); |
| +} |
| + |
| +void OffscreenPresentationManager::UnregisterOffscreenPresentationReceiver( |
| + const std::string& presentation_id) { |
| + DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id; |
| + offscreen_presentations_.erase(presentation_id); |
|
imcheng
2016/09/28 07:28:35
question: how do we inform controllers that the re
zhaobin
2016/09/29 17:20:42
Receiver PSImpl should have controller's PSImpl (o
|
| +} |
| + |
| +// OffscreenPresentation implementation. /////////////////////////////////////// |
| +OffscreenPresentation::OffscreenPresentation(const std::string& presentation_id) |
| + : presentation_id_(presentation_id), receiver_callback_(nullptr) {} |
| + |
| +OffscreenPresentation::~OffscreenPresentation() {} |
| + |
| +void OffscreenPresentation::RegisterController( |
| + int render_frame_id, |
| + content::OffscreenPresentationClient* controller) { |
| + // Connect controller PSImpl and receiver PSImpl. |
| + if (receiver_callback_) { |
| + receiver_callback_.Run( |
| + content::PresentationSessionInfo(presentation_url_, presentation_id_), |
|
imcheng
2016/09/28 07:28:35
Looks like we also need to pass in presentation_ur
zhaobin
2016/09/29 17:20:43
Done.
|
| + controller); |
| + } else { |
| + controllers_.insert(std::make_pair(render_frame_id, controller)); |
| + } |
| +} |
| + |
| +void OffscreenPresentation::UnregisterController(int render_frame_id) { |
| + controllers_.erase(render_frame_id); |
| +} |
| + |
| +void OffscreenPresentation::RegisterReceiver( |
| + const content::ReceiverConnectionAvailableCallback& receiver_callback) { |
| + for (const auto& controller : controllers_) { |
| + receiver_callback.Run( |
| + content::PresentationSessionInfo(presentation_url_, presentation_id_), |
| + controller.second); |
| + } |
| + receiver_callback_ = receiver_callback; |
| + controllers_.clear(); |
| +} |
| + |
| +} // namespace media_router |