1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/cast_config/cast_config_controller_media_router.h"
#include <string>
#include <utility>
#include <vector>
#include "ash/constants/ash_switches.h"
#include "base/check_deref.h"
#include "base/command_line.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ref.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/media/router/discovery/access_code/access_code_cast_feature.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
#include "components/application_locale_storage/application_locale_storage.h"
#include "components/media_router/browser/media_router.h"
#include "components/media_router/browser/media_router_factory.h"
#include "components/media_router/browser/media_routes_observer.h"
#include "components/media_router/browser/media_sinks_observer.h"
#include "components/media_router/common/media_sink.h"
#include "components/media_router/common/media_source.h"
#include "components/user_manager/user_manager.h"
#include "third_party/icu/source/common/unicode/uversion.h"
#include "third_party/icu/source/i18n/unicode/coll.h"
namespace {
std::optional<media_router::MediaRouter*> g_media_router_for_test;
Profile* GetProfile() {
if (!user_manager::UserManager::IsInitialized()) {
return nullptr;
}
auto* user = user_manager::UserManager::Get()->GetPrimaryUser();
if (!user) {
return nullptr;
}
return ash::ProfileHelper::Get()->GetProfileByUser(user);
}
// Returns the MediaRouter instance for the current primary profile, if there is
// one.
media_router::MediaRouter* GetMediaRouter() {
if (g_media_router_for_test) {
return *g_media_router_for_test;
}
Profile* profile = GetProfile();
if (!profile || !media_router::MediaRouterEnabled(profile)) {
return nullptr;
}
auto* router =
media_router::MediaRouterFactory::GetApiForBrowserContext(profile);
DCHECK(router);
return router;
}
} // namespace
// This class caches the values that the observers give us so we can query them
// at any point in time. It also emits a device refresh event when new data is
// available.
class CastDeviceCache : public media_router::MediaRoutesObserver,
public media_router::MediaSinksObserver {
public:
using MediaSinks = std::vector<media_router::MediaSink>;
using MediaRoutes = std::vector<media_router::MediaRoute>;
using MediaRouteIds = std::vector<media_router::MediaRoute::Id>;
// `application_locale_storage` must be non-null and must outlive `this`.
CastDeviceCache(const ApplicationLocaleStorage* application_locale_storage,
const base::RepeatingClosure& update_devices_callback);
CastDeviceCache(const CastDeviceCache&) = delete;
CastDeviceCache& operator=(const CastDeviceCache&) = delete;
~CastDeviceCache() override;
// This may run |update_devices_callback_| before returning.
void Init();
const MediaSinks& sinks() const { return sinks_; }
const MediaRoutes& routes() const { return routes_; }
private:
// media_router::MediaSinksObserver:
void OnSinksReceived(const MediaSinks& sinks) override;
// media_router::MediaRoutesObserver:
void OnRoutesUpdated(const MediaRoutes& routes) override;
// Sorts `sinks_` alphabetically.
void SortSinks();
const raw_ref<const ApplicationLocaleStorage> application_locale_storage_;
MediaSinks sinks_;
MediaRoutes routes_;
std::unique_ptr<icu::Collator> collator_;
base::RepeatingClosure update_devices_callback_;
};
CastDeviceCache::CastDeviceCache(
const ApplicationLocaleStorage* application_locale_storage,
const base::RepeatingClosure& update_devices_callback)
: MediaRoutesObserver(GetMediaRouter()),
MediaSinksObserver(GetMediaRouter(),
media_router::MediaSource::ForUnchosenDesktop(),
url::Origin()),
application_locale_storage_(CHECK_DEREF(application_locale_storage)),
update_devices_callback_(update_devices_callback) {}
CastDeviceCache::~CastDeviceCache() = default;
void CastDeviceCache::Init() {
CHECK(MediaSinksObserver::Init());
}
void CastDeviceCache::OnSinksReceived(const MediaSinks& sinks) {
sinks_.clear();
for (const media_router::MediaSink& sink : sinks) {
// The media router adds a MediaSink instance that doesn't have a name. Make
// sure to filter that sink out from the UI so it is not rendered, as it
// will be a line that only has a icon with no apparent meaning.
if (sink.name().empty()) {
continue;
}
sinks_.push_back(sink);
}
SortSinks();
update_devices_callback_.Run();
}
void CastDeviceCache::OnRoutesUpdated(const MediaRoutes& routes) {
routes_ = routes;
update_devices_callback_.Run();
}
void CastDeviceCache::SortSinks() {
if (sinks_.size() <= 1) {
return;
}
if (!collator_) {
UErrorCode error = U_ZERO_ERROR;
const std::string& locale = application_locale_storage_->Get();
collator_.reset(
icu::Collator::createInstance(icu::Locale(locale.c_str()), error));
if (U_FAILURE(error)) {
collator_.reset();
return;
}
}
const icu::Collator* collator_ptr = collator_.get();
std::sort(sinks_.begin(), sinks_.end(),
[collator_ptr](const media_router::MediaSink& sink1,
const media_router::MediaSink& sink2) {
return sink1.CompareUsingCollator(sink2, collator_ptr);
});
}
////////////////////////////////////////////////////////////////////////////////
// CastConfigControllerMediaRouter:
CastConfigControllerMediaRouter::CastConfigControllerMediaRouter(
const ApplicationLocaleStorage* application_locale_storage)
: application_locale_storage_(CHECK_DEREF(application_locale_storage)) {
// TODO(jdufault): This should use a callback interface once there is an
// equivalent. See crbug.com/666005.
session_observation_.Observe(session_manager::SessionManager::Get());
}
CastConfigControllerMediaRouter::~CastConfigControllerMediaRouter() {
StopObservingMirroringMediaControllerHosts();
}
void CastConfigControllerMediaRouter::OnFreezeInfoChanged() {
UpdateDevices();
}
// static
void CastConfigControllerMediaRouter::SetMediaRouterForTest(
media_router::MediaRouter* media_router) {
g_media_router_for_test = media_router;
}
CastDeviceCache* CastConfigControllerMediaRouter::device_cache() {
// The CastDeviceCache instance is lazily allocated because the MediaRouter
// component is not ready when the constructor is invoked.
if (!device_cache_ && GetMediaRouter()) {
device_cache_ = std::make_unique<CastDeviceCache>(
&application_locale_storage_.get(),
base::BindRepeating(
&CastConfigControllerMediaRouter::RequestDeviceRefresh,
base::Unretained(this)));
device_cache_->Init();
}
return device_cache_.get();
}
void CastConfigControllerMediaRouter::AddObserver(
CastConfigController::Observer* observer) {
observers_.AddObserver(observer);
}
void CastConfigControllerMediaRouter::RemoveObserver(
CastConfigController::Observer* observer) {
observers_.RemoveObserver(observer);
}
bool CastConfigControllerMediaRouter::HasMediaRouterForPrimaryProfile() const {
return !!GetMediaRouter();
}
bool CastConfigControllerMediaRouter::HasSinksAndRoutes() const {
return !devices_.empty();
}
bool CastConfigControllerMediaRouter::HasActiveRoute() const {
for (const auto& device : devices_) {
if (device.route.is_local_source && !device.route.title.empty()) {
return true;
}
}
return false;
}
bool CastConfigControllerMediaRouter::AccessCodeCastingEnabled() const {
Profile* profile = GetProfile();
return profile && media_router::GetAccessCodeCastEnabledPref(profile);
}
void CastConfigControllerMediaRouter::RequestDeviceRefresh() {
// The media router component isn't ready yet.
if (!device_cache()) {
return;
}
// Build the old-style SinkAndRoute set out of the MediaRouter
// source/sink/route setup. We first map the existing sinks, and then we
// update those sinks with activity information.
StopObservingMirroringMediaControllerHosts();
UpdateDevices();
if (!IsAccessCodeCastFreezeUiEnabled()) {
return;
}
for (auto& device : devices_) {
if (device.route.id.size() > 0) {
media_router::MirroringMediaControllerHost* freeze_host =
GetMediaRouter()->GetMirroringMediaControllerHost(device.route.id);
if (freeze_host) {
freeze_host->AddObserver(this);
}
}
}
}
const std::vector<ash::SinkAndRoute>&
CastConfigControllerMediaRouter::GetSinksAndRoutes() {
return devices_;
}
void CastConfigControllerMediaRouter::CastToSink(const std::string& sink_id) {
if (GetMediaRouter()) {
// TODO(takumif): Pass in tab casting timeout.
GetMediaRouter()->CreateRoute(
media_router::MediaSource::ForUnchosenDesktop().id(), sink_id,
url::Origin::Create(GURL("http://cros-cast-origin/")), nullptr,
base::DoNothing(), base::TimeDelta());
}
}
void CastConfigControllerMediaRouter::StopCasting(const std::string& route_id) {
if (GetMediaRouter()) {
GetMediaRouter()->TerminateRoute(route_id);
}
}
void CastConfigControllerMediaRouter::FreezeRoute(const std::string& route_id) {
if (!GetMediaRouter()) {
return;
}
media_router::MirroringMediaControllerHost* freeze_host =
GetMediaRouter()->GetMirroringMediaControllerHost(route_id);
if (!freeze_host) {
return;
}
freeze_host->Freeze();
}
void CastConfigControllerMediaRouter::UnfreezeRoute(
const std::string& route_id) {
if (!GetMediaRouter()) {
return;
}
media_router::MirroringMediaControllerHost* freeze_host =
GetMediaRouter()->GetMirroringMediaControllerHost(route_id);
if (!freeze_host) {
return;
}
freeze_host->Unfreeze();
}
void CastConfigControllerMediaRouter::OnUserProfileLoaded(
const AccountId& account_id) {
// The active profile has changed, which means that the media router has
// as well. Reset the device cache to ensure we are using up-to-date
// object instances.
device_cache_.reset();
RequestDeviceRefresh();
}
bool CastConfigControllerMediaRouter::IsAccessCodeCastFreezeUiEnabled() {
Profile* profile = GetProfile();
return profile && media_router::IsAccessCodeCastFreezeUiEnabled(profile);
}
void CastConfigControllerMediaRouter::
StopObservingMirroringMediaControllerHosts() {
for (const auto& device : devices_) {
auto route_id = device.route.id;
if (route_id.size() > 0) {
media_router::MirroringMediaControllerHost* mirroring_controller_host =
GetMediaRouter()->GetMirroringMediaControllerHost(route_id);
if (mirroring_controller_host) {
// It is safe to call RemoveObserver even if we are not observing a
// particular host.
mirroring_controller_host->RemoveObserver(this);
}
}
}
}
void CastConfigControllerMediaRouter::UpdateDevices() {
devices_.clear();
#if !defined(OFFICIAL_BUILD)
// Optionally add fake cast devices for manual UI testing.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
ash::switches::kQsAddFakeCastDevices)) {
AddFakeCastDevices();
}
#endif
for (const media_router::MediaSink& sink : device_cache()->sinks()) {
ash::SinkAndRoute device;
device.sink.id = sink.id();
device.sink.name = sink.name();
device.sink.sink_icon_type =
static_cast<ash::SinkIconType>(sink.icon_type());
devices_.push_back(std::move(device));
}
for (const media_router::MediaRoute& route : device_cache()->routes()) {
media_router::MirroringMediaControllerHost* freeze_host =
IsAccessCodeCastFreezeUiEnabled()
? GetMediaRouter()->GetMirroringMediaControllerHost(
route.media_route_id())
: nullptr;
for (ash::SinkAndRoute& device : devices_) {
if (device.sink.id == route.media_sink_id()) {
device.route.id = route.media_route_id();
device.route.title = route.description();
device.route.is_local_source = route.is_local();
// Only set freeze info if the appropriate feature is enabled. Else,
// values default to false and freeze ui is not shown.
if (freeze_host) {
device.route.freeze_info.can_freeze = freeze_host->CanFreeze();
device.route.freeze_info.is_frozen = freeze_host->IsFrozen();
}
// Default to a tab/app capture. This will display the media router
// description. This means we will properly support DIAL casts.
device.route.content_source =
route.media_source().IsDesktopMirroringSource()
? ash::ContentSource::kDesktop
: ash::ContentSource::kTab;
break;
}
}
}
for (auto& observer : observers_) {
observer.OnDevicesUpdated(devices_);
}
}
#if !defined(OFFICIAL_BUILD)
void CastConfigControllerMediaRouter::AddFakeCastDevices() {
// Add enough devices that the UI menu will scroll.
for (int i = 1; i <= 10; i++) {
ash::SinkAndRoute device;
device.sink.id = "fake_sink_id_" + base::NumberToString(i);
device.sink.name = "Fake Sink " + base::NumberToString(i);
device.sink.sink_icon_type = ash::SinkIconType::kCast;
devices_.push_back(std::move(device));
}
}
#endif // defined(OFFICIAL_BUILD)
|