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_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"
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 // The number of host messages supported. The first N - 1 of these messages are
41 // specific for the number of hosts; the last one is a catch-all for N or more
43 static const int kNumMessages
= 4;
45 std::vector
<base::string16
> GetHostListFromHosts(
46 const std::set
<std::string
>& hosts
,
47 PermissionMessageProperties properties
) {
48 int host_msg_id
= hosts
.size() < kNumMessages
49 ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN
50 : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST
;
51 std::vector
<base::string16
> host_list
;
52 for (std::set
<std::string
>::const_iterator it
= hosts
.begin();
55 std::string host
= *it
;
57 host
[0] == '*' && host
[1] == '.'
58 ? l10n_util::GetStringFUTF16(host_msg_id
,
59 base::UTF8ToUTF16(host
.erase(0, 2)))
60 : base::UTF8ToUTF16(host
));
62 DCHECK(host_list
.size());
66 void AddHostPermissions(extensions::PermissionIDSet
* permissions
,
67 const std::set
<std::string
>& hosts
,
68 PermissionMessageProperties properties
) {
69 std::vector
<base::string16
> host_list
=
70 GetHostListFromHosts(hosts
, properties
);
72 // Create a separate permission for each host, and add it to the permissions
74 // TODO(sashab): Add coalescing rules for kHostReadOnly and kHostReadWrite
75 // to mimic the current behavior of CreateFromHostList() above.
76 for (const auto& host
: host_list
) {
77 permissions
->insert(properties
== kReadOnly
78 ? extensions::APIPermission::kHostReadOnly
79 : extensions::APIPermission::kHostReadWrite
,
84 std::set
<std::string
> GetDistinctHosts(const URLPatternSet
& host_patterns
,
86 bool exclude_file_scheme
) {
87 // Use a vector to preserve order (also faster than a map on small sets).
88 // Each item is a host split into two parts: host without RCDs and
90 typedef base::StringPairs HostVector
;
91 HostVector hosts_best_rcd
;
92 for (URLPatternSet::const_iterator i
= host_patterns
.begin();
93 i
!= host_patterns
.end();
95 if (exclude_file_scheme
&& i
->scheme() == url::kFileScheme
)
98 std::string host
= i
->host();
100 // Add the subdomain wildcard back to the host, if necessary.
101 if (i
->match_subdomains())
104 // If the host has an RCD, split it off so we can detect duplicates.
106 size_t reg_len
= net::registry_controlled_domains::GetRegistryLength(
108 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES
,
109 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
110 if (reg_len
&& reg_len
!= std::string::npos
) {
111 if (include_rcd
) // else leave rcd empty
112 rcd
= host
.substr(host
.size() - reg_len
);
113 host
= host
.substr(0, host
.size() - reg_len
);
116 // Check if we've already seen this host.
117 HostVector::iterator it
= hosts_best_rcd
.begin();
118 for (; it
!= hosts_best_rcd
.end(); ++it
) {
119 if (it
->first
== host
)
122 // If this host was found, replace the RCD if this one is better.
123 if (it
!= hosts_best_rcd
.end()) {
124 if (include_rcd
&& RcdBetterThan(rcd
, it
->second
))
126 } else { // Previously unseen host, append it.
127 hosts_best_rcd
.push_back(std::make_pair(host
, rcd
));
131 // Build up the final vector by concatenating hosts and RCDs.
132 std::set
<std::string
> distinct_hosts
;
133 for (HostVector::iterator it
= hosts_best_rcd
.begin();
134 it
!= hosts_best_rcd
.end();
136 distinct_hosts
.insert(it
->first
+ it
->second
);
137 return distinct_hosts
;
140 } // namespace permission_message_util