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

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

Issue 906113002: Support listening for available screens for multiple presentation urls in the same frame (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Anton's comments Created 5 years, 10 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 c106757562029e1620064e9d14833b0edf61ab85..02e2773080adf269686f341838ed690469a7c15e 100644
--- a/content/browser/presentation/presentation_service_impl.cc
+++ b/content/browser/presentation/presentation_service_impl.cc
@@ -5,29 +5,200 @@
#include "content/browser/presentation/presentation_service_impl.h"
#include "base/logging.h"
+#include "content/public/browser/content_browser_client.h"
+#include "content/public/browser/navigation_details.h"
+#include "content/public/browser/render_frame_host.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/common/content_client.h"
+#include "content/public/common/frame_navigate_params.h"
namespace content {
-PresentationServiceImpl::PresentationServiceImpl() {
+PresentationServiceImpl::PresentationServiceImpl(
+ RenderFrameHost* render_frame_host,
+ WebContents* web_contents,
+ PresentationServiceDelegate* delegate)
+ : WebContentsObserver(web_contents),
+ render_frame_host_(render_frame_host),
+ delegate_(delegate) {
+ DCHECK(render_frame_host_);
+ DCHECK(web_contents);
+ VLOG(2) << "PresentationServiceImpl: "
+ << render_frame_host_->GetProcess()->GetID() << ", "
+ << render_frame_host_->GetRoutingID();
+ if (delegate_)
+ delegate_->AddObserver(this);
}
PresentationServiceImpl::~PresentationServiceImpl() {
+ if (delegate_)
+ delegate_->RemoveObserver(this);
}
// static
void PresentationServiceImpl::CreateMojoService(
+ RenderFrameHost* render_frame_host,
mojo::InterfaceRequest<presentation::PresentationService> request) {
- mojo::BindToRequest(new PresentationServiceImpl(), &request);
+ VLOG(2) << "PresentationServiceImpl::CreateService";
+ WebContents* web_contents =
+ WebContents::FromRenderFrameHost(render_frame_host);
+ DCHECK(web_contents);
+
+ mojo::BindToRequest(
+ new PresentationServiceImpl(
+ render_frame_host,
+ web_contents,
+ GetContentClient()->browser()->GetPresentationServiceDelegate(
+ web_contents)),
+ &request);
+}
+
+void PresentationServiceImpl::OnConnectionError() {
+ VLOG(1) << "PresentationServiceImpl::OnConnectionError: "
+ << render_frame_host_->GetProcess()->GetID() << ", "
+ << render_frame_host_->GetRoutingID();
}
void PresentationServiceImpl::GetScreenAvailability(
const mojo::String& presentation_url,
- const AvailabilityCallback& callback) {
- NOTIMPLEMENTED();
+ const ScreenAvailabilityMojoCallback& callback) {
+ VLOG(2) << "PresentationServiceImpl::GetScreenAvailability";
+ if (!delegate_)
+ return;
+
+ const std::string& presentation_url_str = !presentation_url.is_null() ?
+ presentation_url.get() : default_presentation_url_;
+
+ // GetScreenAvailability() is called with no URL and there is no default
+ // Presentation URL.
+ if (presentation_url_str.empty())
+ return;
+
+ auto it = availability_contexts_.find(presentation_url_str);
+ if (it == availability_contexts_.end()) {
+ linked_ptr<ScreenAvailabilityContext> context(
+ new ScreenAvailabilityContext(presentation_url_str));
+
+ if (!delegate_->AddScreenAvailabilityListener(
+ render_frame_host_->GetProcess()->GetID(),
+ render_frame_host_->GetRoutingID(),
+ context.get())) {
+ VLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
+ return;
+ }
+
+ it = availability_contexts_.insert(
+ std::make_pair(presentation_url_str, context)).first;
+ }
+
+ it->second->CallbackReceived(callback);
}
void PresentationServiceImpl::OnScreenAvailabilityListenerRemoved() {
NOTIMPLEMENTED();
}
+void PresentationServiceImpl::DidNavigateAnyFrame(
+ content::RenderFrameHost* render_frame_host,
+ const content::LoadCommittedDetails& details,
+ const content::FrameNavigateParams& params) {
+ VLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame";
+ if (render_frame_host_ != render_frame_host)
+ return;
+
+ std::string prev_url_host = details.previous_url.host();
+ std::string curr_url_host = params.url.host();
+
+ // If a frame navigation is in-page (e.g. navigating to a fragment in
+ // same page) then we do not unregister listeners.
+ bool in_page_navigation = details.is_in_page ||
+ details.type == content::NAVIGATION_TYPE_IN_PAGE;
+
+ VLOG(2) << "DidNavigateAnyFrame: "
+ << "prev host: " << prev_url_host << ", curr host: " << curr_url_host
+ << ", in_page_navigation: " << in_page_navigation;
+
+ if (in_page_navigation)
+ return;
+
+ // Unregister all sources if the frame actually navigated.
+ RemoveAllListeners();
+}
+
+void PresentationServiceImpl::RenderFrameDeleted(
+ content::RenderFrameHost* render_frame_host) {
+ VLOG(2) << "PresentationServiceImpl::RenderFrameDeleted";
+ if (render_frame_host_ != render_frame_host)
+ return;
+
+ // RenderFrameDeleted means this object is getting deleted soon.
+ RemoveAllListeners();
+}
+
+void PresentationServiceImpl::RemoveAllListeners() {
+ VLOG(2) << "PresentationServiceImpl::RemoveAllListeners";
+ if (!delegate_)
+ return;
+
+ delegate_->RemoveAllScreenAvailabilityListeners(
+ render_frame_host_->GetProcess()->GetID(),
+ render_frame_host_->GetRoutingID());
+
+ availability_contexts_.clear();
+}
+
+void PresentationServiceImpl::OnDelegateDestroyed() {
+ VLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed";
+ delegate_ = nullptr;
+}
+
+PresentationServiceImpl::ScreenAvailabilityContext::ScreenAvailabilityContext(
+ const std::string& presentation_url)
+ : presentation_url_(presentation_url) {
+}
+
+PresentationServiceImpl::ScreenAvailabilityContext::
+~ScreenAvailabilityContext() {
+}
+
+void PresentationServiceImpl::ScreenAvailabilityContext::CallbackReceived(
+ const ScreenAvailabilityMojoCallback& callback) {
+ // NOTE: This will overwrite previously registered callback if any.
+ if (!available_ptr_) {
+ // No results yet, store callback for later invocation.
+ callback_ptr_.reset(new ScreenAvailabilityMojoCallback(callback));
+ } else {
+ // Run callback now, reset result.
+ // There shouldn't be any callbacks stored in this scenario.
+ DCHECK(!callback_ptr_);
+ callback.Run(*available_ptr_);
+ Reset();
+ }
+}
+
+void PresentationServiceImpl::ScreenAvailabilityContext::Reset() {
+ callback_ptr_.reset();
+ available_ptr_.reset();
+}
+
+std::string PresentationServiceImpl::ScreenAvailabilityContext
+ ::GetPresentationUrl() const {
+ return presentation_url_;
+}
+
+void PresentationServiceImpl::ScreenAvailabilityContext
+ ::OnScreenAvailabilityChanged(bool available) {
+ if (!callback_ptr_) {
+ // No callback, stash the result for now.
+ available_ptr_.reset(new bool(available));
+ } else {
+ // Invoke callback and erase it.
+ // There shouldn't be any result stored in this scenario.
+ DCHECK(!available_ptr_);
+ callback_ptr_->Run(available);
+ Reset();
+ }
+}
+
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698