Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / permissions / api_permission_set.h
blobf96c50ed16c7caa3ec64789f34e1a78e9c278a22
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"
12 namespace base {
13 class ListValue;
14 } // namespace base
16 namespace extensions {
18 class APIPermissionSet;
19 class Extension;
21 template<>
22 struct BaseSetOperatorsTraits<APIPermissionSet> {
23 typedef APIPermission ElementType;
24 typedef APIPermission::ID ElementIDType;
27 class APIPermissionSet : public BaseSetOperators<APIPermissionSet> {
28 public:
29 enum ParseSource {
30 // Don't allow internal permissions to be parsed (e.g. entries in the
31 // "permissions" list in a manifest).
32 kDisallowInternalPermissions,
34 // Allow internal permissions to be parsed (e.g. from the "api" field of a
35 // permissions list in the prefs).
36 kAllowInternalPermissions,
39 void insert(APIPermission::ID id);
41 // Insert |permission| into the APIPermissionSet. The APIPermissionSet will
42 // take the ownership of |permission|,
43 void insert(APIPermission* permission);
45 // Parses permissions from |permissions| and adds the parsed permissions to
46 // |api_permissions|. If |source| is kDisallowInternalPermissions, treat
47 // permissions with kFlagInternal as errors. If |unhandled_permissions| is
48 // not NULL, the names of all permissions that couldn't be parsed will be
49 // added to this vector. If |error| is NULL, parsing will continue with the
50 // next permission if invalid data is detected. If |error| is not NULL, it
51 // will be set to an error message and false is returned when an invalid
52 // permission is found.
53 static bool ParseFromJSON(
54 const base::ListValue* permissions,
55 ParseSource source,
56 APIPermissionSet* api_permissions,
57 base::string16* error,
58 std::vector<std::string>* unhandled_permissions);
60 void AddImpliedPermissions();
63 // An ID representing a single permission that belongs to an app or extension.
65 // Each PermissionID has a required ID to identify the permission. For most
66 // permissions, this is all they have.
68 // Some more complex permissions have a parameter, which acts like an argument
69 // for the permission. For example, host permissions might have the ID
70 // kReadOnlyHost and the argument 'www.google.com' (the host which is
71 // read-only). Parameters are passed to the permission message rules for this
72 // permission, so they can affect the displayed message.
74 // Note: Inheriting from std::pair automatically gives us an operator<
75 // (required for putting these into an std::set).
77 // TODO(sashab): Move this to the same file as PermissionIDSet once that moves
78 // to its own file.
79 class PermissionID : public std::pair<APIPermission::ID, base::string16> {
80 public:
81 explicit PermissionID(APIPermission::ID id);
82 PermissionID(APIPermission::ID id, const base::string16& parameter);
83 virtual ~PermissionID();
85 const APIPermission::ID& id() const { return this->first; }
86 const base::string16& parameter() const { return this->second; }
89 // A set of permissions for an app or extension. Used for passing around groups
90 // of permissions, such as required or optional permissions.
92 // Each permission can also store a string, such as a hostname or device number,
93 // as a parameter that helps identify the permission. This parameter can then
94 // be used when the permission message is generated. For example, the permission
95 // kHostReadOnly might have the parameter "google.com", which means that the app
96 // or extension has the permission to read the host google.com. This parameter
97 // may then be included in the permission message when it is generated later.
99 // Example:
100 // // Create an empty PermissionIDSet.
101 // PermissionIDSet p;
102 // // Add a permission to the set.
103 // p.insert(APIPermission::kNetworkState);
104 // // Add a permission with a parameter to the set.
105 // p.insert(APIPermission::kHostReadOnly,
106 // base::ASCIIToUTF16("http://www.google.com"));
108 // TODO(sashab): Move this to its own file and rename it to PermissionSet after
109 // APIPermission is removed, the current PermissionSet is no longer used, and
110 // APIPermission::ID is the only type of Permission ID.
111 class PermissionIDSet {
112 public:
113 using const_iterator = std::set<PermissionID>::const_iterator;
115 PermissionIDSet();
116 virtual ~PermissionIDSet();
118 // Adds the given permission, and an optional parameter, to the set.
119 void insert(APIPermission::ID permission_id);
120 void insert(APIPermission::ID permission_id,
121 const base::string16& permission_parameter);
122 void InsertAll(const PermissionIDSet& permission_set);
124 void erase(APIPermission::ID permission_id);
126 // Returns the parameters for all PermissionIDs in this set.
127 std::vector<base::string16> GetAllPermissionParameters() const;
129 // Check if the set contains a permission with the given ID.
130 bool ContainsID(APIPermission::ID permission_id) const;
132 // Check if the set contains permissions with all the given IDs.
133 bool ContainsAllIDs(const std::set<APIPermission::ID>& permission_ids) const;
135 // Returns all the permissions in this set with one of the given IDs.
136 PermissionIDSet GetAllPermissionsWithIDs(
137 const std::set<APIPermission::ID>& permission_ids) const;
139 // Convenience functions that call their stl_util counterparts.
140 bool Includes(const PermissionIDSet& subset) const;
141 bool Equals(const PermissionIDSet& set) const;
142 static PermissionIDSet Difference(const PermissionIDSet& set_1,
143 const PermissionIDSet& set_2);
144 static PermissionIDSet Intersection(const PermissionIDSet& set_1,
145 const PermissionIDSet& set_2);
146 static PermissionIDSet Union(const PermissionIDSet& set_1,
147 const PermissionIDSet& set_2);
149 size_t size() const;
150 bool empty() const;
152 const_iterator begin() const { return permissions_.begin(); }
153 const_iterator end() const { return permissions_.end(); }
155 private:
156 PermissionIDSet(const std::set<PermissionID>& permissions);
158 std::set<PermissionID> permissions_;
161 } // namespace extensions
163 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_