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_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_
6 #define CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/threading/thread_checker.h"
16 #include "chrome/browser/media/router/media_routes_observer.h"
17 #include "chrome/browser/media/router/media_sink.h"
18 #include "chrome/browser/media/router/media_source.h"
19 #include "chrome/browser/ui/webui/media_router/media_cast_mode.h"
20 #include "chrome/browser/ui/webui/media_router/media_sink_with_cast_modes.h"
22 namespace media_router
{
25 class MediaSinksObserver
;
26 struct RoutesQueryResult
;
27 struct SinksQueryResult
;
29 // The Media Router dialog allows the user to initiate casting using one of
30 // several actions (each represented by a cast mode). Each cast mode is
31 // associated with a media source. This class allows the dialog to receive
32 // lists of MediaSinks compatible with the cast modes available through the
37 // QueryResultManager::Observer* observer = ...;
38 // QueryResultManager result_manager(router);
39 // result_manager.AddObserver(observer);
40 // result_manager.StartSinksQuery(MediaCastMode::DEFAULT,
41 // MediaSourceForPresentationUrl("http://google.com"));
42 // result_manager.StartSinksQuery(MediaCastMode::TAB_MIRROR,
43 // MediaSourceForTab(123));
45 // [Updates will be received by observer via OnResultsUpdated()]
47 // [When info on MediaSource is needed, i.e. when requesting route for a mode]
48 // CastModeSet cast_modes;
49 // result_manager.GetSupportedCastModes(&cast_modes);
50 // [Logic to select a MediaCastMode from the set]
51 // MediaSource source = result_manager.GetSourceForCastMode(
52 // MediaCastMode::TAB_MIRROR);
53 // if (!source.Empty()) {
57 // Not thread-safe. Must be used on a single thread.
58 class QueryResultManager
{
62 virtual ~Observer() {}
64 // Updated results have been received.
65 // |sinks|: List of sinks and the cast modes they are compatible with.
66 virtual void OnResultsUpdated(
67 const std::vector
<MediaSinkWithCastModes
>& sinks
) = 0;
70 explicit QueryResultManager(MediaRouter
* media_router
);
71 ~QueryResultManager();
73 // Adds/removes an observer that is notified with query results.
74 void AddObserver(Observer
* observer
);
75 void RemoveObserver(Observer
* observer
);
77 // Requests a list of MediaSinks compatible with |source| for |cast_mode|.
78 // Results are sent to all observers registered with AddObserver().
80 // May start a new query in the Media Router for the registered source if
81 // there is no existing query for it. If there is an existing query for
82 // |cast_mode|, it is stopped.
84 // If |source| is empty, no new queries are begun.
85 void StartSinksQuery(MediaCastMode cast_mode
, const MediaSource
& source
);
87 // Stops notifying observers for |cast_mode|.
88 void StopSinksQuery(MediaCastMode cast_mode
);
90 // Gets the set of cast modes that are being actively queried. |cast_mode_set|
92 void GetSupportedCastModes(CastModeSet
* cast_modes
) const;
94 // Returns the MediaSource registered for |cast_mode|. Returns an empty
95 // MediaSource if there is none.
96 MediaSource
GetSourceForCastMode(MediaCastMode cast_mode
) const;
99 class CastModeMediaSinksObserver
;
101 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest
, Observers
);
102 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest
, StartRoutesDiscovery
);
103 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest
, MultipleQueries
);
105 // Sets the media source for |cast_mode|.
106 void SetSourceForCastMode(MediaCastMode cast_mode
, const MediaSource
& source
);
108 // Stops and destroys the MediaSinksObserver for |cast_mode|.
109 void RemoveObserverForCastMode(MediaCastMode cast_mode
);
111 // Returns true if the |entry|'s sink is compatible with at least one cast
113 bool IsValid(const MediaSinkWithCastModes
& entry
) const;
115 // Modifies the current set of results with |result| associated with
117 void UpdateWithSinksQueryResult(MediaCastMode cast_mode
,
118 const std::vector
<MediaSink
>& result
);
120 // Notifies observers that results have been updated.
121 void NotifyOnResultsUpdated();
123 // MediaSinksObservers that listens for compatible MediaSink updates.
124 // Each observer is associated with a MediaCastMode. Results received by
125 // observers are propagated back to this class.
126 // TODO(mfoltz): Remove linked_ptr when there is a ScopedPtrMap available.
127 std::map
<MediaCastMode
, linked_ptr
<MediaSinksObserver
>> sinks_observers_
;
129 // Holds registrations of MediaSources for cast modes.
130 std::map
<MediaCastMode
, MediaSource
> cast_mode_sources_
;
132 // Holds all known sinks and their associated cast modes.
133 std::map
<MediaSink::Id
, MediaSinkWithCastModes
> all_sinks_
;
135 // Registered observers.
136 base::ObserverList
<Observer
> observers_
;
138 // Not owned by this object.
139 MediaRouter
* router_
;
141 base::ThreadChecker thread_checker_
;
143 DISALLOW_COPY_AND_ASSIGN(QueryResultManager
);
146 } // namespace media_router
148 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_