f26dd5b460040dc5201f6e935462dad0b7a1287a
[platform/framework/web/crosswalk.git] / src / extensions / common / permissions / permission_message_util.cc
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/string_split.h"
9 #include "base/strings/utf_string_conversions.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/extensions_strings.h"
14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "url/url_constants.h"
17 #include "base/strings/string_split.h"
18
19 using extensions::PermissionMessage;
20 using extensions::PermissionSet;
21 using extensions::URLPatternSet;
22
23 namespace {
24
25 // Helper for GetDistinctHosts(): com > net > org > everything else.
26 bool RcdBetterThan(const std::string& a, const std::string& b) {
27   if (a == b)
28     return false;
29   if (a == "com")
30     return true;
31   if (a == "net")
32     return b != "com";
33   if (a == "org")
34     return b != "com" && b != "net";
35   return false;
36 }
37
38 }  // namespace
39
40 namespace permission_message_util {
41
42 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts) {
43   typedef std::pair<PermissionMessage::ID, int> MsgPair;
44   const MsgPair kMessagesList[] = {
45       std::make_pair(PermissionMessage::kHosts1,
46                      IDS_EXTENSION_PROMPT_WARNING_1_HOST),
47       std::make_pair(PermissionMessage::kHosts2,
48                      IDS_EXTENSION_PROMPT_WARNING_2_HOSTS),
49       std::make_pair(PermissionMessage::kHosts3,
50                      IDS_EXTENSION_PROMPT_WARNING_3_HOSTS),
51       std::make_pair(PermissionMessage::kHosts4OrMore,
52                      IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST)};
53
54   int host_msg_id = hosts.size() < arraysize(kMessagesList)
55                         ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN
56                         : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST;
57   std::vector<base::string16> host_list;
58   for (std::set<std::string>::const_iterator it = hosts.begin();
59        it != hosts.end();
60        ++it) {
61     std::string host = *it;
62     host_list.push_back(
63         host[0] == '*' && host[1] == '.'
64             ? l10n_util::GetStringFUTF16(host_msg_id,
65                                          base::UTF8ToUTF16(host.erase(0, 2)))
66             : base::UTF8ToUTF16(host));
67   }
68   DCHECK(host_list.size());
69
70   if (host_list.size() < arraysize(kMessagesList)) {
71     return PermissionMessage(
72         kMessagesList[host_list.size() - 1].first,
73         l10n_util::GetStringFUTF16(
74             kMessagesList[host_list.size() - 1].second, host_list, NULL));
75   }
76
77   base::string16 details;
78   for (size_t i = 0; i < host_list.size(); ++i) {
79     if (i > 0)
80       details += base::ASCIIToUTF16("\n");
81     details += l10n_util::GetStringFUTF16(
82         IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY, host_list[i]);
83   }
84   return PermissionMessage(
85       kMessagesList[arraysize(kMessagesList) - 1].first,
86       l10n_util::GetStringUTF16(
87           kMessagesList[arraysize(kMessagesList) - 1].second),
88       details);
89 }
90
91 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns,
92                                        bool include_rcd,
93                                        bool exclude_file_scheme) {
94   // Use a vector to preserve order (also faster than a map on small sets).
95   // Each item is a host split into two parts: host without RCDs and
96   // current best RCD.
97   typedef base::StringPairs HostVector;
98   HostVector hosts_best_rcd;
99   for (URLPatternSet::const_iterator i = host_patterns.begin();
100        i != host_patterns.end();
101        ++i) {
102     if (exclude_file_scheme && i->scheme() == url::kFileScheme)
103       continue;
104
105     std::string host = i->host();
106
107     // Add the subdomain wildcard back to the host, if necessary.
108     if (i->match_subdomains())
109       host = "*." + host;
110
111     // If the host has an RCD, split it off so we can detect duplicates.
112     std::string rcd;
113     size_t reg_len = net::registry_controlled_domains::GetRegistryLength(
114         host,
115         net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
116         net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
117     if (reg_len && reg_len != std::string::npos) {
118       if (include_rcd)  // else leave rcd empty
119         rcd = host.substr(host.size() - reg_len);
120       host = host.substr(0, host.size() - reg_len);
121     }
122
123     // Check if we've already seen this host.
124     HostVector::iterator it = hosts_best_rcd.begin();
125     for (; it != hosts_best_rcd.end(); ++it) {
126       if (it->first == host)
127         break;
128     }
129     // If this host was found, replace the RCD if this one is better.
130     if (it != hosts_best_rcd.end()) {
131       if (include_rcd && RcdBetterThan(rcd, it->second))
132         it->second = rcd;
133     } else {  // Previously unseen host, append it.
134       hosts_best_rcd.push_back(std::make_pair(host, rcd));
135     }
136   }
137
138   // Build up the final vector by concatenating hosts and RCDs.
139   std::set<std::string> distinct_hosts;
140   for (HostVector::iterator it = hosts_best_rcd.begin();
141        it != hosts_best_rcd.end();
142        ++it)
143     distinct_hosts.insert(it->first + it->second);
144   return distinct_hosts;
145 }
146
147 }  // namespace permission_message_util