Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Side by Side Diff: chrome/common/extensions/permissions/extension_permission_message_provider.cc

Issue 27446002: Move permission warning message handling from PermissionSet to PermissionMessageProvider. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/extension_permission_message_prov ider.h"
6
7 #include "base/stl_util.h"
8 #include "chrome/common/extensions/permissions/permission_message_util.h"
9 #include "chrome/common/extensions/permissions/permission_set.h"
10 #include "extensions/common/extensions_client.h"
11 #include "extensions/common/permissions/permission_message.h"
12 #include "extensions/common/url_pattern_set.h"
13 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
15
16 namespace extensions {
17
18 ExtensionPermissionMessageProvider::ExtensionPermissionMessageProvider() {
19 }
20
21 ExtensionPermissionMessageProvider::~ExtensionPermissionMessageProvider() {
22 }
23
24 // static
25 PermissionMessages ExtensionPermissionMessageProvider::GetPermissionMessages(
26 const PermissionSet* permissions) const {
27 PermissionMessages messages;
28
29 if (permissions->HasEffectiveFullAccess()) {
30 messages.push_back(PermissionMessage(
31 PermissionMessage::kFullAccess,
32 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS)));
33 return messages;
34 }
35
36 std::set<PermissionMessage> host_msgs =
37 GetHostPermissionMessages(permissions);
38 std::set<PermissionMessage> api_msgs = GetAPIPermissionMessages(permissions);
39 messages.insert(messages.end(), host_msgs.begin(), host_msgs.end());
40 messages.insert(messages.end(), api_msgs.begin(), api_msgs.end());
41
42 return messages;
43 }
44
45 // static
46 std::vector<string16> ExtensionPermissionMessageProvider::GetWarningMessages(
47 const PermissionSet* permissions) const {
48 std::vector<string16> message_strings;
49 PermissionMessages messages = GetPermissionMessages(permissions);
50
51 bool audio_capture = false;
52 bool video_capture = false;
53 bool media_galleries_read = false;
54 bool media_galleries_copy_to = false;
55 for (PermissionMessages::const_iterator i = messages.begin();
56 i != messages.end(); ++i) {
57 switch (i->id()) {
58 case PermissionMessage::kAudioCapture:
59 audio_capture = true;
60 break;
61 case PermissionMessage::kVideoCapture:
62 video_capture = true;
63 break;
64 case PermissionMessage::kMediaGalleriesAllGalleriesRead:
65 media_galleries_read = true;
66 break;
67 case PermissionMessage::kMediaGalleriesAllGalleriesCopyTo:
68 media_galleries_copy_to = true;
69 break;
70 default:
71 break;
72 }
73 }
74
75 for (PermissionMessages::const_iterator i = messages.begin();
76 i != messages.end(); ++i) {
77 int id = i->id();
78 if (audio_capture && video_capture) {
79 if (id == PermissionMessage::kAudioCapture) {
80 message_strings.push_back(l10n_util::GetStringUTF16(
81 IDS_EXTENSION_PROMPT_WARNING_AUDIO_AND_VIDEO_CAPTURE));
82 continue;
83 } else if (id == PermissionMessage::kVideoCapture) {
84 // The combined message will be pushed above.
85 continue;
86 }
87 }
88 if (media_galleries_read && media_galleries_copy_to) {
89 if (id == PermissionMessage::kMediaGalleriesAllGalleriesRead) {
90 message_strings.push_back(l10n_util::GetStringUTF16(
91 IDS_EXTENSION_PROMPT_WARNING_MEDIA_GALLERIES_READ_WRITE));
92 continue;
93 } else if (id == PermissionMessage::kMediaGalleriesAllGalleriesCopyTo) {
94 // The combined message will be pushed above.
95 continue;
96 }
97 }
98
99 message_strings.push_back(i->message());
100 }
101
102 return message_strings;
103 }
104
105 // static
106 std::vector<string16>
107 ExtensionPermissionMessageProvider::GetWarningMessagesDetails(
108 const PermissionSet* permissions) const {
109 std::vector<string16> message_strings;
110 PermissionMessages messages = GetPermissionMessages(permissions);
111
112 for (PermissionMessages::const_iterator i = messages.begin();
113 i != messages.end(); ++i)
114 message_strings.push_back(i->details());
115
116 return message_strings;
117 }
118
119 // static
120 bool ExtensionPermissionMessageProvider::IsPrivilegeIncrease(
121 const PermissionSet* old_permissions,
122 const PermissionSet* new_permissions) const {
123 // Things can't get worse than native code access.
124 if (old_permissions->HasEffectiveFullAccess())
125 return false;
126
127 // Otherwise, it's a privilege increase if the new one has full access.
128 if (new_permissions->HasEffectiveFullAccess())
129 return true;
130
131 if (IsHostPrivilegeIncrease(old_permissions, new_permissions))
132 return true;
133
134 if (IsAPIPrivilegeIncrease(old_permissions, new_permissions))
135 return true;
136
137 return false;
138 }
139
140 std::set<PermissionMessage>
141 ExtensionPermissionMessageProvider::GetAPIPermissionMessages(
142 const PermissionSet* permissions) const {
143 std::set<PermissionMessage> messages;
144 for (APIPermissionSet::const_iterator permission_it =
145 permissions->apis().begin();
146 permission_it != permissions->apis().end(); ++permission_it) {
147 if (permission_it->HasMessages()) {
148 PermissionMessages new_messages = permission_it->GetMessages();
149 messages.insert(new_messages.begin(), new_messages.end());
150 }
151 }
152
153 // A special hack: If kFileSystemWriteDirectory would be displayed, hide
154 // kFileSystemDirectory and and kFileSystemWrite as the write directory
155 // message implies the other two.
156 // TODO(sammc): Remove this. See http://crbug.com/284849.
157 std::set<PermissionMessage>::iterator write_directory_message =
158 messages.find(PermissionMessage(
159 PermissionMessage::kFileSystemWriteDirectory, string16()));
160 if (write_directory_message != messages.end()) {
161 messages.erase(
162 PermissionMessage(PermissionMessage::kFileSystemWrite, string16()));
163 messages.erase(
164 PermissionMessage(PermissionMessage::kFileSystemDirectory, string16()));
165 }
166
167 // A special hack: The warning message for declarativeWebRequest
168 // permissions speaks about blocking parts of pages, which is a
169 // subset of what the "<all_urls>" access allows. Therefore we
170 // display only the "<all_urls>" warning message if both permissions
171 // are required.
172 if (permissions->HasEffectiveAccessToAllHosts()) {
173 messages.erase(
174 PermissionMessage(
175 PermissionMessage::kDeclarativeWebRequest, string16()));
176 }
177
178 return messages;
179 }
180
181 std::set<PermissionMessage>
182 ExtensionPermissionMessageProvider::GetHostPermissionMessages(
183 const PermissionSet* permissions) const {
184 std::set<PermissionMessage> messages;
185 if (permissions->HasEffectiveAccessToAllHosts()) {
186 messages.insert(PermissionMessage(
187 PermissionMessage::kHostsAll,
188 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_ALL_HOSTS)));
189 } else {
190 URLPatternSet regular_hosts;
191 ExtensionsClient::Get()->FilterHostPermissions(
192 permissions->effective_hosts(), &regular_hosts, &messages);
193
194 std::set<std::string> hosts =
195 permission_message_util::GetDistinctHosts(regular_hosts, true, true);
196 if (!hosts.empty())
197 messages.insert(permission_message_util::CreateFromHostList(hosts));
198 }
199 return messages;
200 }
201
202 bool ExtensionPermissionMessageProvider::IsAPIPrivilegeIncrease(
203 const PermissionSet* old_permissions,
204 const PermissionSet* new_permissions) const {
205 if (new_permissions == NULL)
206 return false;
207
208 typedef std::set<PermissionMessage> PermissionMsgSet;
209 PermissionMsgSet old_warnings = GetAPIPermissionMessages(old_permissions);
210 PermissionMsgSet new_warnings = GetAPIPermissionMessages(new_permissions);
211 PermissionMsgSet delta_warnings =
212 base::STLSetDifference<PermissionMsgSet>(new_warnings, old_warnings);
213
214 // A special hack: kFileSystemWriteDirectory implies kFileSystemDirectory and
215 // kFileSystemWrite.
216 // TODO(sammc): Remove this. See http://crbug.com/284849.
217 if (old_warnings.find(PermissionMessage(
218 PermissionMessage::kFileSystemWriteDirectory, string16())) !=
219 old_warnings.end()) {
220 delta_warnings.erase(
221 PermissionMessage(PermissionMessage::kFileSystemDirectory, string16()));
222 delta_warnings.erase(
223 PermissionMessage(PermissionMessage::kFileSystemWrite, string16()));
224 }
225
226 // We have less privileges if there are additional warnings present.
227 return !delta_warnings.empty();
228 }
229
230 bool ExtensionPermissionMessageProvider::IsHostPrivilegeIncrease(
231 const PermissionSet* old_permissions,
232 const PermissionSet* new_permissions) const {
233 // If the old permission set can access any host, then it can't be elevated.
234 if (old_permissions->HasEffectiveAccessToAllHosts())
235 return false;
236
237 // Likewise, if the new permission set has full host access, then it must be
238 // a privilege increase.
239 if (new_permissions->HasEffectiveAccessToAllHosts())
240 return true;
241
242 const URLPatternSet& old_list = old_permissions->effective_hosts();
243 const URLPatternSet& new_list = new_permissions->effective_hosts();
244
245 // TODO(jstritar): This is overly conservative with respect to subdomains.
246 // For example, going from *.google.com to www.google.com will be
247 // considered an elevation, even though it is not (http://crbug.com/65337).
248 std::set<std::string> new_hosts_set(
249 permission_message_util::GetDistinctHosts(new_list, false, false));
250 std::set<std::string> old_hosts_set(
251 permission_message_util::GetDistinctHosts(old_list, false, false));
252 std::set<std::string> new_hosts_only =
253 base::STLSetDifference<std::set<std::string> >(new_hosts_set,
254 old_hosts_set);
255
256 return !new_hosts_only.empty();
257 }
258
259 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698