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_SESSION_STATE_OBSERVER_H_
6 #define CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SESSION_STATE_OBSERVER_H_
10 #include "base/gtest_prod_util.h"
11 #include "chrome/browser/media/router/media_route.h"
12 #include "chrome/browser/media/router/media_routes_observer.h"
13 #include "content/public/browser/presentation_service_delegate.h"
15 namespace media_router
{
19 // MediaRoutesObserver implementation that allows tracking state changes from
20 // multiple presentation sessions and notifying the client via
21 // |state_changed_callback|.
22 // Clients provide the callback on construction, and incrementally add route
23 // IDs to track by calling |OnPresentationSessionConnected| as they are created,
24 // which also has the effect of invoking the callback that the session has
26 // When a presentation is disconnected, the callback will be invoked and it
27 // will be removed from the tracked set.
28 // When the routes no longer need to tracked, |Reset| can be called.
29 class PresentationSessionStateObserver
: public MediaRoutesObserver
{
31 PresentationSessionStateObserver(
32 const content::SessionStateChangedCallback
& state_changed_callback
,
33 const MediaRouteIdToPresentationSessionMapping
* route_id_to_presentation
,
35 ~PresentationSessionStateObserver() override
;
37 // Called when a presentation with |route_id| has been created in connected
38 // state and invoke the callback.
39 void OnPresentationSessionConnected(const MediaRoute::Id
& route_id
);
41 // Clears the set of presentations being tracked.
45 FRIEND_TEST_ALL_PREFIXES(PresentationSessionStateObserverTest
,
46 InvokeCallbackWithDisconnected
);
47 FRIEND_TEST_ALL_PREFIXES(PresentationSessionStateObserverTest
, Reset
);
49 // MediaRoutesObserver override
50 void OnRoutesUpdated(const std::vector
<MediaRoute
>& routes
) override
;
52 // Invokes |state_changed_callback_| with PresentationSessionInfo derived from
53 // |route_id| and |new_state|, if |route_id| is valid.
54 void InvokeCallback(const MediaRoute::Id
& route_id
,
55 content::PresentationSessionState new_state
);
57 // Route IDs of presentations that are being tracked for state changes.
58 // It is built by adding entries when a presentation is started,
59 // and removing entries when the presentation is no longer in subsequent
60 // route list updates.
61 std::vector
<MediaRoute::Id
> tracked_route_ids_
;
63 // Stores previous list of routes observed from MediaRouter.
64 // It is compared against the new observed list of routes for disconnected
65 // routes that are in |tracked_route_ids_|.
66 // TODO(imcheng): A more explicit set of MediaRoutesObserver APIs (e.g.,
67 // OnRoutesClosed) would be helpful here. (crbug.com/508751)
68 std::vector
<MediaRoute::Id
> previous_route_ids_
;
70 // Callback to invoke when state change occurs.
71 content::SessionStateChangedCallback state_changed_callback_
;
73 // Note that while it is declared const to prevent this class from modifying
74 // it, its contents can be changed by the PresentationFrame that owns it.
75 const MediaRouteIdToPresentationSessionMapping
* const
76 route_id_to_presentation_
;
78 DISALLOW_COPY_AND_ASSIGN(PresentationSessionStateObserver
);
81 } // namespace media_router
83 #endif // CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SESSION_STATE_OBSERVER_H_