Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 kFlagImpliesFullAccess = 1 << 0, | 145 kFlagImpliesFullAccess = 1 << 0, |
| 144 | 146 |
| 145 // Indicates if the permission implies full URL access. | 147 // Indicates if the permission implies full URL access. |
| 146 kFlagImpliesFullURLAccess = 1 << 1, | 148 kFlagImpliesFullURLAccess = 1 << 1, |
| 147 | 149 |
| 148 // Indicates that the permission is private to COMPONENT extensions. | 150 // Indicates that the permission is private to COMPONENT extensions. |
| 149 kFlagComponentOnly = 1 << 2, | 151 kFlagComponentOnly = 1 << 2, |
| 150 | 152 |
| 151 // Indicates that extensions cannot specify the permission as optional. | 153 // Indicates that extensions cannot specify the permission as optional. |
| 152 kFlagCannotBeOptional = 1 << 3, | 154 kFlagCannotBeOptional = 1 << 3, |
| 155 | |
| 156 // When set, the permission can only be acquired by extensions on the | |
| 157 // permission's whitelist. Extensions must pass both tests when used in | |
| 158 // conjunction with kFlagComponentOnly. | |
| 159 kFlagEnforceWhitelist = 1 << 4, | |
|
Aaron Boodman
2012/02/02 08:09:24
Using 'and' here doesn't seem very useful. Are the
jstritar
2012/02/02 17:51:10
Done and deprecated kFlagComponentOnly.
| |
| 153 }; | 160 }; |
| 154 | 161 |
| 155 // Flags for specifying what extension types can use the permission. | 162 // Flags for specifying what extension types can use the permission. |
| 156 enum TypeRestriction { | 163 enum TypeRestriction { |
| 157 kTypeNone = 0, | 164 kTypeNone = 0, |
| 158 | 165 |
| 159 // Extension::TYPE_EXTENSION and Extension::TYPE_USER_SCRIPT | 166 // Extension::TYPE_EXTENSION and Extension::TYPE_USER_SCRIPT |
| 160 kTypeExtension = 1 << 0, | 167 kTypeExtension = 1 << 0, |
| 161 | 168 |
| 162 // Extension::TYPE_HOSTED_APP | 169 // Extension::TYPE_HOSTED_APP |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 bool implies_full_url_access() const { | 212 bool implies_full_url_access() const { |
| 206 return (flags_ & kFlagImpliesFullURLAccess) != 0; | 213 return (flags_ & kFlagImpliesFullURLAccess) != 0; |
| 207 } | 214 } |
| 208 | 215 |
| 209 // Returns true if this permission can only be acquired by COMPONENT | 216 // Returns true if this permission can only be acquired by COMPONENT |
| 210 // extensions. | 217 // extensions. |
| 211 bool is_component_only() const { | 218 bool is_component_only() const { |
| 212 return (flags_ & kFlagComponentOnly) != 0; | 219 return (flags_ & kFlagComponentOnly) != 0; |
| 213 } | 220 } |
| 214 | 221 |
| 222 // Returns true if |extension_id| is whitelisted. You should only use this | |
| 223 // method if should_enforce_whitelist() is true. | |
|
Aaron Boodman
2012/02/02 08:09:24
Why is the second sentence important to note?
jstritar
2012/02/02 17:51:10
Updated comment.
| |
| 224 bool IsWhitelisted(const std::string& extension_id) const; | |
| 225 | |
| 226 // Returns true if access to this permission is restricted by a whitelist. | |
| 227 bool should_enforce_whitelist() const { | |
| 228 return (flags_ & kFlagEnforceWhitelist) != 0; | |
| 229 } | |
| 230 | |
| 215 // Returns true if regular extensions can specify this permission. | 231 // Returns true if regular extensions can specify this permission. |
| 216 bool supports_extensions() const { | 232 bool supports_extensions() const { |
| 217 return (type_restrictions_ & kTypeExtension) != 0; | 233 return (type_restrictions_ & kTypeExtension) != 0; |
| 218 } | 234 } |
| 219 | 235 |
| 220 // Returns true if hosted apps can specify this permission. | 236 // Returns true if hosted apps can specify this permission. |
| 221 bool supports_hosted_apps() const { | 237 bool supports_hosted_apps() const { |
| 222 return (type_restrictions_ & kTypeHostedApp) != 0; | 238 return (type_restrictions_ & kTypeHostedApp) != 0; |
| 223 } | 239 } |
| 224 | 240 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 243 return (type_restrictions_ & type) != 0; | 259 return (type_restrictions_ & type) != 0; |
| 244 } | 260 } |
| 245 | 261 |
| 246 private: | 262 private: |
| 247 // Instances should only be constructed from within ExtensionPermissionsInfo. | 263 // Instances should only be constructed from within ExtensionPermissionsInfo. |
| 248 friend class ExtensionPermissionsInfo; | 264 friend class ExtensionPermissionsInfo; |
| 249 | 265 |
| 250 // Register ALL the permissions! | 266 // Register ALL the permissions! |
| 251 static void RegisterAllPermissions(ExtensionPermissionsInfo* info); | 267 static void RegisterAllPermissions(ExtensionPermissionsInfo* info); |
| 252 | 268 |
| 269 // Register the permission whitelists. | |
| 270 static void RegisterWhitelists(ExtensionPermissionsInfo* info); | |
|
Aaron Boodman
2012/02/02 08:09:24
Not clear what this method does from just its name
jstritar
2012/02/02 17:51:10
Removed method.
| |
| 271 | |
| 272 typedef std::set<std::string> ExtensionWhitelist; | |
| 273 | |
| 253 explicit ExtensionAPIPermission( | 274 explicit ExtensionAPIPermission( |
| 254 ID id, | 275 ID id, |
| 255 const char* name, | 276 const char* name, |
| 256 int l10n_message_id, | 277 int l10n_message_id, |
| 257 ExtensionPermissionMessage::ID message_id, | 278 ExtensionPermissionMessage::ID message_id, |
| 258 int flags, | 279 int flags, |
| 259 int type_restrictions); | 280 int type_restrictions); |
| 260 | 281 |
| 282 // Adds |extension_id| to the whitelist for this permission. | |
| 283 // The kFlagEnforceWhitelist flag must be set to use this method. | |
| 284 void AddToWhitelist(const std::string& extension_id); | |
| 285 | |
| 261 ID id_; | 286 ID id_; |
| 262 const char* name_; | 287 const char* name_; |
| 263 int flags_; | 288 int flags_; |
| 264 int type_restrictions_; | 289 int type_restrictions_; |
| 265 int l10n_message_id_; | 290 int l10n_message_id_; |
| 266 ExtensionPermissionMessage::ID message_id_; | 291 ExtensionPermissionMessage::ID message_id_; |
| 292 ExtensionWhitelist whitelist_; | |
| 267 }; | 293 }; |
| 268 | 294 |
| 269 typedef std::set<ExtensionAPIPermission::ID> ExtensionAPIPermissionSet; | 295 typedef std::set<ExtensionAPIPermission::ID> ExtensionAPIPermissionSet; |
| 270 | 296 |
| 271 // Singleton that holds the extension permission instances and provides static | 297 // Singleton that holds the extension permission instances and provides static |
| 272 // methods for accessing them. | 298 // methods for accessing them. |
| 273 class ExtensionPermissionsInfo { | 299 class ExtensionPermissionsInfo { |
| 274 public: | 300 public: |
| 275 // Returns a pointer to the singleton instance. | 301 // Returns a pointer to the singleton instance. |
| 276 static ExtensionPermissionsInfo* GetInstance(); | 302 static ExtensionPermissionsInfo* GetInstance(); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 303 | 329 |
| 304 // Registers a permission with the specified attributes and flags. | 330 // Registers a permission with the specified attributes and flags. |
| 305 void RegisterPermission( | 331 void RegisterPermission( |
| 306 ExtensionAPIPermission::ID id, | 332 ExtensionAPIPermission::ID id, |
| 307 const char* name, | 333 const char* name, |
| 308 int l10n_message_id, | 334 int l10n_message_id, |
| 309 ExtensionPermissionMessage::ID message_id, | 335 ExtensionPermissionMessage::ID message_id, |
| 310 int flags, | 336 int flags, |
| 311 int type_restrictions); | 337 int type_restrictions); |
| 312 | 338 |
| 339 // Registers the whitelist for the given permission. | |
| 340 void RegisterWhitelist(ExtensionAPIPermission::ID id, | |
| 341 const char* whitelist[], | |
| 342 int whitelist_length); | |
| 343 | |
| 313 // Maps permission ids to permissions. | 344 // Maps permission ids to permissions. |
| 314 typedef std::map<ExtensionAPIPermission::ID, ExtensionAPIPermission*> IDMap; | 345 typedef std::map<ExtensionAPIPermission::ID, ExtensionAPIPermission*> IDMap; |
| 315 | 346 |
| 316 // Maps names and aliases to permissions. | 347 // Maps names and aliases to permissions. |
| 317 typedef std::map<std::string, ExtensionAPIPermission*> NameMap; | 348 typedef std::map<std::string, ExtensionAPIPermission*> NameMap; |
| 318 | 349 |
| 319 IDMap id_map_; | 350 IDMap id_map_; |
| 320 NameMap name_map_; | 351 NameMap name_map_; |
| 321 | 352 |
| 322 size_t hosted_app_permission_count_; | 353 size_t hosted_app_permission_count_; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 428 | 459 |
| 429 const ExtensionAPIPermissionSet& apis() const { return apis_; } | 460 const ExtensionAPIPermissionSet& apis() const { return apis_; } |
| 430 | 461 |
| 431 const URLPatternSet& effective_hosts() const { return effective_hosts_; } | 462 const URLPatternSet& effective_hosts() const { return effective_hosts_; } |
| 432 | 463 |
| 433 const URLPatternSet& explicit_hosts() const { return explicit_hosts_; } | 464 const URLPatternSet& explicit_hosts() const { return explicit_hosts_; } |
| 434 | 465 |
| 435 const URLPatternSet& scriptable_hosts() const { return scriptable_hosts_; } | 466 const URLPatternSet& scriptable_hosts() const { return scriptable_hosts_; } |
| 436 | 467 |
| 437 private: | 468 private: |
| 438 FRIEND_TEST_ALL_PREFIXES(ExtensionPermissionSetTest, | 469 FRIEND_TEST_ALL_PREFIXES(ExtensionPermissionsTest, |
| 439 HasLessHostPrivilegesThan); | 470 HasLessHostPrivilegesThan); |
| 440 | 471 |
| 441 friend class base::RefCountedThreadSafe<ExtensionPermissionSet>; | 472 friend class base::RefCountedThreadSafe<ExtensionPermissionSet>; |
| 442 | 473 |
| 443 static std::set<std::string> GetDistinctHosts( | 474 static std::set<std::string> GetDistinctHosts( |
| 444 const URLPatternSet& host_patterns, | 475 const URLPatternSet& host_patterns, |
| 445 bool include_rcd, | 476 bool include_rcd, |
| 446 bool exclude_file_scheme); | 477 bool exclude_file_scheme); |
| 447 | 478 |
| 448 // Initializes the set based on |extension|'s manifest data. | 479 // Initializes the set based on |extension|'s manifest data. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 474 | 505 |
| 475 // The list of hosts that can be scripted by content scripts. | 506 // The list of hosts that can be scripted by content scripts. |
| 476 // TODO(jstritar): Rename to "user_script_hosts_"? | 507 // TODO(jstritar): Rename to "user_script_hosts_"? |
| 477 URLPatternSet scriptable_hosts_; | 508 URLPatternSet scriptable_hosts_; |
| 478 | 509 |
| 479 // The list of hosts this effectively grants access to. | 510 // The list of hosts this effectively grants access to. |
| 480 URLPatternSet effective_hosts_; | 511 URLPatternSet effective_hosts_; |
| 481 }; | 512 }; |
| 482 | 513 |
| 483 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ | 514 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ |
| OLD | NEW |