summaryrefslogtreecommitdiffstats
path: root/extensions/common/permissions/permissions_info.cc
blob: bac3d9c18e8e4faa815a21df8186e834ac626b6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/common/permissions/permissions_info.h"

#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "extensions/common/extensions_client.h"

namespace extensions {

static base::LazyInstance<PermissionsInfo> g_permissions_info =
    LAZY_INSTANCE_INITIALIZER;

// static
PermissionsInfo* PermissionsInfo::GetInstance() {
  return g_permissions_info.Pointer();
}

PermissionsInfo::~PermissionsInfo() {
  STLDeleteContainerPairSecondPointers(id_map_.begin(), id_map_.end());
}

const APIPermissionInfo* PermissionsInfo::GetByID(
    APIPermission::ID id) const {
  IDMap::const_iterator i = id_map_.find(id);
  return (i == id_map_.end()) ? NULL : i->second;
}

const APIPermissionInfo* PermissionsInfo::GetByName(
    const std::string& name) const {
  NameMap::const_iterator i = name_map_.find(name);
  return (i == name_map_.end()) ? NULL : i->second;
}

APIPermissionSet PermissionsInfo::GetAll() const {
  APIPermissionSet permissions;
  for (IDMap::const_iterator i = id_map_.begin(); i != id_map_.end(); ++i)
    permissions.insert(i->second->id());
  return permissions;
}

APIPermissionSet PermissionsInfo::GetAllByName(
    const std::set<std::string>& permission_names) const {
  APIPermissionSet permissions;
  for (std::set<std::string>::const_iterator i = permission_names.begin();
       i != permission_names.end(); ++i) {
    const APIPermissionInfo* permission_info = GetByName(*i);
    if (permission_info)
      permissions.insert(permission_info->id());
  }
  return permissions;
}

bool PermissionsInfo::HasChildPermissions(const std::string& name) const {
  NameMap::const_iterator i = name_map_.lower_bound(name + '.');
  if (i == name_map_.end()) return false;
  return StartsWithASCII(i->first, name + '.', true);
}

PermissionsInfo::PermissionsInfo()
    : hosted_app_permission_count_(0),
      permission_count_(0) {
  DCHECK(ExtensionsClient::Get());
  InitializeWithProvider(ExtensionsClient::Get()->GetPermissionsProvider());
}

void PermissionsInfo::InitializeWithProvider(
    const PermissionsProvider& provider) {
  std::vector<APIPermissionInfo*> permissions = provider.GetAllPermissions();
  std::vector<PermissionsProvider::AliasInfo> aliases =
      provider.GetAllAliases();

  for (size_t i = 0; i < permissions.size(); ++i)
    RegisterPermission(permissions[i]);
  for (size_t i = 0; i < aliases.size(); ++i)
    RegisterAlias(aliases[i].name, aliases[i].alias);
}

void PermissionsInfo::RegisterAlias(
    const char* name,
    const char* alias) {
  DCHECK(ContainsKey(name_map_, name));
  DCHECK(!ContainsKey(name_map_, alias));
  name_map_[alias] = name_map_[name];
}

void PermissionsInfo::RegisterPermission(APIPermissionInfo* permission) {
  DCHECK(!ContainsKey(id_map_, permission->id()));
  DCHECK(!ContainsKey(name_map_, permission->name()));

  id_map_[permission->id()] = permission;
  name_map_[permission->name()] = permission;

  permission_count_++;
}

}  // namespace extensions