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..b657c14657b046b6d13751971e2ccd1c9b5fcadb |
--- /dev/null |
+++ b/chrome/browser/media/router/offscreen_presentation_manager.cc |
@@ -0,0 +1,121 @@ |
+// 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. //////////////////////////////// |
mark a. foltz
2016/10/06 03:13:54
I don't think the trailing slashes are needed.
zhaobin
2016/10/07 01:07:03
Done.
|
+OffscreenPresentationManager::OffscreenPresentationManager() {} |
+ |
+OffscreenPresentationManager::~OffscreenPresentationManager() {} |
+ |
+void OffscreenPresentationManager::RegisterOffscreenPresentationController( |
+ const std::string& presentation_id, |
+ const std::string& presentation_url, |
+ const RenderFrameHostId& render_frame_host_id, |
+ content::PresentationConnectionPtr controller) { |
+ DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id |
+ << ", [render_frame_host_id]: " << render_frame_host_id.second; |
+ |
+ auto it = offscreen_presentations_.find(presentation_id); |
+ // Create a new presentation. |
mark a. foltz
2016/10/06 03:13:54
It's not clear to me why registering an offscreen
zhaobin
2016/10/07 01:07:03
RegisterController() and RegisterReceiver() are as
|
+ if (it == offscreen_presentations_.end()) { |
+ it = offscreen_presentations_ |
+ .insert(std::make_pair(presentation_id, |
+ base::MakeUnique<OffscreenPresentation>( |
+ presentation_id, presentation_url))) |
+ .first; |
+ } |
mark a. foltz
2016/10/06 03:13:54
This logic is duplicated here and in RegisterOffsc
zhaobin
2016/10/07 01:07:03
Extract a RegisterOffscreenPresentation(id, url) f
|
+ it->second->RegisterController(render_frame_host_id, std::move(controller)); |
+} |
+ |
+void OffscreenPresentationManager::UnregisterOffscreenPresentationController( |
+ const std::string& presentation_id, |
+ const RenderFrameHostId& render_frame_host_id) { |
+ DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id |
+ << ", [render_frame_host_id]: " << render_frame_host_id.second; |
+ |
+ 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_host_id); |
+ if (it->second->pending_controllers_.size() == 0 && |
mark a. foltz
2016/10/06 03:13:54
Even if the last controller connection is closed,
zhaobin
2016/10/07 01:07:03
We only remove presentation if no there no control
|
+ it->second->receiver_callback_.is_null()) |
+ offscreen_presentations_.erase(presentation_id); |
+} |
+ |
+void OffscreenPresentationManager::RegisterOffscreenPresentationReceiver( |
+ const std::string& presentation_id, |
+ const std::string& presentation_url, |
+ const content::ReceiverConnectionAvailableCallback& receiver_callback) { |
+ DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id; |
+ |
+ auto it = offscreen_presentations_.find(presentation_id); |
+ if (it == offscreen_presentations_.end()) { |
+ it = offscreen_presentations_ |
+ .insert(std::make_pair(presentation_id, |
+ base::MakeUnique<OffscreenPresentation>( |
+ presentation_id, presentation_url))) |
+ .first; |
+ } |
+ 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); |
+} |
+ |
+// OffscreenPresentation implementation. /////////////////////////////////////// |
mark a. foltz
2016/10/06 03:13:54
Omit trailing slashes
zhaobin
2016/10/07 01:07:03
Done.
|
+OffscreenPresentationManager::OffscreenPresentation::OffscreenPresentation( |
+ const std::string& presentation_id, |
+ const std::string& presentation_url) |
+ : presentation_id_(presentation_id), |
+ presentation_url_(presentation_url), |
+ receiver_callback_(nullptr) {} |
+ |
+OffscreenPresentationManager::OffscreenPresentation::~OffscreenPresentation() {} |
+ |
+void OffscreenPresentationManager::OffscreenPresentation::RegisterController( |
+ const RenderFrameHostId& render_frame_host_id, |
+ content::PresentationConnectionPtr controller) { |
+ // Connect controller PSImpl and receiver PSImpl. |
mark a. foltz
2016/10/06 03:13:54
It may be difficult for those unfamiliar with our
zhaobin
2016/10/07 01:07:03
Outdated comments. Updated.
|
+ if (receiver_callback_) { |
+ receiver_callback_.Run( |
+ content::PresentationSessionInfo(presentation_url_, presentation_id_), |
+ std::move(controller)); |
+ } else { |
+ pending_controllers_.insert( |
+ std::make_pair(render_frame_host_id, std::move(controller))); |
+ } |
+} |
+ |
+void OffscreenPresentationManager::OffscreenPresentation::UnregisterController( |
+ const RenderFrameHostId& render_frame_host_id) { |
+ pending_controllers_.erase(render_frame_host_id); |
+} |
+ |
+void OffscreenPresentationManager::OffscreenPresentation::RegisterReceiver( |
+ const content::ReceiverConnectionAvailableCallback& receiver_callback) { |
+ for (auto& controller : pending_controllers_) { |
mark a. foltz
2016/10/06 03:13:54
This must be called only once per receiver, correc
zhaobin
2016/10/07 01:07:03
Done.
|
+ receiver_callback.Run( |
+ content::PresentationSessionInfo(presentation_url_, presentation_id_), |
+ std::move(controller.second)); |
+ } |
+ receiver_callback_ = receiver_callback; |
+ pending_controllers_.clear(); |
+} |
+ |
+} // namespace media_router |