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 #include "chrome/browser/ui/webui/media_router/query_result_manager.h" | 5 #include "chrome/browser/ui/webui/media_router/query_result_manager.h" |
| 6 | 6 |
| 7 #include <utility> | |
| 8 | |
| 7 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
| 8 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 9 #include "chrome/browser/media/router/media_router.h" | 11 #include "chrome/browser/media/router/media_router.h" |
| 10 #include "chrome/browser/media/router/media_sinks_observer.h" | 12 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 11 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 12 | 14 |
| 13 namespace media_router { | 15 namespace media_router { |
| 14 | 16 |
| 15 // MediaSinkObserver that propagates results back to |result_manager|. | 17 // MediaSinkObserver that propagates results back to |result_manager|. |
| 16 // An instance of this class is associated with each registered MediaCastMode. | 18 // An instance of this class is associated with each registered MediaSource. |
| 17 class QueryResultManager::CastModeMediaSinksObserver | 19 class QueryResultManager::CastModeMediaSinksObserver |
| 18 : public MediaSinksObserver { | 20 : public MediaSinksObserver { |
| 19 public: | 21 public: |
| 20 CastModeMediaSinksObserver(MediaCastMode cast_mode, | 22 CastModeMediaSinksObserver(MediaCastMode cast_mode, |
| 21 const MediaSource& source, | 23 const MediaSource& source, |
| 22 const GURL& origin, | 24 const GURL& origin, |
| 23 MediaRouter* router, | 25 MediaRouter* router, |
| 24 QueryResultManager* result_manager) | 26 QueryResultManager* result_manager) |
| 25 : MediaSinksObserver(router, source, origin), | 27 : MediaSinksObserver(router, source, origin), |
| 26 cast_mode_(cast_mode), | 28 cast_mode_(cast_mode), |
| 29 source_(source), | |
| 27 result_manager_(result_manager) { | 30 result_manager_(result_manager) { |
| 28 DCHECK(result_manager); | 31 DCHECK(result_manager); |
| 29 } | 32 } |
| 30 | 33 |
| 31 ~CastModeMediaSinksObserver() override {} | 34 ~CastModeMediaSinksObserver() override {} |
| 32 | 35 |
| 33 // MediaSinksObserver | 36 // MediaSinksObserver |
| 34 void OnSinksReceived(const std::vector<MediaSink>& result) override { | 37 void OnSinksReceived(const std::vector<MediaSink>& result) override { |
| 35 latest_sink_ids_.clear(); | 38 latest_sink_ids_.clear(); |
| 36 for (const MediaSink& sink : result) { | 39 for (const MediaSink& sink : result) { |
| 37 latest_sink_ids_.push_back(sink.id()); | 40 latest_sink_ids_.push_back(sink.id()); |
| 38 } | 41 } |
| 39 result_manager_->UpdateWithSinksQueryResult(cast_mode_, result); | 42 result_manager_->UpdateWithSinksQueryResult(cast_mode_, source_, result); |
| 40 result_manager_->NotifyOnResultsUpdated(); | 43 result_manager_->NotifyOnResultsUpdated(); |
| 41 } | 44 } |
| 42 | 45 |
| 43 // Returns the most recent sink IDs that were passed to |OnSinksReceived|. | 46 // Returns the most recent sink IDs that were passed to |OnSinksReceived|. |
| 44 void GetLatestSinkIds(std::vector<MediaSink::Id>* sink_ids) const { | 47 void GetLatestSinkIds(std::vector<MediaSink::Id>* sink_ids) const { |
| 45 DCHECK(sink_ids); | 48 DCHECK(sink_ids); |
| 46 *sink_ids = latest_sink_ids_; | 49 *sink_ids = latest_sink_ids_; |
| 47 } | 50 } |
| 48 | 51 |
| 49 MediaCastMode cast_mode() const { return cast_mode_; } | 52 MediaCastMode cast_mode() const { return cast_mode_; } |
| 50 | 53 |
| 51 private: | 54 private: |
| 52 const MediaCastMode cast_mode_; | 55 const MediaCastMode cast_mode_; |
|
mark a. foltz
2016/08/23 20:46:47
This assumes that a source is associated with one
| |
| 56 const MediaSource source_; | |
| 53 std::vector<MediaSink::Id> latest_sink_ids_; | 57 std::vector<MediaSink::Id> latest_sink_ids_; |
| 54 QueryResultManager* const result_manager_; | 58 QueryResultManager* const result_manager_; |
| 55 }; | 59 }; |
| 56 | 60 |
| 57 QueryResultManager::QueryResultManager(MediaRouter* router) : router_(router) { | 61 QueryResultManager::QueryResultManager(MediaRouter* router) : router_(router) { |
| 58 DCHECK(router_); | 62 DCHECK(router_); |
| 59 } | 63 } |
| 60 | 64 |
| 61 QueryResultManager::~QueryResultManager() { | 65 QueryResultManager::~QueryResultManager() { |
| 62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 63 } | 67 } |
| 64 | 68 |
| 65 void QueryResultManager::AddObserver(Observer* observer) { | 69 void QueryResultManager::AddObserver(Observer* observer) { |
| 66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 67 DCHECK(observer); | 71 DCHECK(observer); |
| 68 observers_.AddObserver(observer); | 72 observers_.AddObserver(observer); |
| 69 } | 73 } |
| 70 | 74 |
| 71 void QueryResultManager::RemoveObserver(Observer* observer) { | 75 void QueryResultManager::RemoveObserver(Observer* observer) { |
| 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 73 DCHECK(observer); | 77 DCHECK(observer); |
| 74 observers_.RemoveObserver(observer); | 78 observers_.RemoveObserver(observer); |
| 75 } | 79 } |
| 76 | 80 |
| 77 void QueryResultManager::StartSinksQuery(MediaCastMode cast_mode, | 81 void QueryResultManager::StartSinksQuery( |
|
mark a. foltz
2016/08/23 20:46:46
This might be better named UpdateSourcesForCastMod
takumif
2016/08/29 23:05:15
Isn't it nicer to have StartSinksQuery() and StopS
| |
| 78 const MediaSource& source, | 82 MediaCastMode cast_mode, |
| 79 const GURL& origin) { | 83 const std::vector<MediaSource>& sources, |
| 84 const GURL& origin) { | |
| 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 81 if (source.Empty()) { | 86 if (sources.empty()) { |
| 82 LOG(WARNING) << "StartSinksQuery called with empty source for " | 87 LOG(WARNING) << "StartSinksQuery called with empty source for " |
| 83 << cast_mode; | 88 << cast_mode; |
| 84 return; | 89 return; |
| 85 } | 90 } |
| 86 | 91 |
| 87 SetSourceForCastMode(cast_mode, source); | 92 SetSourcesForCastMode(cast_mode, sources); |
| 88 RemoveObserverForCastMode(cast_mode); | 93 RemoveObserversForCastMode(cast_mode); |
|
mark a. foltz
2016/08/23 20:46:46
Shouldn't this be computing the difference between
takumif
2016/08/23 22:13:09
That would be more complex, but would make sense i
| |
| 89 UpdateWithSinksQueryResult(cast_mode, std::vector<MediaSink>()); | 94 ResetSinkCompatibilityForCastMode(cast_mode); |
| 90 | 95 |
| 91 std::unique_ptr<CastModeMediaSinksObserver> observer( | 96 for (const MediaSource& source : sources) { |
| 92 new CastModeMediaSinksObserver(cast_mode, source, origin, router_, this)); | 97 std::unique_ptr<CastModeMediaSinksObserver> observer( |
| 93 observer->Init(); | 98 new CastModeMediaSinksObserver( |
| 94 auto result = | 99 cast_mode, source, origin, router_, this)); |
| 95 sinks_observers_.insert(std::make_pair(cast_mode, std::move(observer))); | 100 observer->Init(); |
| 96 DCHECK(result.second); | 101 sinks_observers_[cast_mode].push_back(std::move(observer)); |
|
mark a. foltz
2016/08/23 20:46:46
It would seem more logical to have MediaSource::Id
takumif
2016/08/23 22:13:09
Keeping them in a vector made it simpler to get ri
mark a. foltz
2016/08/29 23:00:29
If that's the common case (adding/removing by cast
| |
| 102 } | |
| 97 NotifyOnResultsUpdated(); | 103 NotifyOnResultsUpdated(); |
| 98 } | 104 } |
| 99 | 105 |
| 100 void QueryResultManager::StopSinksQuery(MediaCastMode cast_mode) { | 106 void QueryResultManager::StopSinksQuery(MediaCastMode cast_mode) { |
| 101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 102 RemoveObserverForCastMode(cast_mode); | 108 RemoveObserversForCastMode(cast_mode); |
| 103 SetSourceForCastMode(cast_mode, MediaSource()); | 109 SetSourcesForCastMode(cast_mode, std::vector<MediaSource>()); |
| 104 UpdateWithSinksQueryResult(cast_mode, std::vector<MediaSink>()); | 110 ResetSinkCompatibilityForCastMode(cast_mode); |
| 105 NotifyOnResultsUpdated(); | 111 NotifyOnResultsUpdated(); |
| 106 } | 112 } |
| 107 | 113 |
| 108 void QueryResultManager::SetSourceForCastMode( | |
| 109 MediaCastMode cast_mode, const MediaSource& source) { | |
| 110 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 111 cast_mode_sources_[cast_mode] = source; | |
| 112 } | |
| 113 | |
| 114 void QueryResultManager::RemoveObserverForCastMode(MediaCastMode cast_mode) { | |
| 115 auto observers_it = sinks_observers_.find(cast_mode); | |
| 116 if (observers_it != sinks_observers_.end()) | |
| 117 sinks_observers_.erase(observers_it); | |
| 118 } | |
| 119 | |
| 120 bool QueryResultManager::IsValid(const MediaSinkWithCastModes& entry) const { | |
| 121 return !entry.cast_modes.empty(); | |
| 122 } | |
| 123 | |
| 124 void QueryResultManager::UpdateWithSinksQueryResult( | |
| 125 MediaCastMode cast_mode, | |
| 126 const std::vector<MediaSink>& result) { | |
| 127 base::hash_set<MediaSink::Id> result_sink_ids; | |
| 128 for (const MediaSink& sink : result) | |
| 129 result_sink_ids.insert(sink.id()); | |
| 130 | |
| 131 // (1) Iterate through current sink set, remove cast mode from those that | |
| 132 // do not appear in latest result. | |
| 133 for (auto it = all_sinks_.begin(); it != all_sinks_.end(); /*no-op*/) { | |
| 134 if (!base::ContainsKey(result_sink_ids, it->first)) { | |
| 135 it->second.cast_modes.erase(cast_mode); | |
| 136 } | |
| 137 if (!IsValid(it->second)) { | |
| 138 all_sinks_.erase(it++); | |
| 139 } else { | |
| 140 ++it; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 // (2) Add / update sinks with latest result. | |
| 145 for (const MediaSink& sink : result) { | |
| 146 auto result = | |
| 147 all_sinks_.insert(std::make_pair(sink.id(), | |
| 148 MediaSinkWithCastModes(sink))); | |
| 149 if (!result.second) | |
| 150 result.first->second.sink = sink; | |
| 151 result.first->second.cast_modes.insert(cast_mode); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 CastModeSet QueryResultManager::GetSupportedCastModes() const { | 114 CastModeSet QueryResultManager::GetSupportedCastModes() const { |
| 156 CastModeSet modes; | 115 CastModeSet modes; |
| 157 for (const auto& observer_pair : sinks_observers_) | 116 for (const auto& observer_pair : sinks_observers_) |
| 158 modes.insert(observer_pair.first); | 117 modes.insert(observer_pair.first); |
| 159 return modes; | 118 return modes; |
| 160 } | 119 } |
| 161 | 120 |
| 162 MediaSource QueryResultManager::GetSourceForCastMode( | 121 MediaSource QueryResultManager::GetSourceForCastModeAndSink( |
| 163 MediaCastMode cast_mode) const { | 122 MediaCastMode cast_mode, MediaSink::Id sink_id) const { |
| 164 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 123 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
|
mark a. foltz
2016/08/23 20:46:46
Please call DCHECK_CURRENTLY_ON consistently for p
takumif
2016/08/29 23:05:14
Done.
| |
| 165 auto source_it = cast_mode_sources_.find(cast_mode); | 124 auto source_it = cast_mode_sources_.find(cast_mode); |
| 166 return source_it == cast_mode_sources_.end() ? | 125 return source_it == cast_mode_sources_.end() ? |
| 167 MediaSource() : source_it->second; | 126 MediaSource() : GetFirstSourceSupportedBySink(source_it->second, sink_id); |
| 127 } | |
| 128 | |
| 129 std::vector<MediaSource> QueryResultManager::GetSourcesForCastMode( | |
| 130 MediaCastMode cast_mode) const { | |
| 131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 132 return cast_mode_sources_.find(cast_mode) == cast_mode_sources_.end() ? | |
| 133 std::vector<MediaSource>() : cast_mode_sources_.at(cast_mode); | |
| 134 } | |
| 135 | |
| 136 void QueryResultManager::SetSourcesForCastMode( | |
| 137 MediaCastMode cast_mode, const std::vector<MediaSource>& sources) { | |
| 138 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 139 cast_mode_sources_[cast_mode] = sources; | |
| 140 } | |
| 141 | |
| 142 void QueryResultManager::RemoveObserversForCastMode(MediaCastMode cast_mode) { | |
| 143 auto observers_it = sinks_observers_.find(cast_mode); | |
| 144 if (observers_it != sinks_observers_.end()) | |
| 145 sinks_observers_.erase(observers_it); | |
| 146 } | |
| 147 | |
| 148 void QueryResultManager::ResetSinkCompatibilityForCastMode( | |
| 149 MediaCastMode cast_mode) { | |
| 150 for (auto it = all_sinks_.begin(); it != all_sinks_.end(); /*no-op*/) { | |
| 151 it->second.erase(cast_mode); | |
| 152 if (it->second.empty()) | |
| 153 all_sinks_.erase(it++); | |
| 154 else | |
| 155 ++it; | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 void QueryResultManager::UpdateWithSinksQueryResult( | |
| 160 MediaCastMode cast_mode, | |
| 161 MediaSource source, | |
| 162 const std::vector<MediaSink>& result_sinks) { | |
| 163 base::hash_set<MediaSink::Id> result_sink_ids; | |
| 164 for (const MediaSink& sink : result_sinks) | |
| 165 result_sink_ids.insert(sink.id()); | |
| 166 | |
| 167 // (1) Iterate through current sink set, remove cast mode from those that | |
| 168 // do not appear in latest result. | |
| 169 for (auto it = all_sinks_.begin(); it != all_sinks_.end(); /*no-op*/) { | |
| 170 const MediaSink& sink = it->first; | |
| 171 if (!base::ContainsKey(result_sink_ids, sink.id())) { | |
| 172 all_sinks_[sink][cast_mode].erase(source.id()); | |
| 173 if (all_sinks_[sink][cast_mode].empty()) | |
| 174 all_sinks_[sink].erase(cast_mode); | |
| 175 } | |
| 176 if (all_sinks_[sink].empty()) | |
| 177 all_sinks_.erase(it++); | |
| 178 else | |
| 179 ++it; | |
| 180 } | |
| 181 | |
| 182 if (source.Empty()) | |
| 183 return; | |
| 184 | |
| 185 // (2) Add / update sinks with latest result. | |
| 186 for (const MediaSink& sink : result_sinks) | |
| 187 all_sinks_[sink][cast_mode].insert(source.id()); | |
| 168 } | 188 } |
| 169 | 189 |
| 170 void QueryResultManager::NotifyOnResultsUpdated() { | 190 void QueryResultManager::NotifyOnResultsUpdated() { |
| 171 std::vector<MediaSinkWithCastModes> sinks; | 191 std::vector<MediaSinkWithCastModes> sinks; |
| 172 for (const auto& sink_pair : all_sinks_) { | 192 for (const auto& sink_pair : all_sinks_) |
| 173 sinks.push_back(sink_pair.second); | 193 sinks.push_back(GetMediaSinkWithCastModes(sink_pair.first)); |
| 174 } | |
| 175 FOR_EACH_OBSERVER(QueryResultManager::Observer, observers_, | 194 FOR_EACH_OBSERVER(QueryResultManager::Observer, observers_, |
| 176 OnResultsUpdated(sinks)); | 195 OnResultsUpdated(sinks)); |
| 177 } | 196 } |
| 178 | 197 |
| 198 MediaSource QueryResultManager::GetFirstSourceSupportedBySink( | |
| 199 std::vector<MediaSource> sources, MediaSink::Id sink_id) const { | |
| 200 for (const auto& sink_pair : all_sinks_) { | |
| 201 if (sink_pair.first.id() == sink_id) { | |
| 202 for (const MediaSource source : sources) { | |
| 203 if (SinkSupportsSource(sink_pair.first, source)) | |
| 204 return source; | |
| 205 } | |
| 206 break; | |
| 207 } | |
| 208 } | |
| 209 return MediaSource(); | |
| 210 } | |
| 211 | |
| 212 bool QueryResultManager::SinkSupportsSource( | |
| 213 MediaSink sink, MediaSource source) const { | |
| 214 if (all_sinks_.find(sink) == all_sinks_.end()) | |
| 215 return false; | |
| 216 | |
| 217 auto mode_sources = all_sinks_.at(sink); | |
| 218 for (const auto& mode_pair : mode_sources) { | |
| 219 if (mode_pair.second.find(source.id()) != mode_pair.second.end()) | |
| 220 return true; | |
| 221 } | |
| 222 return false; | |
| 223 } | |
| 224 | |
| 225 MediaSinkWithCastModes QueryResultManager::GetMediaSinkWithCastModes( | |
| 226 const MediaSink& sink) { | |
| 227 MediaSinkWithCastModes sink_with_cast_modes(sink); | |
| 228 for (const auto& mode_pair : all_sinks_[sink]) | |
| 229 sink_with_cast_modes.cast_modes.insert(mode_pair.first); | |
| 230 return sink_with_cast_modes; | |
| 231 } | |
| 232 | |
| 179 } // namespace media_router | 233 } // namespace media_router |
| OLD | NEW |