Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1288)

Unified Diff: chrome/browser/media/router/receiver_presentation_service_delegate_impl.cc

Issue 1314413005: [Presentation API] 1-UA presentation support + presenter APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase again to pick up Yuri's cl Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..17a1f66c321aa37ec8c3952753ae6a8e4884adaa
--- /dev/null
+++ b/chrome/browser/media/router/receiver_presentation_service_delegate_impl.cc
@@ -0,0 +1,286 @@
+// 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/presentation_session_state_listener.h"
+
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(
+ media_router::ReceiverPresentationServiceDelegateImpl);
+
+using content::PresentationServiceDelegate;
+using content::RenderFrameHost;
+
+namespace media_router {
+
+using OffscreenPresentationSession =
+ OffscreenPresentationManager::OffscreenPresentationSession;
+
+// 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_sessions_.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;
+
+ receiver_sessions_.clear();
+ receiver_available_callback_.Run(nullptr);
+ receiver_available_callback_.Reset();
+}
+
+void ReceiverPresentationServiceDelegateImpl::SetDefaultPresentationUrl(
+ int render_process_id,
+ int render_frame_id,
+ const std::string& default_presentation_url) {
+ NOTIMPLEMENTED();
+}
+
+void ReceiverPresentationServiceDelegateImpl::StartSession(
+ int render_process_id,
+ int render_frame_id,
+ const std::string& presentation_url,
+ const PresentationSessionSuccessCallback& success_cb,
+ const 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 PresentationSessionSuccessCallback& success_cb,
+ const PresentationSessionErrorCallback& error_cb) {
+ NOTIMPLEMENTED();
+ error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
+ "Not implemented"));
+}
+
+void ReceiverPresentationServiceDelegateImpl::CloseSession(
+ 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;
+
+ if (!IsMainFrame(render_process_id, render_frame_id)) {
+ DVLOG(2) << __FUNCTION__ << ": not main frame";
+ return;
+ }
+
+ OffscreenPresentationSession* offscreen_session = FindSession(session_info);
+ if (!offscreen_session)
+ return;
+
+ offscreen_session->ListenForMessages(message_cb);
+}
+
+void ReceiverPresentationServiceDelegateImpl::SendMessage(
+ int render_process_id,
+ int render_frame_id,
+ const content::PresentationSessionInfo& session_info,
+ scoped_ptr<content::PresentationSessionMessage> message,
+ const content::SendMessageCallback& send_message_cb) {
+ DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
+
+ if (!IsMainFrame(render_process_id, render_frame_id)) {
+ DVLOG(2) << __FUNCTION__ << ": not main frame";
+ send_message_cb.Run(false);
+ return;
+ }
+
+ OffscreenPresentationSession* offscreen_session = FindSession(session_info);
+ if (!offscreen_session) {
+ send_message_cb.Run(false);
+ return;
+ }
+
+ offscreen_session->SendMessage(message.Pass(), send_message_cb);
+}
+
+bool ReceiverPresentationServiceDelegateImpl::ListenForSessionStateChange(
+ int render_process_id,
+ int render_frame_id,
+ content::PresentationSessionStateListener* listener) {
+ DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
+
+ if (!IsMainFrame(render_process_id, render_frame_id)) {
+ DVLOG(2) << __FUNCTION__ << ": not main frame";
+ return false;
+ }
+
+ OffscreenPresentationSession* offscreen_session =
+ FindSession(listener->GetSessionInfo());
+ if (!offscreen_session)
+ return false;
+
+ offscreen_session->ListenForStateChanges(listener);
+ return true;
+}
+
+void ReceiverPresentationServiceDelegateImpl::GetPresentationReceiverSession(
+ int render_process_id,
+ int render_frame_id,
+ const content::PresentationReceiverSessionAvailableCallback& callback) {
+ DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
+ DCHECK(!receiver_available_callback_.is_null());
+
+ if (!IsMainFrame(render_process_id, render_frame_id)) {
+ DVLOG(2) << __FUNCTION__ << ": not main frame";
+ callback.Run(nullptr);
+ return;
+ }
+
+ if (!receiver_sessions_.empty()) {
+ content::PresentationSessionInfo session_info(std::string(),
+ presentation_id_);
+ callback.Run(&session_info);
+ return;
+ }
+
+ receiver_available_callback_ = callback;
+}
+
+std::vector<content::PresentationSessionInfo>
+ReceiverPresentationServiceDelegateImpl::GetPresentationReceiverSessions(
+ int render_process_id,
+ int render_frame_id) {
+ DVLOG(2) << __FUNCTION__ << render_process_id << ", " << render_frame_id;
+
+ std::vector<content::PresentationSessionInfo> sessions;
+ if (!IsMainFrame(render_process_id, render_frame_id)) {
+ DVLOG(2) << __FUNCTION__ << ": not main frame";
+ return sessions;
+ }
+
+ // TODO(imcheng): This is currently broken for the multiple controllers case.
+ // We need additional ID to distinguish between controllers. crbug.com/529911
+ for (const auto& session : receiver_sessions_) {
miu 2015/10/07 21:51:46 Hmm...Weird to be creating a vector of receiver_se
imcheng 2015/10/10 04:39:43 That is my intention, for now. The TODO and bug ex
+ sessions.push_back(
+ content::PresentationSessionInfo(std::string(), presentation_id_));
+ }
+
+ return sessions;
+}
+
+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_);
+
+ offscreen_presentation_manager_->RegisterOffscreenPresentationReceiver(
+ presentation_id_,
+ base::Bind(
+ &ReceiverPresentationServiceDelegateImpl::OnReceiverSessionAvailable,
+ base::Unretained(this)));
+}
+
+void ReceiverPresentationServiceDelegateImpl::OnReceiverSessionAvailable(
+ scoped_ptr<OffscreenPresentationSession> session) {
+ if (!receiver_available_callback_.is_null()) {
+ content::PresentationSessionInfo session_info(std::string(),
+ presentation_id_);
+ receiver_available_callback_.Run(&session_info);
+ receiver_available_callback_.Reset();
+ }
+
+ receiver_sessions_.push_back(session.Pass());
+}
+
+bool ReceiverPresentationServiceDelegateImpl::IsMainFrame(int render_process_id,
+ int render_frame_id) {
+ content::RenderFrameHost* render_frame_host = web_contents_->GetMainFrame();
+ DCHECK(render_frame_host);
+
+ return render_process_id == render_frame_host->GetProcess()->GetID() &&
+ render_frame_id == render_frame_host->GetRoutingID();
+}
+
+OffscreenPresentationSession*
+ReceiverPresentationServiceDelegateImpl::FindSession(
+ const content::PresentationSessionInfo& session_info) {
+ // TODO(imcheng): Need additional information to locate correct session object
+ // in multiple controllers case. crbug.com/529911
+ if (receiver_sessions_.empty()) {
+ DVLOG(2) << "Session not found: " << session_info.presentation_id;
+ return nullptr;
+ } else {
+ return receiver_sessions_.front();
+ }
+}
+
+} // namespace media_router

Powered by Google App Engine
This is Rietveld 408576698