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_MEDIA_ROUTER_H_
6 #define CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_H_
11 #include "base/callback.h"
12 #include "base/memory/scoped_vector.h"
13 #include "chrome/browser/media/router/issue.h"
14 #include "chrome/browser/media/router/media_route.h"
15 #include "chrome/browser/media/router/media_sink.h"
16 #include "chrome/browser/media/router/media_source.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "content/public/browser/presentation_session_message.h"
20 namespace media_router
{
23 class MediaRoutesObserver
;
24 class MediaSinksObserver
;
25 class PresentationSessionMessagesObserver
;
27 // Type of callback used in |CreateRoute()| and |JoinRoute()|. Callback is
28 // invoked when the route request either succeeded or failed.
30 // |route|: The route created or joined.
32 // The presentation ID of the route created or joined. In the case of
33 // |CreateRoute()|, the ID is generated by MediaRouter and is guaranteed to
35 // |error|: Empty string.
38 // |presentation_id|: Empty string.
39 // |error|: Non-empty string describing the error.
40 using MediaRouteResponseCallback
=
41 base::Callback
<void(const MediaRoute
* route
,
42 const std::string
& presentation_id
,
43 const std::string
& error
)>;
45 // Used in cases where a tab ID is not applicable in CreateRoute/JoinRoute.
46 const int kInvalidTabId
= -1;
48 // An interface for handling resources related to media routing.
49 // Responsible for registering observers for receiving sink availability
50 // updates, handling route requests/responses, and operating on routes (e.g.
51 // posting messages or closing).
52 class MediaRouter
: public KeyedService
{
54 using PresentationSessionMessageCallback
= base::Callback
<void(
55 scoped_ptr
<ScopedVector
<content::PresentationSessionMessage
>>)>;
56 using SendRouteMessageCallback
= base::Callback
<void(bool sent
)>;
58 ~MediaRouter() override
= default;
60 // Creates a media route from |source_id| to |sink_id|.
61 // |origin| is the URL of requestor's page.
62 // |tab_id| is the ID of the tab in which the request was made.
63 // |origin| and |tab_id| are used for enforcing same-origin and/or same-tab
64 // scope for JoinRoute() requests. (e.g., if enforced, the page
65 // requesting JoinRoute() must have the same origin as the page that requested
67 // The caller may pass in|kInvalidTabId| if tab is not applicable.
68 // Each callback in |callbacks| is invoked with a response indicating
69 // success or failure, in the order they are listed.
70 virtual void CreateRoute(
71 const MediaSource::Id
& source_id
,
72 const MediaSink::Id
& sink_id
,
75 const std::vector
<MediaRouteResponseCallback
>& callbacks
) = 0;
77 // Joins an existing route identified by |presentation_id|.
78 // |source|: The source to route to the existing route.
79 // |presentation_id|: Presentation ID of the existing route.
80 // |origin|, |tab_id|: Origin and tab of the join route request. Used for
81 // validation when enforcing same-origin and/or same-tab scope.
82 // (See CreateRoute documentation).
83 // Each callback in |callbacks| is invoked with a response indicating
84 // success or failure, in the order they are listed.
85 virtual void JoinRoute(
86 const MediaSource::Id
& source
,
87 const std::string
& presentation_id
,
90 const std::vector
<MediaRouteResponseCallback
>& callbacks
) = 0;
92 // Closes the media route specified by |route_id|.
93 virtual void CloseRoute(const MediaRoute::Id
& route_id
) = 0;
95 // Posts |message| to a MediaSink connected via MediaRoute with |route_id|.
96 virtual void SendRouteMessage(const MediaRoute::Id
& route_id
,
97 const std::string
& message
,
98 const SendRouteMessageCallback
& callback
) = 0;
100 // Sends |data| to a MediaSink connected via MediaRoute with |route_id|.
101 // This is called for Blob / ArrayBuffer / ArrayBufferView types.
102 virtual void SendRouteBinaryMessage(
103 const MediaRoute::Id
& route_id
,
104 scoped_ptr
<std::vector
<uint8
>> data
,
105 const SendRouteMessageCallback
& callback
) = 0;
107 // Clears the issue with the id |issue_id|.
108 virtual void ClearIssue(const Issue::Id
& issue_id
) = 0;
110 // Indicates that a presentation session has detached from the underlying
111 // MediaRoute |route_id| (due to navigation, garbage collection, etc.)
112 virtual void OnPresentationSessionDetached(
113 const MediaRoute::Id
& route_id
) = 0;
116 friend class IssuesObserver
;
117 friend class MediaSinksObserver
;
118 friend class MediaRoutesObserver
;
119 friend class PresentationSessionMessagesObserver
;
121 // The following functions are called by friend Observer classes above.
123 // Registers |observer| with this MediaRouter. |observer| specifies a media
124 // source and will receive updates with media sinks that are compatible with
125 // that source. The initial update may happen synchronously.
126 // NOTE: This class does not assume ownership of |observer|. Callers must
127 // manage |observer| and make sure |UnregisterObserver()| is called
128 // before the observer is destroyed.
129 // It is invalid to register the same observer more than once and will result
130 // in undefined behavior.
131 // If the MRPM Host is not available, the registration request will fail
133 virtual void RegisterMediaSinksObserver(MediaSinksObserver
* observer
) = 0;
135 // Removes a previously added MediaSinksObserver. |observer| will stop
136 // receiving further updates.
137 virtual void UnregisterMediaSinksObserver(MediaSinksObserver
* observer
) = 0;
139 // Adds a MediaRoutesObserver to listen for updates on MediaRoutes.
140 // The initial update may happen synchronously.
141 // MediaRouter does not own |observer|. |UnregisterMediaRoutesObserver| should
142 // be called before |observer| is destroyed.
143 // It is invalid to register the same observer more than once and will result
144 // in undefined behavior.
145 virtual void RegisterMediaRoutesObserver(MediaRoutesObserver
* observer
) = 0;
147 // Removes a previously added MediaRoutesObserver. |observer| will stop
148 // receiving further updates.
149 virtual void UnregisterMediaRoutesObserver(MediaRoutesObserver
* observer
) = 0;
151 // Adds the IssuesObserver |observer|.
152 // It is invalid to register the same observer more than once and will result
153 // in undefined behavior.
154 virtual void RegisterIssuesObserver(IssuesObserver
* observer
) = 0;
156 // Removes the IssuesObserver |observer|.
157 virtual void UnregisterIssuesObserver(IssuesObserver
* observer
) = 0;
159 // Registers |observer| with this MediaRouter. |observer| specifies a media
160 // route corresponding to a presentation and will receive messages from the
161 // MediaSink connected to the route. Note that MediaRouter does not own
162 // |observer|. |observer| should be unregistered before it is destroyed.
163 // Registering the same observer more than once will result in undefined
165 virtual void RegisterPresentationSessionMessagesObserver(
166 PresentationSessionMessagesObserver
* observer
) = 0;
168 // Unregisters a previously registered PresentationSessionMessagesObserver.
169 // |observer| will stop receiving further updates.
170 virtual void UnregisterPresentationSessionMessagesObserver(
171 PresentationSessionMessagesObserver
* observer
) = 0;
174 } // namespace media_router
176 #endif // CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_H_