Extensions: Remove the legacy GetMessages/HasMessages
[chromium-blink-merge.git] / extensions / common / permissions / api_permission_set.h
blob476ad13ab88d78da762946b386336b6cc0f3a80f
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_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/strings/string16.h"
13 #include "extensions/common/permissions/api_permission.h"
14 #include "extensions/common/permissions/base_set_operators.h"
16 namespace base {
17 class ListValue;
18 } // namespace base
20 namespace extensions {
22 class APIPermissionSet;
23 class Extension;
25 template<>
26 struct BaseSetOperatorsTraits<APIPermissionSet> {
27 typedef APIPermission ElementType;
28 typedef APIPermission::ID ElementIDType;
31 class APIPermissionSet : public BaseSetOperators<APIPermissionSet> {
32 public:
33 enum ParseSource {
34 // Don't allow internal permissions to be parsed (e.g. entries in the
35 // "permissions" list in a manifest).
36 kDisallowInternalPermissions,
38 // Allow internal permissions to be parsed (e.g. from the "api" field of a
39 // permissions list in the prefs).
40 kAllowInternalPermissions,
43 void insert(APIPermission::ID id);
45 // Insert |permission| into the APIPermissionSet. The APIPermissionSet will
46 // take the ownership of |permission|,
47 void insert(APIPermission* permission);
49 // Parses permissions from |permissions| and adds the parsed permissions to
50 // |api_permissions|. If |source| is kDisallowInternalPermissions, treat
51 // permissions with kFlagInternal as errors. If |unhandled_permissions| is
52 // not NULL, the names of all permissions that couldn't be parsed will be
53 // added to this vector. If |error| is NULL, parsing will continue with the
54 // next permission if invalid data is detected. If |error| is not NULL, it
55 // will be set to an error message and false is returned when an invalid
56 // permission is found.
57 static bool ParseFromJSON(
58 const base::ListValue* permissions,
59 ParseSource source,
60 APIPermissionSet* api_permissions,
61 base::string16* error,
62 std::vector<std::string>* unhandled_permissions);
64 void AddImpliedPermissions();
67 // An ID representing a single permission that belongs to an app or extension.
69 // Each PermissionID has a required ID to identify the permission. For most
70 // permissions, this is all they have.
72 // Some more complex permissions have a parameter, which acts like an argument
73 // for the permission. For example, host permissions might have the ID
74 // kReadOnlyHost and the argument 'www.google.com' (the host which is
75 // read-only). Parameters are passed to the permission message rules for this
76 // permission, so they can affect the displayed message.
78 // Note: Inheriting from std::pair automatically gives us an operator<
79 // (required for putting these into an std::set).
81 // TODO(sashab): Move this to the same file as PermissionIDSet once that moves
82 // to its own file.
83 class PermissionID : public std::pair<APIPermission::ID, base::string16> {
84 public:
85 explicit PermissionID(APIPermission::ID id);
86 PermissionID(APIPermission::ID id, const base::string16& parameter);
87 virtual ~PermissionID();
89 const APIPermission::ID& id() const { return this->first; }
90 const base::string16& parameter() const { return this->second; }
93 // A set of permissions for an app or extension. Used for passing around groups
94 // of permissions, such as required or optional permissions.
96 // Each permission can also store a string, such as a hostname or device number,
97 // as a parameter that helps identify the permission. This parameter can then
98 // be used when the permission message is generated. For example, the permission
99 // kHostReadOnly might have the parameter "google.com", which means that the app
100 // or extension has the permission to read the host google.com. This parameter
101 // may then be included in the permission message when it is generated later.
103 // Example:
104 // // Create an empty PermissionIDSet.
105 // PermissionIDSet p;
106 // // Add a permission to the set.
107 // p.insert(APIPermission::kNetworkState);
108 // // Add a permission with a parameter to the set.
109 // p.insert(APIPermission::kHostReadOnly,
110 // base::ASCIIToUTF16("http://www.google.com"));
112 // TODO(sashab): Move this to its own file and rename it to PermissionSet after
113 // APIPermission is removed, the current PermissionSet is no longer used, and
114 // APIPermission::ID is the only type of Permission ID.
115 class PermissionIDSet {
116 public:
117 using const_iterator = std::set<PermissionID>::const_iterator;
119 PermissionIDSet();
120 virtual ~PermissionIDSet();
122 // Adds the given permission, and an optional parameter, to the set.
123 void insert(APIPermission::ID permission_id);
124 void insert(APIPermission::ID permission_id,
125 const base::string16& permission_parameter);
126 void InsertAll(const PermissionIDSet& permission_set);
128 void erase(APIPermission::ID permission_id);
130 // Returns the parameters for all PermissionIDs in this set.
131 std::vector<base::string16> GetAllPermissionParameters() const;
133 // Check if the set contains a permission with the given ID.
134 bool ContainsID(APIPermission::ID permission_id) const;
136 // Check if the set contains permissions with all the given IDs.
137 bool ContainsAllIDs(const std::set<APIPermission::ID>& permission_ids) const;
139 // Returns all the permissions in this set with one of the given IDs.
140 PermissionIDSet GetAllPermissionsWithIDs(
141 const std::set<APIPermission::ID>& permission_ids) const;
143 // Convenience functions for common set operations.
144 bool Includes(const PermissionIDSet& subset) const;
145 bool Equals(const PermissionIDSet& set) const;
146 static PermissionIDSet Difference(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_