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/webui/media_router/media_cast_mode.h" | 5 #include "chrome/browser/ui/webui/media_router/media_cast_mode.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/grit/generated_resources.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" | 10 #include "ui/base/l10n/l10n_util.h" |
12 | 11 |
13 namespace media_router { | 12 namespace media_router { |
14 | 13 |
15 namespace { | |
16 | |
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; | |
26 } | |
27 return truncated; | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 std::string MediaCastModeToDescription( | 14 std::string MediaCastModeToDescription( |
33 MediaCastMode mode, const std::string& host) { | 15 MediaCastMode mode, const std::string& host) { |
34 switch (mode) { | 16 switch (mode) { |
35 case MediaCastMode::DEFAULT: | 17 case MediaCastMode::DEFAULT: |
36 return l10n_util::GetStringFUTF8( | 18 return l10n_util::GetStringFUTF8( |
37 IDS_MEDIA_ROUTER_DEFAULT_CAST_MODE, | 19 IDS_MEDIA_ROUTER_DEFAULT_CAST_MODE, |
38 base::UTF8ToUTF16(TruncateHostToRegisteredDomain(host))); | 20 base::UTF8ToUTF16(host)); |
39 case MediaCastMode::TAB_MIRROR: | 21 case MediaCastMode::TAB_MIRROR: |
40 return l10n_util::GetStringUTF8( | 22 return l10n_util::GetStringUTF8( |
41 IDS_MEDIA_ROUTER_TAB_MIRROR_CAST_MODE); | 23 IDS_MEDIA_ROUTER_TAB_MIRROR_CAST_MODE); |
42 case MediaCastMode::DESKTOP_MIRROR: | 24 case MediaCastMode::DESKTOP_MIRROR: |
43 return l10n_util::GetStringUTF8( | 25 return l10n_util::GetStringUTF8( |
44 IDS_MEDIA_ROUTER_DESKTOP_MIRROR_CAST_MODE); | 26 IDS_MEDIA_ROUTER_DESKTOP_MIRROR_CAST_MODE); |
45 default: | 27 default: |
46 NOTREACHED(); | 28 NOTREACHED(); |
47 return ""; | 29 return ""; |
48 } | 30 } |
49 } | 31 } |
50 | 32 |
51 bool IsValidCastModeNum(int cast_mode_num) { | 33 bool IsValidCastModeNum(int cast_mode_num) { |
52 return cast_mode_num >= MediaCastMode::DEFAULT && | 34 return cast_mode_num >= MediaCastMode::DEFAULT && |
53 cast_mode_num < MediaCastMode::NUM_CAST_MODES; | 35 cast_mode_num < MediaCastMode::NUM_CAST_MODES; |
54 } | 36 } |
55 | 37 |
56 MediaCastMode GetPreferredCastMode(const CastModeSet& cast_modes) { | 38 MediaCastMode GetPreferredCastMode(const CastModeSet& cast_modes) { |
57 if (cast_modes.empty()) { | 39 if (cast_modes.empty()) { |
58 LOG(ERROR) << "Called with empty cast_modes!"; | 40 LOG(ERROR) << "Called with empty cast_modes!"; |
59 return MediaCastMode::DEFAULT; | 41 return MediaCastMode::DEFAULT; |
60 } | 42 } |
61 return *cast_modes.begin(); | 43 return *cast_modes.begin(); |
62 } | 44 } |
63 | 45 |
64 } // namespace media_router | 46 } // namespace media_router |
OLD | NEW |