1 // Copyright 2013 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/common/extensions/permissions/permission_message_util.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/public/common/url_constants.h"
10 #include "extensions/common/permissions/permission_message.h"
11 #include "extensions/common/permissions/permission_set.h"
12 #include "extensions/common/url_pattern_set.h"
13 #include "grit/generated_resources.h"
14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
15 #include "ui/base/l10n/l10n_util.h"
17 using extensions::PermissionMessage;
18 using extensions::PermissionSet;
19 using extensions::URLPatternSet;
23 // Helper for GetDistinctHosts(): com > net > org > everything else.
24 bool RcdBetterThan(const std::string& a, const std::string& b) {
32 return b != "com" && b != "net";
38 namespace permission_message_util {
40 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts) {
41 std::vector<std::string> host_list(hosts.begin(), hosts.end());
42 DCHECK(host_list.size());
43 PermissionMessage::ID message_id;
47 switch (host_list.size()) {
49 message_id = PermissionMessage::kHosts1;
50 message = l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_1_HOST,
51 UTF8ToUTF16(host_list[0]));
54 message_id = PermissionMessage::kHosts2;
55 message = l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_2_HOSTS,
56 UTF8ToUTF16(host_list[0]),
57 UTF8ToUTF16(host_list[1]));
60 message_id = PermissionMessage::kHosts3;
61 message = l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_3_HOSTS,
62 UTF8ToUTF16(host_list[0]),
63 UTF8ToUTF16(host_list[1]),
64 UTF8ToUTF16(host_list[2]));
67 message_id = PermissionMessage::kHosts4OrMore;
69 const int kRetainedFilesMessageIDs[6] = {
70 IDS_EXTENSION_PROMPT_WARNING_HOSTS_DEFAULT,
71 IDS_EXTENSION_PROMPT_WARNING_HOST_SINGULAR,
72 IDS_EXTENSION_PROMPT_WARNING_HOSTS_ZERO,
73 IDS_EXTENSION_PROMPT_WARNING_HOSTS_TWO,
74 IDS_EXTENSION_PROMPT_WARNING_HOSTS_FEW,
75 IDS_EXTENSION_PROMPT_WARNING_HOSTS_MANY,
77 std::vector<int> message_ids;
78 for (size_t i = 0; i < arraysize(kRetainedFilesMessageIDs); i++) {
79 message_ids.push_back(kRetainedFilesMessageIDs[i]);
81 message = l10n_util::GetPluralStringFUTF16(message_ids, host_list.size());
83 for (size_t i = 0; i < host_list.size(); ++i) {
85 details += ASCIIToUTF16("\n");
86 details += l10n_util::GetStringFUTF16(
87 IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY,
88 UTF8ToUTF16(host_list[i]));
92 return PermissionMessage(message_id, message, details);
95 std::set<std::string> GetDistinctHosts(
96 const URLPatternSet& host_patterns,
98 bool exclude_file_scheme) {
99 // Use a vector to preserve order (also faster than a map on small sets).
100 // Each item is a host split into two parts: host without RCDs and
102 typedef std::vector<std::pair<std::string, std::string> > HostVector;
103 HostVector hosts_best_rcd;
104 for (URLPatternSet::const_iterator i = host_patterns.begin();
105 i != host_patterns.end(); ++i) {
106 if (exclude_file_scheme && i->scheme() == chrome::kFileScheme)
109 std::string host = i->host();
111 // Add the subdomain wildcard back to the host, if necessary.
112 if (i->match_subdomains())
115 // If the host has an RCD, split it off so we can detect duplicates.
117 size_t reg_len = net::registry_controlled_domains::GetRegistryLength(
119 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
120 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
121 if (reg_len && reg_len != std::string::npos) {
122 if (include_rcd) // else leave rcd empty
123 rcd = host.substr(host.size() - reg_len);
124 host = host.substr(0, host.size() - reg_len);
127 // Check if we've already seen this host.
128 HostVector::iterator it = hosts_best_rcd.begin();
129 for (; it != hosts_best_rcd.end(); ++it) {
130 if (it->first == host)
133 // If this host was found, replace the RCD if this one is better.
134 if (it != hosts_best_rcd.end()) {
135 if (include_rcd && RcdBetterThan(rcd, it->second))
137 } else { // Previously unseen host, append it.
138 hosts_best_rcd.push_back(std::make_pair(host, rcd));
142 // Build up the final vector by concatenating hosts and RCDs.
143 std::set<std::string> distinct_hosts;
144 for (HostVector::iterator it = hosts_best_rcd.begin();
145 it != hosts_best_rcd.end(); ++it)
146 distinct_hosts.insert(it->first + it->second);
147 return distinct_hosts;
150 } // namespace permission_message_util