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"
11 #include "extensions/common/extensions_client.h"
13 namespace extensions
{
15 static base::LazyInstance
<PermissionsInfo
> g_permissions_info
=
16 LAZY_INSTANCE_INITIALIZER
;
19 PermissionsInfo
* PermissionsInfo::GetInstance() {
20 return g_permissions_info
.Pointer();
23 PermissionsInfo::~PermissionsInfo() {
24 STLDeleteContainerPairSecondPointers(id_map_
.begin(), id_map_
.end());
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
;
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
;
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());
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
);
53 permissions
.insert(permission_info
->id());
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);
64 PermissionsInfo::PermissionsInfo()
65 : hosted_app_permission_count_(0),
66 permission_count_(0) {
67 InitializeWithProvider(ExtensionsClient::Get()->GetPermissionsProvider());
70 void PermissionsInfo::InitializeWithProvider(
71 const PermissionsProvider
& provider
) {
72 std::vector
<APIPermissionInfo
*> permissions
= provider
.GetAllPermissions();
73 std::vector
<PermissionsProvider::AliasInfo
> aliases
=
74 provider
.GetAllAliases();
76 for (size_t i
= 0; i
< permissions
.size(); ++i
)
77 RegisterPermission(permissions
[i
]);
78 for (size_t i
= 0; i
< aliases
.size(); ++i
)
79 RegisterAlias(aliases
[i
].name
, aliases
[i
].alias
);
82 void PermissionsInfo::RegisterAlias(
85 DCHECK(ContainsKey(name_map_
, name
));
86 DCHECK(!ContainsKey(name_map_
, alias
));
87 name_map_
[alias
] = name_map_
[name
];
90 void PermissionsInfo::RegisterPermission(APIPermissionInfo
* permission
) {
91 DCHECK(!ContainsKey(id_map_
, permission
->id()));
92 DCHECK(!ContainsKey(name_map_
, permission
->name()));
94 id_map_
[permission
->id()] = permission
;
95 name_map_
[permission
->name()] = permission
;
100 } // namespace extensions