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" |
| 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.StartSinksQuery(MediaCastMode::DEFAULT, |
| 42 // MediaSourceForPresentationUrl("http://google.com"), origin); | 44 // {MediaSourceForPresentationUrl("http://google.com")}, origin); |
| 43 // result_manager.StartSinksQuery(MediaCastMode::TAB_MIRROR, | 45 // result_manager.StartSinksQuery(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 // const MediaSource* source = result_manager.GetSourceForCastModeAndSink( |
| 52 // MediaCastMode::TAB_MIRROR); | 54 // MediaCastMode::TAB_MIRROR, sink_of_interest); |
| 53 // if (!source.Empty()) { | 55 // if (source) { |
| 54 // ... | 56 // ... |
| 55 // } | 57 // } |
| 56 // | 58 // |
| 57 // Not thread-safe. Must be used on the UI thread. | 59 // Not thread-safe. Must be used on the UI thread. |
| 58 class QueryResultManager { | 60 class QueryResultManager { |
| 59 public: | 61 public: |
| 60 class Observer { | 62 class Observer { |
| 61 public: | 63 public: |
| 62 virtual ~Observer() {} | 64 virtual ~Observer() {} |
| 63 | 65 |
| 64 // Updated results have been received. | 66 // Updated results have been received. |
| 65 // |sinks|: List of sinks and the cast modes they are compatible with. | 67 // |sinks|: List of sinks and the cast modes they are compatible with. |
| 66 virtual void OnResultsUpdated( | 68 virtual void OnResultsUpdated( |
| 67 const std::vector<MediaSinkWithCastModes>& sinks) = 0; | 69 const std::vector<MediaSinkWithCastModes>& sinks) = 0; |
| 68 }; | 70 }; |
| 69 | 71 |
| 70 explicit QueryResultManager(MediaRouter* media_router); | 72 explicit QueryResultManager(MediaRouter* media_router); |
| 71 ~QueryResultManager(); | 73 ~QueryResultManager(); |
| 72 | 74 |
| 73 // Adds/removes an observer that is notified with query results. | 75 // Adds/removes an observer that is notified with query results. |
| 74 void AddObserver(Observer* observer); | 76 void AddObserver(Observer* observer); |
| 75 void RemoveObserver(Observer* observer); | 77 void RemoveObserver(Observer* observer); |
| 76 | 78 |
| 77 // Requests a list of MediaSinks compatible with |source| for |cast_mode| | 79 // Requests a list of MediaSinks compatible with |sources| for |cast_mode| |
| 78 // from |origin|. | 80 // from |origin|. |sources| should be in descending order of priority. |
| 79 // Results are sent to all observers registered with AddObserver(). | 81 // Results are sent to all observers registered with AddObserver(). |
| 80 // | 82 // |
| 81 // May start a new query in the Media Router for the registered source if | 83 // 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 | 84 // queries for, and stops queries for sources that no longer exist. |
| 83 // |cast_mode|, it is 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 a nullptr if there isn't any. |
| 98 MediaSource GetSourceForCastMode(MediaCastMode cast_mode) const; | 99 const 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 // Stops and destroys the MediaSinksObservers for media sources that no |
| 108 void SetSourceForCastMode(MediaCastMode cast_mode, const MediaSource& source); | 115 // longer exist, and creates observers and starts queries for new sources. |
|
mark a. foltz
2016/09/02 23:00:37
The comments seem out of sync with the API.
Maybe
takumif
2016/09/06 21:53:20
I'm not sure if UpdateObserversForCastMode() is ne
| |
| 116 void RemoveOldSourcesForCastMode( | |
| 117 MediaCastMode cast_mode, const std::vector<MediaSource>& new_sources); | |
| 109 | 118 |
| 110 // Stops and destroys the MediaSinksObserver for |cast_mode|. | 119 void RegisterNewObserversForCastMode(MediaCastMode cast_mode, |
| 111 void RemoveObserverForCastMode(MediaCastMode cast_mode); | 120 const std::vector<MediaSource>& sources, |
| 112 | 121 const GURL& origin); |
| 113 // Returns true if the |entry|'s sink is compatible with at least one cast | |
| 114 // mode. | |
| 115 bool IsValid(const MediaSinkWithCastModes& entry) const; | |
| 116 | 122 |
| 117 // Modifies the current set of results with |result| associated with | 123 // Modifies the current set of results with |result| associated with |
| 118 // |cast_mode|. | 124 // |cast_mode| and |source|. |
| 119 void UpdateWithSinksQueryResult(MediaCastMode cast_mode, | 125 void UpdateWithSinksQueryResult(MediaCastMode cast_mode, |
| 120 const std::vector<MediaSink>& result); | 126 const MediaSource source, |
| 127 const std::vector<MediaSink>& result_sinks); | |
|
mark a. foltz
2016/09/02 23:00:37
Or |current_sinks|?
takumif
2016/09/06 21:53:20
I think it's more like |new_sinks|.
| |
| 128 | |
| 129 // Returns the highest-priority source for |cast_mode| supported by |sink|. | |
| 130 // Returns a nullptr if none exists. | |
| 131 const MediaSource* GetHighestPrioritySourceForCastModeAndSink( | |
| 132 MediaCastMode cast_mode, const MediaSink& sink) const; | |
| 121 | 133 |
| 122 // Notifies observers that results have been updated. | 134 // Notifies observers that results have been updated. |
| 123 void NotifyOnResultsUpdated(); | 135 void NotifyOnResultsUpdated(); |
| 124 | 136 |
| 125 // MediaSinksObservers that listens for compatible MediaSink updates. | 137 // Creates a MediaSinkWithCastModes that contains the sink and the cast modes |
| 126 // Each observer is associated with a MediaCastMode. Results received by | 138 // it supports. |
| 139 MediaSinkWithCastModes GetMediaSinkWithCastModes(const MediaSink& sink); | |
| 140 | |
| 141 // MediaSinksObservers that listen for compatible MediaSink updates. | |
| 142 // Each observer is associated with a MediaSource. Results received by | |
| 127 // observers are propagated back to this class. | 143 // observers are propagated back to this class. |
| 128 std::map<MediaCastMode, std::unique_ptr<MediaSinksObserver>> sinks_observers_; | 144 std::map<MediaSource, std::unique_ptr<MediaSinksObserver>, |
| 145 MediaSource::Compare> sinks_observers_; | |
| 129 | 146 |
| 130 // Holds registrations of MediaSources for cast modes. | 147 // Holds registrations of MediaSources for cast modes. |
| 131 std::map<MediaCastMode, MediaSource> cast_mode_sources_; | 148 std::map<MediaCastMode, std::vector<MediaSource>> cast_mode_sources_; |
|
mark a. foltz
2016/09/02 23:00:37
std::multimap?
takumif
2016/09/06 21:53:20
I think it makes sense to keep this as-is, since |
| |
| 132 | 149 |
| 133 // Holds all known sinks and their associated cast modes. | 150 // Holds all known sinks along with the cast modes and sources they support. |
| 134 std::map<MediaSink::Id, MediaSinkWithCastModes> all_sinks_; | 151 std::map<MediaSink, CastModesWithMediaSources, MediaSink::Compare> all_sinks_; |
| 135 | 152 |
| 136 // Registered observers. | 153 // Registered observers. |
| 137 base::ObserverList<Observer> observers_; | 154 base::ObserverList<Observer> observers_; |
| 138 | 155 |
| 139 // Not owned by this object. | 156 // Not owned by this object. |
| 140 MediaRouter* const router_; | 157 MediaRouter* const router_; |
| 141 | 158 |
| 142 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); | 159 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); |
| 143 }; | 160 }; |
| 144 | 161 |
| 145 } // namespace media_router | 162 } // namespace media_router |
| 146 | 163 |
| 147 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | 164 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ |
| OLD | NEW |