| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <unordered_set> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 16 #include "chrome/browser/media/router/media_routes_observer.h" | 17 #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_sink.h" |
| 18 #include "chrome/browser/media/router/media_source.h" | 19 #include "chrome/browser/media/router/media_source.h" |
| 20 #include "chrome/browser/ui/webui/media_router/cast_modes_with_media_sources.h" |
| 19 #include "chrome/browser/ui/webui/media_router/media_cast_mode.h" | 21 #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 #include "chrome/browser/ui/webui/media_router/media_sink_with_cast_modes.h" |
| 21 | 23 |
| 22 namespace media_router { | 24 namespace media_router { |
| 23 | 25 |
| 24 class MediaRouter; | 26 class MediaRouter; |
| 25 class MediaSinksObserver; | 27 class MediaSinksObserver; |
| 26 struct RoutesQueryResult; | 28 struct RoutesQueryResult; |
| 27 struct SinksQueryResult; | 29 struct SinksQueryResult; |
| 28 | 30 |
| 29 // The Media Router dialog allows the user to initiate casting using one of | 31 // 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 | 32 // 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 | 33 // associated with a vector of media sources. This class allows the dialog to |
| 32 // lists of MediaSinks compatible with the cast modes available through the | 34 // receive lists of MediaSinks compatible with the cast modes available through |
| 33 // dialog. | 35 // the dialog. |
| 34 // | 36 // |
| 35 // Typical use: | 37 // Typical use: |
| 36 // | 38 // |
| 37 // GURL origin("https://origin.com"); | 39 // GURL origin("https://origin.com"); |
| 38 // QueryResultManager::Observer* observer = ...; | 40 // QueryResultManager::Observer* observer = ...; |
| 39 // QueryResultManager result_manager(router); | 41 // QueryResultManager result_manager(router); |
| 40 // result_manager.AddObserver(observer); | 42 // result_manager.AddObserver(observer); |
| 41 // result_manager.StartSinksQuery(MediaCastMode::DEFAULT, | 43 // result_manager.SetSourcesForCastMode(MediaCastMode::DEFAULT, |
| 42 // MediaSourceForPresentationUrl("http://google.com"), origin); | 44 // {MediaSourceForPresentationUrl("http://google.com")}, origin); |
| 43 // result_manager.StartSinksQuery(MediaCastMode::TAB_MIRROR, | 45 // result_manager.SetSourcesForCastMode(MediaCastMode::TAB_MIRROR, |
| 44 // MediaSourceForTab(123), origin); | 46 // {MediaSourceForTab(123)}, origin); |
| 45 // ... | 47 // ... |
| 46 // [Updates will be received by observer via OnResultsUpdated()] | 48 // [Updates will be received by observer via OnResultsUpdated()] |
| 47 // ... | 49 // ... |
| 48 // [When info on MediaSource is needed, i.e. when requesting route for a mode] | 50 // [When info on MediaSource is needed, i.e. when requesting route for a mode] |
| 49 // CastModeSet cast_modes = result_manager.GetSupportedCastModes(); | 51 // CastModeSet cast_modes = result_manager.GetSupportedCastModes(); |
| 50 // [Logic to select a MediaCastMode from the set] | 52 // [Logic to select a MediaCastMode from the set] |
| 51 // MediaSource source = result_manager.GetSourceForCastMode( | 53 // std::unique_ptr<MediaSource> source = |
| 52 // MediaCastMode::TAB_MIRROR); | 54 // result_manager.GetSourceForCastModeAndSink( |
| 53 // if (!source.Empty()) { | 55 // MediaCastMode::TAB_MIRROR, sink_of_interest); |
| 56 // if (source) { |
| 54 // ... | 57 // ... |
| 55 // } | 58 // } |
| 56 // | 59 // |
| 57 // Not thread-safe. Must be used on the UI thread. | 60 // Not thread-safe. Must be used on the UI thread. |
| 58 class QueryResultManager { | 61 class QueryResultManager { |
| 59 public: | 62 public: |
| 60 class Observer { | 63 class Observer { |
| 61 public: | 64 public: |
| 62 virtual ~Observer() {} | 65 virtual ~Observer() {} |
| 63 | 66 |
| 64 // Updated results have been received. | 67 // Updated results have been received. |
| 65 // |sinks|: List of sinks and the cast modes they are compatible with. | 68 // |sinks|: List of sinks and the cast modes they are compatible with. |
| 66 virtual void OnResultsUpdated( | 69 virtual void OnResultsUpdated( |
| 67 const std::vector<MediaSinkWithCastModes>& sinks) = 0; | 70 const std::vector<MediaSinkWithCastModes>& sinks) = 0; |
| 68 }; | 71 }; |
| 69 | 72 |
| 70 explicit QueryResultManager(MediaRouter* media_router); | 73 explicit QueryResultManager(MediaRouter* media_router); |
| 71 ~QueryResultManager(); | 74 ~QueryResultManager(); |
| 72 | 75 |
| 73 // Adds/removes an observer that is notified with query results. | 76 // Adds/removes an observer that is notified with query results. |
| 74 void AddObserver(Observer* observer); | 77 void AddObserver(Observer* observer); |
| 75 void RemoveObserver(Observer* observer); | 78 void RemoveObserver(Observer* observer); |
| 76 | 79 |
| 77 // Requests a list of MediaSinks compatible with |source| for |cast_mode| | 80 // Requests a list of MediaSinks compatible with |sources| for |cast_mode| |
| 78 // from |origin|. | 81 // from |origin|. |sources| should be in descending order of priority. |
| 79 // Results are sent to all observers registered with AddObserver(). | 82 // Results are sent to all observers registered with AddObserver(). |
| 80 // | 83 // |
| 81 // May start a new query in the Media Router for the registered source if | 84 // Starts new queries in the Media Router for sources that we have no existing |
| 82 // there is no existing query for it. If there is an existing query for | 85 // queries for, and stops queries for sources no longer associated with any |
| 83 // |cast_mode|, it is stopped. | 86 // cast mode. |
| 84 // | 87 // |
| 85 // If |source| is empty, no new queries are begun. | 88 // If |sources| is empty or contains a source that has already been registered |
| 86 void StartSinksQuery(MediaCastMode cast_mode, | 89 // with another cast mode, no new queries are begun. |
| 87 const MediaSource& source, | 90 void SetSourcesForCastMode(MediaCastMode cast_mode, |
| 88 const GURL& origin); | 91 const std::vector<MediaSource>& sources, |
| 92 const GURL& origin); |
| 89 | 93 |
| 90 // Stops notifying observers for |cast_mode|. | 94 // Stops notifying observers for |cast_mode|, and removes it from the set of |
| 91 void StopSinksQuery(MediaCastMode cast_mode); | 95 // supported cast modes. |
| 96 void RemoveSourcesForCastMode(MediaCastMode cast_mode); |
| 92 | 97 |
| 93 // Gets the set of cast modes that are being actively queried. | 98 // Gets the set of cast modes that are being actively queried. |
| 94 CastModeSet GetSupportedCastModes() const; | 99 CastModeSet GetSupportedCastModes() const; |
| 95 | 100 |
| 96 // Returns the MediaSource registered for |cast_mode|. Returns an empty | 101 // Gets the highest-priority source for the cast mode that is supported by |
| 97 // MediaSource if there is none. | 102 // the sink. Returns an empty unique_ptr if there isn't any. |
| 98 MediaSource GetSourceForCastMode(MediaCastMode cast_mode) const; | 103 std::unique_ptr<MediaSource> GetSourceForCastModeAndSink( |
| 104 MediaCastMode cast_mode, |
| 105 MediaSink::Id sink_id) const; |
| 106 |
| 107 // Returns all the sources registered for |cast_mode|. Returns an empty |
| 108 // vector if there is none. |
| 109 std::vector<MediaSource> GetSourcesForCastMode(MediaCastMode cast_mode) const; |
| 99 | 110 |
| 100 private: | 111 private: |
| 101 class CastModeMediaSinksObserver; | 112 class MediaSourceMediaSinksObserver; |
| 102 | 113 |
| 103 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers); | 114 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers); |
| 104 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery); | 115 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery); |
| 105 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries); | 116 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries); |
| 117 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleUrls); |
| 118 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, AddInvalidSource); |
| 106 | 119 |
| 107 // Sets the media source for |cast_mode|. | 120 // Stops and destroys the MediaSinksObservers for media sources that |
| 108 void SetSourceForCastMode(MediaCastMode cast_mode, const MediaSource& source); | 121 // |cast_mode| used to support, but isn't in |new_sources|, and disassociates |
| 122 // them from sinks. |
| 123 void RemoveOldSourcesForCastMode(MediaCastMode cast_mode, |
| 124 const std::vector<MediaSource>& new_sources); |
| 109 | 125 |
| 110 // Stops and destroys the MediaSinksObserver for |cast_mode|. | 126 // Creates observers and starts queries for each source in |sources| that |
| 111 void RemoveObserverForCastMode(MediaCastMode cast_mode); | 127 // doesn't already have an associated observer. |
| 128 void AddObserversForCastMode(MediaCastMode cast_mode, |
| 129 const std::vector<MediaSource>& sources, |
| 130 const GURL& origin); |
| 112 | 131 |
| 113 // Returns true if the |entry|'s sink is compatible with at least one cast | 132 // Modifies the set of sinks compatible with |cast_mode| and |source| |
| 114 // mode. | 133 // to |new_sinks|. |
| 115 bool IsValid(const MediaSinkWithCastModes& entry) const; | 134 void SetSinksCompatibleWithSource(MediaCastMode cast_mode, |
| 135 const MediaSource& source, |
| 136 const std::vector<MediaSink>& new_sinks); |
| 116 | 137 |
| 117 // Modifies the current set of results with |result| associated with | 138 // Returns the highest-priority source for |cast_mode| contained in |
| 118 // |cast_mode|. | 139 // |sources_for_sink|. Returns an empty unique_ptr if none exists. |
| 119 void UpdateWithSinksQueryResult(MediaCastMode cast_mode, | 140 std::unique_ptr<MediaSource> GetHighestPrioritySourceForCastModeAndSink( |
| 120 const std::vector<MediaSink>& result); | 141 MediaCastMode cast_mode, |
| 142 const CastModesWithMediaSources& sources_for_sink) const; |
| 143 |
| 144 // Returns true if every source in |sources| is either not registered yet, or |
| 145 // associated with |cast_mode|. This check prevents a source from being |
| 146 // associated with two cast modes. |
| 147 bool AreSourcesValidForCastMode( |
| 148 MediaCastMode cast_mode, |
| 149 const std::vector<MediaSource>& sources) const; |
| 121 | 150 |
| 122 // Notifies observers that results have been updated. | 151 // Notifies observers that results have been updated. |
| 123 void NotifyOnResultsUpdated(); | 152 void NotifyOnResultsUpdated(); |
| 124 | 153 |
| 125 // MediaSinksObservers that listens for compatible MediaSink updates. | 154 // MediaSinksObservers that listen for compatible MediaSink updates. |
| 126 // Each observer is associated with a MediaCastMode. Results received by | 155 // Each observer is associated with a MediaSource. Results received by |
| 127 // observers are propagated back to this class. | 156 // observers are propagated back to this class. |
| 128 std::map<MediaCastMode, std::unique_ptr<MediaSinksObserver>> sinks_observers_; | 157 std::unordered_map<MediaSource, |
| 158 std::unique_ptr<MediaSinksObserver>, |
| 159 MediaSource::Hash> |
| 160 sinks_observers_; |
| 129 | 161 |
| 130 // Holds registrations of MediaSources for cast modes. | 162 // Holds registrations of MediaSources for cast modes. |
| 131 std::map<MediaCastMode, MediaSource> cast_mode_sources_; | 163 std::map<MediaCastMode, std::vector<MediaSource>> cast_mode_sources_; |
| 132 | 164 |
| 133 // Holds all known sinks and their associated cast modes. | 165 // Holds all known sinks along with the cast modes and sources they support. |
| 134 std::map<MediaSink::Id, MediaSinkWithCastModes> all_sinks_; | 166 std::map<MediaSink, CastModesWithMediaSources, MediaSink::Compare> all_sinks_; |
| 135 | 167 |
| 136 // Registered observers. | 168 // Registered observers. |
| 137 base::ObserverList<Observer> observers_; | 169 base::ObserverList<Observer> observers_; |
| 138 | 170 |
| 139 // Not owned by this object. | 171 // Not owned by this object. |
| 140 MediaRouter* const router_; | 172 MediaRouter* const router_; |
| 141 | 173 |
| 142 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); | 174 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); |
| 143 }; | 175 }; |
| 144 | 176 |
| 145 } // namespace media_router | 177 } // namespace media_router |
| 146 | 178 |
| 147 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | 179 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ |
| OLD | NEW |