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/coalesced_permission_message.h"
12 #include "extensions/common/permissions/permission_message.h"
13 #include "extensions/common/permissions/permission_set.h"
14 #include "extensions/common/url_pattern_set.h"
15 #include "grit/extensions_strings.h"
16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "url/url_constants.h"
20 using extensions::PermissionMessage
;
21 using extensions::URLPatternSet
;
25 // Helper for GetDistinctHosts(): com > net > org > everything else.
26 bool RcdBetterThan(const std::string
& a
, const std::string
& b
) {
34 return b
!= "com" && b
!= "net";
40 namespace permission_message_util
{
42 // The number of host messages supported. The first N - 1 of these messages are
43 // specific for the number of hosts; the last one is a catch-all for N or more
45 static const int kNumMessages
= 4;
47 std::vector
<base::string16
> GetHostListFromHosts(
48 const std::set
<std::string
>& hosts
,
49 PermissionMessageProperties properties
) {
50 int host_msg_id
= hosts
.size() < kNumMessages
51 ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN
52 : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST
;
53 std::vector
<base::string16
> host_list
;
54 for (std::set
<std::string
>::const_iterator it
= hosts
.begin();
57 std::string host
= *it
;
59 host
[0] == '*' && host
[1] == '.'
60 ? l10n_util::GetStringFUTF16(host_msg_id
,
61 base::UTF8ToUTF16(host
.erase(0, 2)))
62 : base::UTF8ToUTF16(host
));
64 DCHECK(host_list
.size());
68 PermissionMessage
CreateFromHostList(const std::set
<std::string
>& hosts
,
69 PermissionMessageProperties properties
) {
70 typedef std::pair
<PermissionMessage::ID
, int> MsgPair
;
71 static const MsgPair kReadWriteMessagesList
[] = {
72 std::make_pair(PermissionMessage::kHosts1
,
73 IDS_EXTENSION_PROMPT_WARNING_1_HOST
),
74 std::make_pair(PermissionMessage::kHosts2
,
75 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS
),
76 std::make_pair(PermissionMessage::kHosts3
,
77 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS
),
78 std::make_pair(PermissionMessage::kHosts4OrMore
,
79 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST
)};
80 static const MsgPair kReadOnlyMessagesList
[] = {
81 std::make_pair(PermissionMessage::kHosts1ReadOnly
,
82 IDS_EXTENSION_PROMPT_WARNING_1_HOST_READ_ONLY
),
83 std::make_pair(PermissionMessage::kHosts2ReadOnly
,
84 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS_READ_ONLY
),
85 std::make_pair(PermissionMessage::kHosts3ReadOnly
,
86 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS_READ_ONLY
),
87 std::make_pair(PermissionMessage::kHosts4OrMoreReadOnly
,
88 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST_READ_ONLY
)};
90 arraysize(kReadWriteMessagesList
) == arraysize(kReadOnlyMessagesList
),
91 "message lists should be of different size");
92 static_assert(kNumMessages
== arraysize(kReadWriteMessagesList
),
93 "messages array should be of different size");
95 const MsgPair(&messages_list
)[kNumMessages
] =
96 properties
== kReadOnly
? kReadOnlyMessagesList
: kReadWriteMessagesList
;
97 std::vector
<base::string16
> host_list
=
98 GetHostListFromHosts(hosts
, properties
);
100 if (host_list
.size() < kNumMessages
) {
101 return PermissionMessage(
102 messages_list
[host_list
.size() - 1].first
,
103 l10n_util::GetStringFUTF16(messages_list
[host_list
.size() - 1].second
,
107 base::string16 details
;
108 for (size_t i
= 0; i
< host_list
.size(); ++i
) {
110 details
+= base::ASCIIToUTF16("\n");
111 details
+= host_list
[i
];
113 return PermissionMessage(
114 messages_list
[arraysize(messages_list
) - 1].first
,
115 l10n_util::GetStringUTF16(
116 messages_list
[arraysize(messages_list
) - 1].second
),
120 void AddHostPermissions(extensions::PermissionIDSet
* permissions
,
121 const std::set
<std::string
>& hosts
,
122 PermissionMessageProperties properties
) {
123 std::vector
<base::string16
> host_list
=
124 GetHostListFromHosts(hosts
, properties
);
126 // Create a separate permission for each host, and add it to the permissions
128 // TODO(sashab): Add coalescing rules for kHostReadOnly and kHostReadWrite
129 // to mimic the current behavior of CreateFromHostList() above.
130 for (const auto& host
: host_list
) {
131 permissions
->insert(properties
== kReadOnly
132 ? extensions::APIPermission::kHostReadOnly
133 : extensions::APIPermission::kHostReadWrite
,
138 std::set
<std::string
> GetDistinctHosts(const URLPatternSet
& host_patterns
,
140 bool exclude_file_scheme
) {
141 // Use a vector to preserve order (also faster than a map on small sets).
142 // Each item is a host split into two parts: host without RCDs and
144 typedef base::StringPairs HostVector
;
145 HostVector hosts_best_rcd
;
146 for (URLPatternSet::const_iterator i
= host_patterns
.begin();
147 i
!= host_patterns
.end();
149 if (exclude_file_scheme
&& i
->scheme() == url::kFileScheme
)
152 std::string host
= i
->host();
154 // Add the subdomain wildcard back to the host, if necessary.
155 if (i
->match_subdomains())
158 // If the host has an RCD, split it off so we can detect duplicates.
160 size_t reg_len
= net::registry_controlled_domains::GetRegistryLength(
162 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES
,
163 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
164 if (reg_len
&& reg_len
!= std::string::npos
) {
165 if (include_rcd
) // else leave rcd empty
166 rcd
= host
.substr(host
.size() - reg_len
);
167 host
= host
.substr(0, host
.size() - reg_len
);
170 // Check if we've already seen this host.
171 HostVector::iterator it
= hosts_best_rcd
.begin();
172 for (; it
!= hosts_best_rcd
.end(); ++it
) {
173 if (it
->first
== host
)
176 // If this host was found, replace the RCD if this one is better.
177 if (it
!= hosts_best_rcd
.end()) {
178 if (include_rcd
&& RcdBetterThan(rcd
, it
->second
))
180 } else { // Previously unseen host, append it.
181 hosts_best_rcd
.push_back(std::make_pair(host
, rcd
));
185 // Build up the final vector by concatenating hosts and RCDs.
186 std::set
<std::string
> distinct_hosts
;
187 for (HostVector::iterator it
= hosts_best_rcd
.begin();
188 it
!= hosts_best_rcd
.end();
190 distinct_hosts
.insert(it
->first
+ it
->second
);
191 return distinct_hosts
;
194 } // namespace permission_message_util