| Index: chrome/browser/media/router/receiver_presentation_service_delegate_impl.cc
|
| diff --git a/chrome/browser/media/router/receiver_presentation_service_delegate_impl.cc b/chrome/browser/media/router/receiver_presentation_service_delegate_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..e4207821d8718e8238925ad48ea334752c64bf56
|
| --- /dev/null
|
| +++ b/chrome/browser/media/router/receiver_presentation_service_delegate_impl.cc
|
| @@ -0,0 +1,293 @@
|
| +// 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/receiver_presentation_service_delegate_impl.h"
|
| +
|
| +#include "chrome/browser/media/router/offscreen_presentation_manager.h"
|
| +#include "chrome/browser/media/router/offscreen_presentation_manager_factory.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| +#include "content/public/browser/render_frame_host.h"
|
| +#include "content/public/browser/render_process_host.h"
|
| +
|
| +DEFINE_WEB_CONTENTS_USER_DATA_KEY(
|
| + media_router::ReceiverPresentationServiceDelegateImpl);
|
| +
|
| +using content::PresentationServiceDelegate;
|
| +using content::RenderFrameHost;
|
| +
|
| +namespace media_router {
|
| +
|
| +using OffscreenPresentationConnection =
|
| + OffscreenPresentationManager::OffscreenPresentationConnection;
|
| +
|
| +// static
|
| +void ReceiverPresentationServiceDelegateImpl::CreateForWebContents(
|
| + content::WebContents* web_contents,
|
| + const std::string& presentation_id) {
|
| + DCHECK(web_contents);
|
| +
|
| + if (FromWebContents(web_contents))
|
| + return;
|
| +
|
| + web_contents->SetUserData(UserDataKey(),
|
| + new ReceiverPresentationServiceDelegateImpl(
|
| + web_contents, presentation_id));
|
| +}
|
| +
|
| +ReceiverPresentationServiceDelegateImpl::
|
| + ~ReceiverPresentationServiceDelegateImpl() {
|
| + for (auto& observer_pair : observers_)
|
| + observer_pair.second->OnDelegateDestroyed();
|
| +
|
| + receiver_connections_.clear();
|
| + offscreen_presentation_manager_->UnregisterOffscreenPresentationReceiver(
|
| + presentation_id_);
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::AddObserver(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + content::PresentationServiceDelegate::Observer* observer) {
|
| + DCHECK(observer);
|
| +
|
| + RenderFrameHostId rfh_id(render_process_id, render_frame_id);
|
| + DCHECK(!ContainsKey(observers_, rfh_id));
|
| + observers_[rfh_id] = observer;
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::RemoveObserver(
|
| + int render_process_id,
|
| + int render_frame_id) {
|
| + observers_.erase(RenderFrameHostId(render_process_id, render_frame_id));
|
| +}
|
| +
|
| +bool ReceiverPresentationServiceDelegateImpl::AddScreenAvailabilityListener(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + content::PresentationScreenAvailabilityListener* listener) {
|
| + NOTIMPLEMENTED();
|
| + return false;
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::RemoveScreenAvailabilityListener(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + content::PresentationScreenAvailabilityListener* listener) {
|
| + NOTIMPLEMENTED();
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::Reset(int render_process_id,
|
| + int render_frame_id) {
|
| + DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
|
| +
|
| + RenderFrameHostId rfh_id(render_process_id, render_frame_id);
|
| + if (rfh_id != current_frame_id_) {
|
| + DVLOG(2) << __FUNCTION__ << ": not main frame";
|
| + return;
|
| + }
|
| +
|
| + current_frame_id_ = RenderFrameHostId();
|
| + receiver_connections_.clear();
|
| + receiver_available_callback_.Reset();
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::SetDefaultPresentationUrl(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const std::string& default_presentation_url,
|
| + const content::PresentationSessionStartedCallback& callback) {
|
| + NOTIMPLEMENTED();
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::StartSession(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const std::string& presentation_url,
|
| + const content::PresentationSessionStartedCallback& success_cb,
|
| + const content::PresentationSessionErrorCallback& error_cb) {
|
| + NOTIMPLEMENTED();
|
| + error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
|
| + "Not implemented"));
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::JoinSession(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const std::string& presentation_url,
|
| + const std::string& presentation_id,
|
| + const content::PresentationSessionStartedCallback& success_cb,
|
| + const content::PresentationSessionErrorCallback& error_cb) {
|
| + NOTIMPLEMENTED();
|
| + error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
|
| + "Not implemented"));
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::CloseConnection(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const std::string& presentation_id) {
|
| + NOTIMPLEMENTED();
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::Terminate(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const std::string& presentation_id) {
|
| + NOTIMPLEMENTED();
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::ListenForSessionMessages(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const content::PresentationSessionInfo& session_info,
|
| + const content::PresentationSessionMessageCallback& message_cb) {
|
| + DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
|
| + RenderFrameHostId rfh_id(render_process_id, render_frame_id);
|
| + if (rfh_id != current_frame_id_ && !BindToFrame(rfh_id)) {
|
| + DVLOG(2) << __FUNCTION__ << ": not main frame";
|
| + return;
|
| + }
|
| +
|
| + OffscreenPresentationConnection* connection = FindConnection(session_info);
|
| + if (!connection) {
|
| + LOG(ERROR) << __FUNCTION__ << ": presentation does not exist.";
|
| + return;
|
| + }
|
| +
|
| + connection->ListenForMessages(message_cb);
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::SendMessage(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const content::PresentationSessionInfo& session_info,
|
| + std::unique_ptr<content::PresentationSessionMessage> message,
|
| + const content::SendMessageCallback& send_message_cb) {
|
| + DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
|
| + RenderFrameHostId rfh_id(render_process_id, render_frame_id);
|
| + if (rfh_id != current_frame_id_ && !BindToFrame(rfh_id)) {
|
| + DVLOG(2) << __FUNCTION__ << ": not main frame";
|
| + send_message_cb.Run(false);
|
| + return;
|
| + }
|
| +
|
| + OffscreenPresentationConnection* connection = FindConnection(session_info);
|
| + if (!connection) {
|
| + LOG(ERROR) << __FUNCTION__ << ": presentation does not exist.";
|
| + send_message_cb.Run(false);
|
| + return;
|
| + }
|
| +
|
| + connection->SendMessage(std::move(message), send_message_cb);
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::ListenForConnectionStateChange(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const content::PresentationSessionInfo& connection,
|
| + const content::PresentationConnectionStateChangedCallback&
|
| + state_changed_cb) {
|
| + DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
|
| + RenderFrameHostId rfh_id(render_process_id, render_frame_id);
|
| + if (rfh_id != current_frame_id_ && !BindToFrame(rfh_id)) {
|
| + DVLOG(2) << __FUNCTION__ << ": not main frame";
|
| + return;
|
| + }
|
| +
|
| + OffscreenPresentationConnection* offscreen_connection =
|
| + FindConnection(connection);
|
| + if (!offscreen_connection) {
|
| + LOG(ERROR) << __FUNCTION__ << ": presentation does not exist.";
|
| + return;
|
| + }
|
| +
|
| + offscreen_connection->ListenForStateChange(state_changed_cb);
|
| +}
|
| +
|
| +std::vector<content::PresentationSessionInfo>
|
| +ReceiverPresentationServiceDelegateImpl::GetReceiverConnections(
|
| + int render_process_id,
|
| + int render_frame_id,
|
| + const content::PresentationSessionStartedCallback& callback) {
|
| + DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
|
| + RenderFrameHostId rfh_id(render_process_id, render_frame_id);
|
| + if (rfh_id != current_frame_id_ && !BindToFrame(rfh_id)) {
|
| + DVLOG(2) << __FUNCTION__ << ": not main frame";
|
| + return std::vector<content::PresentationSessionInfo>();
|
| + }
|
| +
|
| + receiver_available_callback_ = callback;
|
| +
|
| + // TODO(imcheng): This is currently broken for the multiple controllers case.
|
| + // We need additional ID to distinguish between controllers. crbug.com/529911
|
| + std::vector<content::PresentationSessionInfo> connections;
|
| + if (!receiver_connections_.empty()) {
|
| + connections.push_back(
|
| + content::PresentationSessionInfo(std::string(), presentation_id_));
|
| + }
|
| + return connections;
|
| +}
|
| +
|
| +ReceiverPresentationServiceDelegateImpl::
|
| + ReceiverPresentationServiceDelegateImpl(content::WebContents* web_contents,
|
| + const std::string& presentation_id)
|
| + : web_contents_(web_contents),
|
| + presentation_id_(presentation_id),
|
| + offscreen_presentation_manager_(
|
| + OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext(
|
| + Profile::FromBrowserContext(web_contents_->GetBrowserContext())
|
| + ->GetOriginalProfile())) {
|
| + DCHECK(web_contents_);
|
| + DCHECK(!presentation_id.empty());
|
| + DCHECK(offscreen_presentation_manager_);
|
| + DCHECK(web_contents_->GetBrowserContext()->IsOffTheRecord());
|
| +
|
| + offscreen_presentation_manager_->RegisterOffscreenPresentationReceiver(
|
| + presentation_id_, base::Bind(&ReceiverPresentationServiceDelegateImpl::
|
| + OnReceiverConnectionAvailable,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void ReceiverPresentationServiceDelegateImpl::OnReceiverConnectionAvailable(
|
| + std::unique_ptr<OffscreenPresentationConnection> connection) {
|
| + // TODO(imcheng): Fix this for the multiple controllers case.
|
| + if (!receiver_available_callback_.is_null())
|
| + receiver_available_callback_.Run(
|
| + content::PresentationSessionInfo(std::string(), presentation_id_));
|
| +
|
| + receiver_connections_.push_back(std::move(connection));
|
| +}
|
| +
|
| +bool ReceiverPresentationServiceDelegateImpl::IsMainFrame(
|
| + const RenderFrameHostId& rfh_id) const {
|
| + content::RenderFrameHost* render_frame_host = web_contents_->GetMainFrame();
|
| + DCHECK(render_frame_host);
|
| +
|
| + return rfh_id.first == render_frame_host->GetProcess()->GetID() &&
|
| + rfh_id.second == render_frame_host->GetRoutingID();
|
| +}
|
| +
|
| +bool ReceiverPresentationServiceDelegateImpl::BindToFrame(
|
| + const RenderFrameHostId& rfh_id) {
|
| + if (current_frame_id_ != RenderFrameHostId() || !IsMainFrame(rfh_id))
|
| + return false;
|
| +
|
| + current_frame_id_ = rfh_id;
|
| + return true;
|
| +}
|
| +
|
| +OffscreenPresentationConnection*
|
| +ReceiverPresentationServiceDelegateImpl::FindConnection(
|
| + const content::PresentationSessionInfo& session_info) {
|
| + // TODO(imcheng): Need additional information to locate correct connection
|
| + // in multiple controllers case. crbug.com/529911
|
| + if (receiver_connections_.empty()) {
|
| + DVLOG(2) << "Session not found: " << session_info.presentation_id;
|
| + return nullptr;
|
| + } else {
|
| + return receiver_connections_.front().get();
|
| + }
|
| +}
|
| +
|
| +} // namespace media_router
|
|
|