Add ICU message format support
[chromium-blink-merge.git] / chrome / common / extensions / permissions / chrome_permission_message_provider.cc
blobbb24ab635774147e1cb8291137e0885cc3b248f8
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.
5 #include "chrome/common/extensions/permissions/chrome_permission_message_provider.h"
7 #include <vector>
9 #include "base/memory/scoped_vector.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/common/extensions/permissions/chrome_permission_message_rules.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "extensions/common/extensions_client.h"
17 #include "extensions/common/permissions/permission_message.h"
18 #include "extensions/common/permissions/permission_message_util.h"
19 #include "extensions/common/permissions/permission_set.h"
20 #include "extensions/common/url_pattern.h"
21 #include "extensions/common/url_pattern_set.h"
22 #include "grit/extensions_strings.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "url/gurl.h"
26 namespace extensions {
28 namespace {
30 // Copyable wrapper to make CoalescedPermissionMessages comparable.
31 class ComparablePermission {
32 public:
33 explicit ComparablePermission(const CoalescedPermissionMessage& msg)
34 : msg_(&msg) {}
36 bool operator<(const ComparablePermission& rhs) const {
37 if (msg_->message() < rhs.msg_->message())
38 return true;
39 if (msg_->message() > rhs.msg_->message())
40 return false;
41 return msg_->submessages() < rhs.msg_->submessages();
44 private:
45 const CoalescedPermissionMessage* msg_;
47 using ComparablePermissions = std::vector<ComparablePermission>;
49 } // namespace
51 typedef std::set<PermissionMessage> PermissionMsgSet;
53 ChromePermissionMessageProvider::ChromePermissionMessageProvider() {
56 ChromePermissionMessageProvider::~ChromePermissionMessageProvider() {
59 CoalescedPermissionMessages
60 ChromePermissionMessageProvider::GetPermissionMessages(
61 const PermissionIDSet& permissions) const {
62 std::vector<ChromePermissionMessageRule> rules =
63 ChromePermissionMessageRule::GetAllRules();
65 // Apply each of the rules, in order, to generate the messages for the given
66 // permissions. Once a permission is used in a rule, remove it from the set
67 // of available permissions so it cannot be applied to subsequent rules.
68 PermissionIDSet remaining_permissions = permissions;
69 CoalescedPermissionMessages messages;
70 for (const auto& rule : rules) {
71 // Only apply the rule if we have all the required permission IDs.
72 if (remaining_permissions.ContainsAllIDs(rule.required_permissions())) {
73 // We can apply the rule. Add all the required permissions, and as many
74 // optional permissions as we can, to the new message.
75 PermissionIDSet used_permissions =
76 remaining_permissions.GetAllPermissionsWithIDs(
77 rule.all_permissions());
78 messages.push_back(rule.GetPermissionMessage(used_permissions));
80 remaining_permissions =
81 PermissionIDSet::Difference(remaining_permissions, used_permissions);
85 return messages;
88 bool ChromePermissionMessageProvider::IsPrivilegeIncrease(
89 const PermissionSet* old_permissions,
90 const PermissionSet* new_permissions,
91 Manifest::Type extension_type) const {
92 // Things can't get worse than native code access.
93 if (old_permissions->HasEffectiveFullAccess())
94 return false;
96 // Otherwise, it's a privilege increase if the new one has full access.
97 if (new_permissions->HasEffectiveFullAccess())
98 return true;
100 if (IsHostPrivilegeIncrease(old_permissions, new_permissions, extension_type))
101 return true;
103 if (IsAPIPrivilegeIncrease(old_permissions, new_permissions))
104 return true;
106 if (IsManifestPermissionPrivilegeIncrease(old_permissions, new_permissions))
107 return true;
109 return false;
112 PermissionIDSet ChromePermissionMessageProvider::GetAllPermissionIDs(
113 const PermissionSet* permissions,
114 Manifest::Type extension_type) const {
115 PermissionIDSet permission_ids;
116 AddAPIPermissions(permissions, &permission_ids);
117 AddManifestPermissions(permissions, &permission_ids);
118 AddHostPermissions(permissions, &permission_ids, extension_type);
119 return permission_ids;
122 void ChromePermissionMessageProvider::AddAPIPermissions(
123 const PermissionSet* permissions,
124 PermissionIDSet* permission_ids) const {
125 for (const APIPermission* permission : permissions->apis())
126 permission_ids->InsertAll(permission->GetPermissions());
128 // A special hack: The warning message for declarativeWebRequest
129 // permissions speaks about blocking parts of pages, which is a
130 // subset of what the "<all_urls>" access allows. Therefore we
131 // display only the "<all_urls>" warning message if both permissions
132 // are required.
133 // TODO(treib): The same should apply to other permissions that are implied by
134 // "<all_urls>" (aka APIPermission::kHostsAll), such as kTab. This would
135 // happen automatically if we didn't differentiate between API/Manifest/Host
136 // permissions here.
137 if (permissions->ShouldWarnAllHosts())
138 permission_ids->erase(APIPermission::kDeclarativeWebRequest);
141 void ChromePermissionMessageProvider::AddManifestPermissions(
142 const PermissionSet* permissions,
143 PermissionIDSet* permission_ids) const {
144 for (const ManifestPermission* p : permissions->manifest_permissions())
145 permission_ids->InsertAll(p->GetPermissions());
148 void ChromePermissionMessageProvider::AddHostPermissions(
149 const PermissionSet* permissions,
150 PermissionIDSet* permission_ids,
151 Manifest::Type extension_type) const {
152 // Since platform apps always use isolated storage, they can't (silently)
153 // access user data on other domains, so there's no need to prompt.
154 // Note: this must remain consistent with IsHostPrivilegeIncrease.
155 // See crbug.com/255229.
156 if (extension_type == Manifest::TYPE_PLATFORM_APP)
157 return;
159 if (permissions->ShouldWarnAllHosts()) {
160 permission_ids->insert(APIPermission::kHostsAll);
161 } else {
162 URLPatternSet regular_hosts;
163 ExtensionsClient::Get()->FilterHostPermissions(
164 permissions->effective_hosts(), &regular_hosts, permission_ids);
166 std::set<std::string> hosts =
167 permission_message_util::GetDistinctHosts(regular_hosts, true, true);
168 if (!hosts.empty()) {
169 permission_message_util::AddHostPermissions(
170 permission_ids, hosts, permission_message_util::kReadWrite);
175 bool ChromePermissionMessageProvider::IsAPIPrivilegeIncrease(
176 const PermissionSet* old_permissions,
177 const PermissionSet* new_permissions) const {
178 PermissionIDSet old_ids;
179 AddAPIPermissions(old_permissions, &old_ids);
180 PermissionIDSet new_ids;
181 AddAPIPermissions(new_permissions, &new_ids);
183 // A special hack: kFileSystemWriteDirectory implies kFileSystemDirectory.
184 // TODO(sammc): Remove this. See http://crbug.com/284849.
185 if (old_ids.ContainsID(APIPermission::kFileSystemWriteDirectory))
186 old_ids.insert(APIPermission::kFileSystemDirectory);
188 return IsAPIOrManifestPrivilegeIncrease(old_ids, new_ids);
191 bool ChromePermissionMessageProvider::IsManifestPermissionPrivilegeIncrease(
192 const PermissionSet* old_permissions,
193 const PermissionSet* new_permissions) const {
194 PermissionIDSet old_ids;
195 AddManifestPermissions(old_permissions, &old_ids);
196 PermissionIDSet new_ids;
197 AddManifestPermissions(new_permissions, &new_ids);
199 return IsAPIOrManifestPrivilegeIncrease(old_ids, new_ids);
202 bool ChromePermissionMessageProvider::IsAPIOrManifestPrivilegeIncrease(
203 const PermissionIDSet& old_ids,
204 const PermissionIDSet& new_ids) const {
205 // If all the IDs were already there, it's not a privilege increase.
206 if (old_ids.Includes(new_ids))
207 return false;
209 // Otherwise, check the actual messages - not all IDs result in a message,
210 // and some messages can suppress others.
211 CoalescedPermissionMessages old_messages = GetPermissionMessages(old_ids);
212 CoalescedPermissionMessages new_messages = GetPermissionMessages(new_ids);
214 ComparablePermissions old_strings(old_messages.begin(), old_messages.end());
215 ComparablePermissions new_strings(new_messages.begin(), new_messages.end());
217 std::sort(old_strings.begin(), old_strings.end());
218 std::sort(new_strings.begin(), new_strings.end());
220 return !base::STLIncludes(old_strings, new_strings);
223 bool ChromePermissionMessageProvider::IsHostPrivilegeIncrease(
224 const PermissionSet* old_permissions,
225 const PermissionSet* new_permissions,
226 Manifest::Type extension_type) const {
227 // Platform apps host permission changes do not count as privilege increases.
228 // Note: this must remain consistent with AddHostPermissions.
229 if (extension_type == Manifest::TYPE_PLATFORM_APP)
230 return false;
232 // If the old permission set can access any host, then it can't be elevated.
233 if (old_permissions->HasEffectiveAccessToAllHosts())
234 return false;
236 // Likewise, if the new permission set has full host access, then it must be
237 // a privilege increase.
238 if (new_permissions->HasEffectiveAccessToAllHosts())
239 return true;
241 const URLPatternSet& old_list = old_permissions->effective_hosts();
242 const URLPatternSet& new_list = new_permissions->effective_hosts();
244 // TODO(jstritar): This is overly conservative with respect to subdomains.
245 // For example, going from *.google.com to www.google.com will be
246 // considered an elevation, even though it is not (http://crbug.com/65337).
247 std::set<std::string> new_hosts_set(
248 permission_message_util::GetDistinctHosts(new_list, false, false));
249 std::set<std::string> old_hosts_set(
250 permission_message_util::GetDistinctHosts(old_list, false, false));
251 std::set<std::string> new_hosts_only =
252 base::STLSetDifference<std::set<std::string> >(new_hosts_set,
253 old_hosts_set);
255 return !new_hosts_only.empty();
258 } // namespace extensions