Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / common / permissions / permissions_info.cc
blobe83fb2e3a78bc70de0b391bdbe274e30984562e0
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;
17 // static
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());
49 return permissions;
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);
58 if (permission_info)
59 permissions.insert(permission_info->id());
61 return permissions;
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 base::StartsWith(i->first, name + '.', base::CompareCase::SENSITIVE);
70 PermissionsInfo::PermissionsInfo()
71 : permission_count_(0) {
74 PermissionsInfo::~PermissionsInfo() {
75 STLDeleteContainerPairSecondPointers(id_map_.begin(), id_map_.end());
78 void PermissionsInfo::RegisterAlias(
79 const char* name,
80 const char* alias) {
81 DCHECK(ContainsKey(name_map_, name));
82 DCHECK(!ContainsKey(name_map_, alias));
83 name_map_[alias] = name_map_[name];
86 void PermissionsInfo::RegisterPermission(APIPermissionInfo* permission) {
87 DCHECK(!ContainsKey(id_map_, permission->id()));
88 DCHECK(!ContainsKey(name_map_, permission->name()));
90 id_map_[permission->id()] = permission;
91 name_map_[permission->name()] = permission;
93 permission_count_++;
96 } // namespace extensions