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

Side by Side Diff: chrome/browser/ui/ash/cast_config_delegate_media_router.cc

Issue 2466043002: Cleanup TrayCast with media-router only support in mind. (Closed)
Patch Set: Rebase Created 4 years, 1 month 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
« no previous file with comments | « chrome/browser/ui/ash/cast_config_delegate_media_router.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/ash/cast_config_delegate_media_router.h" 5 #include "chrome/browser/ui/ash/cast_config_delegate_media_router.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/media/router/media_router.h" 14 #include "chrome/browser/media/router/media_router.h"
14 #include "chrome/browser/media/router/media_router_factory.h" 15 #include "chrome/browser/media/router/media_router_factory.h"
15 #include "chrome/browser/media/router/media_router_feature.h" 16 #include "chrome/browser/media/router/media_router_feature.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_sinks_observer.h" 18 #include "chrome/browser/media/router/media_sinks_observer.h"
18 #include "chrome/browser/media/router/media_source_helper.h" 19 #include "chrome/browser/media/router/media_source_helper.h"
19 #include "chrome/browser/profiles/profile_manager.h" 20 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/common/url_constants.h" 21 #include "chrome/common/url_constants.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_source.h"
21 24
22 namespace { 25 namespace {
23 26
24 media_router::MediaRouter* media_router_for_test_ = nullptr; 27 media_router::MediaRouter* media_router_for_test_ = nullptr;
25 28
29 // Returns the MediaRouter instance for the current primary profile.
26 media_router::MediaRouter* GetMediaRouter() { 30 media_router::MediaRouter* GetMediaRouter() {
27 if (media_router_for_test_) 31 if (media_router_for_test_)
28 return media_router_for_test_; 32 return media_router_for_test_;
29 33
30 auto* router = media_router::MediaRouterFactory::GetApiForBrowserContext( 34 auto* router = media_router::MediaRouterFactory::GetApiForBrowserContext(
31 ProfileManager::GetPrimaryUserProfile()); 35 ProfileManager::GetPrimaryUserProfile());
32 DCHECK(router); 36 DCHECK(router);
33 return router; 37 return router;
34 } 38 }
35 39
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 100
97 void CastDeviceCache::OnSinksReceived(const MediaSinks& sinks) { 101 void CastDeviceCache::OnSinksReceived(const MediaSinks& sinks) {
98 sinks_.clear(); 102 sinks_.clear();
99 for (const media_router::MediaSink& sink : sinks) { 103 for (const media_router::MediaSink& sink : sinks) {
100 // The media router adds a MediaSink instance that doesn't have a name. Make 104 // The media router adds a MediaSink instance that doesn't have a name. Make
101 // sure to filter that sink out from the UI so it is not rendered, as it 105 // sure to filter that sink out from the UI so it is not rendered, as it
102 // will be a line that only has a icon with no apparent meaning. 106 // will be a line that only has a icon with no apparent meaning.
103 if (sink.name().empty()) 107 if (sink.name().empty())
104 continue; 108 continue;
105 109
106 // Temporarily hide sinks that have a domain. This is to meet cast privacy 110 // Hide all sinks which have a domain (ie, castouts) to meet privacy
107 // requirements. See bug/28691645. 111 // requirements. This will be enabled once UI can display the domain. See
112 // crbug.com/624016.
108 if (!sink.domain().empty()) 113 if (!sink.domain().empty())
109 continue; 114 continue;
110 115
111 sinks_.push_back(sink); 116 sinks_.push_back(sink);
112 } 117 }
113 118
114 cast_config_delegate_->RequestDeviceRefresh(); 119 cast_config_delegate_->RequestDeviceRefresh();
115 } 120 }
116 121
117 void CastDeviceCache::OnRoutesUpdated( 122 void CastDeviceCache::OnRoutesUpdated(
118 const MediaRoutes& routes, 123 const MediaRoutes& routes,
119 const MediaRouteIds& unused_joinable_route_ids) { 124 const MediaRouteIds& unused_joinable_route_ids) {
120 routes_ = routes; 125 routes_ = routes;
121 cast_config_delegate_->RequestDeviceRefresh(); 126 cast_config_delegate_->RequestDeviceRefresh();
122 } 127 }
123 128
124 //////////////////////////////////////////////////////////////////////////////// 129 ////////////////////////////////////////////////////////////////////////////////
125 // CastConfigDelegateMediaRouter: 130 // CastConfigDelegateMediaRouter:
126 131
127 // static
128 bool CastConfigDelegateMediaRouter::IsEnabled() {
129 return media_router::MediaRouterEnabled(
130 ProfileManager::GetPrimaryUserProfile()) ||
131 media_router_for_test_;
132 }
133
134 void CastConfigDelegateMediaRouter::SetMediaRouterForTest( 132 void CastConfigDelegateMediaRouter::SetMediaRouterForTest(
135 media_router::MediaRouter* media_router) { 133 media_router::MediaRouter* media_router) {
136 media_router_for_test_ = media_router; 134 media_router_for_test_ = media_router;
137 } 135 }
138 136
139 CastConfigDelegateMediaRouter::CastConfigDelegateMediaRouter() {} 137 CastConfigDelegateMediaRouter::CastConfigDelegateMediaRouter() {
138 registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED,
139 content::NotificationService::AllSources());
140 }
140 141
141 CastConfigDelegateMediaRouter::~CastConfigDelegateMediaRouter() {} 142 CastConfigDelegateMediaRouter::~CastConfigDelegateMediaRouter() {}
142 143
143 CastDeviceCache* CastConfigDelegateMediaRouter::devices() { 144 CastDeviceCache* CastConfigDelegateMediaRouter::devices() {
144 // The CastDeviceCache instance is lazily allocated because the MediaRouter 145 // The CastDeviceCache instance is lazily allocated because the MediaRouter
145 // component is not ready when the constructor is invoked. 146 // component is not ready when the constructor is invoked.
146 if (!devices_ && GetMediaRouter() != nullptr) { 147 if (!devices_ && GetMediaRouter()) {
147 devices_.reset(new CastDeviceCache(this)); 148 devices_ = base::MakeUnique<CastDeviceCache>(this);
148 devices_->Init(); 149 devices_->Init();
149 } 150 }
150 151
151 return devices_.get(); 152 return devices_.get();
152 } 153 }
153 154
154 void CastConfigDelegateMediaRouter::RequestDeviceRefresh() { 155 void CastConfigDelegateMediaRouter::RequestDeviceRefresh() {
155 // The media router component isn't ready yet. 156 // The media router component isn't ready yet.
156 if (!devices()) 157 if (!devices())
157 return; 158 return;
158 159
159 // Build the old-style SinkAndRoute set out of the MediaRouter 160 // Build the old-style SinkAndRoute set out of the MediaRouter
160 // source/sink/route setup. We first map the existing sinks, and then we 161 // source/sink/route setup. We first map the existing sinks, and then we
161 // update those sinks with activity information. 162 // update those sinks with activity information.
162 163
163 SinksAndRoutes items; 164 SinksAndRoutes items;
164 165
165 for (const media_router::MediaSink& sink : devices()->sinks()) { 166 for (const media_router::MediaSink& sink : devices()->sinks()) {
166 SinkAndRoute sr; 167 SinkAndRoute sr;
167 sr.sink.id = sink.id(); 168 sr.sink.id = sink.id();
168 sr.sink.name = base::UTF8ToUTF16(sink.name()); 169 sr.sink.name = base::UTF8ToUTF16(sink.name());
170 sr.sink.domain = base::UTF8ToUTF16(sink.domain());
169 items.push_back(sr); 171 items.push_back(sr);
170 } 172 }
171 173
172 for (const media_router::MediaRoute& route : devices()->routes()) { 174 for (const media_router::MediaRoute& route : devices()->routes()) {
173 if (!route.for_display()) 175 if (!route.for_display())
174 continue; 176 continue;
175 177
176 for (SinkAndRoute& item : items) { 178 for (SinkAndRoute& item : items) {
177 if (item.sink.id == route.media_sink_id()) { 179 if (item.sink.id == route.media_sink_id()) {
178 item.route.id = route.media_route_id(); 180 item.route.id = route.media_route_id();
179 item.route.title = 181 item.route.title =
180 base::UTF8ToUTF16(StripEndingTab(route.description())); 182 base::UTF8ToUTF16(StripEndingTab(route.description()));
181 item.route.is_local_source = route.is_local(); 183 item.route.is_local_source = route.is_local();
182 184
183 if (route.is_local()) { 185 // Default to a tab/app capture. This will display the media router
184 // TODO(jdufault): Once the extension backend is removed, we can 186 // description. This means we will properly support DIAL casts.
185 // remove tab_id and specify the Desktop/Tab capture directly. 187 item.route.content_source = Route::ContentSource::TAB;
186 // crbug.com/551132. 188 if (media_router::IsDesktopMirroringMediaSource(route.media_source()))
187 // TODO(jdufault): We currently don't actually display DIAL casts to 189 item.route.content_source = Route::ContentSource::DESKTOP;
188 // the user even though we have all the information necessary. We'll
189 // do this once the extension backend is gone because supporting both
190 // introduces extra complexity. crbug.com/551132.
191
192 // Default to a tab/app capture. This will display the media router
193 // description. This means we will properly support DIAL casts.
194 item.route.tab_id = 0;
195 if (media_router::IsDesktopMirroringMediaSource(route.media_source()))
196 item.route.tab_id = Route::TabId::DESKTOP;
197 }
198 190
199 break; 191 break;
200 } 192 }
201 } 193 }
202 } 194 }
203 195
204 for (ash::CastConfigDelegate::Observer& observer : observer_list_) 196 for (ash::CastConfigDelegate::Observer& observer : observer_list_)
205 observer.OnDevicesUpdated(items); 197 observer.OnDevicesUpdated(items);
206 } 198 }
207 199
208 void CastConfigDelegateMediaRouter::CastToSink(const std::string& sink_id) { 200 void CastConfigDelegateMediaRouter::CastToSink(const Sink& sink) {
209 // TODO(imcheng): Pass in tab casting timeout. 201 // TODO(imcheng): Pass in tab casting timeout.
210 GetMediaRouter()->CreateRoute( 202 GetMediaRouter()->CreateRoute(
211 media_router::MediaSourceForDesktop().id(), sink_id, 203 media_router::MediaSourceForDesktop().id(), sink.id,
212 GURL("http://cros-cast-origin/"), nullptr, 204 GURL("http://cros-cast-origin/"), nullptr,
213 std::vector<media_router::MediaRouteResponseCallback>(), 205 std::vector<media_router::MediaRouteResponseCallback>(),
214 base::TimeDelta(), false); 206 base::TimeDelta(), false);
215 } 207 }
216 208
217 void CastConfigDelegateMediaRouter::StopCasting(const std::string& route_id) { 209 void CastConfigDelegateMediaRouter::StopCasting(const Route& route) {
218 GetMediaRouter()->TerminateRoute(route_id); 210 GetMediaRouter()->TerminateRoute(route.id);
219 } 211 }
220 212
221 void CastConfigDelegateMediaRouter::AddObserver( 213 void CastConfigDelegateMediaRouter::AddObserver(
222 ash::CastConfigDelegate::Observer* observer) { 214 ash::CastConfigDelegate::Observer* observer) {
223 observer_list_.AddObserver(observer); 215 observer_list_.AddObserver(observer);
224 } 216 }
225 217
226 void CastConfigDelegateMediaRouter::RemoveObserver( 218 void CastConfigDelegateMediaRouter::RemoveObserver(
227 ash::CastConfigDelegate::Observer* observer) { 219 ash::CastConfigDelegate::Observer* observer) {
228 observer_list_.RemoveObserver(observer); 220 observer_list_.RemoveObserver(observer);
229 } 221 }
222
223 void CastConfigDelegateMediaRouter::Observe(
224 int type,
225 const content::NotificationSource& source,
226 const content::NotificationDetails& details) {
227 switch (type) {
228 case chrome::NOTIFICATION_SESSION_STARTED:
oshima 2016/11/10 18:10:18 notification is deprecated and it's discouraged to
229 // The active profile has changed, which means that the media router has
230 // as well. Reset the device cache to ensure we are using up-to-date
231 // object instances.
232 devices_.reset();
233 RequestDeviceRefresh();
234 break;
235 }
236 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/cast_config_delegate_media_router.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698