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

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: Rebase + small cleanup 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 {
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);
57
58 // Creates the corresponding permission message.
59 ExtensionPermissionMessage(ID id, const string16& message);
60 ~ExtensionPermissionMessage();
61
62 // Gets the id of the permission message, which can be used in UMA
63 // histograms.
64 ID id() const { return id_; }
65
66 // Gets a localized message describing this permission. Please note that
67 // the message will be empty for message types TYPE_NONE and TYPE_UNKNOWN.
68 const string16& message() const { return message_; }
69
70 // Comparator to work with std::set.
71 bool operator<(const ExtensionPermissionMessage& that) const {
72 return id_ < that.id_;
73 }
74
75 private:
76 ID id_;
77 string16 message_;
78 };
79
80 typedef std::vector<ExtensionPermissionMessage> ExtensionPermissionMessages;
81
82 // The ExtensionAPIPermission is an immutable class that describes a single
83 // named permission (API permission).
84 class ExtensionAPIPermission {
85 public:
86 // Do not reorder this list. If you need to add a new enum, add it just prior
Matt Perry 2011/06/22 00:01:22 This is no longer a requirement, right?
jstritar 2011/06/22 15:44:47 Oh, yes. Removed.
87 // to kEnumBoundary.
88 enum ID {
89 // Error codes.
90 kInvalid = -2,
91 kUnknown = -1,
92
93 // Default permission that every extension has implicity.
94 kDefault,
95
96 // Real permissions.
97 kBackground,
98 kBookmark,
99 kClipboardRead,
100 kClipboardWrite,
101 kContentSettings,
102 kContextMenus,
103 kCookie,
104 kChromePrivate,
105 kChromeosInfoPrivate,
106 kDebugger,
107 kExperimental,
108 kFileBrowserHandler,
109 kFileBrowserPrivate,
110 kGeolocation,
111 kHistory,
112 kIdle,
113 kManagement,
114 kMediaPlayerPrivate,
115 kNotification,
116 kProxy,
117 kTab,
118 kUnlimitedStorage,
119 kWebSocketProxyPrivate,
120 kWebstorePrivate,
121 kDevtools,
122 kPlugin,
123 kEnumBoundary
124 };
125
126 typedef std::set<ID> IDSet;
127
128 ~ExtensionAPIPermission();
129
130 // Returns the localized permission message associated with this api.
131 ExtensionPermissionMessage GetMessage() const;
132
133 ID id() const { return id_; }
134
135 // Returns the message id associated with this permission.
136 ExtensionPermissionMessage::ID message_id() const {
137 return message_id_;
138 }
139
140 // Returns the name of this permission.
141 const char* name() const { return name_; }
142
143 // Returns true if this permission implies full access (e.g., native code).
144 bool implies_full_access() const { return implies_full_access_; }
145
146 // Returns true if this permission implies full URL access.
147 bool implies_full_url_access() const { return implies_full_url_access_; }
148
149 // Returns true if this permission can be accessed by hosted apps.
150 bool is_hosted_app() const { return is_hosted_app_; }
151
152 // Returns true if this permission can only be acquired by COMPONENT
153 // extensions.
154 bool is_component_only() const { return is_component_only_; }
155
156 private:
157 // Instances should only be constructed from within ExtensionPermissionsInfo.
158 friend class ExtensionPermissionsInfo;
159
160 explicit ExtensionAPIPermission(
161 ID id,
162 const char* name,
163 bool is_hosted_app,
164 bool is_component_only,
165 int l10n_message_id,
166 ExtensionPermissionMessage::ID message_id,
167 bool implies_full_access,
168 bool implies_full_url_access);
169
170 ID id_;
171 const char* name_;
172 bool implies_full_access_;
173 bool implies_full_url_access_;
174 bool is_hosted_app_;
175 bool is_component_only_;
176 int l10n_message_id_;
177 ExtensionPermissionMessage::ID message_id_;
178 };
179
180 typedef std::set<ExtensionAPIPermission::ID> ExtensionAPIPermissionSet;
181
182 // Singleton that holds the extension permission instances and provides static
183 // methods for accessing them.
184 class ExtensionPermissionsInfo {
185 public:
186 // Returns a pointer to the singleton instance.
187 static ExtensionPermissionsInfo* GetInstance();
188
189 // Returns the permission with the given |id|, and NULL if it doesn't exist.
190 ExtensionAPIPermission* GetByID(ExtensionAPIPermission::ID id);
191
192 // Returns the permission with the given |name|, and NULL if none
193 // exists.
194 ExtensionAPIPermission* GetByName(std::string name);
195
196 // Returns a set containing all valid api permission ids.
197 ExtensionAPIPermissionSet GetAll();
198
199 // Converts all the permission names in |permission_names| to permission ids.
200 ExtensionAPIPermissionSet GetAllByName(
201 const std::set<std::string>& permission_names);
202
203 // Gets the total number of API permissions available to hosted apps.
204 size_t get_hosted_app_permission_count() {
205 return hosted_app_permission_count_;
206 }
207
208 // Gets the total number of API permissions.
209 size_t get_permission_count() { return permission_count_; }
210
211 private:
212 ~ExtensionPermissionsInfo();
213 ExtensionPermissionsInfo();
214
215 // Registers an |alias| for a given permission |name|.
216 void RegisterAlias(const char* name, const char* alias);
217
218 // Registers a standard extension permission.
219 void RegisterExtensionPermission(
220 ExtensionAPIPermission::ID id,
221 const char* name,
222 int l10n_message_id,
223 ExtensionPermissionMessage::ID message_id);
224
225 // Registers a permission that can be accessed by hosted apps.
226 void RegisterHostedAppPermission(
227 ExtensionAPIPermission::ID id,
228 const char* name,
229 int l10n_message_id,
230 ExtensionPermissionMessage::ID message_id);
231
232 // Registers a permission accessible only by COMPONENT extensions.
233 void RegisterPrivatePermission(
234 ExtensionAPIPermission::ID id,
235 const char* name);
236
237 // Registers a permission with a custom set of attributes not satisfied
238 // by the other registration functions.
239 void RegisterPermission(
240 ExtensionAPIPermission::ID id,
241 const char* name,
242 int l10n_message_id,
243 ExtensionPermissionMessage::ID message_id,
244 bool is_hosted_app,
245 bool is_component_only,
246 bool implies_full_access,
247 bool implies_full_url_access);
248
249 // Maps permission ids to permissions.
250 typedef std::map<ExtensionAPIPermission::ID, ExtensionAPIPermission*> IDMap;
251
252 // Maps names and aliases to permissions.
253 typedef std::map<std::string, ExtensionAPIPermission*> NameMap;
254
255 IDMap id_map_;
256 NameMap name_map_;
257
258 size_t hosted_app_permission_count_;
259 size_t permission_count_;
260
261 friend struct DefaultSingletonTraits<ExtensionPermissionsInfo>;
262 DISALLOW_COPY_AND_ASSIGN(ExtensionPermissionsInfo);
263 };
264
265 // The ExtensionPermissionSet is an immutable class that encapsulates an
266 // extension's permissions. The class exposes set operations for combining and
267 // manipulating the permissions.
268 class ExtensionPermissionSet {
269 public:
270 // Creates an empty permission set (e.g. default permissions).
271 ExtensionPermissionSet();
272
273 // Creates a new permission set based on the |extension| manifest data, and
274 // the api and host permissions (|apis| and |hosts|). The effective hosts
275 // of the newly created permission set will be inferred from the |extension|
276 // manifest, |apis| and |hosts|.
277 ExtensionPermissionSet(const Extension* extension,
278 const ExtensionAPIPermissionSet& apis,
279 const URLPatternSet& explicit_hosts);
280
281 // Creates a new permission set based on the specified data.
282 ExtensionPermissionSet(const ExtensionAPIPermissionSet& apis,
283 const URLPatternSet& explicit_hosts,
284 const URLPatternSet& scriptable_hosts);
285
286 ~ExtensionPermissionSet();
287
288 // Creates a new permission set equal to the union of |set1| and |set2|.
289 // Passes ownership of the new set to the caller.
290 static ExtensionPermissionSet* CreateUnion(
291 const ExtensionPermissionSet* set1, const ExtensionPermissionSet* set2);
292
293 // Gets the API permissions in this set as a set of strings.
294 std::set<std::string> GetAPIsAsStrings() const;
295
296 // Gets a list of the distinct hosts for displaying to the user.
297 // NOTE: do not use this for comparing permissions, since this disgards some
298 // information.
299 std::vector<std::string> GetDistinctHostsForDisplay() const;
300
301 // Gets the localized permission messages that represent this set.
302 ExtensionPermissionMessages GetPermissionMessages() const;
303
304 // Gets the localized permission messages that represent this set (represented
305 // as strings).
306 std::vector<string16> GetWarningMessages() const;
307
308 // Returns true if this is an empty set (e.g., the default permission set).
309 bool IsEmpty() const;
310
311 // Returns true if the set has the specified API permission.
312 bool HasAPIPermission(ExtensionAPIPermission::ID permission) const;
313
314 // Returns true if the permissions in this set grant access to the specified
315 // |function_name|.
316 bool HasAccessToFunction(const std::string& function_name) const;
317
318 // Returns true if this includes permission to access |origin|.
319 bool HasExplicitAccessToOrigin(const GURL& origin) const;
320
321 // Returns true if this permission set includes access to script |url|.
322 bool HasScriptableAccessToURL(const GURL& url) const;
323
324 // Returns true if this permission set includes effective access to all
325 // origins.
326 bool HasEffectiveAccessToAllHosts() const;
327
328 // Returns true if this permission set includes effective access to |url|.
329 bool HasEffectiveAccessToURL(const GURL& url) const;
330
331 // Returns ture if this permission set effectively represents full access
332 // (e.g. native code).
333 bool HasEffectiveFullAccess() const;
334
335 // Returns true if this permission set includes permissions that are
336 // restricted to internal extensions.
337 bool HasPrivatePermissions() const;
338
339 // Returns true if |permissions| has a greater privilege level than this
340 // permission set (e.g., this permission set has less permissions).
341 bool HasLessPrivilegesThan(const ExtensionPermissionSet* permissions) const;
342
343 const ExtensionAPIPermissionSet& apis() const { return apis_; }
344
345 const URLPatternSet& effective_hosts() const { return effective_hosts_; }
346
347 const URLPatternSet& explicit_hosts() const { return explicit_hosts_; }
348
349 const URLPatternSet& scriptable_hosts() const { return scriptable_hosts_; }
350
351 private:
352 FRIEND_TEST_ALL_PREFIXES(ExtensionPermissionSetTest,
353 HasLessHostPrivilegesThan);
354
355 static std::vector<std::string> GetDistinctHosts(
356 const URLPatternList& host_patterns, bool include_rcd);
357
358 // Initializes the set based on |extension|'s manifest data.
359 void InitImplicitExtensionPermissions(const Extension* extension);
360
361 // Initializes the effective host permission based on the data in this set.
362 void InitEffectiveHosts();
363
364 // Gets the permission messages for the API permissions.
365 std::set<ExtensionPermissionMessage> GetSimplePermissionMessages() const;
366
367 // Returns true if |permissions| has an elevated API privilege level than
368 // this set.
369 bool HasLessAPIPrivilegesThan(
370 const ExtensionPermissionSet* permissions) const;
371
372 // Returns true if |permissions| has more host permissions compared to this
373 // set.
374 bool HasLessHostPrivilegesThan(
375 const ExtensionPermissionSet* permissions) const;
376
377 // The api list is used when deciding if an extension can access certain
378 // extension APIs and features.
379 ExtensionAPIPermissionSet apis_;
380
381 // The list of hosts that can be accessed directly from the extension.
382 URLPatternSet explicit_hosts_;
383
384 // The list of hosts that can be scripted by content scripts.
385 URLPatternSet scriptable_hosts_;
386
387 // The list of hosts this effectively grants access to.
388 URLPatternSet effective_hosts_;
389 };
390
391 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_
OLDNEW
« no previous file with comments | « chrome/common/extensions/extension_manifests_unittest.cc ('k') | chrome/common/extensions/extension_permission_set.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698