Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / ui / webui / media_router / media_cast_mode.cc
blob66c76ebd3955851f29e9e54b03739e06c998971e
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 {
15 namespace {
17 std::string TruncateHostToRegisteredDomain(const std::string& host) {
18 const std::string truncated =
19 net::registry_controlled_domains::GetDomainAndRegistry(
20 host,
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()) {
25 return host;
27 return truncated;
30 } // namespace
32 std::string MediaCastModeToTitle(MediaCastMode mode, const std::string& host) {
33 switch (mode) {
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);
44 default:
45 NOTREACHED();
46 return "";
50 std::string MediaCastModeToDescription(
51 MediaCastMode mode, const std::string& host) {
52 switch (mode) {
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);
63 default:
64 NOTREACHED();
65 return "";
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