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

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

Issue 9317013: Add a centralized mechanism for whitelisting access to extension permissions. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: comments Created 8 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ 5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/singleton.h" 17 #include "base/memory/singleton.h"
18 #include "base/string16.h" 18 #include "base/string16.h"
19 #include "chrome/common/extensions/url_pattern_set.h" 19 #include "chrome/common/extensions/url_pattern_set.h"
20 20
21 // TODO(jstritar): Move each class to its own file in extensions/permissions.
22
21 class Extension; 23 class Extension;
22 class ExtensionPermissionsInfo; 24 class ExtensionPermissionsInfo;
23 25
24 // When prompting the user to install or approve permissions, we display 26 // When prompting the user to install or approve permissions, we display
25 // messages describing the effects of the permissions rather than listing the 27 // messages describing the effects of the permissions rather than listing the
26 // permissions themselves. Each ExtensionPermissionMessage represents one of the 28 // permissions themselves. Each ExtensionPermissionMessage represents one of the
27 // messages shown to the user. 29 // messages shown to the user.
28 class ExtensionPermissionMessage { 30 class ExtensionPermissionMessage {
29 public: 31 public:
30 // Do not reorder this enumeration. If you need to add a new enum, add it just 32 // Do not reorder this enumeration. If you need to add a new enum, add it just
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 enum Flag { 142 enum Flag {
141 kFlagNone = 0, 143 kFlagNone = 0,
142 144
143 // Indicates if the permission implies full access (native code). 145 // Indicates if the permission implies full access (native code).
144 kFlagImpliesFullAccess = 1 << 0, 146 kFlagImpliesFullAccess = 1 << 0,
145 147
146 // Indicates if the permission implies full URL access. 148 // Indicates if the permission implies full URL access.
147 kFlagImpliesFullURLAccess = 1 << 1, 149 kFlagImpliesFullURLAccess = 1 << 1,
148 150
149 // Indicates that the permission is private to COMPONENT extensions. 151 // Indicates that the permission is private to COMPONENT extensions.
150 kFlagComponentOnly = 1 << 2, 152 // Depcrecated: please use the whitelist.
153 kFlagComponentOnly_Deprecated = 1 << 2,
Aaron Boodman 2012/02/02 18:17:31 Sorry another nit here: I think it is fine to hav
jstritar 2012/02/03 14:33:17 Done.
151 154
152 // Indicates that extensions cannot specify the permission as optional. 155 // Indicates that extensions cannot specify the permission as optional.
153 kFlagCannotBeOptional = 1 << 3, 156 kFlagCannotBeOptional = 1 << 3
154 }; 157 };
155 158
156 // Flags for specifying what extension types can use the permission. 159 // Flags for specifying what extension types can use the permission.
157 enum TypeRestriction { 160 enum TypeRestriction {
158 kTypeNone = 0, 161 kTypeNone = 0,
159 162
160 // Extension::TYPE_EXTENSION and Extension::TYPE_USER_SCRIPT 163 // Extension::TYPE_EXTENSION and Extension::TYPE_USER_SCRIPT
161 kTypeExtension = 1 << 0, 164 kTypeExtension = 1 << 0,
162 165
163 // Extension::TYPE_HOSTED_APP 166 // Extension::TYPE_HOSTED_APP
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 206 }
204 207
205 // Returns true if this permission implies full URL access. 208 // Returns true if this permission implies full URL access.
206 bool implies_full_url_access() const { 209 bool implies_full_url_access() const {
207 return (flags_ & kFlagImpliesFullURLAccess) != 0; 210 return (flags_ & kFlagImpliesFullURLAccess) != 0;
208 } 211 }
209 212
210 // Returns true if this permission can only be acquired by COMPONENT 213 // Returns true if this permission can only be acquired by COMPONENT
211 // extensions. 214 // extensions.
212 bool is_component_only() const { 215 bool is_component_only() const {
213 return (flags_ & kFlagComponentOnly) != 0; 216 return (flags_ & kFlagComponentOnly_Deprecated) != 0;
214 } 217 }
215 218
219 // Returns true if access to this permission is restricted by a whitelist.
220 bool HasWhitelist() const;
221
222 // Returns true if |extension_id| is whitelisted. The return value is only
223 // relevant if this permission has a whitelist.
224 bool IsWhitelisted(const std::string& extension_id) const;
225
216 // Returns true if regular extensions can specify this permission. 226 // Returns true if regular extensions can specify this permission.
217 bool supports_extensions() const { 227 bool supports_extensions() const {
218 return (type_restrictions_ & kTypeExtension) != 0; 228 return (type_restrictions_ & kTypeExtension) != 0;
219 } 229 }
220 230
221 // Returns true if hosted apps can specify this permission. 231 // Returns true if hosted apps can specify this permission.
222 bool supports_hosted_apps() const { 232 bool supports_hosted_apps() const {
223 return (type_restrictions_ & kTypeHostedApp) != 0; 233 return (type_restrictions_ & kTypeHostedApp) != 0;
224 } 234 }
225 235
(...skipping 18 matching lines...) Expand all
244 return (type_restrictions_ & type) != 0; 254 return (type_restrictions_ & type) != 0;
245 } 255 }
246 256
247 private: 257 private:
248 // Instances should only be constructed from within ExtensionPermissionsInfo. 258 // Instances should only be constructed from within ExtensionPermissionsInfo.
249 friend class ExtensionPermissionsInfo; 259 friend class ExtensionPermissionsInfo;
250 260
251 // Register ALL the permissions! 261 // Register ALL the permissions!
252 static void RegisterAllPermissions(ExtensionPermissionsInfo* info); 262 static void RegisterAllPermissions(ExtensionPermissionsInfo* info);
253 263
264 typedef std::set<std::string> ExtensionWhitelist;
265
254 explicit ExtensionAPIPermission( 266 explicit ExtensionAPIPermission(
255 ID id, 267 ID id,
256 const char* name, 268 const char* name,
257 int l10n_message_id, 269 int l10n_message_id,
258 ExtensionPermissionMessage::ID message_id, 270 ExtensionPermissionMessage::ID message_id,
259 int flags, 271 int flags,
260 int type_restrictions); 272 int type_restrictions);
261 273
274 // Adds |extension_id| to the whitelist for this permission.
275 void AddToWhitelist(const std::string& extension_id);
276
262 ID id_; 277 ID id_;
263 const char* name_; 278 const char* name_;
264 int flags_; 279 int flags_;
265 int type_restrictions_; 280 int type_restrictions_;
266 int l10n_message_id_; 281 int l10n_message_id_;
267 ExtensionPermissionMessage::ID message_id_; 282 ExtensionPermissionMessage::ID message_id_;
283 ExtensionWhitelist whitelist_;
268 }; 284 };
269 285
270 typedef std::set<ExtensionAPIPermission::ID> ExtensionAPIPermissionSet; 286 typedef std::set<ExtensionAPIPermission::ID> ExtensionAPIPermissionSet;
271 287
272 // Singleton that holds the extension permission instances and provides static 288 // Singleton that holds the extension permission instances and provides static
273 // methods for accessing them. 289 // methods for accessing them.
274 class ExtensionPermissionsInfo { 290 class ExtensionPermissionsInfo {
275 public: 291 public:
276 // Returns a pointer to the singleton instance. 292 // Returns a pointer to the singleton instance.
277 static ExtensionPermissionsInfo* GetInstance(); 293 static ExtensionPermissionsInfo* GetInstance();
(...skipping 18 matching lines...) Expand all
296 private: 312 private:
297 friend class ExtensionAPIPermission; 313 friend class ExtensionAPIPermission;
298 314
299 ~ExtensionPermissionsInfo(); 315 ~ExtensionPermissionsInfo();
300 ExtensionPermissionsInfo(); 316 ExtensionPermissionsInfo();
301 317
302 // Registers an |alias| for a given permission |name|. 318 // Registers an |alias| for a given permission |name|.
303 void RegisterAlias(const char* name, const char* alias); 319 void RegisterAlias(const char* name, const char* alias);
304 320
305 // Registers a permission with the specified attributes and flags. 321 // Registers a permission with the specified attributes and flags.
306 void RegisterPermission( 322 ExtensionAPIPermission* RegisterPermission(
307 ExtensionAPIPermission::ID id, 323 ExtensionAPIPermission::ID id,
308 const char* name, 324 const char* name,
309 int l10n_message_id, 325 int l10n_message_id,
310 ExtensionPermissionMessage::ID message_id, 326 ExtensionPermissionMessage::ID message_id,
311 int flags, 327 int flags,
312 int type_restrictions); 328 int type_restrictions);
313 329
314 // Maps permission ids to permissions. 330 // Maps permission ids to permissions.
315 typedef std::map<ExtensionAPIPermission::ID, ExtensionAPIPermission*> IDMap; 331 typedef std::map<ExtensionAPIPermission::ID, ExtensionAPIPermission*> IDMap;
316 332
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 445
430 const ExtensionAPIPermissionSet& apis() const { return apis_; } 446 const ExtensionAPIPermissionSet& apis() const { return apis_; }
431 447
432 const URLPatternSet& effective_hosts() const { return effective_hosts_; } 448 const URLPatternSet& effective_hosts() const { return effective_hosts_; }
433 449
434 const URLPatternSet& explicit_hosts() const { return explicit_hosts_; } 450 const URLPatternSet& explicit_hosts() const { return explicit_hosts_; }
435 451
436 const URLPatternSet& scriptable_hosts() const { return scriptable_hosts_; } 452 const URLPatternSet& scriptable_hosts() const { return scriptable_hosts_; }
437 453
438 private: 454 private:
439 FRIEND_TEST_ALL_PREFIXES(ExtensionPermissionSetTest, 455 FRIEND_TEST_ALL_PREFIXES(ExtensionPermissionsTest,
440 HasLessHostPrivilegesThan); 456 HasLessHostPrivilegesThan);
441 457
442 friend class base::RefCountedThreadSafe<ExtensionPermissionSet>; 458 friend class base::RefCountedThreadSafe<ExtensionPermissionSet>;
443 459
444 static std::set<std::string> GetDistinctHosts( 460 static std::set<std::string> GetDistinctHosts(
445 const URLPatternSet& host_patterns, 461 const URLPatternSet& host_patterns,
446 bool include_rcd, 462 bool include_rcd,
447 bool exclude_file_scheme); 463 bool exclude_file_scheme);
448 464
449 // Initializes the set based on |extension|'s manifest data. 465 // Initializes the set based on |extension|'s manifest data.
(...skipping 25 matching lines...) Expand all
475 491
476 // The list of hosts that can be scripted by content scripts. 492 // The list of hosts that can be scripted by content scripts.
477 // TODO(jstritar): Rename to "user_script_hosts_"? 493 // TODO(jstritar): Rename to "user_script_hosts_"?
478 URLPatternSet scriptable_hosts_; 494 URLPatternSet scriptable_hosts_;
479 495
480 // The list of hosts this effectively grants access to. 496 // The list of hosts this effectively grants access to.
481 URLPatternSet effective_hosts_; 497 URLPatternSet effective_hosts_;
482 }; 498 };
483 499
484 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ 500 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698