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 #ifndef EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_
6 #define EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_
9 #include "extensions/common/permissions/api_permission.h"
10 #include "extensions/common/permissions/base_set_operators.h"
16 namespace extensions
{
18 class APIPermissionSet
;
20 class PermissionIDSet
;
23 struct BaseSetOperatorsTraits
<APIPermissionSet
> {
24 typedef APIPermission ElementType
;
25 typedef APIPermission::ID ElementIDType
;
28 class APIPermissionSet
: public BaseSetOperators
<APIPermissionSet
> {
31 // Don't allow internal permissions to be parsed (e.g. entries in the
32 // "permissions" list in a manifest).
33 kDisallowInternalPermissions
,
35 // Allow internal permissions to be parsed (e.g. from the "api" field of a
36 // permissions list in the prefs).
37 kAllowInternalPermissions
,
40 void insert(APIPermission::ID id
);
42 // Insert |permission| into the APIPermissionSet. The APIPermissionSet will
43 // take the ownership of |permission|,
44 void insert(APIPermission
* permission
);
46 // Parses permissions from |permissions| and adds the parsed permissions to
47 // |api_permissions|. If |source| is kDisallowInternalPermissions, treat
48 // permissions with kFlagInternal as errors. If |unhandled_permissions| is
49 // not NULL, the names of all permissions that couldn't be parsed will be
50 // added to this vector. If |error| is NULL, parsing will continue with the
51 // next permission if invalid data is detected. If |error| is not NULL, it
52 // will be set to an error message and false is returned when an invalid
53 // permission is found.
54 static bool ParseFromJSON(
55 const base::ListValue
* permissions
,
57 APIPermissionSet
* api_permissions
,
58 base::string16
* error
,
59 std::vector
<std::string
>* unhandled_permissions
);
61 void AddImpliedPermissions();
64 // An ID representing a single permission that belongs to an app or extension.
66 // Each PermissionID has a required ID to identify the permission. For most
67 // permissions, this is all they have.
69 // Some more complex permissions have a parameter, which acts like an argument
70 // for the permission. For example, host permissions might have the ID
71 // kReadOnlyHost and the argument 'www.google.com' (the host which is
72 // read-only). Parameters are passed to the permission message rules for this
73 // permission, so they can affect the displayed message.
75 // Note: Inheriting from std::pair automatically gives us an operator<
76 // (required for putting these into an std::set).
78 // TODO(sashab): Move this to the same file as PermissionIDSet once that moves
80 class PermissionID
: public std::pair
<APIPermission::ID
, base::string16
> {
82 explicit PermissionID(APIPermission::ID id
);
83 PermissionID(APIPermission::ID id
, const base::string16
& parameter
);
84 virtual ~PermissionID();
86 const APIPermission::ID
& id() const { return this->first
; }
87 const base::string16
& parameter() const { return this->second
; }
90 // A set of permissions for an app or extension. Used for passing around groups
91 // of permissions, such as required or optional permissions.
93 // Each permission can also store a string, such as a hostname or device number,
94 // as a parameter that helps identify the permission. This parameter can then
95 // be used when the permission message is generated. For example, the permission
96 // kHostReadOnly might have the parameter "google.com", which means that the app
97 // or extension has the permission to read the host google.com. This parameter
98 // may then be included in the permission message when it is generated later.
101 // // Create an empty PermissionIDSet.
102 // PermissionIDSet p;
103 // // Add a permission to the set.
104 // p.insert(APIPermission::kNetworkState);
105 // // Add a permission with a parameter to the set.
106 // p.insert(APIPermission::kHostReadOnly,
107 // base::ASCIIToUTF16("http://www.google.com"));
109 // TODO(sashab): Move this to its own file and rename it to PermissionSet after
110 // APIPermission is removed, the current PermissionSet is no longer used, and
111 // APIPermission::ID is the only type of Permission ID.
112 class PermissionIDSet
{
114 using const_iterator
= std::set
<PermissionID
>::const_iterator
;
117 virtual ~PermissionIDSet();
119 // Adds the given permission, and an optional parameter, to the set.
120 void insert(APIPermission::ID permission_id
);
121 void insert(APIPermission::ID permission_id
,
122 const base::string16
& permission_parameter
);
123 void InsertAll(const PermissionIDSet
& permission_set
);
125 // Returns the parameters for all PermissionIDs in this set.
126 std::vector
<base::string16
> GetAllPermissionParameters() const;
128 // Check if the set contains permissions with all the given IDs.
129 bool ContainsAllIDs(const std::set
<APIPermission::ID
>& permission_ids
) const;
131 // Returns all the permissions in this set with one of the given IDs.
132 PermissionIDSet
GetAllPermissionsWithIDs(
133 const std::set
<APIPermission::ID
>& permission_ids
) const;
135 // Convenience functions that call their stl_util counterparts.
136 bool Includes(const PermissionIDSet
& subset
) const;
137 bool Equals(const PermissionIDSet
& set
) const;
138 static PermissionIDSet
Difference(const PermissionIDSet
& set_1
,
139 const PermissionIDSet
& set_2
);
140 static PermissionIDSet
Intersection(const PermissionIDSet
& set_1
,
141 const PermissionIDSet
& set_2
);
142 static PermissionIDSet
Union(const PermissionIDSet
& set_1
,
143 const PermissionIDSet
& set_2
);
148 const_iterator
begin() const { return permissions_
.begin(); }
149 const_iterator
end() const { return permissions_
.end(); }
152 PermissionIDSet(const std::set
<PermissionID
>& permissions
);
154 // Check if the set contains a permission with the given ID.
155 bool ContainsID(APIPermission::ID permission_id
) const;
157 std::set
<PermissionID
> permissions_
;
160 } // namespace extensions
162 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_