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 #include "extensions/common/permissions/permissions_info.h" 6 7 #include "base/lazy_instance.h" 8 #include "base/logging.h" 9 #include "base/stl_util.h" 10 #include "base/strings/string_util.h" 11 #include "extensions/common/extensions_client.h" 12 13 namespace extensions { 14 15 static base::LazyInstance<PermissionsInfo> g_permissions_info = 16 LAZY_INSTANCE_INITIALIZER; 17 18 // static 19 PermissionsInfo* PermissionsInfo::GetInstance() { 20 return g_permissions_info.Pointer(); 21 } 22 23 PermissionsInfo::~PermissionsInfo() { 24 STLDeleteContainerPairSecondPointers(id_map_.begin(), id_map_.end()); 25 } 26 27 const APIPermissionInfo* PermissionsInfo::GetByID( 28 APIPermission::ID id) const { 29 IDMap::const_iterator i = id_map_.find(id); 30 return (i == id_map_.end()) ? NULL : i->second; 31 } 32 33 const APIPermissionInfo* PermissionsInfo::GetByName( 34 const std::string& name) const { 35 NameMap::const_iterator i = name_map_.find(name); 36 return (i == name_map_.end()) ? NULL : i->second; 37 } 38 39 APIPermissionSet PermissionsInfo::GetAll() const { 40 APIPermissionSet permissions; 41 for (IDMap::const_iterator i = id_map_.begin(); i != id_map_.end(); ++i) 42 permissions.insert(i->second->id()); 43 return permissions; 44 } 45 46 APIPermissionSet PermissionsInfo::GetAllByName( 47 const std::set<std::string>& permission_names) const { 48 APIPermissionSet permissions; 49 for (std::set<std::string>::const_iterator i = permission_names.begin(); 50 i != permission_names.end(); ++i) { 51 const APIPermissionInfo* permission_info = GetByName(*i); 52 if (permission_info) 53 permissions.insert(permission_info->id()); 54 } 55 return permissions; 56 } 57 58 bool PermissionsInfo::HasChildPermissions(const std::string& name) const { 59 NameMap::const_iterator i = name_map_.lower_bound(name + '.'); 60 if (i == name_map_.end()) return false; 61 return StartsWithASCII(i->first, name + '.', true); 62 } 63 64 PermissionsInfo::PermissionsInfo() 65 : hosted_app_permission_count_(0), 66 permission_count_(0) { 67 DCHECK(ExtensionsClient::Get()); 68 InitializeWithProvider(ExtensionsClient::Get()->GetPermissionsProvider()); 69 } 70 71 void PermissionsInfo::InitializeWithProvider( 72 const PermissionsProvider& provider) { 73 std::vector<APIPermissionInfo*> permissions = provider.GetAllPermissions(); 74 std::vector<PermissionsProvider::AliasInfo> aliases = 75 provider.GetAllAliases(); 76 77 for (size_t i = 0; i < permissions.size(); ++i) 78 RegisterPermission(permissions[i]); 79 for (size_t i = 0; i < aliases.size(); ++i) 80 RegisterAlias(aliases[i].name, aliases[i].alias); 81 } 82 83 void PermissionsInfo::RegisterAlias( 84 const char* name, 85 const char* alias) { 86 DCHECK(ContainsKey(name_map_, name)); 87 DCHECK(!ContainsKey(name_map_, alias)); 88 name_map_[alias] = name_map_[name]; 89 } 90 91 void PermissionsInfo::RegisterPermission(APIPermissionInfo* permission) { 92 DCHECK(!ContainsKey(id_map_, permission->id())); 93 DCHECK(!ContainsKey(name_map_, permission->name())); 94 95 id_map_[permission->id()] = permission; 96 name_map_[permission->name()] = permission; 97 98 permission_count_++; 99 } 100 101 } // namespace extensions 102