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/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()); |
|
achuithb
2016/10/27 19:38:59
Is there still a problem with this and multi-profi
jdufault
2016/10/31 21:12:08
Nope, media router doesn't work very well with mul
| |
| 32 DCHECK(router); | 36 DCHECK(router); |
| 33 return router; | 37 return router; |
| 34 } | 38 } |
| 35 | 39 |
| 36 // The media router will sometimes append " (Tab)" to the tab title. This | 40 // The media router will sometimes append " (Tab)" to the tab title. This |
| 37 // function will remove that data from the inout param |string|. | 41 // function will remove that data from the inout param |string|. |
| 38 std::string StripEndingTab(const std::string& str) { | 42 std::string StripEndingTab(const std::string& str) { |
| 39 static const char ending[] = " (Tab)"; | 43 static const char ending[] = " (Tab)"; |
| 40 if (base::EndsWith(str, ending, base::CompareCase::SENSITIVE)) | 44 if (base::EndsWith(str, ending, base::CompareCase::SENSITIVE)) |
| 41 return str.substr(0, str.size() - strlen(ending)); | 45 return str.substr(0, str.size() - strlen(ending)); |
| (...skipping 54 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 // b/28691645. | |
|
achuithb
2016/10/27 19:38:59
Not sure about this internal bug link. Wonder if i
jdufault
2016/10/31 21:12:08
Done.
| |
| 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 bool CastConfigDelegateMediaRouter::HasCastExtension() const { | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 void CastConfigDelegateMediaRouter::RequestDeviceRefresh() { | 155 void CastConfigDelegateMediaRouter::RequestDeviceRefresh() { |
| 159 // The media router component isn't ready yet. | 156 // The media router component isn't ready yet. |
| 160 if (!devices()) | 157 if (!devices()) |
| 161 return; | 158 return; |
| 162 | 159 |
| 163 // Build the old-style ReceiverAndActivity set out of the MediaRouter | 160 // Build the old-style ReceiverAndActivity set out of the MediaRouter |
| 164 // 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 |
| 165 // update those sinks with activity information. | 162 // update those sinks with activity information. |
| 166 | 163 |
| 167 ReceiversAndActivities items; | 164 SinksAndRoutes items; |
| 168 | 165 |
| 169 for (const media_router::MediaSink& sink : devices()->sinks()) { | 166 for (const media_router::MediaSink& sink : devices()->sinks()) { |
| 170 ReceiverAndActivity ra; | 167 SinkAndRoute sr; |
| 171 ra.receiver.id = sink.id(); | 168 sr.sink.id = sink.id(); |
| 172 ra.receiver.name = base::UTF8ToUTF16(sink.name()); | 169 sr.sink.name = base::UTF8ToUTF16(sink.name()); |
| 173 items.push_back(ra); | 170 sr.sink.domain = base::UTF8ToUTF16(sink.domain()); |
| 171 items.push_back(sr); | |
| 174 } | 172 } |
| 175 | 173 |
| 176 for (const media_router::MediaRoute& route : devices()->routes()) { | 174 for (const media_router::MediaRoute& route : devices()->routes()) { |
| 177 if (!route.for_display()) | 175 if (!route.for_display()) |
| 178 continue; | 176 continue; |
| 179 | 177 |
| 180 for (ReceiverAndActivity& item : items) { | 178 for (SinkAndRoute& item : items) { |
| 181 if (item.receiver.id == route.media_sink_id()) { | 179 if (item.sink.id == route.media_sink_id()) { |
| 182 item.activity.id = route.media_route_id(); | 180 item.route.id = route.media_route_id(); |
| 183 item.activity.title = | 181 item.route.title = |
| 184 base::UTF8ToUTF16(StripEndingTab(route.description())); | 182 base::UTF8ToUTF16(StripEndingTab(route.description())); |
| 185 item.activity.is_local_source = route.is_local(); | 183 item.route.is_local_source = route.is_local(); |
| 186 | 184 |
| 187 if (route.is_local()) { | 185 // Default to a tab/app capture. This will display the media router |
| 188 // TODO(jdufault): Once the extension backend is removed, we can | 186 // description. This means we will properly support DIAL casts. |
| 189 // remove tab_id and specify the Desktop/Tab capture directly. | 187 item.route.content_source = Route::ContentSource::TAB; |
| 190 // crbug.com/551132. | 188 if (media_router::IsDesktopMirroringMediaSource(route.media_source())) |
| 191 // TODO(jdufault): We currently don't actually display DIAL casts to | 189 item.route.content_source = Route::ContentSource::DESKTOP; |
| 192 // the user even though we have all the information necessary. We'll | |
| 193 // do this once the extension backend is gone because supporting both | |
| 194 // introduces extra complexity. crbug.com/551132. | |
| 195 | |
| 196 // Default to a tab/app capture. This will display the media router | |
| 197 // description. This means we will properly support DIAL casts. | |
| 198 item.activity.tab_id = 0; | |
| 199 if (media_router::IsDesktopMirroringMediaSource(route.media_source())) | |
| 200 item.activity.tab_id = Activity::TabId::DESKTOP; | |
| 201 } | |
| 202 | 190 |
| 203 break; | 191 break; |
| 204 } | 192 } |
| 205 } | 193 } |
| 206 } | 194 } |
| 207 | 195 |
| 208 for (ash::CastConfigDelegate::Observer& observer : observer_list_) | 196 for (ash::CastConfigDelegate::Observer& observer : observer_list_) |
| 209 observer.OnDevicesUpdated(items); | 197 observer.OnDevicesUpdated(items); |
| 210 } | 198 } |
| 211 | 199 |
| 212 void CastConfigDelegateMediaRouter::CastToReceiver( | 200 void CastConfigDelegateMediaRouter::CastToReceiver(const Sink& sink) { |
| 213 const std::string& receiver_id) { | |
| 214 // TODO(imcheng): Pass in tab casting timeout. | 201 // TODO(imcheng): Pass in tab casting timeout. |
| 215 GetMediaRouter()->CreateRoute( | 202 GetMediaRouter()->CreateRoute( |
| 216 media_router::MediaSourceForDesktop().id(), receiver_id, | 203 media_router::MediaSourceForDesktop().id(), sink.id, |
| 217 GURL("http://cros-cast-origin/"), nullptr, | 204 GURL("http://cros-cast-origin/"), nullptr, |
| 218 std::vector<media_router::MediaRouteResponseCallback>(), | 205 std::vector<media_router::MediaRouteResponseCallback>(), |
| 219 base::TimeDelta(), false); | 206 base::TimeDelta(), false); |
| 220 } | 207 } |
| 221 | 208 |
| 222 void CastConfigDelegateMediaRouter::StopCasting(const std::string& route_id) { | 209 void CastConfigDelegateMediaRouter::StopCasting(const Route& route) { |
| 223 GetMediaRouter()->TerminateRoute(route_id); | 210 GetMediaRouter()->TerminateRoute(route.id); |
| 224 } | 211 } |
| 225 | 212 |
| 226 bool CastConfigDelegateMediaRouter::HasOptions() const { | |
| 227 // There are no plans to have an options page for the MediaRouter. | |
| 228 return false; | |
| 229 } | |
| 230 | |
| 231 void CastConfigDelegateMediaRouter::LaunchCastOptions() {} | |
| 232 | |
| 233 void CastConfigDelegateMediaRouter::AddObserver( | 213 void CastConfigDelegateMediaRouter::AddObserver( |
| 234 ash::CastConfigDelegate::Observer* observer) { | 214 ash::CastConfigDelegate::Observer* observer) { |
| 235 observer_list_.AddObserver(observer); | 215 observer_list_.AddObserver(observer); |
| 236 } | 216 } |
| 237 | 217 |
| 238 void CastConfigDelegateMediaRouter::RemoveObserver( | 218 void CastConfigDelegateMediaRouter::RemoveObserver( |
| 239 ash::CastConfigDelegate::Observer* observer) { | 219 ash::CastConfigDelegate::Observer* observer) { |
| 240 observer_list_.RemoveObserver(observer); | 220 observer_list_.RemoveObserver(observer); |
| 241 } | 221 } |
| 222 | |
| 223 void CastConfigDelegateMediaRouter::Observe( | |
| 224 int type, | |
| 225 const content::NotificationSource& source, | |
| 226 const content::NotificationDetails& details) { | |
| 227 switch (type) { | |
|
achuithb
2016/10/27 19:38:59
Is this a change in behavior?
jdufault
2016/10/31 21:12:08
Yea, this behavior works a bit better though and f
| |
| 228 case chrome::NOTIFICATION_SESSION_STARTED: | |
| 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 } | |
| OLD | NEW |