OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/common/extensions/permissions/permission_message_util.h" | 5 #include "chrome/common/extensions/permissions/permission_message_util.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/common/extensions/permissions/permission_set.h" |
| 10 #include "content/public/common/url_constants.h" |
9 #include "extensions/common/permissions/permission_message.h" | 11 #include "extensions/common/permissions/permission_message.h" |
| 12 #include "extensions/common/url_pattern_set.h" |
10 #include "grit/generated_resources.h" | 13 #include "grit/generated_resources.h" |
| 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
11 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
12 | 16 |
13 using extensions::PermissionMessage; | 17 using extensions::PermissionMessage; |
| 18 using extensions::PermissionSet; |
| 19 using extensions::URLPatternSet; |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Helper for GetDistinctHosts(): com > net > org > everything else. |
| 24 bool RcdBetterThan(const std::string& a, const std::string& b) { |
| 25 if (a == b) |
| 26 return false; |
| 27 if (a == "com") |
| 28 return true; |
| 29 if (a == "net") |
| 30 return b != "com"; |
| 31 if (a == "org") |
| 32 return b != "com" && b != "net"; |
| 33 return false; |
| 34 } |
| 35 |
| 36 } // namespace |
14 | 37 |
15 namespace permission_message_util { | 38 namespace permission_message_util { |
16 | 39 |
17 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts) { | 40 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts) { |
18 std::vector<std::string> host_list(hosts.begin(), hosts.end()); | 41 std::vector<std::string> host_list(hosts.begin(), hosts.end()); |
19 DCHECK(host_list.size()); | 42 DCHECK(host_list.size()); |
20 PermissionMessage::ID message_id; | 43 PermissionMessage::ID message_id; |
21 string16 message; | 44 string16 message; |
22 string16 details; | 45 string16 details; |
23 | 46 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 details += ASCIIToUTF16("\n"); | 85 details += ASCIIToUTF16("\n"); |
63 details += l10n_util::GetStringFUTF16( | 86 details += l10n_util::GetStringFUTF16( |
64 IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY, | 87 IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY, |
65 UTF8ToUTF16(host_list[i])); | 88 UTF8ToUTF16(host_list[i])); |
66 } | 89 } |
67 } | 90 } |
68 | 91 |
69 return PermissionMessage(message_id, message, details); | 92 return PermissionMessage(message_id, message, details); |
70 } | 93 } |
71 | 94 |
| 95 std::set<std::string> GetDistinctHosts( |
| 96 const URLPatternSet& host_patterns, |
| 97 bool include_rcd, |
| 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 |
| 101 // current best RCD. |
| 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) |
| 107 continue; |
| 108 |
| 109 std::string host = i->host(); |
| 110 |
| 111 // Add the subdomain wildcard back to the host, if necessary. |
| 112 if (i->match_subdomains()) |
| 113 host = "*." + host; |
| 114 |
| 115 // If the host has an RCD, split it off so we can detect duplicates. |
| 116 std::string rcd; |
| 117 size_t reg_len = net::registry_controlled_domains::GetRegistryLength( |
| 118 host, |
| 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); |
| 125 } |
| 126 |
| 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) |
| 131 break; |
| 132 } |
| 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)) |
| 136 it->second = rcd; |
| 137 } else { // Previously unseen host, append it. |
| 138 hosts_best_rcd.push_back(std::make_pair(host, rcd)); |
| 139 } |
| 140 } |
| 141 |
| 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; |
| 148 } |
| 149 |
72 } // namespace permission_message_util | 150 } // namespace permission_message_util |
OLD | NEW |