| 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/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 Loading... |
| 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 // TODO(jdufault): This should use a callback interface once there is an |
| 139 // equivalent. See crbug.com/666005. |
| 140 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, |
| 141 content::NotificationService::AllSources()); |
| 142 } |
| 140 | 143 |
| 141 CastConfigDelegateMediaRouter::~CastConfigDelegateMediaRouter() {} | 144 CastConfigDelegateMediaRouter::~CastConfigDelegateMediaRouter() {} |
| 142 | 145 |
| 143 CastDeviceCache* CastConfigDelegateMediaRouter::devices() { | 146 CastDeviceCache* CastConfigDelegateMediaRouter::devices() { |
| 144 // The CastDeviceCache instance is lazily allocated because the MediaRouter | 147 // The CastDeviceCache instance is lazily allocated because the MediaRouter |
| 145 // component is not ready when the constructor is invoked. | 148 // component is not ready when the constructor is invoked. |
| 146 if (!devices_ && GetMediaRouter() != nullptr) { | 149 if (!devices_ && GetMediaRouter()) { |
| 147 devices_.reset(new CastDeviceCache(this)); | 150 devices_ = base::MakeUnique<CastDeviceCache>(this); |
| 148 devices_->Init(); | 151 devices_->Init(); |
| 149 } | 152 } |
| 150 | 153 |
| 151 return devices_.get(); | 154 return devices_.get(); |
| 152 } | 155 } |
| 153 | 156 |
| 154 void CastConfigDelegateMediaRouter::RequestDeviceRefresh() { | 157 void CastConfigDelegateMediaRouter::RequestDeviceRefresh() { |
| 155 // The media router component isn't ready yet. | 158 // The media router component isn't ready yet. |
| 156 if (!devices()) | 159 if (!devices()) |
| 157 return; | 160 return; |
| 158 | 161 |
| 159 // Build the old-style SinkAndRoute set out of the MediaRouter | 162 // Build the old-style SinkAndRoute set out of the MediaRouter |
| 160 // source/sink/route setup. We first map the existing sinks, and then we | 163 // source/sink/route setup. We first map the existing sinks, and then we |
| 161 // update those sinks with activity information. | 164 // update those sinks with activity information. |
| 162 | 165 |
| 163 SinksAndRoutes items; | 166 SinksAndRoutes items; |
| 164 | 167 |
| 165 for (const media_router::MediaSink& sink : devices()->sinks()) { | 168 for (const media_router::MediaSink& sink : devices()->sinks()) { |
| 166 SinkAndRoute sr; | 169 SinkAndRoute sr; |
| 167 sr.sink.id = sink.id(); | 170 sr.sink.id = sink.id(); |
| 168 sr.sink.name = base::UTF8ToUTF16(sink.name()); | 171 sr.sink.name = base::UTF8ToUTF16(sink.name()); |
| 172 sr.sink.domain = base::UTF8ToUTF16(sink.domain()); |
| 169 items.push_back(sr); | 173 items.push_back(sr); |
| 170 } | 174 } |
| 171 | 175 |
| 172 for (const media_router::MediaRoute& route : devices()->routes()) { | 176 for (const media_router::MediaRoute& route : devices()->routes()) { |
| 173 if (!route.for_display()) | 177 if (!route.for_display()) |
| 174 continue; | 178 continue; |
| 175 | 179 |
| 176 for (SinkAndRoute& item : items) { | 180 for (SinkAndRoute& item : items) { |
| 177 if (item.sink.id == route.media_sink_id()) { | 181 if (item.sink.id == route.media_sink_id()) { |
| 178 item.route.id = route.media_route_id(); | 182 item.route.id = route.media_route_id(); |
| 179 item.route.title = | 183 item.route.title = |
| 180 base::UTF8ToUTF16(StripEndingTab(route.description())); | 184 base::UTF8ToUTF16(StripEndingTab(route.description())); |
| 181 item.route.is_local_source = route.is_local(); | 185 item.route.is_local_source = route.is_local(); |
| 182 | 186 |
| 183 if (route.is_local()) { | 187 // Default to a tab/app capture. This will display the media router |
| 184 // TODO(jdufault): Once the extension backend is removed, we can | 188 // description. This means we will properly support DIAL casts. |
| 185 // remove tab_id and specify the Desktop/Tab capture directly. | 189 item.route.content_source = Route::ContentSource::TAB; |
| 186 // crbug.com/551132. | 190 if (media_router::IsDesktopMirroringMediaSource(route.media_source())) |
| 187 // TODO(jdufault): We currently don't actually display DIAL casts to | 191 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 | 192 |
| 199 break; | 193 break; |
| 200 } | 194 } |
| 201 } | 195 } |
| 202 } | 196 } |
| 203 | 197 |
| 204 for (ash::CastConfigDelegate::Observer& observer : observer_list_) | 198 for (ash::CastConfigDelegate::Observer& observer : observer_list_) |
| 205 observer.OnDevicesUpdated(items); | 199 observer.OnDevicesUpdated(items); |
| 206 } | 200 } |
| 207 | 201 |
| 208 void CastConfigDelegateMediaRouter::CastToSink(const std::string& sink_id) { | 202 void CastConfigDelegateMediaRouter::CastToSink(const Sink& sink) { |
| 209 // TODO(imcheng): Pass in tab casting timeout. | 203 // TODO(imcheng): Pass in tab casting timeout. |
| 210 GetMediaRouter()->CreateRoute( | 204 GetMediaRouter()->CreateRoute( |
| 211 media_router::MediaSourceForDesktop().id(), sink_id, | 205 media_router::MediaSourceForDesktop().id(), sink.id, |
| 212 GURL("http://cros-cast-origin/"), nullptr, | 206 GURL("http://cros-cast-origin/"), nullptr, |
| 213 std::vector<media_router::MediaRouteResponseCallback>(), | 207 std::vector<media_router::MediaRouteResponseCallback>(), |
| 214 base::TimeDelta(), false); | 208 base::TimeDelta(), false); |
| 215 } | 209 } |
| 216 | 210 |
| 217 void CastConfigDelegateMediaRouter::StopCasting(const std::string& route_id) { | 211 void CastConfigDelegateMediaRouter::StopCasting(const Route& route) { |
| 218 GetMediaRouter()->TerminateRoute(route_id); | 212 GetMediaRouter()->TerminateRoute(route.id); |
| 219 } | 213 } |
| 220 | 214 |
| 221 void CastConfigDelegateMediaRouter::AddObserver( | 215 void CastConfigDelegateMediaRouter::AddObserver( |
| 222 ash::CastConfigDelegate::Observer* observer) { | 216 ash::CastConfigDelegate::Observer* observer) { |
| 223 observer_list_.AddObserver(observer); | 217 observer_list_.AddObserver(observer); |
| 224 } | 218 } |
| 225 | 219 |
| 226 void CastConfigDelegateMediaRouter::RemoveObserver( | 220 void CastConfigDelegateMediaRouter::RemoveObserver( |
| 227 ash::CastConfigDelegate::Observer* observer) { | 221 ash::CastConfigDelegate::Observer* observer) { |
| 228 observer_list_.RemoveObserver(observer); | 222 observer_list_.RemoveObserver(observer); |
| 229 } | 223 } |
| 224 |
| 225 void CastConfigDelegateMediaRouter::Observe( |
| 226 int type, |
| 227 const content::NotificationSource& source, |
| 228 const content::NotificationDetails& details) { |
| 229 switch (type) { |
| 230 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED: |
| 231 // The active profile has changed, which means that the media router has |
| 232 // as well. Reset the device cache to ensure we are using up-to-date |
| 233 // object instances. |
| 234 devices_.reset(); |
| 235 RequestDeviceRefresh(); |
| 236 break; |
| 237 } |
| 238 } |
| OLD | NEW |