OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/media/router/presentation_session_state_observer.h" | |
6 | |
7 #include "chrome/browser/media/router/media_router.h" | |
8 | |
9 namespace media_router { | |
10 | |
11 PresentationSessionStateObserver::PresentationSessionStateObserver( | |
12 const content::SessionStateChangedCallback& state_changed_callback, | |
13 const MediaRouteIdToPresentationSessionMapping* route_id_to_presentation, | |
14 MediaRouter* router) | |
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_); | |
19 } | |
20 | |
21 PresentationSessionStateObserver::~PresentationSessionStateObserver() { | |
22 } | |
23 | |
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_CONNECTION_STATE_CONNECTED); | |
29 } | |
30 | |
31 void PresentationSessionStateObserver::Reset() { | |
32 tracked_route_ids_.clear(); | |
33 previous_route_ids_.clear(); | |
34 } | |
35 | |
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()); | |
42 | |
43 for (auto it = tracked_route_ids_.begin(); it != tracked_route_ids_.end(); | |
44 /*no-op*/) { | |
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, content::PRESENTATION_CONNECTION_STATE_CLOSED); | |
49 it = tracked_route_ids_.erase(it); | |
50 } else { | |
51 ++it; | |
52 } | |
53 } | |
54 previous_route_ids_.swap(current_route_ids_); | |
55 } | |
56 | |
57 void PresentationSessionStateObserver::InvokeCallback( | |
58 const MediaRoute::Id& route_id, | |
59 content::PresentationConnectionState new_state) { | |
60 const content::PresentationSessionInfo* session_info = | |
61 route_id_to_presentation_->Get(route_id); | |
62 if (session_info) | |
63 state_changed_callback_.Run(*session_info, new_state); | |
64 } | |
65 | |
66 } // namespace media_router | |
OLD | NEW |