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 // 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 that no longer exist. |
|
mark a. foltz
2016/09/09 22:22:27
Maybe:
stops queries for sources no longer associ
takumif
2016/09/13 03:48:22
Done.
| |
| 83 // |cast_mode|, it is stopped. | |
| 84 // | 86 // |
| 85 // If |source| is empty, no new queries are begun. | 87 // If |sources| is empty, no new queries are begun. |
| 86 void StartSinksQuery(MediaCastMode cast_mode, | 88 void StartSinksQuery(MediaCastMode cast_mode, |
|
mark a. foltz
2016/09/09 22:22:27
IIUC, the set of sources and set of cast modes are
takumif
2016/09/13 03:48:22
Added a method AreSourcesValidForCastMode() for ch
| |
| 87 const MediaSource& source, | 89 const std::vector<MediaSource>& sources, |
| 88 const GURL& origin); | 90 const GURL& origin); |
| 89 | 91 |
| 90 // Stops notifying observers for |cast_mode|. | 92 // Stops notifying observers for |cast_mode|, and removes it from the set of |
| 93 // supported cast modes. | |
| 91 void StopSinksQuery(MediaCastMode cast_mode); | 94 void StopSinksQuery(MediaCastMode cast_mode); |
| 92 | 95 |
| 93 // Gets the set of cast modes that are being actively queried. | 96 // Gets the set of cast modes that are being actively queried. |
| 94 CastModeSet GetSupportedCastModes() const; | 97 CastModeSet GetSupportedCastModes() const; |
| 95 | 98 |
| 96 // Returns the MediaSource registered for |cast_mode|. Returns an empty | 99 // Gets the highest-priority source for the cast mode that is supported by |
| 97 // MediaSource if there is none. | 100 // the sink. Returns an empty unique_ptr if there isn't any. |
| 98 MediaSource GetSourceForCastMode(MediaCastMode cast_mode) const; | 101 std::unique_ptr<MediaSource> GetSourceForCastModeAndSink( |
| 102 MediaCastMode cast_mode, MediaSink::Id sink_id) const; | |
| 103 | |
| 104 // Returns all the sources registered for |cast_mode|. Returns an empty | |
| 105 // vector if there is none. | |
| 106 std::vector<MediaSource> GetSourcesForCastMode(MediaCastMode cast_mode) const; | |
| 99 | 107 |
| 100 private: | 108 private: |
| 101 class CastModeMediaSinksObserver; | 109 class CastModeMediaSinksObserver; |
| 102 | 110 |
| 103 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers); | 111 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, Observers); |
| 104 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery); | 112 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, StartRoutesDiscovery); |
| 105 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries); | 113 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleQueries); |
| 114 FRIEND_TEST_ALL_PREFIXES(QueryResultManagerTest, MultipleUrls); | |
| 106 | 115 |
| 107 // Sets the media source for |cast_mode|. | 116 // Stops and destroys the MediaSinksObservers for media sources that |
| 108 void SetSourceForCastMode(MediaCastMode cast_mode, const MediaSource& source); | 117 // |cast_mode| used to support, but isn't in |new_sources|. |
|
mark a. foltz
2016/09/09 22:22:27
What if the sources were registered for a differen
takumif
2016/09/13 03:48:22
Added a method AreSourcesValidForCastMode() for ch
| |
| 118 void RemoveOldSourcesForCastMode( | |
|
mark a. foltz
2016/09/09 22:22:27
RemoveObserversForCastMode sounds better to me, bu
takumif
2016/09/13 03:48:22
Not only do we remove observers, but also disassoc
| |
| 119 MediaCastMode cast_mode, const std::vector<MediaSource>& new_sources); | |
| 109 | 120 |
| 110 // Stops and destroys the MediaSinksObserver for |cast_mode|. | 121 // Creates observers and starts queries for each source in |sources| that |
| 111 void RemoveObserverForCastMode(MediaCastMode cast_mode); | 122 // doesn't already have an associated observer. |
| 123 void AddObserversForCastMode(MediaCastMode cast_mode, | |
| 124 const std::vector<MediaSource>& sources, | |
|
mark a. foltz
2016/09/09 22:22:27
|new_sources| ?
takumif
2016/09/13 03:48:22
Not all sources are necessarily new.
| |
| 125 const GURL& origin); | |
| 112 | 126 |
| 113 // Returns true if the |entry|'s sink is compatible with at least one cast | 127 // Modifies the set of sinks compatible with |cast_mode| and |source| |
| 114 // mode. | 128 // to |new_sinks|. |
| 115 bool IsValid(const MediaSinkWithCastModes& entry) const; | 129 void SetSinksCompatibleWithSource(MediaCastMode cast_mode, |
| 130 const MediaSource source, | |
|
mark a. foltz
2016/09/09 22:22:27
const MediaSource&
takumif
2016/09/13 03:48:22
Done.
| |
| 131 const std::vector<MediaSink>& new_sinks); | |
| 116 | 132 |
| 117 // Modifies the current set of results with |result| associated with | 133 // Returns the highest-priority source for |cast_mode| supported by |sink|. |
| 118 // |cast_mode|. | 134 // Returns an empty unique_ptr if none exists. |
| 119 void UpdateWithSinksQueryResult(MediaCastMode cast_mode, | 135 std::unique_ptr<MediaSource> GetHighestPrioritySourceForCastModeAndSink( |
| 120 const std::vector<MediaSink>& result); | 136 MediaCastMode cast_mode, const MediaSink& sink) const; |
| 121 | 137 |
| 122 // Notifies observers that results have been updated. | 138 // Notifies observers that results have been updated. |
| 123 void NotifyOnResultsUpdated(); | 139 void NotifyOnResultsUpdated(); |
| 124 | 140 |
| 125 // MediaSinksObservers that listens for compatible MediaSink updates. | 141 // Creates a MediaSinkWithCastModes that contains the sink and the cast modes |
| 126 // Each observer is associated with a MediaCastMode. Results received by | 142 // it supports. |
| 143 MediaSinkWithCastModes GetMediaSinkWithCastModes(const MediaSink& sink); | |
| 144 | |
| 145 // MediaSinksObservers that listen for compatible MediaSink updates. | |
| 146 // Each observer is associated with a MediaSource. 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<MediaSource, std::unique_ptr<MediaSinksObserver>, |
|
imcheng
2016/09/12 19:26:29
Is there any reason we need to maintain order for
takumif
2016/09/13 03:48:22
Replaced with unordered_map.
| |
| 149 MediaSource::Compare> 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, CastModesWithMediaSources, MediaSink::Compare> all_sinks_; |
|
imcheng
2016/09/12 19:26:29
Ditto on ordering/Compare struct.
takumif
2016/09/13 03:48:22
Using map rather than unordered_map here allows us
imcheng
2016/09/14 01:51:23
It isn't ideal to add the Compare solely because o
takumif
2016/09/14 18:49:22
Acknowledged.
| |
| 135 | 156 |
| 136 // Registered observers. | 157 // Registered observers. |
| 137 base::ObserverList<Observer> observers_; | 158 base::ObserverList<Observer> observers_; |
| 138 | 159 |
| 139 // Not owned by this object. | 160 // Not owned by this object. |
| 140 MediaRouter* const router_; | 161 MediaRouter* const router_; |
| 141 | 162 |
| 142 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); | 163 DISALLOW_COPY_AND_ASSIGN(QueryResultManager); |
| 143 }; | 164 }; |
| 144 | 165 |
| 145 } // namespace media_router | 166 } // namespace media_router |
| 146 | 167 |
| 147 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ | 168 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_QUERY_RESULT_MANAGER_H_ |
| OLD | NEW |