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.
5 #include "extensions/common/permissions/permissions_info.h"
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string_util.h"
12 namespace extensions
{
14 static base::LazyInstance
<PermissionsInfo
> g_permissions_info
=
15 LAZY_INSTANCE_INITIALIZER
;
18 PermissionsInfo
* PermissionsInfo::GetInstance() {
19 return g_permissions_info
.Pointer();
22 void PermissionsInfo::AddProvider(const PermissionsProvider
& provider
) {
23 std::vector
<APIPermissionInfo
*> permissions
= provider
.GetAllPermissions();
24 std::vector
<PermissionsProvider::AliasInfo
> aliases
=
25 provider
.GetAllAliases();
27 for (size_t i
= 0; i
< permissions
.size(); ++i
)
28 RegisterPermission(permissions
[i
]);
29 for (size_t i
= 0; i
< aliases
.size(); ++i
)
30 RegisterAlias(aliases
[i
].name
, aliases
[i
].alias
);
33 const APIPermissionInfo
* PermissionsInfo::GetByID(
34 APIPermission::ID id
) const {
35 IDMap::const_iterator i
= id_map_
.find(id
);
36 return (i
== id_map_
.end()) ? NULL
: i
->second
;
39 const APIPermissionInfo
* PermissionsInfo::GetByName(
40 const std::string
& name
) const {
41 NameMap::const_iterator i
= name_map_
.find(name
);
42 return (i
== name_map_
.end()) ? NULL
: i
->second
;
45 APIPermissionSet
PermissionsInfo::GetAll() const {
46 APIPermissionSet permissions
;
47 for (IDMap::const_iterator i
= id_map_
.begin(); i
!= id_map_
.end(); ++i
)
48 permissions
.insert(i
->second
->id());
52 APIPermissionSet
PermissionsInfo::GetAllByName(
53 const std::set
<std::string
>& permission_names
) const {
54 APIPermissionSet permissions
;
55 for (std::set
<std::string
>::const_iterator i
= permission_names
.begin();
56 i
!= permission_names
.end(); ++i
) {
57 const APIPermissionInfo
* permission_info
= GetByName(*i
);
59 permissions
.insert(permission_info
->id());
64 bool PermissionsInfo::HasChildPermissions(const std::string
& name
) const {
65 NameMap::const_iterator i
= name_map_
.lower_bound(name
+ '.');
66 if (i
== name_map_
.end()) return false;
67 return StartsWithASCII(i
->first
, name
+ '.', true);
70 PermissionsInfo::PermissionsInfo()
71 : hosted_app_permission_count_(0),
72 permission_count_(0) {
75 PermissionsInfo::~PermissionsInfo() {
76 STLDeleteContainerPairSecondPointers(id_map_
.begin(), id_map_
.end());
79 void PermissionsInfo::RegisterAlias(
82 DCHECK(ContainsKey(name_map_
, name
));
83 DCHECK(!ContainsKey(name_map_
, alias
));
84 name_map_
[alias
] = name_map_
[name
];
87 void PermissionsInfo::RegisterPermission(APIPermissionInfo
* permission
) {
88 DCHECK(!ContainsKey(id_map_
, permission
->id()));
89 DCHECK(!ContainsKey(name_map_
, permission
->name()));
91 id_map_
[permission
->id()] = permission
;
92 name_map_
[permission
->name()] = permission
;
97 } // namespace extensions