Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: chrome/browser/ui/webui/media_router/query_result_manager.cc

Issue 2264153002: [Presentation API] Add support for multiple URLs in PresentationRequest on Media Router UI side (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Mark's comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_->SetSinksCompatibleWithSource(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_;
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(
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 RemoveOldSourcesForCastMode(cast_mode, sources);
88 RemoveObserverForCastMode(cast_mode); 93 AddObserversForCastMode(cast_mode, sources, origin);
89 UpdateWithSinksQueryResult(cast_mode, std::vector<MediaSink>()); 94 cast_mode_sources_[cast_mode] = sources;
90
91 std::unique_ptr<CastModeMediaSinksObserver> observer(
92 new CastModeMediaSinksObserver(cast_mode, source, origin, router_, this));
93 observer->Init();
94 auto result =
95 sinks_observers_.insert(std::make_pair(cast_mode, std::move(observer)));
96 DCHECK(result.second);
97 NotifyOnResultsUpdated(); 95 NotifyOnResultsUpdated();
98 } 96 }
99 97
100 void QueryResultManager::StopSinksQuery(MediaCastMode cast_mode) { 98 void QueryResultManager::StopSinksQuery(MediaCastMode cast_mode) {
101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
102 RemoveObserverForCastMode(cast_mode); 100 RemoveOldSourcesForCastMode(cast_mode, std::vector<MediaSource>());
103 SetSourceForCastMode(cast_mode, MediaSource()); 101 cast_mode_sources_.erase(cast_mode);
104 UpdateWithSinksQueryResult(cast_mode, std::vector<MediaSink>());
105 NotifyOnResultsUpdated(); 102 NotifyOnResultsUpdated();
106 } 103 }
107 104
108 void QueryResultManager::SetSourceForCastMode( 105 CastModeSet QueryResultManager::GetSupportedCastModes() const {
109 MediaCastMode cast_mode, const MediaSource& source) {
110 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
111 cast_mode_sources_[cast_mode] = source; 107 CastModeSet modes;
108 for (const auto& observer_pair : cast_mode_sources_)
109 modes.insert(observer_pair.first);
mark a. foltz 2016/09/09 22:22:27 Blank line after for statement
takumif 2016/09/13 03:48:21 Done.
110 return modes;
112 } 111 }
113 112
114 void QueryResultManager::RemoveObserverForCastMode(MediaCastMode cast_mode) { 113 std::unique_ptr<MediaSource> QueryResultManager::GetSourceForCastModeAndSink(
115 auto observers_it = sinks_observers_.find(cast_mode); 114 MediaCastMode cast_mode, MediaSink::Id sink_id) const {
116 if (observers_it != sinks_observers_.end()) 115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
117 sinks_observers_.erase(observers_it); 116 for (const auto& sink_pair : all_sinks_) {
117 if (sink_pair.first.id() == sink_id) {
mark a. foltz 2016/09/09 22:22:27 std::find_if ?
takumif 2016/09/13 03:48:21 find_if would look messier, since we can't use aut
118 return GetHighestPrioritySourceForCastModeAndSink(
119 cast_mode, sink_pair.first);
120 }
121 }
122 return std::unique_ptr<MediaSource>();
118 } 123 }
119 124
120 bool QueryResultManager::IsValid(const MediaSinkWithCastModes& entry) const { 125 std::vector<MediaSource> QueryResultManager::GetSourcesForCastMode(
121 return !entry.cast_modes.empty(); 126 MediaCastMode cast_mode) const {
127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
128 return base::ContainsKey(cast_mode_sources_, cast_mode) ?
mark a. foltz 2016/09/09 22:22:27 cast_mode_sources_.find() to avoid double lookup
takumif 2016/09/13 03:48:21 Done.
129 cast_mode_sources_.at(cast_mode) : std::vector<MediaSource>();
122 } 130 }
123 131
124 void QueryResultManager::UpdateWithSinksQueryResult( 132 void QueryResultManager::RemoveOldSourcesForCastMode(
133 MediaCastMode cast_mode, const std::vector<MediaSource>& new_sources) {
134 if (!base::ContainsKey(cast_mode_sources_, cast_mode))
135 return;
136
137 for (const MediaSource& source : cast_mode_sources_.at(cast_mode)) {
138 if (!base::ContainsValue(new_sources, source)) {
139 sinks_observers_.erase(source);
mark a. foltz 2016/09/09 22:22:27 This assumes that two different modes can't add th
takumif 2016/09/13 03:48:22 Added a method AreSourcesValidForCastMode() for ch
140 SetSinksCompatibleWithSource(cast_mode, source, std::vector<MediaSink>());
141 }
142 }
143 }
144
145 void QueryResultManager::AddObserversForCastMode(
125 MediaCastMode cast_mode, 146 MediaCastMode cast_mode,
126 const std::vector<MediaSink>& result) { 147 const std::vector<MediaSource>& sources,
127 base::hash_set<MediaSink::Id> result_sink_ids; 148 const GURL& origin) {
128 for (const MediaSink& sink : result) 149 for (const MediaSource& source : sources) {
129 result_sink_ids.insert(sink.id()); 150 if (!base::ContainsKey(sinks_observers_, source)) {
151 std::unique_ptr<CastModeMediaSinksObserver> observer(
152 new CastModeMediaSinksObserver(
153 cast_mode, source, origin, router_, this));
154 observer->Init();
155 sinks_observers_[source] = std::move(observer);
156 }
157 }
158 }
159
160 void QueryResultManager::SetSinksCompatibleWithSource(
161 MediaCastMode cast_mode,
162 MediaSource source,
163 const std::vector<MediaSink>& new_sinks) {
164 base::hash_set<MediaSink::Id> new_sink_ids;
165 for (const MediaSink& sink : new_sinks)
166 new_sink_ids.insert(sink.id());
130 167
131 // (1) Iterate through current sink set, remove cast mode from those that 168 // (1) Iterate through current sink set, remove cast mode from those that
132 // do not appear in latest result. 169 // do not appear in latest result.
133 for (auto it = all_sinks_.begin(); it != all_sinks_.end(); /*no-op*/) { 170 for (auto it = all_sinks_.begin(); it != all_sinks_.end(); /*no-op*/) {
134 if (!base::ContainsKey(result_sink_ids, it->first)) { 171 const MediaSink& sink = it->first;
135 it->second.cast_modes.erase(cast_mode); 172 CastModesWithMediaSources& sources_for_sink = it->second;
136 } 173 if (!base::ContainsKey(new_sink_ids, sink.id()))
137 if (!IsValid(it->second)) { 174 sources_for_sink.RemoveSource(cast_mode, source);
175 if (sources_for_sink.IsEmpty())
138 all_sinks_.erase(it++); 176 all_sinks_.erase(it++);
139 } else { 177 else
140 ++it; 178 ++it;
141 }
142 } 179 }
143 180
144 // (2) Add / update sinks with latest result. 181 // (2) Add / update sinks with latest result.
145 for (const MediaSink& sink : result) { 182 for (const MediaSink& sink : new_sinks)
146 auto result = 183 all_sinks_[sink].AddSource(cast_mode, source);
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 } 184 }
154 185
155 CastModeSet QueryResultManager::GetSupportedCastModes() const { 186 std::unique_ptr<MediaSource>
156 CastModeSet modes; 187 QueryResultManager::GetHighestPrioritySourceForCastModeAndSink(
157 for (const auto& observer_pair : sinks_observers_) 188 MediaCastMode cast_mode, const MediaSink& sink) const {
158 modes.insert(observer_pair.first); 189 if (!base::ContainsKey(cast_mode_sources_, cast_mode))
159 return modes; 190 return std::unique_ptr<MediaSource>();
160 }
161 191
162 MediaSource QueryResultManager::GetSourceForCastMode( 192 const CastModesWithMediaSources& sources_for_sink = all_sinks_.at(sink);
imcheng 2016/09/12 19:26:28 I don't think we should be using the at() methods
takumif 2016/09/13 03:48:21 I'd like to keep this a separate method to avoid h
163 MediaCastMode cast_mode) const { 193 for (const MediaSource& source : cast_mode_sources_.at(cast_mode)) {
164 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 194 if (sources_for_sink.HasSource(cast_mode, source))
165 auto source_it = cast_mode_sources_.find(cast_mode); 195 return std::unique_ptr<MediaSource>(new MediaSource(source.id()));
166 return source_it == cast_mode_sources_.end() ? 196 }
167 MediaSource() : source_it->second; 197 return std::unique_ptr<MediaSource>();
168 } 198 }
169 199
170 void QueryResultManager::NotifyOnResultsUpdated() { 200 void QueryResultManager::NotifyOnResultsUpdated() {
171 std::vector<MediaSinkWithCastModes> sinks; 201 std::vector<MediaSinkWithCastModes> sinks;
172 for (const auto& sink_pair : all_sinks_) { 202 for (const auto& sink_pair : all_sinks_)
173 sinks.push_back(sink_pair.second); 203 sinks.push_back(GetMediaSinkWithCastModes(sink_pair.first));
174 }
175 FOR_EACH_OBSERVER(QueryResultManager::Observer, observers_, 204 FOR_EACH_OBSERVER(QueryResultManager::Observer, observers_,
176 OnResultsUpdated(sinks)); 205 OnResultsUpdated(sinks));
177 } 206 }
178 207
208 MediaSinkWithCastModes QueryResultManager::GetMediaSinkWithCastModes(
209 const MediaSink& sink) {
210 MediaSinkWithCastModes sink_with_cast_modes(sink);
211 sink_with_cast_modes.cast_modes = all_sinks_[sink].GetCastModes();
212 return sink_with_cast_modes;
213 }
214
179 } // namespace media_router 215 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698