Chromium Code Reviews| 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_sinks_observer.h" | |
| 19 #include "chrome/browser/media/router/media_source.h" | |
| 20 #include "chrome/browser/ui/webui/media_router/media_cast_mode.h" | |
| 21 #include "chrome/browser/ui/webui/media_router/media_sink_with_cast_modes.h" | |
| 22 | |
| 23 namespace media_router { | |
| 24 | |
| 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. | |
|
Bernhard Bauer
2015/05/08 08:34:33
Nit: Thread safety (or lack thereof) comes in so m
mark a. foltz
2015/05/12 06:23:15
That's correct. I clarified the comment.
| |
| 59 class QueryResultManager { | |
| 60 public: | |
| 61 class Observer { | |
|
Bernhard Bauer
2015/05/08 08:34:34
Optional: Instead of a single-method Observer inte
mark a. foltz
2015/05/12 06:23:15
Thanks I was not aware of that. It looks like Cal
Bernhard Bauer
2015/05/12 09:34:34
I definitely don't feel strongly. Personally, I ju
mark a. foltz
2015/05/12 19:05:26
Acknowledged.
| |
| 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_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers); | |
| 101 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery); | |
| 102 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries); | |
| 103 | |
| 104 // MediaSinkObserver that propagates results back to |this|. | |
| 105 // An instance of this class is associated with each registered MediaCastMode. | |
| 106 class CastModeMediaSinksObserver : public MediaSinksObserver { | |
|
Bernhard Bauer
2015/05/08 08:34:33
Can you define this class in the .cc file and just
mark a. foltz
2015/05/12 06:23:15
Done.
| |
| 107 public: | |
| 108 CastModeMediaSinksObserver(MediaCastMode cast_mode, | |
| 109 const MediaSource& source, | |
| 110 MediaRouter* router, | |
| 111 QueryResultManager* result_manager); | |
| 112 ~CastModeMediaSinksObserver() override; | |
| 113 | |
| 114 // MediaSinksObserver | |
| 115 void OnSinksReceived(const std::vector<MediaSink>& result) override; | |
| 116 | |
| 117 // Returns the most recent sink IDs that were passed to |OnSinksReceived|. | |
| 118 void GetLatestSinkIds(std::vector<MediaSinkId>* sink_ids) const; | |
| 119 | |
| 120 MediaCastMode cast_mode() const { return cast_mode_; } | |
| 121 | |
| 122 private: | |
| 123 MediaCastMode cast_mode_; | |
| 124 std::vector<MediaSinkId> latest_sink_ids_; | |
| 125 QueryResultManager* result_manager_; | |
| 126 }; | |
| 127 | |
| 128 // Sets the media source for |cast_mode|. | |
| 129 void SetSourceForCastMode(MediaCastMode cast_mode, const MediaSource& source); | |
| 130 | |
| 131 // Stops and destroys the CastModeMediaSinksObserver for |cast_mode|. | |
| 132 void RemoveObserverForCastMode(MediaCastMode cast_mode); | |
| 133 | |
| 134 // Returns true if the |entry|'s sink is compatible with at least one cast | |
| 135 // mode. | |
| 136 bool IsValid(const MediaSinkWithCastModes& entry) const; | |
| 137 | |
| 138 // Modifies the current set of results with |result| associated with | |
| 139 // |cast_mode|. | |
| 140 void UpdateWithSinksQueryResult(MediaCastMode cast_mode, | |
| 141 const std::vector<MediaSink>& result); | |
| 142 | |
| 143 // Notifies observers that results have been updated. | |
| 144 void NotifyOnResultsUpdated(); | |
| 145 | |
| 146 // MediaSinksObservers that listens for compatible MediaSink updates. | |
| 147 // Each observer is associated with a MediaCastMode. Results received by | |
| 148 // observers are propagated back to this class. | |
| 149 std::map<MediaCastMode, linked_ptr<CastModeMediaSinksObserver>> | |
|
Bernhard Bauer
2015/05/08 08:34:33
Can you avoid using linked_ptr? Its use is somwhat
mark a. foltz
2015/05/12 06:23:15
Is there a "scoped" replacement for std::map? I d
Bernhard Bauer
2015/05/12 09:34:34
No, there's only one for hash_map so far (for what
mark a. foltz
2015/05/12 19:05:26
The ScopedPtrHashMap is really a different API fro
| |
| 150 sinks_observers_; | |
| 151 | |
| 152 // Holds registrations of MediaSources for cast modes. | |
| 153 std::map<MediaCastMode, MediaSource> cast_mode_sources_; | |
| 154 | |
| 155 // Holds all known sinks and their associated cast modes. | |
| 156 std::map<MediaSinkId, MediaSinkWithCastModes> all_sinks_; | |
| 157 | |
| 158 // Registered observers. | |
| 159 ObserverList<Observer> observers_; | |
| 160 | |
| 161 // Not owned by this object. | |
| 162 MediaRouter* router_; | |
| 163 | |
| 164 base::ThreadChecker thread_checker_; | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); | |
| 167 }; | |
| 168 | |
| 169 } // namespace media_router | |
| 170 | |
| 171 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | |
| OLD | NEW |