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 #ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_
6 #define CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/containers/hash_tables.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/macros.h"
13 #include "base/memory/linked_ptr.h"
14 #include "content/common/content_export.h"
15 #include "content/common/presentation/presentation_service.mojom.h"
16 #include "content/common/presentation/presentation_session.mojom.h"
17 #include "content/public/browser/navigation_details.h"
18 #include "content/public/browser/presentation_screen_availability_listener.h"
19 #include "content/public/browser/presentation_service_delegate.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/common/frame_navigate_params.h"
25 struct FrameNavigateParams
;
26 struct LoadCommittedDetails
;
27 class RenderFrameHost
;
29 // Implementation of Mojo PresentationService.
30 // It handles Presentation API requests coming from Blink / renderer process
31 // and delegates the requests to the embedder's media router via
32 // PresentationServiceDelegate.
33 // An instance of this class tied to a RenderFrameHost and listens to events
34 // related to the RFH via implementing WebContentsObserver.
35 // This class is instantiated on-demand via Mojo's ConnectToRemoteService
36 // from the renderer when the first presentation API request is handled.
37 class CONTENT_EXPORT PresentationServiceImpl
38 : public NON_EXPORTED_BASE(
39 mojo::InterfaceImpl
<presentation::PresentationService
>),
40 public WebContentsObserver
,
41 public PresentationServiceDelegate::Observer
{
43 ~PresentationServiceImpl() override
;
45 // Static factory method to create an instance of PresentationServiceImpl.
46 // |render_frame_host|: The RFH the instance is associated with.
47 // |request|: The instance will be bound to this request. Used for Mojo setup.
48 static void CreateMojoService(
49 RenderFrameHost
* render_frame_host
,
50 mojo::InterfaceRequest
<presentation::PresentationService
> request
);
53 using ScreenAvailabilityMojoCallback
= mojo::Callback
<void(bool)>;
54 using NewSessionMojoCallback
=
55 mojo::Callback
<void(presentation::PresentationSessionInfoPtr
,
56 presentation::PresentationErrorPtr
)>;
58 friend class PresentationServiceImplTest
;
59 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
, RemoveAllListeners
);
60 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
61 DidNavigateThisFrame
);
62 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
63 DidNavigateNotThisFrame
);
64 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
65 ThisRenderFrameDeleted
);
66 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest
,
67 NotThisRenderFrameDeleted
);
69 // |render_frame_host|: The RFH this instance is associated with.
70 // |web_contents|: The WebContents to observe.
71 // |delegate|: Where Presentation API requests are delegated to. Not owned
73 PresentationServiceImpl(
74 RenderFrameHost
* render_frame_host
,
75 WebContents
* web_contents
,
76 PresentationServiceDelegate
* delegate
);
78 // PresentationService implementation.
79 void GetScreenAvailability(
80 const mojo::String
& presentation_url
,
81 const ScreenAvailabilityMojoCallback
& callback
) override
;
82 void OnScreenAvailabilityListenerRemoved() override
;
84 const mojo::String
& presentation_url
,
85 const mojo::String
& presentation_id
,
86 const NewSessionMojoCallback
& callback
) override
;
88 const mojo::String
& presentation_url
,
89 const mojo::String
& presentation_id
,
90 const NewSessionMojoCallback
& callback
) override
;
92 // mojo::InterfaceImpl override.
93 // Note that this is called when the RenderFrameHost is deleted.
94 void OnConnectionError() override
;
96 // WebContentsObserver override.
97 void DidNavigateAnyFrame(
98 content::RenderFrameHost
* render_frame_host
,
99 const content::LoadCommittedDetails
& details
,
100 const content::FrameNavigateParams
& params
) override
;
101 void RenderFrameDeleted(content::RenderFrameHost
* render_frame_host
) override
;
103 // PresentationServiceDelegate::Observer
104 void OnDelegateDestroyed() override
;
106 // Removes all listeners on this instance and informs the
107 // PresentationServiceDelegate of such.
108 void RemoveAllListeners();
110 RenderFrameHost
* render_frame_host_
;
111 PresentationServiceDelegate
* delegate_
;
113 // A helper data class used by PresentationServiceImpl to do bookkeeping
114 // of currently registered screen availability listeners.
115 // An instance of this class is a simple state machine that waits for both
116 // the available bit and the Mojo callback to become available.
117 // Once this happens, the Mojo callback will be invoked with the available
118 // bit, and the state machine will reset.
119 // The available bit is obtained from the embedder's media router.
120 // The callback is obtained from the renderer via PresentationServiceImpl's
121 // GetScreenAvailability().
122 class ScreenAvailabilityContext
123 : public PresentationScreenAvailabilityListener
{
125 explicit ScreenAvailabilityContext(
126 const std::string
& presentation_url
);
127 ~ScreenAvailabilityContext() override
;
129 // If available bit exists, |callback| will be invoked with the bit and
130 // this state machine will reset.
131 // Otherwise |callback| is saved for later use.
132 // |callback|: Callback to the client of PresentationService
133 // (i.e. the renderer) that screen availability has changed, via Mojo.
134 void CallbackReceived(const ScreenAvailabilityMojoCallback
& callback
);
136 // Clears both callback and available bit.
139 // PresentationScreenAvailabilityListener implementation.
140 std::string
GetPresentationUrl() const override
;
141 // If callback exists, it will be invoked with |available| and
142 // this state machine will reset.
143 // Otherwise |available| is saved for later use.
144 // |available|: New screen availability for the presentation URL.
145 void OnScreenAvailabilityChanged(bool available
) override
;
148 std::string presentation_url_
;
149 scoped_ptr
<ScreenAvailabilityMojoCallback
> callback_ptr_
;
150 scoped_ptr
<bool> available_ptr_
;
153 // Map from presentation URL to its ScreenAvailabilityContext state machine.
154 base::hash_map
<std::string
, linked_ptr
<ScreenAvailabilityContext
>>
155 availability_contexts_
;
157 std::string default_presentation_url_
;
159 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl
);
162 } // namespace content
164 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_