| 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_client_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/chrome_notification_types.h" |
| 14 #include "chrome/browser/media/router/media_router.h" | 14 #include "chrome/browser/media/router/media_router.h" |
| 15 #include "chrome/browser/media/router/media_router_factory.h" | 15 #include "chrome/browser/media/router/media_router_factory.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 // This class caches the values that the observers give us so we can query them | 51 // This class caches the values that the observers give us so we can query them |
| 52 // at any point in time. It also emits a device refresh event when new data is | 52 // at any point in time. It also emits a device refresh event when new data is |
| 53 // available. | 53 // available. |
| 54 class CastDeviceCache : public media_router::MediaRoutesObserver, | 54 class CastDeviceCache : public media_router::MediaRoutesObserver, |
| 55 public media_router::MediaSinksObserver { | 55 public media_router::MediaSinksObserver { |
| 56 public: | 56 public: |
| 57 using MediaSinks = std::vector<media_router::MediaSink>; | 57 using MediaSinks = std::vector<media_router::MediaSink>; |
| 58 using MediaRoutes = std::vector<media_router::MediaRoute>; | 58 using MediaRoutes = std::vector<media_router::MediaRoute>; |
| 59 using MediaRouteIds = std::vector<media_router::MediaRoute::Id>; | 59 using MediaRouteIds = std::vector<media_router::MediaRoute::Id>; |
| 60 | 60 |
| 61 explicit CastDeviceCache(ash::CastConfigDelegate* cast_config_delegate); | 61 explicit CastDeviceCache(ash::mojom::CastConfigClient* cast_config_client); |
| 62 ~CastDeviceCache() override; | 62 ~CastDeviceCache() override; |
| 63 | 63 |
| 64 // This may call cast_config_delegate->RequestDeviceRefresh() before | 64 // This may call cast_config_client->RequestDeviceRefresh() before |
| 65 // returning. | 65 // returning. |
| 66 void Init(); | 66 void Init(); |
| 67 | 67 |
| 68 const MediaSinks& sinks() const { return sinks_; } | 68 const MediaSinks& sinks() const { return sinks_; } |
| 69 const MediaRoutes& routes() const { return routes_; } | 69 const MediaRoutes& routes() const { return routes_; } |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 // media_router::MediaSinksObserver: | 72 // media_router::MediaSinksObserver: |
| 73 void OnSinksReceived(const MediaSinks& sinks) override; | 73 void OnSinksReceived(const MediaSinks& sinks) override; |
| 74 | 74 |
| 75 // media_router::MediaRoutesObserver: | 75 // media_router::MediaRoutesObserver: |
| 76 void OnRoutesUpdated(const MediaRoutes& routes, | 76 void OnRoutesUpdated(const MediaRoutes& routes, |
| 77 const MediaRouteIds& unused_joinable_route_ids) override; | 77 const MediaRouteIds& unused_joinable_route_ids) override; |
| 78 | 78 |
| 79 MediaSinks sinks_; | 79 MediaSinks sinks_; |
| 80 MediaRoutes routes_; | 80 MediaRoutes routes_; |
| 81 | 81 |
| 82 // Not owned. | 82 // Not owned. |
| 83 ash::CastConfigDelegate* cast_config_delegate_; | 83 ash::mojom::CastConfigClient* cast_config_client_; |
| 84 | 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(CastDeviceCache); | 85 DISALLOW_COPY_AND_ASSIGN(CastDeviceCache); |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 CastDeviceCache::CastDeviceCache(ash::CastConfigDelegate* cast_config_delegate) | 88 CastDeviceCache::CastDeviceCache( |
| 89 ash::mojom::CastConfigClient* cast_config_client) |
| 89 : MediaRoutesObserver(GetMediaRouter()), | 90 : MediaRoutesObserver(GetMediaRouter()), |
| 90 MediaSinksObserver(GetMediaRouter(), | 91 MediaSinksObserver(GetMediaRouter(), |
| 91 media_router::MediaSourceForDesktop(), | 92 media_router::MediaSourceForDesktop(), |
| 92 GURL(chrome::kChromeUIMediaRouterURL)), | 93 GURL(chrome::kChromeUIMediaRouterURL)), |
| 93 cast_config_delegate_(cast_config_delegate) {} | 94 cast_config_client_(cast_config_client) {} |
| 94 | 95 |
| 95 CastDeviceCache::~CastDeviceCache() {} | 96 CastDeviceCache::~CastDeviceCache() {} |
| 96 | 97 |
| 97 void CastDeviceCache::Init() { | 98 void CastDeviceCache::Init() { |
| 98 CHECK(MediaSinksObserver::Init()); | 99 CHECK(MediaSinksObserver::Init()); |
| 99 } | 100 } |
| 100 | 101 |
| 101 void CastDeviceCache::OnSinksReceived(const MediaSinks& sinks) { | 102 void CastDeviceCache::OnSinksReceived(const MediaSinks& sinks) { |
| 102 sinks_.clear(); | 103 sinks_.clear(); |
| 103 for (const media_router::MediaSink& sink : sinks) { | 104 for (const media_router::MediaSink& sink : sinks) { |
| 104 // The media router adds a MediaSink instance that doesn't have a name. Make | 105 // The media router adds a MediaSink instance that doesn't have a name. Make |
| 105 // sure to filter that sink out from the UI so it is not rendered, as it | 106 // sure to filter that sink out from the UI so it is not rendered, as it |
| 106 // will be a line that only has a icon with no apparent meaning. | 107 // will be a line that only has a icon with no apparent meaning. |
| 107 if (sink.name().empty()) | 108 if (sink.name().empty()) |
| 108 continue; | 109 continue; |
| 109 | 110 |
| 110 // Hide all sinks which have a domain (ie, castouts) to meet privacy | 111 // Hide all sinks which have a domain (ie, castouts) to meet privacy |
| 111 // requirements. This will be enabled once UI can display the domain. See | 112 // requirements. This will be enabled once UI can display the domain. See |
| 112 // crbug.com/624016. | 113 // crbug.com/624016. |
| 113 if (!sink.domain().empty()) | 114 if (!sink.domain().empty()) |
| 114 continue; | 115 continue; |
| 115 | 116 |
| 116 sinks_.push_back(sink); | 117 sinks_.push_back(sink); |
| 117 } | 118 } |
| 118 | 119 |
| 119 cast_config_delegate_->RequestDeviceRefresh(); | 120 cast_config_client_->RequestDeviceRefresh(); |
| 120 } | 121 } |
| 121 | 122 |
| 122 void CastDeviceCache::OnRoutesUpdated( | 123 void CastDeviceCache::OnRoutesUpdated( |
| 123 const MediaRoutes& routes, | 124 const MediaRoutes& routes, |
| 124 const MediaRouteIds& unused_joinable_route_ids) { | 125 const MediaRouteIds& unused_joinable_route_ids) { |
| 125 routes_ = routes; | 126 routes_ = routes; |
| 126 cast_config_delegate_->RequestDeviceRefresh(); | 127 cast_config_client_->RequestDeviceRefresh(); |
| 127 } | 128 } |
| 128 | 129 |
| 129 //////////////////////////////////////////////////////////////////////////////// | 130 //////////////////////////////////////////////////////////////////////////////// |
| 130 // CastConfigDelegateMediaRouter: | 131 // CastConfigClientMediaRouter: |
| 131 | 132 |
| 132 void CastConfigDelegateMediaRouter::SetMediaRouterForTest( | 133 void CastConfigClientMediaRouter::SetMediaRouterForTest( |
| 133 media_router::MediaRouter* media_router) { | 134 media_router::MediaRouter* media_router) { |
| 134 media_router_for_test_ = media_router; | 135 media_router_for_test_ = media_router; |
| 135 } | 136 } |
| 136 | 137 |
| 137 CastConfigDelegateMediaRouter::CastConfigDelegateMediaRouter() { | 138 CastConfigClientMediaRouter::CastConfigClientMediaRouter() { |
| 138 // TODO(jdufault): This should use a callback interface once there is an | 139 // TODO(jdufault): This should use a callback interface once there is an |
| 139 // equivalent. See crbug.com/666005. | 140 // equivalent. See crbug.com/666005. |
| 140 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, | 141 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, |
| 141 content::NotificationService::AllSources()); | 142 content::NotificationService::AllSources()); |
| 142 } | 143 } |
| 143 | 144 |
| 144 CastConfigDelegateMediaRouter::~CastConfigDelegateMediaRouter() {} | 145 CastConfigClientMediaRouter::~CastConfigClientMediaRouter() {} |
| 145 | 146 |
| 146 CastDeviceCache* CastConfigDelegateMediaRouter::devices() { | 147 CastDeviceCache* CastConfigClientMediaRouter::devices() { |
| 147 // The CastDeviceCache instance is lazily allocated because the MediaRouter | 148 // The CastDeviceCache instance is lazily allocated because the MediaRouter |
| 148 // component is not ready when the constructor is invoked. | 149 // component is not ready when the constructor is invoked. |
| 149 if (!devices_ && GetMediaRouter()) { | 150 if (!devices_ && GetMediaRouter()) { |
| 150 devices_ = base::MakeUnique<CastDeviceCache>(this); | 151 devices_ = base::MakeUnique<CastDeviceCache>(this); |
| 151 devices_->Init(); | 152 devices_->Init(); |
| 152 } | 153 } |
| 153 | 154 |
| 154 return devices_.get(); | 155 return devices_.get(); |
| 155 } | 156 } |
| 156 | 157 |
| 157 void CastConfigDelegateMediaRouter::RequestDeviceRefresh() { | 158 void CastConfigClientMediaRouter::RequestDeviceRefresh() { |
| 158 // The media router component isn't ready yet. | 159 // The media router component isn't ready yet. |
| 159 if (!devices()) | 160 if (!devices()) |
| 160 return; | 161 return; |
| 161 | 162 |
| 162 // Build the old-style SinkAndRoute set out of the MediaRouter | 163 // Build the old-style SinkAndRoute set out of the MediaRouter |
| 163 // source/sink/route setup. We first map the existing sinks, and then we | 164 // source/sink/route setup. We first map the existing sinks, and then we |
| 164 // update those sinks with activity information. | 165 // update those sinks with activity information. |
| 165 | 166 |
| 166 SinksAndRoutes items; | 167 std::vector<ash::mojom::SinkAndRoutePtr> items; |
| 167 | 168 |
| 168 for (const media_router::MediaSink& sink : devices()->sinks()) { | 169 for (const media_router::MediaSink& sink : devices()->sinks()) { |
| 169 SinkAndRoute sr; | 170 ash::mojom::SinkAndRoutePtr sr = ash::mojom::SinkAndRoute::New(); |
| 170 sr.sink.id = sink.id(); | 171 sr->route = ash::mojom::CastRoute::New(); |
| 171 sr.sink.name = base::UTF8ToUTF16(sink.name()); | 172 sr->sink = ash::mojom::CastSink::New(); |
| 172 sr.sink.domain = base::UTF8ToUTF16(sink.domain()); | 173 sr->sink->id = sink.id(); |
| 173 items.push_back(sr); | 174 sr->sink->name = sink.name(); |
| 175 sr->sink->domain = sink.domain(); |
| 176 items.push_back(std::move(sr)); |
| 174 } | 177 } |
| 175 | 178 |
| 176 for (const media_router::MediaRoute& route : devices()->routes()) { | 179 for (const media_router::MediaRoute& route : devices()->routes()) { |
| 177 if (!route.for_display()) | 180 if (!route.for_display()) |
| 178 continue; | 181 continue; |
| 179 | 182 |
| 180 for (SinkAndRoute& item : items) { | 183 for (ash::mojom::SinkAndRoutePtr& item : items) { |
| 181 if (item.sink.id == route.media_sink_id()) { | 184 if (item->sink->id == route.media_sink_id()) { |
| 182 item.route.id = route.media_route_id(); | 185 item->route->id = route.media_route_id(); |
| 183 item.route.title = | 186 item->route->title = StripEndingTab(route.description()); |
| 184 base::UTF8ToUTF16(StripEndingTab(route.description())); | 187 item->route->is_local_source = route.is_local(); |
| 185 item.route.is_local_source = route.is_local(); | |
| 186 | 188 |
| 187 // Default to a tab/app capture. This will display the media router | 189 // Default to a tab/app capture. This will display the media router |
| 188 // description. This means we will properly support DIAL casts. | 190 // description. This means we will properly support DIAL casts. |
| 189 item.route.content_source = Route::ContentSource::TAB; | 191 item->route->content_source = ash::mojom::ContentSource::TAB; |
| 190 if (media_router::IsDesktopMirroringMediaSource(route.media_source())) | 192 if (media_router::IsDesktopMirroringMediaSource(route.media_source())) |
| 191 item.route.content_source = Route::ContentSource::DESKTOP; | 193 item->route->content_source = ash::mojom::ContentSource::DESKTOP; |
| 192 | 194 |
| 193 break; | 195 break; |
| 194 } | 196 } |
| 195 } | 197 } |
| 196 } | 198 } |
| 197 | 199 |
| 198 for (ash::CastConfigDelegate::Observer& observer : observer_list_) | 200 observers_.ForAllPtrs([&items](ash::mojom::CastConfigObserver* observer) { |
| 199 observer.OnDevicesUpdated(items); | 201 std::vector<ash::mojom::SinkAndRoutePtr> copy; |
| 202 for (auto& item : items) |
| 203 copy.push_back(item->Clone()); |
| 204 observer->OnDevicesUpdated(std::move(copy)); |
| 205 }); |
| 200 } | 206 } |
| 201 | 207 |
| 202 void CastConfigDelegateMediaRouter::CastToSink(const Sink& sink) { | 208 void CastConfigClientMediaRouter::CastToSink(ash::mojom::CastSinkPtr sink) { |
| 203 // TODO(imcheng): Pass in tab casting timeout. | 209 // TODO(imcheng): Pass in tab casting timeout. |
| 204 GetMediaRouter()->CreateRoute( | 210 GetMediaRouter()->CreateRoute( |
| 205 media_router::MediaSourceForDesktop().id(), sink.id, | 211 media_router::MediaSourceForDesktop().id(), sink->id, |
| 206 GURL("http://cros-cast-origin/"), nullptr, | 212 GURL("http://cros-cast-origin/"), nullptr, |
| 207 std::vector<media_router::MediaRouteResponseCallback>(), | 213 std::vector<media_router::MediaRouteResponseCallback>(), |
| 208 base::TimeDelta(), false); | 214 base::TimeDelta(), false); |
| 209 } | 215 } |
| 210 | 216 |
| 211 void CastConfigDelegateMediaRouter::StopCasting(const Route& route) { | 217 void CastConfigClientMediaRouter::StopCasting(ash::mojom::CastRoutePtr route) { |
| 212 GetMediaRouter()->TerminateRoute(route.id); | 218 GetMediaRouter()->TerminateRoute(route->id); |
| 213 } | 219 } |
| 214 | 220 |
| 215 void CastConfigDelegateMediaRouter::AddObserver( | 221 void CastConfigClientMediaRouter::AddObserver( |
| 216 ash::CastConfigDelegate::Observer* observer) { | 222 ash::mojom::CastConfigObserverAssociatedPtrInfo observer) { |
| 217 observer_list_.AddObserver(observer); | 223 ash::mojom::CastConfigObserverAssociatedPtr observer_ptr; |
| 224 observer_ptr.Bind(std::move(observer)); |
| 225 observers_.AddPtr(std::move(observer_ptr)); |
| 218 } | 226 } |
| 219 | 227 |
| 220 void CastConfigDelegateMediaRouter::RemoveObserver( | 228 void CastConfigClientMediaRouter::Observe( |
| 221 ash::CastConfigDelegate::Observer* observer) { | |
| 222 observer_list_.RemoveObserver(observer); | |
| 223 } | |
| 224 | |
| 225 void CastConfigDelegateMediaRouter::Observe( | |
| 226 int type, | 229 int type, |
| 227 const content::NotificationSource& source, | 230 const content::NotificationSource& source, |
| 228 const content::NotificationDetails& details) { | 231 const content::NotificationDetails& details) { |
| 229 switch (type) { | 232 switch (type) { |
| 230 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED: | 233 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED: |
| 231 // The active profile has changed, which means that the media router has | 234 // 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 | 235 // as well. Reset the device cache to ensure we are using up-to-date |
| 233 // object instances. | 236 // object instances. |
| 234 devices_.reset(); | 237 devices_.reset(); |
| 235 RequestDeviceRefresh(); | 238 RequestDeviceRefresh(); |
| 236 break; | 239 break; |
| 237 } | 240 } |
| 238 } | 241 } |
| OLD | NEW |