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#ifndef EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_INFO_H_
6#define EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_INFO_H_
7
8#include <stddef.h>
9
10#include <map>
11#include <memory>
12#include <set>
13#include <string>
14#include <unordered_map>
15#include <vector>
16
17#include "base/callback.h"
18#include "base/containers/span.h"
19#include "base/lazy_instance.h"
20#include "base/macros.h"
21#include "extensions/common/permissions/api_permission.h"
22#include "extensions/common/permissions/api_permission_set.h"
23
24namespace extensions {
25
26struct Alias;
27
28// A global object that holds the extension permission instances and provides
29// methods for accessing them.
30class PermissionsInfo {
31 public:
32 static PermissionsInfo* GetInstance();
33
34 // Registers the permissions specified by |infos| along with the
35 // |aliases|.
36 void RegisterPermissions(base::span<const APIPermissionInfo::InitInfo> infos,
37 base::span<const Alias> aliases);
38
39 // Returns the permission with the given |id|, and NULL if it doesn't exist.
40 const APIPermissionInfo* GetByID(APIPermission::ID id) const;
41
42 // Returns the permission with the given |name|, and NULL if none
43 // exists.
44 const APIPermissionInfo* GetByName(const std::string& name) const;
45
46 // Returns a set containing all valid api permission ids.
47 APIPermissionSet GetAll() const;
48
49 // Converts all the permission names in |permission_names| to permission ids.
50 APIPermissionSet GetAllByName(
51 const std::set<std::string>& permission_names) const;
52
53 // Checks if any permissions have names that start with |name| followed by a
54 // period.
55 bool HasChildPermissions(const std::string& name) const;
56
57 // Gets the total number of API permissions.
58 size_t get_permission_count() const { return permission_count_; }
59
60 private:
61 friend struct base::LazyInstanceTraitsBase<PermissionsInfo>;
62
63 PermissionsInfo();
64
65 virtual ~PermissionsInfo();
66
67 // Registers an |alias| for a given permission |name|.
68 void RegisterAlias(const Alias& alias);
69
70 // Registers a permission with the specified attributes and flags.
71 void RegisterPermission(std::unique_ptr<APIPermissionInfo> permission);
72
73 // Maps permission ids to permissions. Owns the permissions.
74 typedef std::unordered_map<APIPermission::ID,
75 std::unique_ptr<APIPermissionInfo>>
76 IDMap;
77
78 // Maps names and aliases to permissions. Doesn't own the permissions.
79 typedef std::map<std::string, APIPermissionInfo*> NameMap;
80
81 IDMap id_map_;
82 NameMap name_map_;
83
84 size_t permission_count_;
85
86 DISALLOW_COPY_AND_ASSIGN(PermissionsInfo);
87};
88
89} // namespace extensions
90
91#endif // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_INFO_H_
92