Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / extensions / common / permissions / permission_message_util.cc
blobe4c1d09d742fb184ce011bf083c00e62954bec18
1 // Copyright 2014 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 "extensions/common/permissions/permission_message_util.h"
7 #include "base/macros.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "extensions/common/permissions/permission_message.h"
12 #include "extensions/common/permissions/permission_set.h"
13 #include "extensions/common/url_pattern_set.h"
14 #include "grit/extensions_strings.h"
15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "url/url_constants.h"
18 #include "base/strings/string_split.h"
20 using extensions::PermissionMessage;
21 using extensions::PermissionSet;
22 using extensions::URLPatternSet;
24 namespace {
26 // Helper for GetDistinctHosts(): com > net > org > everything else.
27 bool RcdBetterThan(const std::string& a, const std::string& b) {
28 if (a == b)
29 return false;
30 if (a == "com")
31 return true;
32 if (a == "net")
33 return b != "com";
34 if (a == "org")
35 return b != "com" && b != "net";
36 return false;
39 } // namespace
41 namespace permission_message_util {
43 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts,
44 PermissionMessageProperties properties) {
45 typedef std::pair<PermissionMessage::ID, int> MsgPair;
46 static const MsgPair kReadWriteMessagesList[] = {
47 std::make_pair(PermissionMessage::kHosts1,
48 IDS_EXTENSION_PROMPT_WARNING_1_HOST),
49 std::make_pair(PermissionMessage::kHosts2,
50 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS),
51 std::make_pair(PermissionMessage::kHosts3,
52 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS),
53 std::make_pair(PermissionMessage::kHosts4OrMore,
54 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST)};
55 static const MsgPair kReadOnlyMessagesList[] = {
56 std::make_pair(PermissionMessage::kHosts1ReadOnly,
57 IDS_EXTENSION_PROMPT_WARNING_1_HOST_READ_ONLY),
58 std::make_pair(PermissionMessage::kHosts2ReadOnly,
59 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS_READ_ONLY),
60 std::make_pair(PermissionMessage::kHosts3ReadOnly,
61 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS_READ_ONLY),
62 std::make_pair(PermissionMessage::kHosts4OrMoreReadOnly,
63 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST_READ_ONLY)};
64 COMPILE_ASSERT(
65 arraysize(kReadWriteMessagesList) == arraysize(kReadOnlyMessagesList),
66 message_lists_different_size);
67 static const int kNumMessages = arraysize(kReadWriteMessagesList);
68 const MsgPair(&messages_list)[kNumMessages] =
69 properties == kReadOnly ? kReadOnlyMessagesList : kReadWriteMessagesList;
71 int host_msg_id = hosts.size() < kNumMessages
72 ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN
73 : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST;
74 std::vector<base::string16> host_list;
75 for (std::set<std::string>::const_iterator it = hosts.begin();
76 it != hosts.end();
77 ++it) {
78 std::string host = *it;
79 host_list.push_back(
80 host[0] == '*' && host[1] == '.'
81 ? l10n_util::GetStringFUTF16(host_msg_id,
82 base::UTF8ToUTF16(host.erase(0, 2)))
83 : base::UTF8ToUTF16(host));
85 DCHECK(host_list.size());
87 if (host_list.size() < kNumMessages) {
88 return PermissionMessage(
89 messages_list[host_list.size() - 1].first,
90 l10n_util::GetStringFUTF16(messages_list[host_list.size() - 1].second,
91 host_list, NULL));
94 base::string16 details;
95 for (size_t i = 0; i < host_list.size(); ++i) {
96 if (i > 0)
97 details += base::ASCIIToUTF16("\n");
98 details += host_list[i];
100 return PermissionMessage(
101 messages_list[arraysize(messages_list) - 1].first,
102 l10n_util::GetStringUTF16(
103 messages_list[arraysize(messages_list) - 1].second),
104 details);
107 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns,
108 bool include_rcd,
109 bool exclude_file_scheme) {
110 // Use a vector to preserve order (also faster than a map on small sets).
111 // Each item is a host split into two parts: host without RCDs and
112 // current best RCD.
113 typedef base::StringPairs HostVector;
114 HostVector hosts_best_rcd;
115 for (URLPatternSet::const_iterator i = host_patterns.begin();
116 i != host_patterns.end();
117 ++i) {
118 if (exclude_file_scheme && i->scheme() == url::kFileScheme)
119 continue;
121 std::string host = i->host();
123 // Add the subdomain wildcard back to the host, if necessary.
124 if (i->match_subdomains())
125 host = "*." + host;
127 // If the host has an RCD, split it off so we can detect duplicates.
128 std::string rcd;
129 size_t reg_len = net::registry_controlled_domains::GetRegistryLength(
130 host,
131 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
132 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
133 if (reg_len && reg_len != std::string::npos) {
134 if (include_rcd) // else leave rcd empty
135 rcd = host.substr(host.size() - reg_len);
136 host = host.substr(0, host.size() - reg_len);
139 // Check if we've already seen this host.
140 HostVector::iterator it = hosts_best_rcd.begin();
141 for (; it != hosts_best_rcd.end(); ++it) {
142 if (it->first == host)
143 break;
145 // If this host was found, replace the RCD if this one is better.
146 if (it != hosts_best_rcd.end()) {
147 if (include_rcd && RcdBetterThan(rcd, it->second))
148 it->second = rcd;
149 } else { // Previously unseen host, append it.
150 hosts_best_rcd.push_back(std::make_pair(host, rcd));
154 // Build up the final vector by concatenating hosts and RCDs.
155 std::set<std::string> distinct_hosts;
156 for (HostVector::iterator it = hosts_best_rcd.begin();
157 it != hosts_best_rcd.end();
158 ++it)
159 distinct_hosts.insert(it->first + it->second);
160 return distinct_hosts;
163 } // namespace permission_message_util