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

Side by Side Diff: chrome/common/extensions/extension_permission_set.h

Issue 7003098: Start refractoring extension permissions into ExtensionPermissionSet. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup and update ExtensionPermissionSet data model. Created 9 years, 6 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 (c) 2011 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 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_
7 #pragma once
8
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
13
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/singleton.h"
16 #include "base/scoped_ptr.h"
17 #include "base/string16.h"
18 #include "chrome/common/extensions/url_pattern_set.h"
19
20 class DictionaryValue;
21 class Extension;
22 class ExtensionPrefs;
23 class ListValue;
24
25 // When prompting the user to install or approve permissions, we display
26 // messages describing the effects of the permissions rather than listing the
27 // permissions themselves. Each ExtensionPermissionMessage represents one of the
28 // messages shown to the user.
29 class ExtensionPermissionMessage {
30 public:
31 // Do not reorder this enumeration. If you need to add a new enum, add it just
32 // prior to kEnumBoundary.
33 enum Id {
Matt Perry 2011/06/21 00:43:10 nit: rename to ID
jstritar 2011/06/21 23:12:16 Done.
34 kUnknown,
35 kNone,
36 kBookmarks,
37 kGeolocation,
38 kBrowsingHistory,
39 kTabs,
40 kManagement,
41 kDebugger,
42 kHosts1,
43 kHosts2,
44 kHosts3,
45 kHosts4OrMore,
46 kHostsAll,
47 kFullAccess,
48 kClipboard,
49 kEnumBoundary
50 };
51
52 // Creates the corresponding permission message for a list of hosts. This is
53 // simply a convenience method around the constructor, since the messages
54 // change depending on what hosts are present.
55 static ExtensionPermissionMessage CreateFromHostList(
56 const std::vector<std::string> hosts);
Matt Perry 2011/06/21 00:43:10 pass by ref: const std::vector<std::string>&
jstritar 2011/06/21 23:12:16 Done.
57
58 // Creates the corresponding permission message.
59 ExtensionPermissionMessage(Id id, string16 message);
Matt Perry 2011/06/21 00:43:10 pass string16 by const ref
jstritar 2011/06/21 23:12:16 Done.
60
Matt Perry 2011/06/21 00:43:10 nit: add a destructor, and define it in the .cc. (
jstritar 2011/06/21 23:12:16 Done.
61 // Gets the id of the permission message, which can be used in UMA
62 // histograms.
63 Id id() const { return id_; }
64
65 // Gets a localized message describing this permission. Please note that
66 // the message will be empty for message types TYPE_NONE and TYPE_UNKNOWN.
67 const string16& message() const { return message_; }
68
69 // Comparator to work with std::set.
70 bool operator<(const ExtensionPermissionMessage& that) const {
71 return id_ < that.id_;
72 }
73
74 private:
75 Id id_;
76 string16 message_;
77 };
78
79 typedef std::vector<ExtensionPermissionMessage> ExtensionPermissionMessages;
80
81 // The ExtensionAPIPermission is an immutable class that describes a single
82 // named permission (API permission).
83 class ExtensionAPIPermission {
84 public:
85 // Do not reorder this list. If you need to add a new enum, add it just prior
86 // to kEnumBoundary.
87 enum Id {
88 // Error codes.
89 kInvalid,
Matt Perry 2011/06/21 00:43:10 nit: can you make these 2 negative?
jstritar 2011/06/21 23:12:16 Done.
90 kUnknown,
91
92 // Default permission that every extension has implicity.
93 kDefault,
94
95 // Real permissions.
96 kBackground,
97 kBookmark,
98 kClipboardRead,
99 kClipboardWrite,
100 kContentSettings,
101 kContextMenus,
102 kCookie,
103 kChromePrivate,
104 kChromeosInfoPrivate,
105 kDebugger,
106 kExperimental,
107 kFileBrowserHandler,
108 kFileBrowserPrivate,
109 kGeolocation,
110 kHistory,
111 kIdle,
112 kManagement,
113 kMediaPlayerPrivate,
114 kNotification,
115 kProxy,
116 kTab,
117 kUnlimitedStorage,
118 kWebSocketProxyPrivate,
119 kWebstorePrivate,
120 kDevtools,
121 kPlugin,
122 kEnumBoundary
123 };
124
125 typedef std::set<Id> IdSet;
Matt Perry 2011/06/21 00:43:10 IDSet
jstritar 2011/06/21 23:12:16 Done.
126
127 ~ExtensionAPIPermission();
128
129 // Returns the localized permission message associated with this api.
130 ExtensionPermissionMessage GetMessage() const;
131
132 // TODO(jstritar): Extend ExtensionAPIPermission so each knows what functions
133 // it grants access to. (e.g., kDefault will grant access to functions that
134 // don't require explicit permissions).
135 // bool GrantsAccessToFunction(const std::string& function_name) const;
136
137 Id id() const { return id_; }
138
139 // Returns the message id associated with this permission.
140 ExtensionPermissionMessage::Id message_id() const {
141 return message_id_;
142 }
143
144 // Returns the name of this permission.
145 const char* name() const { return name_; }
146
147 // Returns true if this permission implies full access (e.g., native code).
148 bool implies_full_access() const { return implies_full_access_; }
149
150 // Returns true if this permission implies full URL access.
151 bool implies_full_url_access() const { return implies_full_url_access_; }
152
153 // Returns true if this permission can be accessed by hosted apps.
154 bool is_hosted_app() const { return is_hosted_app_; }
155
156 // Returns true if this permission can only be acquired by COMPONENT
157 // extensions.
158 bool is_component_only() const { return is_component_only_; }
159
160 private:
161 // Instances should only be constructed from within ExtensionPermissionsInfo.
162 friend class ExtensionPermissionsInfo;
163
164 explicit ExtensionAPIPermission(
165 Id id,
166 const char* name,
167 bool is_hosted_app,
168 bool is_component_only,
169 int l10n_message_id,
170 ExtensionPermissionMessage::Id message_id,
171 bool implies_full_access,
172 bool implies_full_url_access);
173
174 Id id_;
175 const char* name_;
176 bool implies_full_access_;
177 bool implies_full_url_access_;
178 bool is_hosted_app_;
179 bool is_component_only_;
180 int l10n_message_id_;
181 ExtensionPermissionMessage::Id message_id_;
182
183 };
184
185 typedef std::set<ExtensionAPIPermission::Id> ExtensionAPIPermissionSet;
186
187 // Singleton that holds the extension permission instances and provides static
188 // methods for accessing them.
189 class ExtensionPermissionsInfo {
190 public:
191 // Returns a pointer to the singleton instance.
192 static ExtensionPermissionsInfo* GetInstance();
193
194 // Returns the permission with the given |id|, and NULL if it doesn't exist.
195 static ExtensionAPIPermission* GetById(ExtensionAPIPermission::Id id);
Matt Perry 2011/06/21 00:43:10 GetByID
Matt Perry 2011/06/21 00:43:10 Why are all the public methods static? Consumers c
jstritar 2011/06/21 23:12:16 Done.
jstritar 2011/06/21 23:12:16 I originally thought they'd be easier to access li
196
197 // Returns the permission with the given |name|, and NULL if none
198 // exists.
199 static ExtensionAPIPermission* GetByName(std::string name);
200
201 // Returns a set containing all valid api permission ids.
202 static ExtensionAPIPermissionSet GetAll();
203
204 // Converts all the permission names in |permission_names| to permission ids.
205 static ExtensionAPIPermissionSet GetAllByName(
206 const std::set<std::string>& permission_names);
207
208 // Gets the total number of API permissions available to hosted apps.
209 static size_t GetHostedAppPermissionCount();
210
211 // Gets the total number of API permissions.
212 static size_t GetPermissionCount();
213
214 private:
215 ~ExtensionPermissionsInfo();
216 ExtensionPermissionsInfo();
217
218 // Registers an |alias| for a given permission |name|.
219 void RegisterAlias(const char* name, const char* alias);
220
221 // Registers a standard extension permission.
222 void RegisterExtensionPermission(
223 ExtensionAPIPermission::Id id,
224 const char* name,
225 int l10n_message_id,
226 ExtensionPermissionMessage::Id message_id);
227
228 // Registers a permission that can be accessed by hosted apps.
229 void RegisterHostedAppPermission(
230 ExtensionAPIPermission::Id id,
231 const char* name,
232 int l10n_message_id,
233 ExtensionPermissionMessage::Id message_id);
234
235 // Registers a permission accessible only by COMPONENT extensions.
236 void RegisterPrivatePermission(
237 ExtensionAPIPermission::Id id,
238 const char* name);
239
240 // Registers a permission with a custom set of attributes not satisfied
241 // by the other registration functions.
242 void RegisterPermission(
243 ExtensionAPIPermission::Id id,
244 const char* name,
245 int l10n_message_id,
246 ExtensionPermissionMessage::Id message_id,
247 bool is_hosted_app,
248 bool is_component_only,
249 bool implies_full_access,
250 bool implies_full_url_access);
251
252 // Maps permission ids to permissions.
253 typedef std::map<ExtensionAPIPermission::Id, ExtensionAPIPermission*> IdMap;
Matt Perry 2011/06/21 00:43:10 IDMap
jstritar 2011/06/21 23:12:16 Done.
254
255 // Maps names and aliases to permissions.
256 typedef std::map<std::string, ExtensionAPIPermission*> NameMap;
257
258 IdMap id_map_;
259 NameMap name_map_;
260
261 size_t hosted_app_permission_count_;
262 size_t permission_count_;
263
264 friend struct DefaultSingletonTraits<ExtensionPermissionsInfo>;
265 DISALLOW_COPY_AND_ASSIGN(ExtensionPermissionsInfo);
266 };
267
268 // The ExtensionPermissionSet is an immutable class that encapsulates an
269 // extension's permissions. The class exposes set operations for combining and
270 // manipulating the permissions.
271 class ExtensionPermissionSet {
272 public:
273 // Creates an empty permission set (e.g. default permissions).
274 ExtensionPermissionSet();
275
276 // Creates a new permission set based on the |extension| manifest data, and
277 // the api and host permissions (|apis| and |hosts|). The effective hosts
278 // of the newly created permission set will be infered from the |extension|
Matt Perry 2011/06/21 00:43:10 inferred
jstritar 2011/06/21 23:12:16 Done.
279 // manifest, |apis| and |hosts|.
280 ExtensionPermissionSet(const Extension* extension,
281 ExtensionAPIPermissionSet apis,
Matt Perry 2011/06/21 00:43:10 pass these args by const ref
jstritar 2011/06/21 23:12:16 Done.
282 URLPatternSet explicit_hosts);
283
284 // Creates a new permission set based on the specified data.
285 ExtensionPermissionSet(ExtensionAPIPermissionSet apis,
286 URLPatternSet explicit_hosts,
287 URLPatternSet scriptable_hosts);
288
289 ~ExtensionPermissionSet();
290
291 // TODO(jstritar): Implement this...
Matt Perry 2011/06/21 00:43:10 do you have a use case planned for these methods?
jstritar 2011/06/21 23:12:16 Yes, they'll be used by the optional permissions A
292 // Creates a new permission set equal to |set1| - |set2|.
293 // Passes ownership of the new set to the caller.
294 // static ExtensionPermissionSet* CreateDifference(
295 // const ExtensionPermissionSet* set1, const ExtensionPermissionSet* set2);
296
297 // TODO(jstritar): Implement this...
298 // Creates a new permission set equal to the intersection of |set1| and
299 // |set2|.
300 // Passes ownership of the new set to the caller.
301 // static ExtensionPermissionSet* CreateIntersection(
302 // const ExtensionPermissionSet* set1, const ExtensionPermissionSet* set2);
303
304 // Creates a new permission set equal to the union of |set1| and |set2|.
305 // Passes ownership of the new set to the caller.
306 static ExtensionPermissionSet* CreateUnion(
307 const ExtensionPermissionSet* set1, const ExtensionPermissionSet* set2);
308
309 // TODO(jstritar): Implement this...
310 // Returns true if |set| is a subset of this.
311 // bool Contains(const ExtensionPermissionSet* set) const;
312
313 // Gets the API permissions in this set as a set of strings.
314 std::set<std::string> GetAPIsAsStrings() const;
315
316 // Gets a list of the distinct hosts for displaying to the user.
317 // NOTE: do not use this for comparing permissions, since this disgards some
318 // information.
319 std::vector<std::string> GetDistinctHostsForDisplay() const;
320
321 // Gets the localized permission messages that represent this set.
322 ExtensionPermissionMessages GetPermissionMessages() const;
323
324 // Gets the localized permission messages that represent this set (represented
325 // as strings).
326 std::vector<string16> GetWarningMessages() const;
327
328 // Returns true if this is an empty set (e.g., the default permission set).
329 bool IsDefault() const;
Matt Perry 2011/06/21 00:43:10 I think IsEmpty would convey this more clearly.
jstritar 2011/06/21 23:12:16 Done.
330
331 // Returns true if the set has the specified API permission.
332 bool HasAPIPermission(ExtensionAPIPermission::Id permission) const;
333
334 // Returns true if the permissions in this set grant access to the specified
335 // |function_name|.
336 bool HasAccessToFunction(const std::string& function_name) const;
337
338 // Returns true if this includes permission to access |origin|.
339 bool HasExplicitAccessToOrigin(const GURL& origin) const;
Matt Perry 2011/06/21 00:43:10 Looks like this actually matches an URL, not an Or
jstritar 2011/06/21 23:12:16 The host permission paths are set to "/*" within E
Matt Perry 2011/06/22 00:01:22 But this class does nothing to enforce that the ar
jstritar 2011/06/22 15:44:47 Yeah, good point. I now remove all the explicit ho
340
341 // Returns true if this permission set includes access to script |url|.
342 bool HasScriptableAccessToURL(const GURL& url) const;
343
344 // Returns true if this permission set includes effective access to all
345 // origins.
346 bool HasEffectiveAccessToAllHosts() const;
347
348 // Returns true if this permission set includes effective access to |origin|.
349 bool HasEffectiveAccessToHost(const GURL& origin) const;
Matt Perry 2011/06/21 00:43:10 should be HasEffectAccessToURL - doesn't just matc
jstritar 2011/06/21 23:12:16 Yeah, good point, since the user script URLPattern
350
351 // Returns ture if this permission set effectively represents full access
352 // (e.g. native code).
353 bool HasEffectiveFullAccess() const;
354
355 // Returns true if this permission set includes permissions that are
356 // restricted to internal extensions.
357 bool HasPrivatePermissions() const;
358
359 // Returns true if |permissions| has a greater privilege level than this
360 // permission set (e.g., this permission set has less permissions).
361 bool HasLessPrivilegesThan(const ExtensionPermissionSet* permissions) const;
362
363 const ExtensionAPIPermissionSet& apis() const { return apis_; }
364
365 const URLPatternSet& effective_hosts() const { return effective_hosts_; }
366
367 const URLPatternSet& explicit_hosts() const { return explicit_hosts_; }
368
369 const URLPatternSet& scriptable_hosts() const { return scriptable_hosts_; }
370
371
372 private:
373 FRIEND_TEST_ALL_PREFIXES(ExtensionPermissionSetTest,
374 HasLessHostPrivilegesThan);
375
376 static std::vector<std::string> GetDistinctHosts(
377 const URLPatternList& host_patterns, bool include_rcd);
378
379 // Initializes the set based on |extension|'s manifest data.
380 void InitFromExtension(const Extension* extension);
381
382 // Initializes the effective host permission based on the data in this set.
383 void InitEffectiveHosts();
384
385 // Gets the permission messages for the API permissions.
386 std::set<ExtensionPermissionMessage> GetSimplePermissionMessages() const;
387
388 // Returns true if |permissions| has an elevated API privilege level than
389 // this set.
390 bool HasLessAPIPrivilegesThan(
391 const ExtensionPermissionSet* permissions) const;
392
393 // Returns true if |permissions| has more host permissions compared to this
394 // set.
395 bool HasLessHostPrivilegesThan(
396 const ExtensionPermissionSet* permissions) const;
397
398 // The api list is used when deciding if an extension can access certain
399 // extension APIs and features.
400 ExtensionAPIPermissionSet apis_;
401
402 // The list of hosts that can be accessed directly from the extension.
403 URLPatternSet explicit_hosts_;
404
405 // The list of hosts that can be scripted by content scripts.
406 URLPatternSet scriptable_hosts_;
407
408 // The list of hosts this effectively grants access to.
409 URLPatternSet effective_hosts_;
410 };
411
412 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698