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

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

Issue 979413002: [Presentation API] Additional plumbing for PresentationServiceImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@REAL-NEW-MASTER
Patch Set: updated comments Created 5 years, 9 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: 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 4a1db13ea7a41d8e8d79981ecfe079f8b731a41a..0306bd6627e8508aa2204e35e3f6ada831841723 100644
--- a/content/browser/presentation/presentation_service_impl.cc
+++ b/content/browser/presentation/presentation_service_impl.cc
@@ -5,6 +5,7 @@
#include "content/browser/presentation/presentation_service_impl.h"
#include "base/logging.h"
+#include "content/browser/presentation/presentation_type_converters.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/render_frame_host.h"
@@ -21,7 +22,8 @@ PresentationServiceImpl::PresentationServiceImpl(
PresentationServiceDelegate* delegate)
: WebContentsObserver(web_contents),
render_frame_host_(render_frame_host),
- delegate_(delegate) {
+ delegate_(delegate),
+ weak_factory_(this) {
DCHECK(render_frame_host_);
DCHECK(web_contents);
VLOG(2) << "PresentationServiceImpl: "
@@ -40,7 +42,7 @@ PresentationServiceImpl::~PresentationServiceImpl() {
void PresentationServiceImpl::CreateMojoService(
RenderFrameHost* render_frame_host,
mojo::InterfaceRequest<presentation::PresentationService> request) {
- VLOG(2) << "PresentationServiceImpl::CreateService";
+ VLOG(2) << "CreateMojoService";
WebContents* web_contents =
WebContents::FromRenderFrameHost(render_frame_host);
DCHECK(web_contents);
@@ -55,7 +57,7 @@ void PresentationServiceImpl::CreateMojoService(
}
void PresentationServiceImpl::OnConnectionError() {
- VLOG(1) << "PresentationServiceImpl::OnConnectionError: "
+ VLOG(1) << "OnConnectionError: "
<< render_frame_host_->GetProcess()->GetID() << ", "
<< render_frame_host_->GetRoutingID();
}
@@ -63,7 +65,7 @@ void PresentationServiceImpl::OnConnectionError() {
void PresentationServiceImpl::GetScreenAvailability(
const mojo::String& presentation_url,
const ScreenAvailabilityMojoCallback& callback) {
- VLOG(2) << "PresentationServiceImpl::GetScreenAvailability";
+ VLOG(2) << "GetScreenAvailability";
if (!delegate_)
return;
@@ -72,8 +74,10 @@ void PresentationServiceImpl::GetScreenAvailability(
// GetScreenAvailability() is called with no URL and there is no default
// Presentation URL.
whywhat 2015/03/10 17:05:04 BTW, are we going to support this situation for th
imcheng 2015/03/10 18:31:15 Maybe there's something I don't understand here.
whywhat 2015/03/10 23:20:22 I believe that the absence of DPU from the spec me
imcheng 2015/03/12 20:03:40 Ok, gotcha. We also talked about this briefly in t
- if (presentation_url_str.empty())
+ if (presentation_url_str.empty()) {
+ VLOG(1) << "No valid presentation URL.";
return;
+ }
auto it = availability_contexts_.find(presentation_url_str);
if (it == availability_contexts_.end()) {
@@ -89,28 +93,153 @@ void PresentationServiceImpl::GetScreenAvailability(
}
it = availability_contexts_.insert(
- std::make_pair(presentation_url_str, context)).first;
+ std::make_pair(context->GetPresentationUrl(), context)).first;
}
it->second->CallbackReceived(callback);
}
void PresentationServiceImpl::OnScreenAvailabilityListenerRemoved() {
- NOTIMPLEMENTED();
+ VLOG(2) << "OnScreenAvailabilityListenerRemoved";
+ if (!delegate_)
+ return;
+
+ const std::string presentation_url_str = default_presentation_url_;
whywhat 2015/03/10 17:05:04 It seems like you could eliminate this whole parag
imcheng 2015/03/10 18:31:15 Done. But shouldn't we support removing listener b
whywhat 2015/03/10 23:20:22 Yes, but we don't pass a URL there yet. Let's fix
imcheng 2015/03/12 20:03:40 I see you have added the param to the .mojom in yo
+ if (presentation_url_str.empty())
+ return;
+
+ auto it = availability_contexts_.find(presentation_url_str);
+ if (it == availability_contexts_.end())
+ return;
+
+ delegate_->RemoveScreenAvailabilityListener(
+ render_frame_host_->GetProcess()->GetID(),
+ render_frame_host_->GetRoutingID(),
+ it->second.get());
+ availability_contexts_.erase(it);
}
void PresentationServiceImpl::StartSession(
const mojo::String& presentation_url,
const mojo::String& presentation_id,
const NewSessionMojoCallback& callback) {
- NOTIMPLEMENTED();
+ if (start_or_join_session_cb_.get())
+ return;
whywhat 2015/03/10 17:05:04 VLOG?
imcheng 2015/03/10 18:31:15 Done.
+
+ delegate_->StartSession(
+ render_frame_host_->GetProcess()->GetID(),
+ render_frame_host_->GetRoutingID(),
+ presentation_url,
+ presentation_id,
+ base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionSucceeded,
+ weak_factory_.GetWeakPtr()),
+ base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionError,
+ weak_factory_.GetWeakPtr()));
+ start_or_join_session_cb_.reset(new NewSessionMojoCallback(callback));
}
void PresentationServiceImpl::JoinSession(
const mojo::String& presentation_url,
const mojo::String& presentation_id,
const NewSessionMojoCallback& callback) {
- NOTIMPLEMENTED();
+ if (start_or_join_session_cb_.get())
+ return;
+
+ delegate_->JoinSession(
+ render_frame_host_->GetProcess()->GetID(),
+ render_frame_host_->GetRoutingID(),
+ presentation_url,
+ presentation_id,
+ base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionSucceeded,
+ weak_factory_.GetWeakPtr()),
+ base::Bind(&PresentationServiceImpl::OnStartOrJoinSessionError,
+ weak_factory_.GetWeakPtr()));
+ start_or_join_session_cb_.reset(new NewSessionMojoCallback(callback));
+}
+
+void PresentationServiceImpl::OnStartOrJoinSessionSucceeded(
+ const PresentationSessionInfo& session_info) {
+ CHECK(start_or_join_session_cb_.get());
+ start_or_join_session_cb_->Run(
+ presentation::PresentationSessionInfo::From(session_info),
+ presentation::PresentationErrorPtr());
+ start_or_join_session_cb_.reset();
+}
+
+void PresentationServiceImpl::OnStartOrJoinSessionError(
+ const PresentationError& error) {
+ CHECK(start_or_join_session_cb_.get());
+ start_or_join_session_cb_->Run(
+ presentation::PresentationSessionInfoPtr(),
+ presentation::PresentationError::From(error));
+ start_or_join_session_cb_.reset();
+}
+
+
+void PresentationServiceImpl::DoSetDefaultPresentationUrl(
+ const std::string& presentation_url) {
+ delegate_->SetDefaultPresentationUrl(
+ render_frame_host_->GetProcess()->GetID(),
+ render_frame_host_->GetRoutingID(),
+ presentation_url);
+ default_presentation_url_ = presentation_url;
+}
+
+void PresentationServiceImpl::SetDefaultPresentationUrl(
+ const mojo::String& presentation_url) {
+ VLOG(2) << "SetDefaultPresentationUrl";
+ if (!delegate_)
+ return;
+
+ std::string old_default_url = default_presentation_url_;
+ std::string new_default_url = presentation_url;
+
+ if (old_default_url == new_default_url)
+ return;
+
+ if (old_default_url.empty()) {
+ DoSetDefaultPresentationUrl(new_default_url);
+ return;
+ }
+
+ auto old_it = availability_contexts_.find(old_default_url);
+ // Haven't started listening yet.
+ if (old_it == availability_contexts_.end()) {
+ DoSetDefaultPresentationUrl(new_default_url);
+ return;
+ }
+
+ // Have already started listening. Create a listener for the new URL and
+ // transfer the callback from the old listener, if any.
+ // This is done so that a listener added before default URL is changed
+ // will continue to work.
+ ScreenAvailabilityMojoCallback* old_callback = old_it->second->callback();
+ if (!new_default_url.empty()) {
+ auto new_it = availability_contexts_.find(new_default_url);
+ if (new_it == availability_contexts_.end()) {
+ linked_ptr<ScreenAvailabilityContext> context(
+ new ScreenAvailabilityContext(new_default_url));
+ if (!delegate_->AddScreenAvailabilityListener(
+ render_frame_host_->GetProcess()->GetID(),
+ render_frame_host_->GetRoutingID(),
+ context.get())) {
+ VLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
+ return;
+ }
+ new_it = availability_contexts_.insert(
+ std::make_pair(context->GetPresentationUrl(), context)).first;
+ }
+ if (old_callback)
+ new_it->second->CallbackReceived(*old_callback);
+ }
+
+ // Remove listener for old default presentation URL.
+ delegate_->RemoveScreenAvailabilityListener(
+ render_frame_host_->GetProcess()->GetID(),
+ render_frame_host_->GetRoutingID(),
+ old_it->second.get());
+ availability_contexts_.erase(old_it);
+ DoSetDefaultPresentationUrl(new_default_url);
}
void PresentationServiceImpl::DidNavigateAnyFrame(
@@ -136,8 +265,8 @@ void PresentationServiceImpl::DidNavigateAnyFrame(
if (in_page_navigation)
return;
- // Unregister all sources if the frame actually navigated.
- RemoveAllListeners();
+ // Reset if the frame actually navigated.
+ Reset();
}
void PresentationServiceImpl::RenderFrameDeleted(
@@ -147,24 +276,30 @@ void PresentationServiceImpl::RenderFrameDeleted(
return;
// RenderFrameDeleted means this object is getting deleted soon.
- RemoveAllListeners();
+ Reset();
}
-void PresentationServiceImpl::RemoveAllListeners() {
- VLOG(2) << "PresentationServiceImpl::RemoveAllListeners";
+void PresentationServiceImpl::Reset() {
+ VLOG(2) << "PresentationServiceImpl::Reset";
if (!delegate_)
return;
+ // Unregister all sources.
delegate_->RemoveAllScreenAvailabilityListeners(
render_frame_host_->GetProcess()->GetID(),
render_frame_host_->GetRoutingID());
-
availability_contexts_.clear();
+
+ // Clear default presentation URL.
+ DoSetDefaultPresentationUrl(std::string());
+
+ start_or_join_session_cb_.reset();
}
void PresentationServiceImpl::OnDelegateDestroyed() {
VLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed";
delegate_ = nullptr;
+ start_or_join_session_cb_.reset();
}
PresentationServiceImpl::ScreenAvailabilityContext::ScreenAvailabilityContext(

Powered by Google App Engine
This is Rietveld 408576698