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_H_
6 #define EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/pickle.h"
15 #include "base/values.h"
16 #include "extensions/common/permissions/permission_message.h"
22 namespace extensions {
24 class APIPermissionInfo;
25 class ChromeAPIPermissions;
27 // APIPermission is for handling some complex permissions. Please refer to
28 // extensions::SocketPermission as an example.
29 // There is one instance per permission per loaded extension.
38 kAccessibilityFeaturesModify,
39 kAccessibilityFeaturesRead,
51 kBookmarkManagerPrivate,
52 kBrailleDisplayPrivate,
69 kDeclarativeWebRequest,
79 kEnterprisePlatformKeysPrivate,
83 kFileBrowserHandlerInternal,
88 kFileSystemRetainEntries,
90 kFileSystemWriteDirectory,
110 kMediaGalleriesPrivate,
114 kMusicManagerPrivate,
118 kOverrideEscFullscreen,
156 kVirtualKeyboardPrivate,
159 kWebConnectable, // for externally_connectable manifest key
165 kWebrtcLoggingPrivate,
182 explicit APIPermission(const APIPermissionInfo* info);
184 virtual ~APIPermission();
186 // Returns the id of this permission.
189 // Returns the name of this permission.
190 const char* name() const;
192 // Returns the APIPermission of this permission.
193 const APIPermissionInfo* info() const {
197 // Returns true if this permission has any PermissionMessages.
198 virtual bool HasMessages() const = 0;
200 // Returns the localized permission messages of this permission.
201 virtual PermissionMessages GetMessages() const = 0;
203 // Returns true if the given permission is allowed.
204 virtual bool Check(const CheckParam* param) const = 0;
206 // Returns true if |rhs| is a subset of this.
207 virtual bool Contains(const APIPermission* rhs) const = 0;
209 // Returns true if |rhs| is equal to this.
210 virtual bool Equal(const APIPermission* rhs) const = 0;
212 // Parses the APIPermission from |value|. Returns false if an error happens
213 // and optionally set |error| if |error| is not NULL.
214 virtual bool FromValue(const base::Value* value, std::string* error) = 0;
216 // Stores this into a new created |value|.
217 virtual scoped_ptr<base::Value> ToValue() const = 0;
220 virtual APIPermission* Clone() const = 0;
222 // Returns a new API permission which equals this - |rhs|.
223 virtual APIPermission* Diff(const APIPermission* rhs) const = 0;
225 // Returns a new API permission which equals the union of this and |rhs|.
226 virtual APIPermission* Union(const APIPermission* rhs) const = 0;
228 // Returns a new API permission which equals the intersect of this and |rhs|.
229 virtual APIPermission* Intersect(const APIPermission* rhs) const = 0;
232 // Writes this into the given IPC message |m|.
233 virtual void Write(IPC::Message* m) const = 0;
235 // Reads from the given IPC message |m|.
236 virtual bool Read(const IPC::Message* m, PickleIterator* iter) = 0;
238 // Logs this permission.
239 virtual void Log(std::string* log) const = 0;
242 // Returns the localized permission message associated with this api.
243 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
244 PermissionMessage GetMessage_() const;
247 const APIPermissionInfo* const info_;
251 // The APIPermissionInfo is an immutable class that describes a single
252 // named permission (API permission).
253 // There is one instance per permission.
254 class APIPermissionInfo {
259 // Indicates if the permission implies full access (native code).
260 kFlagImpliesFullAccess = 1 << 0,
262 // Indicates if the permission implies full URL access.
263 kFlagImpliesFullURLAccess = 1 << 1,
265 // Indicates that extensions cannot specify the permission as optional.
266 kFlagCannotBeOptional = 1 << 3,
268 // Indicates that the permission is internal to the extensions
269 // system and cannot be specified in the "permissions" list.
270 kFlagInternal = 1 << 4,
273 typedef APIPermission* (*APIPermissionConstructor)(const APIPermissionInfo*);
275 typedef std::set<APIPermission::ID> IDSet;
277 ~APIPermissionInfo();
279 // Creates a APIPermission instance.
280 APIPermission* CreateAPIPermission() const;
282 int flags() const { return flags_; }
284 APIPermission::ID id() const { return id_; }
286 // Returns the message id associated with this permission.
287 PermissionMessage::ID message_id() const {
291 // Returns the name of this permission.
292 const char* name() const { return name_; }
294 // Returns true if this permission implies full access (e.g., native code).
295 bool implies_full_access() const {
296 return (flags_ & kFlagImpliesFullAccess) != 0;
299 // Returns true if this permission implies full URL access.
300 bool implies_full_url_access() const {
301 return (flags_ & kFlagImpliesFullURLAccess) != 0;
304 // Returns true if this permission can be added and removed via the
305 // optional permissions extension API.
306 bool supports_optional() const {
307 return (flags_ & kFlagCannotBeOptional) == 0;
310 // Returns true if this permission is internal rather than a
311 // "permissions" list entry.
312 bool is_internal() const {
313 return (flags_ & kFlagInternal) != 0;
317 // Instances should only be constructed from within a PermissionsProvider.
318 friend class ChromeAPIPermissions;
319 // Implementations of APIPermission will want to get the permission message,
320 // but this class's implementation should be hidden from everyone else.
321 friend class APIPermission;
323 explicit APIPermissionInfo(
324 APIPermission::ID id,
327 PermissionMessage::ID message_id,
329 APIPermissionConstructor api_permission_constructor);
331 // Returns the localized permission message associated with this api.
332 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
333 PermissionMessage GetMessage_() const;
335 const APIPermission::ID id_;
336 const char* const name_;
338 const int l10n_message_id_;
339 const PermissionMessage::ID message_id_;
340 const APIPermissionConstructor api_permission_constructor_;
343 } // namespace extensions
345 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_