Chromium Code Reviews| 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" |
| 19 #include "chrome/browser/ui/webui/media_router/media_cast_mode.h" | 20 #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 #include "chrome/browser/ui/webui/media_router/media_sink_with_cast_modes.h" |
| 21 | 22 |
| 22 namespace media_router { | 23 namespace media_router { |
| 23 | 24 |
| 24 class MediaRouter; | 25 class MediaRouter; |
| 25 class MediaSinksObserver; | 26 class MediaSinksObserver; |
| 26 struct RoutesQueryResult; | 27 struct RoutesQueryResult; |
| 27 struct SinksQueryResult; | 28 struct SinksQueryResult; |
| 28 | 29 |
| 29 // The Media Router dialog allows the user to initiate casting using one of | 30 // 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 // 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 // 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 | 33 // receive lists of MediaSinks compatible with the cast modes available through |
| 33 // dialog. | 34 // the dialog. |
| 34 // | 35 // |
| 35 // Typical use: | 36 // Typical use: |
| 36 // | 37 // |
| 37 // GURL origin("https://origin.com"); | 38 // GURL origin("https://origin.com"); |
| 38 // QueryResultManager::Observer* observer = ...; | 39 // QueryResultManager::Observer* observer = ...; |
| 39 // QueryResultManager result_manager(router); | 40 // QueryResultManager result_manager(router); |
| 40 // result_manager.AddObserver(observer); | 41 // result_manager.AddObserver(observer); |
| 41 // result_manager.StartSinksQuery(MediaCastMode::DEFAULT, | 42 // result_manager.StartSinksQuery(MediaCastMode::DEFAULT, |
| 42 // MediaSourceForPresentationUrl("http://google.com"), origin); | 43 // {MediaSourceForPresentationUrl("http://google.com")}, origin); |
| 43 // result_manager.StartSinksQuery(MediaCastMode::TAB_MIRROR, | 44 // result_manager.StartSinksQuery(MediaCastMode::TAB_MIRROR, |
| 44 // MediaSourceForTab(123), origin); | 45 // {MediaSourceForTab(123)}, origin); |
| 45 // ... | 46 // ... |
| 46 // [Updates will be received by observer via OnResultsUpdated()] | 47 // [Updates will be received by observer via OnResultsUpdated()] |
| 47 // ... | 48 // ... |
| 48 // [When info on MediaSource is needed, i.e. when requesting route for a mode] | 49 // [When info on MediaSource is needed, i.e. when requesting route for a mode] |
| 49 // CastModeSet cast_modes = result_manager.GetSupportedCastModes(); | 50 // CastModeSet cast_modes = result_manager.GetSupportedCastModes(); |
| 50 // [Logic to select a MediaCastMode from the set] | 51 // [Logic to select a MediaCastMode from the set] |
| 51 // MediaSource source = result_manager.GetSourceForCastMode( | 52 // MediaSource source = result_manager.GetSourceForCastModeAndSink( |
| 52 // MediaCastMode::TAB_MIRROR); | 53 // MediaCastMode::TAB_MIRROR, sink_of_interest); |
| 53 // if (!source.Empty()) { | 54 // if (!source.Empty()) { |
| 54 // ... | 55 // ... |
| 55 // } | 56 // } |
| 56 // | 57 // |
| 57 // Not thread-safe. Must be used on the UI thread. | 58 // Not thread-safe. Must be used on the UI thread. |
| 58 class QueryResultManager { | 59 class QueryResultManager { |
| 59 public: | 60 public: |
| 60 class Observer { | 61 class Observer { |
| 61 public: | 62 public: |
| 62 virtual ~Observer() {} | 63 virtual ~Observer() {} |
| 63 | 64 |
| 64 // Updated results have been received. | 65 // Updated results have been received. |
| 65 // |sinks|: List of sinks and the cast modes they are compatible with. | 66 // |sinks|: List of sinks and the cast modes they are compatible with. |
| 66 virtual void OnResultsUpdated( | 67 virtual void OnResultsUpdated( |
| 67 const std::vector<MediaSinkWithCastModes>& sinks) = 0; | 68 const std::vector<MediaSinkWithCastModes>& sinks) = 0; |
| 68 }; | 69 }; |
| 69 | 70 |
| 70 explicit QueryResultManager(MediaRouter* media_router); | 71 explicit QueryResultManager(MediaRouter* media_router); |
| 71 ~QueryResultManager(); | 72 ~QueryResultManager(); |
| 72 | 73 |
| 73 // Adds/removes an observer that is notified with query results. | 74 // Adds/removes an observer that is notified with query results. |
| 74 void AddObserver(Observer* observer); | 75 void AddObserver(Observer* observer); |
| 75 void RemoveObserver(Observer* observer); | 76 void RemoveObserver(Observer* observer); |
| 76 | 77 |
| 77 // Requests a list of MediaSinks compatible with |source| for |cast_mode| | 78 // Requests a list of MediaSinks compatible with |sources| for |cast_mode| |
| 78 // from |origin|. | 79 // from |origin|. |sources| should be in descending order of priority. |
| 79 // Results are sent to all observers registered with AddObserver(). | 80 // Results are sent to all observers registered with AddObserver(). |
| 80 // | 81 // |
| 81 // May start a new query in the Media Router for the registered source if | 82 // May start new queries in the Media Router for the registered sources if |
| 82 // there is no existing query for it. If there is an existing query for | 83 // there are no existing queries for it. If there are existing queries for |
| 83 // |cast_mode|, it is stopped. | 84 // |cast_mode|, they are stopped. |
| 84 // | 85 // |
| 85 // If |source| is empty, no new queries are begun. | 86 // If |sources| is empty, no new queries are begun. |
| 86 void StartSinksQuery(MediaCastMode cast_mode, | 87 void StartSinksQuery(MediaCastMode cast_mode, |
| 87 const MediaSource& source, | 88 const std::vector<MediaSource>& sources, |
| 88 const GURL& origin); | 89 const GURL& origin); |
| 89 | 90 |
| 90 // Stops notifying observers for |cast_mode|. | 91 // Stops notifying observers for |cast_mode|. |
| 91 void StopSinksQuery(MediaCastMode cast_mode); | 92 void StopSinksQuery(MediaCastMode cast_mode); |
| 92 | 93 |
| 93 // Gets the set of cast modes that are being actively queried. | 94 // Gets the set of cast modes that are being actively queried. |
| 94 CastModeSet GetSupportedCastModes() const; | 95 CastModeSet GetSupportedCastModes() const; |
| 95 | 96 |
| 96 // Returns the MediaSource registered for |cast_mode|. Returns an empty | 97 // Gets the highest-priority source for the cast mode that is supported by |
| 97 // MediaSource if there is none. | 98 // the sink. Returns an empty MediaSource if there isn't any. |
| 98 MediaSource GetSourceForCastMode(MediaCastMode cast_mode) const; | 99 MediaSource GetSourceForCastModeAndSink( |
| 100 MediaCastMode cast_mode, MediaSink::Id sink_id) const; | |
| 101 | |
| 102 // Returns all the sources registered for |cast_mode|. Returns an empty | |
| 103 // vector if there is none. | |
| 104 std::vector<MediaSource> GetSourcesForCastMode(MediaCastMode cast_mode) const; | |
| 99 | 105 |
| 100 private: | 106 private: |
| 101 class CastModeMediaSinksObserver; | 107 class CastModeMediaSinksObserver; |
| 102 | 108 |
| 103 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers); | 109 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers); |
| 104 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery); | 110 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery); |
| 105 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries); | 111 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries); |
| 112 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleUrls); | |
| 106 | 113 |
| 107 // Sets the media source for |cast_mode|. | 114 // Sets the media sources for |cast_mode|. |
| 108 void SetSourceForCastMode(MediaCastMode cast_mode, const MediaSource& source); | 115 void SetSourcesForCastMode( |
|
mark a. foltz
2016/08/23 20:46:47
Doesn't this depend on the sink? Or is this to ke
takumif
2016/08/23 22:13:09
It's the latter.
| |
| 116 MediaCastMode cast_mode, const std::vector<MediaSource>& source); | |
| 109 | 117 |
| 110 // Stops and destroys the MediaSinksObserver for |cast_mode|. | 118 // Stops and destroys the MediaSinksObservers for |cast_mode|. |
| 111 void RemoveObserverForCastMode(MediaCastMode cast_mode); | 119 void RemoveObserversForCastMode(MediaCastMode cast_mode); |
| 112 | 120 |
| 113 // Returns true if the |entry|'s sink is compatible with at least one cast | 121 // Set all the sinks to not support the cast mode. |
| 114 // mode. | 122 void ResetSinkCompatibilityForCastMode(MediaCastMode cast_mode); |
| 115 bool IsValid(const MediaSinkWithCastModes& entry) const; | |
| 116 | 123 |
| 117 // Modifies the current set of results with |result| associated with | 124 // Modifies the current set of results with |result| associated with |
| 118 // |cast_mode|. | 125 // |cast_mode| and |source|. |
| 119 void UpdateWithSinksQueryResult(MediaCastMode cast_mode, | 126 void UpdateWithSinksQueryResult(MediaCastMode cast_mode, |
| 120 const std::vector<MediaSink>& result); | 127 const MediaSource source, |
| 128 const std::vector<MediaSink>& result_sinks); | |
| 121 | 129 |
| 122 // Notifies observers that results have been updated. | 130 // Notifies observers that results have been updated. |
| 123 void NotifyOnResultsUpdated(); | 131 void NotifyOnResultsUpdated(); |
| 124 | 132 |
| 133 // Returns the first source on the vector supported by the sink. Returns an | |
| 134 // empty |MediaSource| if none exists. | |
| 135 MediaSource GetFirstSourceSupportedBySink( | |
|
mark a. foltz
2016/08/23 20:46:47
Doesn't this depend on the cast mode?
Also, this
| |
| 136 std::vector<MediaSource> sources, MediaSink::Id sink_id) const; | |
| 137 | |
| 138 // Returns true if the source is in the set of sources supported by the sink. | |
| 139 bool SinkSupportsSource(MediaSink sink, MediaSource source) const; | |
|
mark a. foltz
2016/08/23 20:46:47
This could be a method on MediaSinkWithCastModes.
| |
| 140 | |
| 141 // Creates a MediaSinkWithCastModes that contains the sink and the cast modes | |
| 142 // it supports. | |
| 143 MediaSinkWithCastModes GetMediaSinkWithCastModes(const MediaSink& sink); | |
|
mark a. foltz
2016/08/23 20:46:47
return const ref
| |
| 144 | |
| 125 // MediaSinksObservers that listens for compatible MediaSink updates. | 145 // MediaSinksObservers that listens for compatible MediaSink updates. |
| 126 // Each observer is associated with a MediaCastMode. Results received by | 146 // Each observer is associated with a MediaCastMode. Results received by |
| 127 // observers are propagated back to this class. | 147 // observers are propagated back to this class. |
| 128 std::map<MediaCastMode, std::unique_ptr<MediaSinksObserver>> sinks_observers_; | 148 std::map<MediaCastMode, std::vector<std::unique_ptr<MediaSinksObserver>>> |
| 149 sinks_observers_; | |
| 129 | 150 |
| 130 // Holds registrations of MediaSources for cast modes. | 151 // Holds registrations of MediaSources for cast modes. |
| 131 std::map<MediaCastMode, MediaSource> cast_mode_sources_; | 152 std::map<MediaCastMode, std::vector<MediaSource>> cast_mode_sources_; |
| 132 | 153 |
| 133 // Holds all known sinks and their associated cast modes. | 154 // Holds all known sinks along with the cast modes and sources they support. |
| 134 std::map<MediaSink::Id, MediaSinkWithCastModes> all_sinks_; | 155 std::map<MediaSink, std::map<MediaCastMode, |
|
mark a. foltz
2016/08/23 20:46:47
These nested data structures are really hard to un
takumif
2016/08/23 22:13:09
This could be a map from sink to unordered set of
| |
| 156 std::unordered_set<MediaSource::Id>>, MediaSink::Compare> all_sinks_; | |
| 135 | 157 |
| 136 // Registered observers. | 158 // Registered observers. |
| 137 base::ObserverList<Observer> observers_; | 159 base::ObserverList<Observer> observers_; |
| 138 | 160 |
| 139 // Not owned by this object. | 161 // Not owned by this object. |
| 140 MediaRouter* const router_; | 162 MediaRouter* const router_; |
| 141 | 163 |
| 142 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); | 164 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); |
| 143 }; | 165 }; |
| 144 | 166 |
| 145 } // namespace media_router | 167 } // namespace media_router |
| 146 | 168 |
| 147 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | 169 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ |
| OLD | NEW |