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

Unified Diff: content/browser/presentation/presentation_service_impl.cc

Issue 2379703002: [Presentation API] (alternative) 1-UA: send message between controller and receiver page (Closed)
Patch Set: merge with master Created 4 years, 1 month 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: content/browser/presentation/presentation_service_impl.cc
diff --git a/content/browser/presentation/presentation_service_impl.cc b/content/browser/presentation/presentation_service_impl.cc
index 46ec67cf0158791d666a023c92abfb3e71e47546..8c3bdab4e699585fcb73132dc6746bfb77ef09c2 100644
--- a/content/browser/presentation/presentation_service_impl.cc
+++ b/content/browser/presentation/presentation_service_impl.cc
@@ -126,9 +126,11 @@ void InvokeNewSessionCallbackWithError(
PresentationServiceImpl::PresentationServiceImpl(
RenderFrameHost* render_frame_host,
WebContents* web_contents,
- PresentationServiceDelegate* delegate)
+ ControllerPresentationServiceDelegate* controller_delegate,
+ ReceiverPresentationServiceDelegate* receiver_delegate)
: WebContentsObserver(web_contents),
- delegate_(delegate),
+ controller_delegate_(controller_delegate),
+ receiver_delegate_(receiver_delegate),
start_session_request_id_(kInvalidRequestSessionId),
weak_factory_(this) {
DCHECK(render_frame_host);
@@ -139,13 +141,17 @@ PresentationServiceImpl::PresentationServiceImpl(
render_frame_id_ = render_frame_host->GetRoutingID();
DVLOG(2) << "PresentationServiceImpl: "
<< render_process_id_ << ", " << render_frame_id_;
- if (delegate_)
- delegate_->AddObserver(render_process_id_, render_frame_id_, this);
+
+ if (auto delegate = GetPresentationServiceDelegate())
+ delegate->AddObserver(render_process_id_, render_frame_id_, this);
}
PresentationServiceImpl::~PresentationServiceImpl() {
- if (delegate_)
- delegate_->RemoveObserver(render_process_id_, render_frame_id_);
+ DVLOG(2) << __FUNCTION__ << ": " << render_process_id_ << ", "
+ << render_frame_id_;
+
+ if (auto delegate = GetPresentationServiceDelegate())
+ delegate->RemoveObserver(render_process_id_, render_frame_id_);
}
// static
@@ -160,10 +166,16 @@ void PresentationServiceImpl::CreateMojoService(
// This object will be deleted when the RenderFrameHost is about to be
// deleted (RenderFrameDeleted).
PresentationServiceImpl* impl = new PresentationServiceImpl(
- render_frame_host,
- web_contents,
- GetContentClient()->browser()->GetPresentationServiceDelegate(
+ render_frame_host, web_contents,
+ GetContentClient()->browser()->GetControllerPresentationServiceDelegate(
+ web_contents),
+ GetContentClient()->browser()->GetReceiverPresentationServiceDelegate(
web_contents));
+ if (impl->receiver_delegate_) {
+ // In current implementation, web contents can be controller or receiver
+ // but not both.
+ impl->controller_delegate_ = nullptr;
+ }
impl->Bind(std::move(request));
}
@@ -178,11 +190,17 @@ void PresentationServiceImpl::SetClient(
DCHECK(!client_.get());
// TODO(imcheng): Set ErrorHandler to listen for errors.
client_ = std::move(client);
+
+ if (receiver_delegate_) {
+ receiver_delegate_->RegisterReceiverConnectionAvailableCallback(
+ base::Bind(&PresentationServiceImpl::OnReceiverConnectionAvailable,
+ weak_factory_.GetWeakPtr()));
+ }
}
void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) {
DVLOG(2) << "ListenForScreenAvailability " << url.spec();
- if (!delegate_) {
+ if (!controller_delegate_) {
client_->OnScreenAvailabilityUpdated(url, false);
return;
}
@@ -192,10 +210,8 @@ void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) {
std::unique_ptr<ScreenAvailabilityListenerImpl> listener(
new ScreenAvailabilityListenerImpl(url, this));
- if (delegate_->AddScreenAvailabilityListener(
- render_process_id_,
- render_frame_id_,
- listener.get())) {
+ if (controller_delegate_->AddScreenAvailabilityListener(
+ render_process_id_, render_frame_id_, listener.get())) {
screen_availability_listeners_[url] = std::move(listener);
} else {
DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
@@ -205,14 +221,14 @@ void PresentationServiceImpl::ListenForScreenAvailability(const GURL& url) {
void PresentationServiceImpl::StopListeningForScreenAvailability(
const GURL& url) {
DVLOG(2) << "StopListeningForScreenAvailability " << url.spec();
- if (!delegate_)
+ if (!controller_delegate_)
return;
auto listener_it = screen_availability_listeners_.find(url);
if (listener_it == screen_availability_listeners_.end())
return;
- delegate_->RemoveScreenAvailabilityListener(
+ controller_delegate_->RemoveScreenAvailabilityListener(
render_process_id_, render_frame_id_, listener_it->second.get());
screen_availability_listeners_.erase(listener_it);
}
@@ -221,7 +237,7 @@ void PresentationServiceImpl::StartSession(
const std::vector<GURL>& presentation_urls,
const NewSessionCallback& callback) {
DVLOG(2) << "StartSession";
- if (!delegate_) {
+ if (!controller_delegate_) {
callback.Run(
blink::mojom::PresentationSessionInfoPtr(),
blink::mojom::PresentationError::From(PresentationError(
@@ -238,7 +254,7 @@ void PresentationServiceImpl::StartSession(
start_session_request_id_ = GetNextRequestSessionId();
pending_start_session_cb_.reset(new NewSessionCallbackWrapper(callback));
- delegate_->StartSession(
+ controller_delegate_->StartSession(
render_process_id_, render_frame_id_, presentation_urls,
base::Bind(&PresentationServiceImpl::OnStartSessionSucceeded,
weak_factory_.GetWeakPtr(), start_session_request_id_),
@@ -251,7 +267,7 @@ void PresentationServiceImpl::JoinSession(
const base::Optional<std::string>& presentation_id,
const NewSessionCallback& callback) {
DVLOG(2) << "JoinSession";
- if (!delegate_) {
+ if (!controller_delegate_) {
callback.Run(blink::mojom::PresentationSessionInfoPtr(),
blink::mojom::PresentationError::From(PresentationError(
PRESENTATION_ERROR_NO_PRESENTATION_FOUND,
@@ -264,7 +280,7 @@ void PresentationServiceImpl::JoinSession(
InvokeNewSessionCallbackWithError(callback);
return;
}
- delegate_->JoinSession(
+ controller_delegate_->JoinSession(
render_process_id_, render_frame_id_, presentation_urls,
presentation_id.value_or(std::string()),
base::Bind(&PresentationServiceImpl::OnJoinSessionSucceeded,
@@ -286,8 +302,8 @@ int PresentationServiceImpl::RegisterJoinSessionCallback(
void PresentationServiceImpl::ListenForConnectionStateChange(
const PresentationSessionInfo& connection) {
- if (delegate_) {
- delegate_->ListenForConnectionStateChange(
+ if (controller_delegate_) {
+ controller_delegate_->ListenForConnectionStateChange(
render_process_id_, render_frame_id_, connection,
base::Bind(&PresentationServiceImpl::OnConnectionStateChanged,
weak_factory_.GetWeakPtr(), connection));
@@ -297,6 +313,8 @@ void PresentationServiceImpl::ListenForConnectionStateChange(
void PresentationServiceImpl::OnStartSessionSucceeded(
int request_session_id,
const PresentationSessionInfo& session_info) {
+ DVLOG(2) << "PresentationServiceImpl::OnStartSessionSucceeded"
+ << " [is_offscreen]: " << session_info.is_offscreen;
if (request_session_id != start_session_request_id_)
return;
@@ -325,6 +343,8 @@ void PresentationServiceImpl::OnStartSessionError(
void PresentationServiceImpl::OnJoinSessionSucceeded(
int request_session_id,
const PresentationSessionInfo& session_info) {
+ DVLOG(2) << "PresentationServiceImpl::OnJoinSessionSucceeded"
+ << " [is_offscreen]: " << session_info.is_offscreen;
if (RunAndEraseJoinSessionMojoCallback(
request_session_id,
blink::mojom::PresentationSessionInfo::From(session_info),
@@ -358,14 +378,14 @@ bool PresentationServiceImpl::RunAndEraseJoinSessionMojoCallback(
void PresentationServiceImpl::SetDefaultPresentationUrls(
const std::vector<GURL>& presentation_urls) {
DVLOG(2) << "SetDefaultPresentationUrls";
- if (!delegate_)
+ if (!controller_delegate_)
return;
if (default_presentation_urls_ == presentation_urls)
return;
default_presentation_urls_ = presentation_urls;
- delegate_->SetDefaultPresentationUrls(
+ controller_delegate_->SetDefaultPresentationUrls(
render_process_id_, render_frame_id_, presentation_urls,
base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted,
weak_factory_.GetWeakPtr()));
@@ -375,17 +395,18 @@ void PresentationServiceImpl::SendSessionMessage(
blink::mojom::PresentationSessionInfoPtr session,
blink::mojom::SessionMessagePtr session_message,
const SendSessionMessageCallback& callback) {
- DVLOG(2) << "SendSessionMessage";
+ DVLOG(2) << "SendSessionMessage"
+ << " [id]: " << session->id;
DCHECK(!session_message.is_null());
// send_message_callback_ should be null by now, otherwise resetting of
// send_message_callback_ with new callback will drop the old callback.
- if (!delegate_ || send_message_callback_) {
+ if (!controller_delegate_ || send_message_callback_) {
callback.Run(false);
return;
}
send_message_callback_.reset(new SendSessionMessageCallback(callback));
- delegate_->SendMessage(
+ controller_delegate_->SendMessage(
render_process_id_, render_frame_id_,
session.To<PresentationSessionInfo>(),
GetPresentationSessionMessage(std::move(session_message)),
@@ -406,16 +427,17 @@ void PresentationServiceImpl::CloseConnection(
const GURL& presentation_url,
const std::string& presentation_id) {
DVLOG(2) << "CloseConnection " << presentation_id;
- if (delegate_)
- delegate_->CloseConnection(render_process_id_, render_frame_id_,
- presentation_id);
+ if (controller_delegate_)
+ controller_delegate_->CloseConnection(render_process_id_, render_frame_id_,
+ presentation_id);
}
void PresentationServiceImpl::Terminate(const GURL& presentation_url,
const std::string& presentation_id) {
DVLOG(2) << "Terminate " << presentation_id;
- if (delegate_)
- delegate_->Terminate(render_process_id_, render_frame_id_, presentation_id);
+ if (controller_delegate_)
+ controller_delegate_->Terminate(render_process_id_, render_frame_id_,
+ presentation_id);
}
void PresentationServiceImpl::OnConnectionStateChanged(
@@ -443,26 +465,48 @@ bool PresentationServiceImpl::FrameMatches(
render_frame_host->GetRoutingID() == render_frame_id_;
}
+PresentationServiceDelegateBase*
+PresentationServiceImpl::GetPresentationServiceDelegate() {
+ if (receiver_delegate_)
+ return receiver_delegate_;
+
+ return controller_delegate_;
+}
+
void PresentationServiceImpl::ListenForSessionMessages(
blink::mojom::PresentationSessionInfoPtr session) {
DVLOG(2) << "ListenForSessionMessages";
- if (!delegate_)
+ if (!controller_delegate_)
return;
PresentationSessionInfo session_info(session.To<PresentationSessionInfo>());
- delegate_->ListenForSessionMessages(
+ controller_delegate_->ListenForSessionMessages(
render_process_id_, render_frame_id_, session_info,
base::Bind(&PresentationServiceImpl::OnSessionMessages,
weak_factory_.GetWeakPtr(), session_info));
}
+void PresentationServiceImpl::SetPresentationConnection(
+ blink::mojom::PresentationSessionInfoPtr session,
+ blink::mojom::PresentationConnectionPtr connection) {
+ DVLOG(2) << "SetPresentationConnection";
+
+ if (!controller_delegate_)
+ return;
+
+ PresentationSessionInfo session_info(session.To<PresentationSessionInfo>());
+ controller_delegate_->ConnectToOffscreenPresentation(
+ render_process_id_, render_frame_id_, session_info,
+ std::move(connection));
+}
+
void PresentationServiceImpl::OnSessionMessages(
const PresentationSessionInfo& session,
const ScopedVector<PresentationSessionMessage>& messages,
bool pass_ownership) {
DCHECK(client_);
-
- DVLOG(2) << "OnSessionMessages";
+ DVLOG(2) << "OnSessionMessages"
+ << " [id]: " << session.presentation_id;
std::vector<blink::mojom::SessionMessagePtr> mojo_messages(messages.size());
std::transform(messages.begin(), messages.end(), mojo_messages.begin(),
[pass_ownership](PresentationSessionMessage* message) {
@@ -474,6 +518,16 @@ void PresentationServiceImpl::OnSessionMessages(
std::move(mojo_messages));
}
+void PresentationServiceImpl::OnReceiverConnectionAvailable(
+ const content::PresentationSessionInfo& session_info,
+ PresentationConnectionPtr&& controller) {
+ DVLOG(2) << "PresentationServiceImpl::OnReceiverConnectionAvailable";
+
+ client_->OnReceiverConnectionAvailable(
+ blink::mojom::PresentationSessionInfo::From(session_info),
+ std::move(controller));
+}
+
void PresentationServiceImpl::DidNavigateAnyFrame(
content::RenderFrameHost* render_frame_host,
const content::LoadCommittedDetails& details,
@@ -519,8 +573,9 @@ void PresentationServiceImpl::WebContentsDestroyed() {
void PresentationServiceImpl::Reset() {
DVLOG(2) << "PresentationServiceImpl::Reset";
- if (delegate_)
- delegate_->Reset(render_process_id_, render_frame_id_);
+
+ if (auto delegate = GetPresentationServiceDelegate())
+ delegate->Reset(render_process_id_, render_frame_id_);
default_presentation_urls_.clear();
@@ -547,12 +602,15 @@ void PresentationServiceImpl::Reset() {
void PresentationServiceImpl::OnDelegateDestroyed() {
DVLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed";
- delegate_ = nullptr;
+ controller_delegate_ = nullptr;
+ receiver_delegate_ = nullptr;
Reset();
}
void PresentationServiceImpl::OnDefaultPresentationStarted(
const PresentationSessionInfo& connection) {
+ DVLOG(2) << "PresentationServiceImpl::OnDefaultPresentationStarted"
+ << " [is_offscreen]: " << connection.is_offscreen;
DCHECK(client_.get());
client_->OnDefaultSessionStarted(
blink::mojom::PresentationSessionInfo::From(connection));

Powered by Google App Engine
This is Rietveld 408576698