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 #ifndef CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | |
6 #define CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <vector> | |
11 | |
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" | |
21 | |
22 namespace media_router { | |
23 | |
24 class CastModeMediaSinksObserver; | |
25 class MediaRouter; | |
26 class MediaSinksObserver; | |
27 struct RoutesQueryResult; | |
28 struct SinksQueryResult; | |
29 | |
30 // The Media Router dialog allows the user to initiate casting using one of | |
31 // several actions (each represented by a cast mode). Each cast mode is | |
32 // associated with a media source. This class allows the dialog to receive | |
33 // lists of MediaSinks compatible with the cast modes available through the | |
34 // dialog. | |
35 // | |
36 // Typical use: | |
37 // | |
38 // QueryResultManager::Observer* observer = ...; | |
39 // QueryResultManager result_manager(router); | |
40 // result_manager.AddObserver(observer); | |
41 // result_manager.StartSinksQuery(MediaCastMode::DEFAULT, | |
42 // ForPresentationUrl("http://google.com")); | |
43 // result_manager.StartSinksQuery(MediaCastMode::TAB_MIRROR, | |
44 // ForTabMediaSource(123)); | |
45 // ... | |
46 // [Updates will be received by observer via OnResultsUpdated()] | |
47 // ... | |
48 // [When info on MediaSource is needed, i.e. when requesting route for a mode] | |
49 // CastModeSet cast_modes; | |
50 // result_manager.GetSupportedCastModes(&cast_modes); | |
51 // [Logic to select a MediaCastMode from the set] | |
52 // MediaSource source = result_manager.GetSourceForCastMode( | |
53 // MediaCastMode::TAB_MIRROR); | |
54 // if (!source.Empty()) { | |
55 // ... | |
56 // } | |
57 // | |
58 // Not thread-safe. Must be used on a single thread. | |
59 class QueryResultManager { | |
60 public: | |
61 class Observer { | |
62 public: | |
63 virtual ~Observer() {} | |
64 | |
65 // Updated results have been received. | |
66 // |sinks|: List of sinks and the cast modes they are compatible with. | |
67 virtual void OnResultsUpdated( | |
68 const std::vector<MediaSinkWithCastModes>& sinks) = 0; | |
69 }; | |
70 | |
71 explicit QueryResultManager(MediaRouter* media_router); | |
72 ~QueryResultManager(); | |
73 | |
74 // Adds/removes an observer that is notified with query results. | |
75 void AddObserver(Observer* observer); | |
76 void RemoveObserver(Observer* observer); | |
77 | |
78 // Requests a list of MediaSinks compatible with |source| for |cast_mode|. | |
79 // Results are sent to all observers registered with AddObserver(). | |
80 // | |
81 // May start a new query in the Media Router for the registered source if | |
82 // there is no existing query for it. If there is an existing query for | |
83 // |cast_mode|, it is stopped. | |
84 // | |
85 // If |source| is empty, no new queries are begun. | |
86 void StartSinksQuery(MediaCastMode cast_mode, const MediaSource& source); | |
87 | |
88 // Stops notifying observers for |cast_mode|. | |
89 void StopSinksQuery(MediaCastMode cast_mode); | |
90 | |
91 // Gets the set of cast modes that are being actively queried. |cast_mode_set| | |
92 // should be empty. | |
93 void GetSupportedCastModes(CastModeSet* cast_modes) const; | |
94 | |
95 // Returns the MediaSource registered for |cast_mode|. Returns an empty | |
96 // MediaSource if there is none. | |
97 MediaSource GetSourceForCastMode(MediaCastMode cast_mode) const; | |
98 | |
99 private: | |
100 friend class CastModeMediaSinksObserver; | |
Bernhard Bauer
2015/05/12 09:34:34
You could still keep this class inside of QueryRes
mark a. foltz
2015/05/12 19:05:26
Done.
| |
101 | |
102 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers); | |
103 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery); | |
104 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries); | |
105 | |
106 // Sets the media source for |cast_mode|. | |
107 void SetSourceForCastMode(MediaCastMode cast_mode, const MediaSource& source); | |
108 | |
109 // Stops and destroys the MediaSinksObserver for |cast_mode|. | |
110 void RemoveObserverForCastMode(MediaCastMode cast_mode); | |
111 | |
112 // Returns true if the |entry|'s sink is compatible with at least one cast | |
113 // mode. | |
114 bool IsValid(const MediaSinkWithCastModes& entry) const; | |
115 | |
116 // Modifies the current set of results with |result| associated with | |
117 // |cast_mode|. | |
118 void UpdateWithSinksQueryResult(MediaCastMode cast_mode, | |
119 const std::vector<MediaSink>& result); | |
120 | |
121 // Notifies observers that results have been updated. | |
122 void NotifyOnResultsUpdated(); | |
123 | |
124 // MediaSinksObservers that listens for compatible MediaSink updates. | |
125 // Each observer is associated with a MediaCastMode. Results received by | |
126 // observers are propagated back to this class. | |
127 std::map<MediaCastMode, linked_ptr<MediaSinksObserver>> sinks_observers_; | |
128 | |
129 // Holds registrations of MediaSources for cast modes. | |
130 std::map<MediaCastMode, MediaSource> cast_mode_sources_; | |
131 | |
132 // Holds all known sinks and their associated cast modes. | |
133 std::map<MediaSinkId, MediaSinkWithCastModes> all_sinks_; | |
134 | |
135 // Registered observers. | |
136 ObserverList<Observer> observers_; | |
137 | |
138 // Not owned by this object. | |
139 MediaRouter* router_; | |
140 | |
141 base::ThreadChecker thread_checker_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); | |
144 }; | |
145 | |
146 } // namespace media_router | |
147 | |
148 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | |
OLD | NEW |