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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/media_router/media_cast_mode.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
11 #include "ui/base/l10n/l10n_util.h"
13 namespace media_router
{
17 std::string
TruncateHostToRegisteredDomain(const std::string
& host
) {
18 const std::string truncated
=
19 net::registry_controlled_domains::GetDomainAndRegistry(
21 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
22 // The truncation will be empty in some scenarios (e.g. host is
23 // simply an IP address). Fail gracefully.
24 if (truncated
.empty()) {
32 std::string
MediaCastModeToTitle(MediaCastMode mode
, const std::string
& host
) {
34 case MediaCastMode::DEFAULT
:
35 return l10n_util::GetStringFUTF8(
36 IDS_MEDIA_ROUTER_DEFAULT_CAST_MODE_TITLE
,
37 base::UTF8ToUTF16(TruncateHostToRegisteredDomain(host
)));
38 case MediaCastMode::TAB_MIRROR
:
39 return l10n_util::GetStringUTF8(
40 IDS_MEDIA_ROUTER_TAB_MIRROR_CAST_MODE_TITLE
);
41 case MediaCastMode::DESKTOP_MIRROR
:
42 return l10n_util::GetStringUTF8(
43 IDS_MEDIA_ROUTER_DESKTOP_MIRROR_CAST_MODE_TITLE
);
50 std::string
MediaCastModeToDescription(
51 MediaCastMode mode
, const std::string
& host
) {
53 case MediaCastMode::DEFAULT
:
54 return l10n_util::GetStringFUTF8(
55 IDS_MEDIA_ROUTER_DEFAULT_CAST_MODE
,
56 base::UTF8ToUTF16(TruncateHostToRegisteredDomain(host
)));
57 case MediaCastMode::TAB_MIRROR
:
58 return l10n_util::GetStringUTF8(
59 IDS_MEDIA_ROUTER_TAB_MIRROR_CAST_MODE
);
60 case MediaCastMode::DESKTOP_MIRROR
:
61 return l10n_util::GetStringUTF8(
62 IDS_MEDIA_ROUTER_DESKTOP_MIRROR_CAST_MODE
);
69 bool IsValidCastModeNum(int cast_mode_num
) {
70 return cast_mode_num
>= MediaCastMode::DEFAULT
&&
71 cast_mode_num
< MediaCastMode::NUM_CAST_MODES
;
74 MediaCastMode
GetPreferredCastMode(const CastModeSet
& cast_modes
) {
75 if (cast_modes
.empty()) {
76 LOG(ERROR
) << "Called with empty cast_modes!";
77 return MediaCastMode::DEFAULT
;
79 return *cast_modes
.begin();
82 } // namespace media_router