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 "chrome/browser/media/router/presentation_session_state_observer.h"
7 #include "chrome/browser/media/router/media_router.h"
9 namespace media_router
{
11 PresentationSessionStateObserver::PresentationSessionStateObserver(
12 const content::SessionStateChangedCallback
& state_changed_callback
,
13 const MediaRouteIdToPresentationSessionMapping
* route_id_to_presentation
,
15 : MediaRoutesObserver(router
),
16 state_changed_callback_(state_changed_callback
),
17 route_id_to_presentation_(route_id_to_presentation
) {
18 DCHECK(route_id_to_presentation_
);
21 PresentationSessionStateObserver::~PresentationSessionStateObserver() {
24 void PresentationSessionStateObserver::OnPresentationSessionConnected(
25 const MediaRoute::Id
& route_id
) {
26 if (!ContainsValue(tracked_route_ids_
, route_id
))
27 tracked_route_ids_
.push_back(route_id
);
28 InvokeCallback(route_id
, content::PRESENTATION_SESSION_STATE_CONNECTED
);
31 void PresentationSessionStateObserver::Reset() {
32 tracked_route_ids_
.clear();
33 previous_route_ids_
.clear();
36 void PresentationSessionStateObserver::OnRoutesUpdated(
37 const std::vector
<MediaRoute
>& routes
) {
38 std::vector
<MediaRoute::Id
> current_route_ids_
;
39 current_route_ids_
.reserve(routes
.size());
40 for (const MediaRoute
& route
: routes
)
41 current_route_ids_
.push_back(route
.media_route_id());
43 for (auto it
= tracked_route_ids_
.begin(); it
!= tracked_route_ids_
.end();
45 const MediaRoute::Id
& route_id
= *it
;
46 if (ContainsValue(previous_route_ids_
, route_id
) &&
47 !ContainsValue(current_route_ids_
, route_id
)) {
48 InvokeCallback(route_id
,
49 content::PRESENTATION_SESSION_STATE_DISCONNECTED
);
50 it
= tracked_route_ids_
.erase(it
);
55 previous_route_ids_
.swap(current_route_ids_
);
58 void PresentationSessionStateObserver::InvokeCallback(
59 const MediaRoute::Id
& route_id
,
60 content::PresentationSessionState new_state
) {
61 const content::PresentationSessionInfo
* session_info
=
62 route_id_to_presentation_
->Get(route_id
);
64 state_changed_callback_
.Run(*session_info
, new_state
);
67 } // namespace media_router