Extensions: Remove PermissionMessage (permission_message.h/cc)
[chromium-blink-merge.git] / extensions / common / permissions / permission_message_util.cc
blobccd472297e4772a2c186adc10989a7c45b26ca5a
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;
21 namespace {
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;
36 } // namespace
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
42 // hosts.
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();
53 it != hosts.end();
54 ++it) {
55 std::string host = *it;
56 host_list.push_back(
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());
63 return host_list;
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
73 // list.
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,
80 host);
84 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns,
85 bool include_rcd,
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
89 // current best RCD.
90 typedef base::StringPairs HostVector;
91 HostVector hosts_best_rcd;
92 for (URLPatternSet::const_iterator i = host_patterns.begin();
93 i != host_patterns.end();
94 ++i) {
95 if (exclude_file_scheme && i->scheme() == url::kFileScheme)
96 continue;
98 std::string host = i->host();
100 // Add the subdomain wildcard back to the host, if necessary.
101 if (i->match_subdomains())
102 host = "*." + host;
104 // If the host has an RCD, split it off so we can detect duplicates.
105 std::string rcd;
106 size_t reg_len = net::registry_controlled_domains::GetRegistryLength(
107 host,
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)
120 break;
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))
125 it->second = rcd;
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();
135 ++it)
136 distinct_hosts.insert(it->first + it->second);
137 return distinct_hosts;
140 } // namespace permission_message_util