| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 class Extension; | 21 class Extension; |
| 22 class ExtensionPermissionsInfo; |
| 22 | 23 |
| 23 // When prompting the user to install or approve permissions, we display | 24 // When prompting the user to install or approve permissions, we display |
| 24 // messages describing the effects of the permissions rather than listing the | 25 // messages describing the effects of the permissions rather than listing the |
| 25 // permissions themselves. Each ExtensionPermissionMessage represents one of the | 26 // permissions themselves. Each ExtensionPermissionMessage represents one of the |
| 26 // messages shown to the user. | 27 // messages shown to the user. |
| 27 class ExtensionPermissionMessage { | 28 class ExtensionPermissionMessage { |
| 28 public: | 29 public: |
| 29 // Do not reorder this enumeration. If you need to add a new enum, add it just | 30 // Do not reorder this enumeration. If you need to add a new enum, add it just |
| 30 // prior to kEnumBoundary. | 31 // prior to kEnumBoundary. |
| 31 enum ID { | 32 enum ID { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 kWebRequest, | 126 kWebRequest, |
| 126 kWebRequestBlocking, | 127 kWebRequestBlocking, |
| 127 kWebSocketProxyPrivate, | 128 kWebSocketProxyPrivate, |
| 128 kWebstorePrivate, | 129 kWebstorePrivate, |
| 129 kEnumBoundary | 130 kEnumBoundary |
| 130 }; | 131 }; |
| 131 | 132 |
| 132 enum Flag { | 133 enum Flag { |
| 133 kFlagNone = 0, | 134 kFlagNone = 0, |
| 134 | 135 |
| 135 // Indicates if the permission can be accessed by hosted apps. | |
| 136 kFlagHostedApp = 1 << 0, | |
| 137 | |
| 138 // Indicates if the permission implies full access (native code). | 136 // Indicates if the permission implies full access (native code). |
| 139 kFlagImpliesFullAccess = 1 << 1, | 137 kFlagImpliesFullAccess = 1 << 0, |
| 140 | 138 |
| 141 // Indicates if the permission implies full URL access. | 139 // Indicates if the permission implies full URL access. |
| 142 kFlagImpliesFullURLAccess = 1 << 2, | 140 kFlagImpliesFullURLAccess = 1 << 1, |
| 143 | 141 |
| 144 // Indicates that the permission is private to COMPONENT extensions. | 142 // Indicates that the permission is private to COMPONENT extensions. |
| 145 kFlagComponentOnly = 1 << 3, | 143 kFlagComponentOnly = 1 << 2, |
| 146 | 144 |
| 147 // Indicates that the permission supports the optional permissions API. | 145 // Indicates that the permission supports the optional permissions API. |
| 148 kFlagSupportsOptional = 1 << 4, | 146 kFlagSupportsOptional = 1 << 3, |
| 147 }; |
| 149 | 148 |
| 150 // Indicates whether the permission is available only to platform apps. | 149 // Flags for specifying what extension types can use the permission. |
| 151 kFlagPlatformAppOnly = 1 << 5, | 150 enum TypeRestriction { |
| 151 kTypeNone = 0, |
| 152 |
| 153 // Extension::TYPE_EXTENSION and Extension::TYPE_USER_SCRIPT |
| 154 kTypeExtension = 1 << 0, |
| 155 |
| 156 // Extension::TYPE_HOSTED_APP |
| 157 kTypeHostedApp = 1 << 1, |
| 158 |
| 159 // Extension::TYPE_PACKAGED_APP |
| 160 kTypePackagedApp = 1 << 2, |
| 161 |
| 162 // Extension::TYPE_PLATFORM_APP |
| 163 kTypePlatformApp = 1 << 3, |
| 164 |
| 165 // Supports all types. |
| 166 kTypeAll = (1 << 4) - 1, |
| 167 |
| 168 // Convenience flag for all types except hosted apps. |
| 169 kTypeDefault = kTypeAll - kTypeHostedApp, |
| 152 }; | 170 }; |
| 153 | 171 |
| 154 typedef std::set<ID> IDSet; | 172 typedef std::set<ID> IDSet; |
| 155 | 173 |
| 156 ~ExtensionAPIPermission(); | 174 ~ExtensionAPIPermission(); |
| 157 | 175 |
| 158 // Returns the localized permission message associated with this api. | 176 // Returns the localized permission message associated with this api. |
| 159 ExtensionPermissionMessage GetMessage() const; | 177 ExtensionPermissionMessage GetMessage() const; |
| 160 | 178 |
| 161 int flags() const { return flags_; } | 179 int flags() const { return flags_; } |
| 162 | 180 |
| 181 int type_restrictions() const { return type_restrictions_; } |
| 182 |
| 163 ID id() const { return id_; } | 183 ID id() const { return id_; } |
| 164 | 184 |
| 165 // Returns the message id associated with this permission. | 185 // Returns the message id associated with this permission. |
| 166 ExtensionPermissionMessage::ID message_id() const { | 186 ExtensionPermissionMessage::ID message_id() const { |
| 167 return message_id_; | 187 return message_id_; |
| 168 } | 188 } |
| 169 | 189 |
| 170 // Returns the name of this permission. | 190 // Returns the name of this permission. |
| 171 const char* name() const { return name_; } | 191 const char* name() const { return name_; } |
| 172 | 192 |
| 173 // Returns true if this permission implies full access (e.g., native code). | 193 // Returns true if this permission implies full access (e.g., native code). |
| 174 bool implies_full_access() const { | 194 bool implies_full_access() const { |
| 175 return (flags_ & kFlagImpliesFullAccess) != 0; | 195 return (flags_ & kFlagImpliesFullAccess) != 0; |
| 176 } | 196 } |
| 177 | 197 |
| 178 // Returns true if this permission implies full URL access. | 198 // Returns true if this permission implies full URL access. |
| 179 bool implies_full_url_access() const { | 199 bool implies_full_url_access() const { |
| 180 return (flags_ & kFlagImpliesFullURLAccess) != 0; | 200 return (flags_ & kFlagImpliesFullURLAccess) != 0; |
| 181 } | 201 } |
| 182 | 202 |
| 183 // Returns true if this permission can be accessed by hosted apps. | |
| 184 bool is_hosted_app() const { | |
| 185 return (flags_ & kFlagHostedApp) != 0; | |
| 186 } | |
| 187 | |
| 188 // Returns true if this permission can only be acquired by COMPONENT | 203 // Returns true if this permission can only be acquired by COMPONENT |
| 189 // extensions. | 204 // extensions. |
| 190 bool is_component_only() const { | 205 bool is_component_only() const { |
| 191 return (flags_ & kFlagComponentOnly) != 0; | 206 return (flags_ & kFlagComponentOnly) != 0; |
| 192 } | 207 } |
| 193 | 208 |
| 209 // Returns true if regular extensions can specify this permission. |
| 210 bool supports_extensions() const { |
| 211 return (type_restrictions_ & kTypeExtension) != 0; |
| 212 } |
| 213 |
| 214 // Returns true if hosted apps can specify this permission. |
| 215 bool supports_hosted_apps() const { |
| 216 return (type_restrictions_ & kTypeHostedApp) != 0; |
| 217 } |
| 218 |
| 219 // Returns true if packaged apps can specify this permission. |
| 220 bool supports_packaged_apps() const { |
| 221 return (type_restrictions_ & kTypePackagedApp) != 0; |
| 222 } |
| 223 |
| 224 // Returns true if platform apps can specify this permission. |
| 225 bool supports_platform_apps() const { |
| 226 return (type_restrictions_ & kTypePlatformApp) != 0; |
| 227 } |
| 228 |
| 194 // Returns true if this permission can be added and removed via the | 229 // Returns true if this permission can be added and removed via the |
| 195 // optional permissions extension API. | 230 // optional permissions extension API. |
| 196 bool supports_optional() const { | 231 bool supports_optional() const { |
| 197 return (flags_ & kFlagSupportsOptional) != 0; | 232 return (flags_ & kFlagSupportsOptional) != 0; |
| 198 } | 233 } |
| 199 | 234 |
| 200 // Returns true if this permission can only be acquired by platform apps. | 235 // Returns true if this permissions supports the specified |type|. |
| 201 bool is_platform_app_only() const { | 236 bool supports_type(TypeRestriction type) const { |
| 202 return (flags_ & kFlagPlatformAppOnly) != 0; | 237 return (type_restrictions_ & type) != 0; |
| 203 } | 238 } |
| 204 | 239 |
| 205 private: | 240 private: |
| 206 // Instances should only be constructed from within ExtensionPermissionsInfo. | 241 // Instances should only be constructed from within ExtensionPermissionsInfo. |
| 207 friend class ExtensionPermissionsInfo; | 242 friend class ExtensionPermissionsInfo; |
| 208 | 243 |
| 244 // Register ALL the permissions! |
| 245 static void RegisterAllPermissions(ExtensionPermissionsInfo* info); |
| 246 |
| 209 explicit ExtensionAPIPermission( | 247 explicit ExtensionAPIPermission( |
| 210 ID id, | 248 ID id, |
| 211 const char* name, | 249 const char* name, |
| 212 int l10n_message_id, | 250 int l10n_message_id, |
| 213 ExtensionPermissionMessage::ID message_id, | 251 ExtensionPermissionMessage::ID message_id, |
| 214 int flags); | 252 int flags, |
| 253 int type_restrictions); |
| 215 | 254 |
| 216 ID id_; | 255 ID id_; |
| 217 const char* name_; | 256 const char* name_; |
| 218 int flags_; | 257 int flags_; |
| 258 int type_restrictions_; |
| 219 int l10n_message_id_; | 259 int l10n_message_id_; |
| 220 ExtensionPermissionMessage::ID message_id_; | 260 ExtensionPermissionMessage::ID message_id_; |
| 221 }; | 261 }; |
| 222 | 262 |
| 223 typedef std::set<ExtensionAPIPermission::ID> ExtensionAPIPermissionSet; | 263 typedef std::set<ExtensionAPIPermission::ID> ExtensionAPIPermissionSet; |
| 224 | 264 |
| 225 // Singleton that holds the extension permission instances and provides static | 265 // Singleton that holds the extension permission instances and provides static |
| 226 // methods for accessing them. | 266 // methods for accessing them. |
| 227 class ExtensionPermissionsInfo { | 267 class ExtensionPermissionsInfo { |
| 228 public: | 268 public: |
| 229 // Returns a pointer to the singleton instance. | 269 // Returns a pointer to the singleton instance. |
| 230 static ExtensionPermissionsInfo* GetInstance(); | 270 static ExtensionPermissionsInfo* GetInstance(); |
| 231 | 271 |
| 232 // Returns the permission with the given |id|, and NULL if it doesn't exist. | 272 // Returns the permission with the given |id|, and NULL if it doesn't exist. |
| 233 ExtensionAPIPermission* GetByID(ExtensionAPIPermission::ID id); | 273 ExtensionAPIPermission* GetByID(ExtensionAPIPermission::ID id); |
| 234 | 274 |
| 235 // Returns the permission with the given |name|, and NULL if none | 275 // Returns the permission with the given |name|, and NULL if none |
| 236 // exists. | 276 // exists. |
| 237 ExtensionAPIPermission* GetByName(std::string name); | 277 ExtensionAPIPermission* GetByName(std::string name); |
| 238 | 278 |
| 239 // Returns a set containing all valid api permission ids. | 279 // Returns a set containing all valid api permission ids. |
| 240 ExtensionAPIPermissionSet GetAll(); | 280 ExtensionAPIPermissionSet GetAll(); |
| 241 | 281 |
| 242 // Converts all the permission names in |permission_names| to permission ids. | 282 // Converts all the permission names in |permission_names| to permission ids. |
| 243 ExtensionAPIPermissionSet GetAllByName( | 283 ExtensionAPIPermissionSet GetAllByName( |
| 244 const std::set<std::string>& permission_names); | 284 const std::set<std::string>& permission_names); |
| 245 | 285 |
| 246 // Gets the total number of API permissions available to hosted apps. | |
| 247 size_t get_hosted_app_permission_count() { | |
| 248 return hosted_app_permission_count_; | |
| 249 } | |
| 250 | |
| 251 // Gets the total number of API permissions. | 286 // Gets the total number of API permissions. |
| 252 size_t get_permission_count() { return permission_count_; } | 287 size_t get_permission_count() { return permission_count_; } |
| 253 | 288 |
| 254 private: | 289 private: |
| 290 friend class ExtensionAPIPermission; |
| 291 |
| 255 ~ExtensionPermissionsInfo(); | 292 ~ExtensionPermissionsInfo(); |
| 256 ExtensionPermissionsInfo(); | 293 ExtensionPermissionsInfo(); |
| 257 | 294 |
| 258 // Registers an |alias| for a given permission |name|. | 295 // Registers an |alias| for a given permission |name|. |
| 259 void RegisterAlias(const char* name, const char* alias); | 296 void RegisterAlias(const char* name, const char* alias); |
| 260 | 297 |
| 261 // Registers a permission with the specified attributes and flags. | 298 // Registers a permission with the specified attributes and flags. |
| 262 void RegisterPermission( | 299 void RegisterPermission( |
| 263 ExtensionAPIPermission::ID id, | 300 ExtensionAPIPermission::ID id, |
| 264 const char* name, | 301 const char* name, |
| 265 int l10n_message_id, | 302 int l10n_message_id, |
| 266 ExtensionPermissionMessage::ID message_id, | 303 ExtensionPermissionMessage::ID message_id, |
| 267 int flags); | 304 int flags, |
| 305 int type_restrictions); |
| 268 | 306 |
| 269 // Maps permission ids to permissions. | 307 // Maps permission ids to permissions. |
| 270 typedef std::map<ExtensionAPIPermission::ID, ExtensionAPIPermission*> IDMap; | 308 typedef std::map<ExtensionAPIPermission::ID, ExtensionAPIPermission*> IDMap; |
| 271 | 309 |
| 272 // Maps names and aliases to permissions. | 310 // Maps names and aliases to permissions. |
| 273 typedef std::map<std::string, ExtensionAPIPermission*> NameMap; | 311 typedef std::map<std::string, ExtensionAPIPermission*> NameMap; |
| 274 | 312 |
| 275 IDMap id_map_; | 313 IDMap id_map_; |
| 276 NameMap name_map_; | 314 NameMap name_map_; |
| 277 | 315 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 bool HasEffectiveAccessToURL(const GURL& url) const; | 403 bool HasEffectiveAccessToURL(const GURL& url) const; |
| 366 | 404 |
| 367 // Returns ture if this permission set effectively represents full access | 405 // Returns ture if this permission set effectively represents full access |
| 368 // (e.g. native code). | 406 // (e.g. native code). |
| 369 bool HasEffectiveFullAccess() const; | 407 bool HasEffectiveFullAccess() const; |
| 370 | 408 |
| 371 // Returns true if this permission set includes permissions that are | 409 // Returns true if this permission set includes permissions that are |
| 372 // restricted to internal extensions. | 410 // restricted to internal extensions. |
| 373 bool HasPrivatePermissions() const; | 411 bool HasPrivatePermissions() const; |
| 374 | 412 |
| 375 // Returns true if this permission set includes permissions that are | |
| 376 // restricted to platform apps. | |
| 377 bool HasPlatformAppPermissions() const; | |
| 378 | |
| 379 // Returns true if |permissions| has a greater privilege level than this | 413 // Returns true if |permissions| has a greater privilege level than this |
| 380 // permission set (e.g., this permission set has less permissions). | 414 // permission set (e.g., this permission set has less permissions). |
| 381 bool HasLessPrivilegesThan(const ExtensionPermissionSet* permissions) const; | 415 bool HasLessPrivilegesThan(const ExtensionPermissionSet* permissions) const; |
| 382 | 416 |
| 383 const ExtensionAPIPermissionSet& apis() const { return apis_; } | 417 const ExtensionAPIPermissionSet& apis() const { return apis_; } |
| 384 | 418 |
| 385 const URLPatternSet& effective_hosts() const { return effective_hosts_; } | 419 const URLPatternSet& effective_hosts() const { return effective_hosts_; } |
| 386 | 420 |
| 387 const URLPatternSet& explicit_hosts() const { return explicit_hosts_; } | 421 const URLPatternSet& explicit_hosts() const { return explicit_hosts_; } |
| 388 | 422 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 | 462 |
| 429 // The list of hosts that can be scripted by content scripts. | 463 // The list of hosts that can be scripted by content scripts. |
| 430 // TODO(jstritar): Rename to "user_script_hosts_"? | 464 // TODO(jstritar): Rename to "user_script_hosts_"? |
| 431 URLPatternSet scriptable_hosts_; | 465 URLPatternSet scriptable_hosts_; |
| 432 | 466 |
| 433 // The list of hosts this effectively grants access to. | 467 // The list of hosts this effectively grants access to. |
| 434 URLPatternSet effective_hosts_; | 468 URLPatternSet effective_hosts_; |
| 435 }; | 469 }; |
| 436 | 470 |
| 437 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ | 471 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ |
| OLD | NEW |