OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_ | 5 #ifndef EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_ |
6 #define EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_ | 6 #define EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_ |
7 | 7 |
8 #include <iterator> | |
9 #include <map> | |
10 | 8 |
11 #include "base/memory/linked_ptr.h" | |
12 #include "extensions/common/permissions/api_permission.h" | 9 #include "extensions/common/permissions/api_permission.h" |
| 10 #include "extensions/common/permissions/base_set_operators.h" |
13 | 11 |
14 namespace base { | 12 namespace base { |
15 class ListValue; | 13 class ListValue; |
16 } // namespace base | 14 } // namespace base |
17 | 15 |
18 namespace extensions { | 16 namespace extensions { |
19 | 17 |
20 class Extension; | 18 class Extension; |
| 19 class APIPermissionSet; |
21 | 20 |
22 typedef std::map<APIPermission::ID, | 21 template<> |
23 linked_ptr<APIPermission> > APIPermissionMap; | 22 struct BaseSetOperatorsTraits<APIPermissionSet> { |
| 23 typedef APIPermission ElementType; |
| 24 typedef APIPermission::ID ElementIDType; |
| 25 }; |
24 | 26 |
25 class APIPermissionSet { | 27 class APIPermissionSet : public BaseSetOperators<APIPermissionSet> { |
26 public: | 28 public: |
27 class const_iterator : | |
28 public std::iterator<std::input_iterator_tag, const APIPermission*> { | |
29 public: | |
30 const_iterator(const APIPermissionMap::const_iterator& it); | |
31 const_iterator(const const_iterator& ids_it); | |
32 | |
33 const_iterator& operator++() { | |
34 ++it_; | |
35 return *this; | |
36 } | |
37 | |
38 const_iterator operator++(int) { | |
39 const_iterator tmp(it_++); | |
40 return tmp; | |
41 } | |
42 | |
43 bool operator==(const const_iterator& rhs) const { | |
44 return it_ == rhs.it_; | |
45 } | |
46 | |
47 bool operator!=(const const_iterator& rhs) const { | |
48 return it_ != rhs.it_; | |
49 } | |
50 | |
51 const APIPermission* operator*() const { | |
52 return it_->second.get(); | |
53 } | |
54 | |
55 const APIPermission* operator->() const { | |
56 return it_->second.get(); | |
57 } | |
58 | |
59 private: | |
60 APIPermissionMap::const_iterator it_; | |
61 }; | |
62 | |
63 enum ParseSource { | 29 enum ParseSource { |
64 // Don't allow internal permissions to be parsed (e.g. entries in the | 30 // Don't allow internal permissions to be parsed (e.g. entries in the |
65 // "permissions" list in a manifest). | 31 // "permissions" list in a manifest). |
66 kDisallowInternalPermissions, | 32 kDisallowInternalPermissions, |
67 | 33 |
68 // Allow internal permissions to be parsed (e.g. from the "api" field of a | 34 // Allow internal permissions to be parsed (e.g. from the "api" field of a |
69 // permissions list in the prefs). | 35 // permissions list in the prefs). |
70 kAllowInternalPermissions, | 36 kAllowInternalPermissions, |
71 }; | 37 }; |
72 | 38 |
73 APIPermissionSet(); | |
74 | |
75 APIPermissionSet(const APIPermissionSet& set); | |
76 | |
77 ~APIPermissionSet(); | |
78 | |
79 const_iterator begin() const { | |
80 return const_iterator(map().begin()); | |
81 } | |
82 | |
83 const_iterator end() const { | |
84 return map().end(); | |
85 } | |
86 | |
87 const_iterator find(APIPermission::ID id) const { | |
88 return map().find(id); | |
89 } | |
90 | |
91 const APIPermissionMap& map() const { | |
92 return map_; | |
93 } | |
94 | |
95 APIPermissionMap& map() { | |
96 return map_; | |
97 } | |
98 | |
99 void clear() { | |
100 map_.clear(); | |
101 } | |
102 | |
103 size_t count(APIPermission::ID id) const { | |
104 return map().count(id); | |
105 } | |
106 | |
107 bool empty() const { | |
108 return map().empty(); | |
109 } | |
110 | |
111 size_t erase(APIPermission::ID id) { | |
112 return map().erase(id); | |
113 } | |
114 | |
115 size_t size() const { | |
116 return map().size(); | |
117 } | |
118 | |
119 APIPermissionSet& operator=(const APIPermissionSet& rhs); | |
120 | |
121 bool operator==(const APIPermissionSet& rhs) const; | |
122 | |
123 bool operator!=(const APIPermissionSet& rhs) const { | |
124 return !operator==(rhs); | |
125 } | |
126 | |
127 void insert(APIPermission::ID id); | 39 void insert(APIPermission::ID id); |
128 | 40 |
129 // Insert |permission| into the APIPermissionSet. The APIPermissionSet will | 41 // Insert |permission| into the APIPermissionSet. The APIPermissionSet will |
130 // take the ownership of |permission|, | 42 // take the ownership of |permission|, |
131 void insert(APIPermission* permission); | 43 void insert(APIPermission* permission); |
132 | 44 |
133 bool Contains(const APIPermissionSet& rhs) const; | |
134 | |
135 static void Difference( | |
136 const APIPermissionSet& set1, | |
137 const APIPermissionSet& set2, | |
138 APIPermissionSet* set3); | |
139 | |
140 static void Intersection( | |
141 const APIPermissionSet& set1, | |
142 const APIPermissionSet& set2, | |
143 APIPermissionSet* set3); | |
144 | |
145 static void Union( | |
146 const APIPermissionSet& set1, | |
147 const APIPermissionSet& set2, | |
148 APIPermissionSet* set3); | |
149 | |
150 // Parses permissions from |permissions| and adds the parsed permissions to | 45 // Parses permissions from |permissions| and adds the parsed permissions to |
151 // |api_permissions|. If |source| is kDisallowInternalPermissions, treat | 46 // |api_permissions|. If |source| is kDisallowInternalPermissions, treat |
152 // permissions with kFlagInternal as errors. If |unhandled_permissions| | 47 // permissions with kFlagInternal as errors. If |unhandled_permissions| is |
153 // is not NULL, the names of all permissions that couldn't be parsed will be | 48 // not NULL, the names of all permissions that couldn't be parsed will be |
154 // added to this vector. If |error| is NULL, parsing will continue with the | 49 // added to this vector. If |error| is NULL, parsing will continue with the |
155 // next permission if invalid data is detected. If |error| is not NULL, it | 50 // next permission if invalid data is detected. If |error| is not NULL, it |
156 // will be set to an error message and false is returned when an invalid | 51 // will be set to an error message and false is returned when an invalid |
157 // permission is found. | 52 // permission is found. |
158 static bool ParseFromJSON( | 53 static bool ParseFromJSON( |
159 const base::ListValue* permissions, | 54 const base::ListValue* permissions, |
160 ParseSource source, | 55 ParseSource source, |
161 APIPermissionSet* api_permissions, | 56 APIPermissionSet* api_permissions, |
162 string16* error, | 57 string16* error, |
163 std::vector<std::string>* unhandled_permissions); | 58 std::vector<std::string>* unhandled_permissions); |
164 | 59 |
165 void AddImpliedPermissions(); | 60 void AddImpliedPermissions(); |
166 | |
167 private: | |
168 APIPermissionMap map_; | |
169 }; | 61 }; |
170 | 62 |
171 } // namespace extensions | 63 } // namespace extensions |
172 | 64 |
173 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_ | 65 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_ |
OLD | NEW |