OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ | 5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ |
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ | 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/memory/singleton.h" | 17 #include "base/memory/singleton.h" |
18 #include "base/string16.h" | 18 #include "base/string16.h" |
19 #include "chrome/common/extensions/url_pattern_set.h" | 19 #include "chrome/common/extensions/url_pattern_set.h" |
20 | 20 |
21 class Extension; | 21 class Extension; |
22 class ExtensionPermissionsInfo; | |
22 | 23 |
23 // When prompting the user to install or approve permissions, we display | 24 // When prompting the user to install or approve permissions, we display |
24 // messages describing the effects of the permissions rather than listing the | 25 // messages describing the effects of the permissions rather than listing the |
25 // permissions themselves. Each ExtensionPermissionMessage represents one of the | 26 // permissions themselves. Each ExtensionPermissionMessage represents one of the |
26 // messages shown to the user. | 27 // messages shown to the user. |
27 class ExtensionPermissionMessage { | 28 class ExtensionPermissionMessage { |
28 public: | 29 public: |
29 // Do not reorder this enumeration. If you need to add a new enum, add it just | 30 // Do not reorder this enumeration. If you need to add a new enum, add it just |
30 // prior to kEnumBoundary. | 31 // prior to kEnumBoundary. |
31 enum ID { | 32 enum ID { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 kWebNavigation, | 124 kWebNavigation, |
124 kWebRequest, | 125 kWebRequest, |
125 kWebSocketProxyPrivate, | 126 kWebSocketProxyPrivate, |
126 kWebstorePrivate, | 127 kWebstorePrivate, |
127 kEnumBoundary | 128 kEnumBoundary |
128 }; | 129 }; |
129 | 130 |
130 enum Flag { | 131 enum Flag { |
131 kFlagNone = 0, | 132 kFlagNone = 0, |
132 | 133 |
133 // Indicates if the permission can be accessed by hosted apps. | |
134 kFlagHostedApp = 1 << 0, | |
135 | |
136 // Indicates if the permission implies full access (native code). | 134 // Indicates if the permission implies full access (native code). |
137 kFlagImpliesFullAccess = 1 << 1, | 135 kFlagImpliesFullAccess = 1 << 0, |
138 | 136 |
139 // Indicates if the permission implies full URL access. | 137 // Indicates if the permission implies full URL access. |
140 kFlagImpliesFullURLAccess = 1 << 2, | 138 kFlagImpliesFullURLAccess = 1 << 1, |
141 | 139 |
142 // Indicates that the permission is private to COMPONENT extensions. | 140 // Indicates that the permission is private to COMPONENT extensions. |
143 kFlagComponentOnly = 1 << 3, | 141 kFlagComponentOnly = 1 << 2, |
144 | 142 |
145 // Indicates that the permission supports the optional permissions API. | 143 // Indicates that the permission supports the optional permissions API. |
146 kFlagSupportsOptional = 1 << 4, | 144 kFlagSupportsOptional = 1 << 3, |
145 }; | |
146 | |
147 // Flags for specifying what extension types can use the permission. | |
148 enum TypeRestriction { | |
149 kTypeNone = 0, | |
150 | |
151 // Extension::TYPE_EXTENSION and Extension::TYPE_USER_SCRIPT | |
152 kTypeExtension = 1 << 0, | |
153 | |
154 // Extension::TYPE_HOSTED_APP | |
155 kTypeHostedApp = 1 << 1, | |
156 | |
157 // Extension::TYPE_PACKAGED_APP | |
158 kTypePackagedApp = 1 << 2, | |
159 | |
160 // Extension::TYPE_PLATFORM_APP | |
161 kTypePlatformApp = 1 << 3, | |
162 | |
163 // Supports all types. | |
164 kTypeAll = (1 << 4) - 1, | |
165 | |
166 // Convenience flag for all types except hosted apps. | |
167 kTypeDefault = kTypeAll - kTypeHostedApp, | |
147 }; | 168 }; |
148 | 169 |
149 typedef std::set<ID> IDSet; | 170 typedef std::set<ID> IDSet; |
150 | 171 |
151 ~ExtensionAPIPermission(); | 172 ~ExtensionAPIPermission(); |
152 | 173 |
153 // Returns the localized permission message associated with this api. | 174 // Returns the localized permission message associated with this api. |
154 ExtensionPermissionMessage GetMessage() const; | 175 ExtensionPermissionMessage GetMessage() const; |
155 | 176 |
156 int flags() const { return flags_; } | 177 int flags() const { return flags_; } |
(...skipping 11 matching lines...) Expand all Loading... | |
168 // Returns true if this permission implies full access (e.g., native code). | 189 // Returns true if this permission implies full access (e.g., native code). |
169 bool implies_full_access() const { | 190 bool implies_full_access() const { |
170 return (flags_ & kFlagImpliesFullAccess) != 0; | 191 return (flags_ & kFlagImpliesFullAccess) != 0; |
171 } | 192 } |
172 | 193 |
173 // Returns true if this permission implies full URL access. | 194 // Returns true if this permission implies full URL access. |
174 bool implies_full_url_access() const { | 195 bool implies_full_url_access() const { |
175 return (flags_ & kFlagImpliesFullURLAccess) != 0; | 196 return (flags_ & kFlagImpliesFullURLAccess) != 0; |
176 } | 197 } |
177 | 198 |
178 // Returns true if this permission can be accessed by hosted apps. | |
179 bool is_hosted_app() const { | |
180 return (flags_ & kFlagHostedApp) != 0; | |
181 } | |
182 | |
183 // Returns true if this permission can only be acquired by COMPONENT | 199 // Returns true if this permission can only be acquired by COMPONENT |
184 // extensions. | 200 // extensions. |
185 bool is_component_only() const { | 201 bool is_component_only() const { |
186 return (flags_ & kFlagComponentOnly) != 0; | 202 return (flags_ & kFlagComponentOnly) != 0; |
187 } | 203 } |
188 | 204 |
205 // Returns true if regular extensions can specify this permission. | |
206 bool supports_extensions() const { | |
207 return (type_restrictions_ & kTypeExtension) != 0; | |
208 } | |
209 | |
210 // Returns true if hosted apps can specify this permission. | |
211 bool supports_hosted_apps() const { | |
212 return (type_restrictions_ & kTypeHostedApp) != 0; | |
213 } | |
214 | |
215 // Returns true if packaged apps can specify this permission. | |
216 bool supports_packaged_apps() const { | |
217 return (type_restrictions_ & kTypePackagedApp) != 0; | |
218 } | |
219 | |
220 // Returns true if platform apps can specify this permission. | |
221 bool supports_platform_apps() const { | |
222 return (type_restrictions_ & kTypePlatformApp) != 0; | |
223 } | |
224 | |
189 // Returns true if this permission can be added and removed via the | 225 // Returns true if this permission can be added and removed via the |
190 // optional permissions extension API. | 226 // optional permissions extension API. |
191 bool supports_optional() const { | 227 bool supports_optional() const { |
192 return (flags_ & kFlagSupportsOptional) != 0; | 228 return (flags_ & kFlagSupportsOptional) != 0; |
193 } | 229 } |
194 | 230 |
195 private: | 231 private: |
196 // Instances should only be constructed from within ExtensionPermissionsInfo. | 232 // Instances should only be constructed from within ExtensionPermissionsInfo. |
197 friend class ExtensionPermissionsInfo; | 233 friend class ExtensionPermissionsInfo; |
198 | 234 |
235 // Registers all the permissions. | |
Aaron Boodman
2011/11/22 09:40:32
I sorta feel like this should be:
// Register ALL
jstritar
2011/11/22 15:58:29
Haha, done. I also renamed the method to RegisterA
| |
236 static void RegisterPermissions(ExtensionPermissionsInfo* info); | |
237 | |
199 explicit ExtensionAPIPermission( | 238 explicit ExtensionAPIPermission( |
200 ID id, | 239 ID id, |
201 const char* name, | 240 const char* name, |
202 int l10n_message_id, | 241 int l10n_message_id, |
203 ExtensionPermissionMessage::ID message_id, | 242 ExtensionPermissionMessage::ID message_id, |
204 int flags); | 243 int flags, |
244 int type_restrictions); | |
205 | 245 |
206 ID id_; | 246 ID id_; |
207 const char* name_; | 247 const char* name_; |
208 int flags_; | 248 int flags_; |
249 int type_restrictions_; | |
209 int l10n_message_id_; | 250 int l10n_message_id_; |
210 ExtensionPermissionMessage::ID message_id_; | 251 ExtensionPermissionMessage::ID message_id_; |
211 }; | 252 }; |
212 | 253 |
213 typedef std::set<ExtensionAPIPermission::ID> ExtensionAPIPermissionSet; | 254 typedef std::set<ExtensionAPIPermission::ID> ExtensionAPIPermissionSet; |
214 | 255 |
215 // Singleton that holds the extension permission instances and provides static | 256 // Singleton that holds the extension permission instances and provides static |
216 // methods for accessing them. | 257 // methods for accessing them. |
217 class ExtensionPermissionsInfo { | 258 class ExtensionPermissionsInfo { |
218 public: | 259 public: |
219 // Returns a pointer to the singleton instance. | 260 // Returns a pointer to the singleton instance. |
220 static ExtensionPermissionsInfo* GetInstance(); | 261 static ExtensionPermissionsInfo* GetInstance(); |
221 | 262 |
222 // Returns the permission with the given |id|, and NULL if it doesn't exist. | 263 // Returns the permission with the given |id|, and NULL if it doesn't exist. |
223 ExtensionAPIPermission* GetByID(ExtensionAPIPermission::ID id); | 264 ExtensionAPIPermission* GetByID(ExtensionAPIPermission::ID id); |
224 | 265 |
225 // Returns the permission with the given |name|, and NULL if none | 266 // Returns the permission with the given |name|, and NULL if none |
226 // exists. | 267 // exists. |
227 ExtensionAPIPermission* GetByName(std::string name); | 268 ExtensionAPIPermission* GetByName(std::string name); |
228 | 269 |
229 // Returns a set containing all valid api permission ids. | 270 // Returns a set containing all valid api permission ids. |
230 ExtensionAPIPermissionSet GetAll(); | 271 ExtensionAPIPermissionSet GetAll(); |
231 | 272 |
232 // Converts all the permission names in |permission_names| to permission ids. | 273 // Converts all the permission names in |permission_names| to permission ids. |
233 ExtensionAPIPermissionSet GetAllByName( | 274 ExtensionAPIPermissionSet GetAllByName( |
234 const std::set<std::string>& permission_names); | 275 const std::set<std::string>& permission_names); |
235 | 276 |
236 // Gets the total number of API permissions available to hosted apps. | |
237 size_t get_hosted_app_permission_count() { | |
238 return hosted_app_permission_count_; | |
239 } | |
240 | |
241 // Gets the total number of API permissions. | 277 // Gets the total number of API permissions. |
242 size_t get_permission_count() { return permission_count_; } | 278 size_t get_permission_count() { return permission_count_; } |
243 | 279 |
244 private: | 280 private: |
281 friend class ExtensionAPIPermission; | |
282 | |
245 ~ExtensionPermissionsInfo(); | 283 ~ExtensionPermissionsInfo(); |
246 ExtensionPermissionsInfo(); | 284 ExtensionPermissionsInfo(); |
247 | 285 |
248 // Registers an |alias| for a given permission |name|. | 286 // Registers an |alias| for a given permission |name|. |
249 void RegisterAlias(const char* name, const char* alias); | 287 void RegisterAlias(const char* name, const char* alias); |
250 | 288 |
251 // Registers a permission with the specified attributes and flags. | 289 // Registers a permission with the specified attributes and flags. |
252 void RegisterPermission( | 290 void RegisterPermission( |
253 ExtensionAPIPermission::ID id, | 291 ExtensionAPIPermission::ID id, |
254 const char* name, | 292 const char* name, |
255 int l10n_message_id, | 293 int l10n_message_id, |
256 ExtensionPermissionMessage::ID message_id, | 294 ExtensionPermissionMessage::ID message_id, |
257 int flags); | 295 int flags, |
296 int type_restrictions); | |
258 | 297 |
259 // Maps permission ids to permissions. | 298 // Maps permission ids to permissions. |
260 typedef std::map<ExtensionAPIPermission::ID, ExtensionAPIPermission*> IDMap; | 299 typedef std::map<ExtensionAPIPermission::ID, ExtensionAPIPermission*> IDMap; |
261 | 300 |
262 // Maps names and aliases to permissions. | 301 // Maps names and aliases to permissions. |
263 typedef std::map<std::string, ExtensionAPIPermission*> NameMap; | 302 typedef std::map<std::string, ExtensionAPIPermission*> NameMap; |
264 | 303 |
265 IDMap id_map_; | 304 IDMap id_map_; |
266 NameMap name_map_; | 305 NameMap name_map_; |
267 | 306 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 | 453 |
415 // The list of hosts that can be scripted by content scripts. | 454 // The list of hosts that can be scripted by content scripts. |
416 // TODO(jstritar): Rename to "user_script_hosts_"? | 455 // TODO(jstritar): Rename to "user_script_hosts_"? |
417 URLPatternSet scriptable_hosts_; | 456 URLPatternSet scriptable_hosts_; |
418 | 457 |
419 // The list of hosts this effectively grants access to. | 458 // The list of hosts this effectively grants access to. |
420 URLPatternSet effective_hosts_; | 459 URLPatternSet effective_hosts_; |
421 }; | 460 }; |
422 | 461 |
423 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ | 462 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_PERMISSION_SET_H_ |
OLD | NEW |