1 // Copyright 2013 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 EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_INFO_H_ 6 #define EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_INFO_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 12 #include "base/callback.h" 13 #include "base/lazy_instance.h" 14 #include "extensions/common/permissions/api_permission.h" 15 #include "extensions/common/permissions/api_permission_set.h" 16 #include "extensions/common/permissions/permission_message.h" 17 #include "extensions/common/permissions/permissions_provider.h" 18 19 namespace extensions { 20 21 // A global object that holds the extension permission instances and provides 22 // methods for accessing them. 23 class PermissionsInfo { 24 public: 25 static PermissionsInfo* GetInstance(); 26 27 virtual ~PermissionsInfo(); 28 29 // Returns the permission with the given |id|, and NULL if it doesn't exist. 30 const APIPermissionInfo* GetByID(APIPermission::ID id) const; 31 32 // Returns the permission with the given |name|, and NULL if none 33 // exists. 34 const APIPermissionInfo* GetByName(const std::string& name) const; 35 36 // Returns a set containing all valid api permission ids. 37 APIPermissionSet GetAll() const; 38 39 // Converts all the permission names in |permission_names| to permission ids. 40 APIPermissionSet GetAllByName( 41 const std::set<std::string>& permission_names) const; 42 43 // Checks if any permissions have names that start with |name| followed by a 44 // period. 45 bool HasChildPermissions(const std::string& name) const; 46 47 // Gets the total number of API permissions. 48 size_t get_permission_count() const { return permission_count_; } 49 50 private: 51 friend struct base::DefaultLazyInstanceTraits<PermissionsInfo>; 52 53 PermissionsInfo(); 54 55 // Initializes the permissions from the provider. 56 void InitializeWithProvider(const PermissionsProvider& provider); 57 58 // Registers an |alias| for a given permission |name|. 59 void RegisterAlias(const char* name, const char* alias); 60 61 // Registers a permission with the specified attributes and flags. 62 void RegisterPermission(APIPermissionInfo* permission); 63 64 // Maps permission ids to permissions. 65 typedef std::map<APIPermission::ID, APIPermissionInfo*> IDMap; 66 67 // Maps names and aliases to permissions. 68 typedef std::map<std::string, APIPermissionInfo*> NameMap; 69 70 IDMap id_map_; 71 NameMap name_map_; 72 73 size_t hosted_app_permission_count_; 74 size_t permission_count_; 75 76 DISALLOW_COPY_AND_ASSIGN(PermissionsInfo); 77 }; 78 79 } // namespace extensions 80 81 #endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_INFO_H_ 82