OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/permissions/permission_message_util.h" | 5 #include "extensions/common/permissions/permission_message_util.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "extensions/common/permissions/coalesced_permission_message.h" | 11 #include "extensions/common/permissions/coalesced_permission_message.h" |
12 #include "extensions/common/permissions/permission_message.h" | |
13 #include "extensions/common/permissions/permission_set.h" | 12 #include "extensions/common/permissions/permission_set.h" |
14 #include "extensions/common/url_pattern_set.h" | 13 #include "extensions/common/url_pattern_set.h" |
15 #include "grit/extensions_strings.h" | 14 #include "grit/extensions_strings.h" |
16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
17 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
18 #include "url/url_constants.h" | 17 #include "url/url_constants.h" |
19 | 18 |
20 using extensions::PermissionMessage; | |
21 using extensions::URLPatternSet; | 19 using extensions::URLPatternSet; |
22 | 20 |
23 namespace { | 21 namespace { |
24 | 22 |
25 // Helper for GetDistinctHosts(): com > net > org > everything else. | 23 // Helper for GetDistinctHosts(): com > net > org > everything else. |
26 bool RcdBetterThan(const std::string& a, const std::string& b) { | 24 bool RcdBetterThan(const std::string& a, const std::string& b) { |
27 if (a == b) | 25 if (a == b) |
28 return false; | 26 return false; |
29 if (a == "com") | 27 if (a == "com") |
30 return true; | 28 return true; |
(...skipping 27 matching lines...) Expand all Loading... |
58 host_list.push_back( | 56 host_list.push_back( |
59 host[0] == '*' && host[1] == '.' | 57 host[0] == '*' && host[1] == '.' |
60 ? l10n_util::GetStringFUTF16(host_msg_id, | 58 ? l10n_util::GetStringFUTF16(host_msg_id, |
61 base::UTF8ToUTF16(host.erase(0, 2))) | 59 base::UTF8ToUTF16(host.erase(0, 2))) |
62 : base::UTF8ToUTF16(host)); | 60 : base::UTF8ToUTF16(host)); |
63 } | 61 } |
64 DCHECK(host_list.size()); | 62 DCHECK(host_list.size()); |
65 return host_list; | 63 return host_list; |
66 } | 64 } |
67 | 65 |
68 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts, | |
69 PermissionMessageProperties properties) { | |
70 typedef std::pair<PermissionMessage::ID, int> MsgPair; | |
71 static const MsgPair kReadWriteMessagesList[] = { | |
72 std::make_pair(PermissionMessage::kHosts1, | |
73 IDS_EXTENSION_PROMPT_WARNING_1_HOST), | |
74 std::make_pair(PermissionMessage::kHosts2, | |
75 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS), | |
76 std::make_pair(PermissionMessage::kHosts3, | |
77 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS), | |
78 std::make_pair(PermissionMessage::kHosts4OrMore, | |
79 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST)}; | |
80 static const MsgPair kReadOnlyMessagesList[] = { | |
81 std::make_pair(PermissionMessage::kHosts1ReadOnly, | |
82 IDS_EXTENSION_PROMPT_WARNING_1_HOST_READ_ONLY), | |
83 std::make_pair(PermissionMessage::kHosts2ReadOnly, | |
84 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS_READ_ONLY), | |
85 std::make_pair(PermissionMessage::kHosts3ReadOnly, | |
86 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS_READ_ONLY), | |
87 std::make_pair(PermissionMessage::kHosts4OrMoreReadOnly, | |
88 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST_READ_ONLY)}; | |
89 static_assert( | |
90 arraysize(kReadWriteMessagesList) == arraysize(kReadOnlyMessagesList), | |
91 "message lists should be of different size"); | |
92 static_assert(kNumMessages == arraysize(kReadWriteMessagesList), | |
93 "messages array should be of different size"); | |
94 | |
95 const MsgPair(&messages_list)[kNumMessages] = | |
96 properties == kReadOnly ? kReadOnlyMessagesList : kReadWriteMessagesList; | |
97 std::vector<base::string16> host_list = | |
98 GetHostListFromHosts(hosts, properties); | |
99 | |
100 if (host_list.size() < kNumMessages) { | |
101 return PermissionMessage( | |
102 messages_list[host_list.size() - 1].first, | |
103 l10n_util::GetStringFUTF16(messages_list[host_list.size() - 1].second, | |
104 host_list, NULL)); | |
105 } | |
106 | |
107 base::string16 details; | |
108 for (size_t i = 0; i < host_list.size(); ++i) { | |
109 if (i > 0) | |
110 details += base::ASCIIToUTF16("\n"); | |
111 details += host_list[i]; | |
112 } | |
113 return PermissionMessage( | |
114 messages_list[arraysize(messages_list) - 1].first, | |
115 l10n_util::GetStringUTF16( | |
116 messages_list[arraysize(messages_list) - 1].second), | |
117 details); | |
118 } | |
119 | |
120 void AddHostPermissions(extensions::PermissionIDSet* permissions, | 66 void AddHostPermissions(extensions::PermissionIDSet* permissions, |
121 const std::set<std::string>& hosts, | 67 const std::set<std::string>& hosts, |
122 PermissionMessageProperties properties) { | 68 PermissionMessageProperties properties) { |
123 std::vector<base::string16> host_list = | 69 std::vector<base::string16> host_list = |
124 GetHostListFromHosts(hosts, properties); | 70 GetHostListFromHosts(hosts, properties); |
125 | 71 |
126 // Create a separate permission for each host, and add it to the permissions | 72 // Create a separate permission for each host, and add it to the permissions |
127 // list. | 73 // list. |
128 // TODO(sashab): Add coalescing rules for kHostReadOnly and kHostReadWrite | 74 // TODO(sashab): Add coalescing rules for kHostReadOnly and kHostReadWrite |
129 // to mimic the current behavior of CreateFromHostList() above. | 75 // to mimic the current behavior of CreateFromHostList() above. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 // Build up the final vector by concatenating hosts and RCDs. | 131 // Build up the final vector by concatenating hosts and RCDs. |
186 std::set<std::string> distinct_hosts; | 132 std::set<std::string> distinct_hosts; |
187 for (HostVector::iterator it = hosts_best_rcd.begin(); | 133 for (HostVector::iterator it = hosts_best_rcd.begin(); |
188 it != hosts_best_rcd.end(); | 134 it != hosts_best_rcd.end(); |
189 ++it) | 135 ++it) |
190 distinct_hosts.insert(it->first + it->second); | 136 distinct_hosts.insert(it->first + it->second); |
191 return distinct_hosts; | 137 return distinct_hosts; |
192 } | 138 } |
193 | 139 |
194 } // namespace permission_message_util | 140 } // namespace permission_message_util |
OLD | NEW |