Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / media_router / media_cast_mode.cc
blob2ea1bbc6de85094c0b931cab798d0ccaaa1e0df1
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 MediaCastModeToDescription(
33 MediaCastMode mode, const std::string& host) {
34 switch (mode) {
35 case MediaCastMode::DEFAULT:
36 return l10n_util::GetStringFUTF8(
37 IDS_MEDIA_ROUTER_DEFAULT_CAST_MODE,
38 base::UTF8ToUTF16(TruncateHostToRegisteredDomain(host)));
39 case MediaCastMode::TAB_MIRROR:
40 return l10n_util::GetStringUTF8(
41 IDS_MEDIA_ROUTER_TAB_MIRROR_CAST_MODE);
42 case MediaCastMode::DESKTOP_MIRROR:
43 return l10n_util::GetStringUTF8(
44 IDS_MEDIA_ROUTER_DESKTOP_MIRROR_CAST_MODE);
45 default:
46 NOTREACHED();
47 return "";
51 bool IsValidCastModeNum(int cast_mode_num) {
52 return cast_mode_num >= MediaCastMode::DEFAULT &&
53 cast_mode_num < MediaCastMode::NUM_CAST_MODES;
56 MediaCastMode GetPreferredCastMode(const CastModeSet& cast_modes) {
57 if (cast_modes.empty()) {
58 LOG(ERROR) << "Called with empty cast_modes!";
59 return MediaCastMode::DEFAULT;
61 return *cast_modes.begin();
64 } // namespace media_router