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

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