Home | History | Annotate | Download | only in permissions
      1 // Copyright 2013 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 "chrome/common/extensions/permissions/permission_message_util.h"
      6 
      7 #include "base/strings/string_number_conversions.h"
      8 #include "base/strings/utf_string_conversions.h"
      9 #include "content/public/common/url_constants.h"
     10 #include "extensions/common/permissions/permission_message.h"
     11 #include "extensions/common/permissions/permission_set.h"
     12 #include "extensions/common/url_pattern_set.h"
     13 #include "grit/generated_resources.h"
     14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
     15 #include "ui/base/l10n/l10n_util.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.
     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 
     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                                            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                                            UTF8ToUTF16(host_list[0]),
     57                                            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                                            UTF8ToUTF16(host_list[0]),
     63                                            UTF8ToUTF16(host_list[1]),
     64                                            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       };
     77       std::vector<int> message_ids;
     78       for (size_t i = 0; i < arraysize(kRetainedFilesMessageIDs); i++) {
     79         message_ids.push_back(kRetainedFilesMessageIDs[i]);
     80       }
     81       message = l10n_util::GetPluralStringFUTF16(message_ids, host_list.size());
     82 
     83       for (size_t i = 0; i < host_list.size(); ++i) {
     84         if (i > 0)
     85           details += ASCIIToUTF16("\n");
     86         details += l10n_util::GetStringFUTF16(
     87             IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY,
     88             UTF8ToUTF16(host_list[i]));
     89       }
     90   }
     91 
     92   return PermissionMessage(message_id, message, details);
     93 }
     94 
     95 std::set<std::string> GetDistinctHosts(
     96     const URLPatternSet& host_patterns,
     97     bool include_rcd,
     98     bool exclude_file_scheme) {
     99   // Use a vector to preserve order (also faster than a map on small sets).
    100   // Each item is a host split into two parts: host without RCDs and
    101   // current best RCD.
    102   typedef std::vector<std::pair<std::string, std::string> > HostVector;
    103   HostVector hosts_best_rcd;
    104   for (URLPatternSet::const_iterator i = host_patterns.begin();
    105        i != host_patterns.end(); ++i) {
    106     if (exclude_file_scheme && i->scheme() == chrome::kFileScheme)
    107       continue;
    108 
    109     std::string host = i->host();
    110 
    111     // Add the subdomain wildcard back to the host, if necessary.
    112     if (i->match_subdomains())
    113       host = "*." + host;
    114 
    115     // If the host has an RCD, split it off so we can detect duplicates.
    116     std::string rcd;
    117     size_t reg_len = net::registry_controlled_domains::GetRegistryLength(
    118         host,
    119         net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
    120         net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
    121     if (reg_len && reg_len != std::string::npos) {
    122       if (include_rcd)  // else leave rcd empty
    123         rcd = host.substr(host.size() - reg_len);
    124       host = host.substr(0, host.size() - reg_len);
    125     }
    126 
    127     // Check if we've already seen this host.
    128     HostVector::iterator it = hosts_best_rcd.begin();
    129     for (; it != hosts_best_rcd.end(); ++it) {
    130       if (it->first == host)
    131         break;
    132     }
    133     // If this host was found, replace the RCD if this one is better.
    134     if (it != hosts_best_rcd.end()) {
    135       if (include_rcd && RcdBetterThan(rcd, it->second))
    136         it->second = rcd;
    137     } else {  // Previously unseen host, append it.
    138       hosts_best_rcd.push_back(std::make_pair(host, rcd));
    139     }
    140   }
    141 
    142   // Build up the final vector by concatenating hosts and RCDs.
    143   std::set<std::string> distinct_hosts;
    144   for (HostVector::iterator it = hosts_best_rcd.begin();
    145        it != hosts_best_rcd.end(); ++it)
    146     distinct_hosts.insert(it->first + it->second);
    147   return distinct_hosts;
    148 }
    149 
    150 }  // namespace permission_message_util
    151