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 CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_
6 #define CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "chrome/browser/media/router/media_router.h"
18 #include "chrome/browser/media/router/media_source.h"
19 #include "content/public/browser/presentation_service_delegate.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/browser/web_contents_user_data.h"
24 class RenderFrameHost
;
25 class PresentationScreenAvailabilityListener
;
27 struct PresentationSessionInfo
;
28 struct PresentationSessionMessage
;
29 } // namespace content
31 namespace media_router
{
34 class MediaSinksObserver
;
35 class PresentationFrameManager
;
36 class PresentationSessionStateObserver
;
38 // Implementation of PresentationServiceDelegate that interfaces an
39 // instance of WebContents with the Chrome Media Router. It uses the Media
40 // Router to handle presentation API calls forwarded from
41 // PresentationServiceImpl. In addition, it also
42 // provides default presentation URL that is required for creating
43 // browser-initiated sessions.
44 // It is scoped to the lifetime of a WebContents, and is managed by the
45 // associated WebContents.
46 class PresentationServiceDelegateImpl
47 : public content::WebContentsUserData
<PresentationServiceDelegateImpl
>,
48 public content::PresentationServiceDelegate
{
50 // Retrieves the instance of PresentationServiceDelegateImpl that was attached
51 // to the specified WebContents. If no instance was attached, creates one,
52 // and attaches it to the specified WebContents.
53 static PresentationServiceDelegateImpl
* GetOrCreateForWebContents(
54 content::WebContents
* web_contents
);
56 ~PresentationServiceDelegateImpl() override
;
58 // content::PresentationServiceDelegate implementation.
60 int render_process_id
,
62 content::PresentationServiceDelegate::Observer
* observer
) override
;
63 void RemoveObserver(int render_process_id
, int render_frame_id
) override
;
64 bool AddScreenAvailabilityListener(
65 int render_process_id
,
67 content::PresentationScreenAvailabilityListener
* listener
) override
;
68 void RemoveScreenAvailabilityListener(
69 int render_process_id
,
71 content::PresentationScreenAvailabilityListener
* listener
) override
;
72 void Reset(int render_process_id
, int render_frame_id
) override
;
73 void SetDefaultPresentationUrl(
74 int render_process_id
,
76 const std::string
& default_presentation_url
) override
;
77 void StartSession(int render_process_id
,
79 const std::string
& presentation_url
,
80 const PresentationSessionSuccessCallback
& success_cb
,
81 const PresentationSessionErrorCallback
& error_cb
) override
;
82 void JoinSession(int render_process_id
,
84 const std::string
& presentation_url
,
85 const std::string
& presentation_id
,
86 const PresentationSessionSuccessCallback
& success_cb
,
87 const PresentationSessionErrorCallback
& error_cb
) override
;
88 void CloseSession(int render_process_id
,
90 const std::string
& presentation_id
) override
;
91 void ListenForSessionMessages(
92 int render_process_id
,
94 const content::PresentationSessionInfo
& session
,
95 const content::PresentationSessionMessageCallback
& message_cb
) override
;
96 void SendMessage(int render_process_id
,
98 const content::PresentationSessionInfo
& session
,
99 scoped_ptr
<content::PresentationSessionMessage
> message
,
100 const SendMessageCallback
& send_message_cb
) override
;
101 void ListenForSessionStateChange(
102 int render_process_id
,
104 const content::SessionStateChangedCallback
& state_changed_cb
) override
;
106 // Callback invoked when there is a route response from CreateRoute/JoinRoute
107 // outside of a Presentation API request. This could be due to
108 // browser action (e.g., browser initiated media router dialog) or
109 // a media route provider (e.g., autojoin).
110 void OnRouteResponse(const MediaRoute
* route
,
111 const std::string
& presentation_id
,
112 const std::string
& error
);
114 // Returns the default MediaSource for this tab if there is one.
115 // Returns an empty MediaSource otherwise.
116 MediaSource
default_source() const { return default_source_
; }
118 content::WebContents
* web_contents() const { return web_contents_
; }
119 const GURL
& default_frame_url() const { return default_frame_url_
; }
121 // Observer interface for listening to default MediaSource changes for the
123 class DefaultMediaSourceObserver
{
125 virtual ~DefaultMediaSourceObserver() {}
127 // Called when default media source for the corresponding WebContents has
129 // |source|: New default MediaSource, or empty if default was removed.
130 // |frame_url|: URL of the frame that contains the default media
131 // source, or empty if there is no default media source.
132 virtual void OnDefaultMediaSourceChanged(const MediaSource
& source
,
133 const GURL
& frame_url
) = 0;
136 // Adds / removes an observer for listening to default MediaSource changes.
137 void AddDefaultMediaSourceObserver(DefaultMediaSourceObserver
* observer
);
138 void RemoveDefaultMediaSourceObserver(DefaultMediaSourceObserver
* observer
);
140 void SetMediaRouterForTest(MediaRouter
* router
);
141 bool HasScreenAvailabilityListenerForTest(
142 int render_process_id
,
144 const MediaSource::Id
& source_id
) const;
146 base::WeakPtr
<PresentationServiceDelegateImpl
> GetWeakPtr();
149 explicit PresentationServiceDelegateImpl(content::WebContents
* web_contents
);
150 friend class content::WebContentsUserData
<PresentationServiceDelegateImpl
>;
152 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest
,
155 // Returns |listener|'s presentation URL as a MediaSource. If |listener| does
156 // not have a persentation URL, returns the tab mirroring MediaSource.
157 MediaSource
GetMediaSourceFromListener(
158 content::PresentationScreenAvailabilityListener
* listener
);
160 void OnJoinRouteResponse(int render_process_id
,
162 const content::PresentationSessionInfo
& session
,
163 const PresentationSessionSuccessCallback
& success_cb
,
164 const PresentationSessionErrorCallback
& error_cb
,
165 const MediaRoute
* route
,
166 const std::string
& presentation_id
,
167 const std::string
& error_text
);
169 void OnStartSessionSucceeded(
170 int render_process_id
,
172 const PresentationSessionSuccessCallback
& success_cb
,
173 const content::PresentationSessionInfo
& new_session
,
174 const MediaRoute::Id
& route_id
);
176 // Returns |true| if the frame is the main frame of |web_contents_|.
177 bool IsMainFrame(int render_process_id
, int render_frame_id
) const;
179 // Updates tab-level default MediaSource and/or default frame URL. If either
180 // changed, notify the observers.
181 void UpdateDefaultMediaSourceAndNotifyObservers(
182 const MediaSource
& new_default_source
,
183 const GURL
& new_default_frame_url
);
185 // Default MediaSource for the tab associated with this instance.
186 MediaSource default_source_
;
187 // URL of the frame that contains the default MediaSource.
188 GURL default_frame_url_
;
190 // References to the observers listening for changes to default media source.
192 DefaultMediaSourceObserver
> default_media_source_observers_
;
194 // References to the WebContents that owns this instance, and associated
195 // browser profile's MediaRouter instance.
196 content::WebContents
* web_contents_
;
197 MediaRouter
* router_
;
199 scoped_ptr
<PresentationFrameManager
> frame_manager_
;
201 base::WeakPtrFactory
<PresentationServiceDelegateImpl
> weak_factory_
;
203 DISALLOW_COPY_AND_ASSIGN(PresentationServiceDelegateImpl
);
206 } // namespace media_router
208 #endif // CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_