• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "extensions/common/permissions/permission_message_util.h"
6 
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "extensions/common/permissions/permission_message.h"
10 #include "extensions/common/permissions/permission_set.h"
11 #include "extensions/common/url_pattern_set.h"
12 #include "grit/extensions_strings.h"
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "url/url_constants.h"
16 
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.
RcdBetterThan(const std::string & a,const std::string & b)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
37 
38 namespace permission_message_util {
39 
CreateFromHostList(const std::set<std::string> & hosts)40 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts) {
41   std::vector<std::string> host_list(hosts.begin(), hosts.end());
42   DCHECK(host_list.size());
43   PermissionMessage::ID message_id;
44   base::string16 message;
45   base::string16 details;
46 
47   switch (host_list.size()) {
48     case 1:
49       message_id = PermissionMessage::kHosts1;
50       message = l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_1_HOST,
51                                            base::UTF8ToUTF16(host_list[0]));
52       break;
53     case 2:
54       message_id = PermissionMessage::kHosts2;
55       message = l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_2_HOSTS,
56                                            base::UTF8ToUTF16(host_list[0]),
57                                            base::UTF8ToUTF16(host_list[1]));
58       break;
59     case 3:
60       message_id = PermissionMessage::kHosts3;
61       message = l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_3_HOSTS,
62                                            base::UTF8ToUTF16(host_list[0]),
63                                            base::UTF8ToUTF16(host_list[1]),
64                                            base::UTF8ToUTF16(host_list[2]));
65       break;
66     default:
67       message_id = PermissionMessage::kHosts4OrMore;
68 
69       const int kRetainedFilesMessageIDs[6] = {
70           IDS_EXTENSION_PROMPT_WARNING_HOSTS_DEFAULT,
71           IDS_EXTENSION_PROMPT_WARNING_HOST_SINGULAR,
72           IDS_EXTENSION_PROMPT_WARNING_HOSTS_ZERO,
73           IDS_EXTENSION_PROMPT_WARNING_HOSTS_TWO,
74           IDS_EXTENSION_PROMPT_WARNING_HOSTS_FEW,
75           IDS_EXTENSION_PROMPT_WARNING_HOSTS_MANY, };
76       std::vector<int> message_ids;
77       for (size_t i = 0; i < arraysize(kRetainedFilesMessageIDs); i++) {
78         message_ids.push_back(kRetainedFilesMessageIDs[i]);
79       }
80       message = l10n_util::GetPluralStringFUTF16(message_ids, host_list.size());
81 
82       for (size_t i = 0; i < host_list.size(); ++i) {
83         if (i > 0)
84           details += base::ASCIIToUTF16("\n");
85         details += l10n_util::GetStringFUTF16(
86             IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY,
87             base::UTF8ToUTF16(host_list[i]));
88       }
89   }
90 
91   return PermissionMessage(message_id, message, details);
92 }
93 
GetDistinctHosts(const URLPatternSet & host_patterns,bool include_rcd,bool exclude_file_scheme)94 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns,
95                                        bool include_rcd,
96                                        bool exclude_file_scheme) {
97   // Use a vector to preserve order (also faster than a map on small sets).
98   // Each item is a host split into two parts: host without RCDs and
99   // current best RCD.
100   typedef std::vector<std::pair<std::string, std::string> > HostVector;
101   HostVector hosts_best_rcd;
102   for (URLPatternSet::const_iterator i = host_patterns.begin();
103        i != host_patterns.end();
104        ++i) {
105     if (exclude_file_scheme && i->scheme() == url::kFileScheme)
106       continue;
107 
108     std::string host = i->host();
109 
110     // Add the subdomain wildcard back to the host, if necessary.
111     if (i->match_subdomains())
112       host = "*." + host;
113 
114     // If the host has an RCD, split it off so we can detect duplicates.
115     std::string rcd;
116     size_t reg_len = net::registry_controlled_domains::GetRegistryLength(
117         host,
118         net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
119         net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
120     if (reg_len && reg_len != std::string::npos) {
121       if (include_rcd)  // else leave rcd empty
122         rcd = host.substr(host.size() - reg_len);
123       host = host.substr(0, host.size() - reg_len);
124     }
125 
126     // Check if we've already seen this host.
127     HostVector::iterator it = hosts_best_rcd.begin();
128     for (; it != hosts_best_rcd.end(); ++it) {
129       if (it->first == host)
130         break;
131     }
132     // If this host was found, replace the RCD if this one is better.
133     if (it != hosts_best_rcd.end()) {
134       if (include_rcd && RcdBetterThan(rcd, it->second))
135         it->second = rcd;
136     } else {  // Previously unseen host, append it.
137       hosts_best_rcd.push_back(std::make_pair(host, rcd));
138     }
139   }
140 
141   // Build up the final vector by concatenating hosts and RCDs.
142   std::set<std::string> distinct_hosts;
143   for (HostVector::iterator it = hosts_best_rcd.begin();
144        it != hosts_best_rcd.end();
145        ++it)
146     distinct_hosts.insert(it->first + it->second);
147   return distinct_hosts;
148 }
149 
150 }  // namespace permission_message_util
151