1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/presentation/presentation_dispatcher.h"
7 #include "content/common/presentation/presentation_service.mojom.h"
8 #include "content/public/common/service_registry.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentationController.h"
14 PresentationDispatcher::PresentationDispatcher(RenderFrame
* render_frame
)
15 : RenderFrameObserver(render_frame
),
16 controller_(nullptr) {
19 PresentationDispatcher::~PresentationDispatcher() {
20 // Controller should be destroyed before the dispatcher when frame is
25 void PresentationDispatcher::setController(
26 blink::WebPresentationController
* controller
) {
27 // There shouldn't be any swapping from one non-null controller to another.
28 DCHECK(controller
!= controller_
&& (!controller
|| !controller_
));
29 controller_
= controller
;
30 // The controller is set to null when the frame is about to be detached.
31 // Nothing is listening for screen availability anymore but the Mojo service
32 // will know about the frame being detached anyway.
35 void PresentationDispatcher::updateAvailableChangeWatched(bool watched
) {
36 ConnectToPresentationServiceIfNeeded();
38 presentation_service_
->GetScreenAvailability(
40 base::Bind(&PresentationDispatcher::OnScreenAvailabilityChanged
,
41 base::Unretained(this)));
43 presentation_service_
->OnScreenAvailabilityListenerRemoved();
47 void PresentationDispatcher::OnScreenAvailabilityChanged(bool available
) {
51 // Reset the callback to get the next event.
52 updateAvailableChangeWatched(controller_
->isAvailableChangeWatched());
54 controller_
->didChangeAvailability(available
);
57 void PresentationDispatcher::ConnectToPresentationServiceIfNeeded() {
58 if (presentation_service_
.get())
61 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
62 &presentation_service_
);
65 } // namespace content