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
,
40 kAccessibilityPrivate
,
54 kBookmarkManagerPrivate
,
55 kBrailleDisplayPrivate
,
64 kCommandsAccessibility
,
75 kDeclarativeWebRequest
,
86 kEmbeddedExtensionOptions
,
87 kEnterprisePlatformKeys
,
88 kEnterprisePlatformKeysPrivate
,
89 kExperienceSamplingPrivate
,
93 kFileBrowserHandlerInternal
,
98 kFileSystemRetainEntries
,
100 kFileSystemWriteDirectory
,
122 kMediaGalleriesPrivate
,
126 kMusicManagerPrivate
,
129 kNotificationProvider
,
131 kOverrideEscFullscreen
,
154 kSyncedNotificationsPrivate
,
170 kVirtualKeyboardPrivate
,
174 kWebConnectable
, // for externally_connectable manifest key
179 kWebrtcLoggingPrivate
,
197 explicit APIPermission(const APIPermissionInfo
* info
);
199 virtual ~APIPermission();
201 // Returns the id of this permission.
204 // Returns the name of this permission.
205 const char* name() const;
207 // Returns the APIPermission of this permission.
208 const APIPermissionInfo
* info() const {
212 // Returns true if this permission has any PermissionMessages.
213 virtual bool HasMessages() const = 0;
215 // Returns the localized permission messages of this permission.
216 virtual PermissionMessages
GetMessages() const = 0;
218 // Returns true if the given permission is allowed.
219 virtual bool Check(const CheckParam
* param
) const = 0;
221 // Returns true if |rhs| is a subset of this.
222 virtual bool Contains(const APIPermission
* rhs
) const = 0;
224 // Returns true if |rhs| is equal to this.
225 virtual bool Equal(const APIPermission
* rhs
) const = 0;
227 // Parses the APIPermission from |value|. Returns false if an error happens
228 // and optionally set |error| if |error| is not NULL. If |value| represents
229 // multiple permissions, some are invalid, and |unhandled_permissions| is
230 // not NULL, the invalid ones are put into |unhandled_permissions| and the
231 // function returns true.
232 virtual bool FromValue(const base::Value
* value
,
234 std::vector
<std::string
>* unhandled_permissions
) = 0;
236 // Stores this into a new created |value|.
237 virtual scoped_ptr
<base::Value
> ToValue() const = 0;
240 virtual APIPermission
* Clone() const = 0;
242 // Returns a new API permission which equals this - |rhs|.
243 virtual APIPermission
* Diff(const APIPermission
* rhs
) const = 0;
245 // Returns a new API permission which equals the union of this and |rhs|.
246 virtual APIPermission
* Union(const APIPermission
* rhs
) const = 0;
248 // Returns a new API permission which equals the intersect of this and |rhs|.
249 virtual APIPermission
* Intersect(const APIPermission
* rhs
) const = 0;
252 // Writes this into the given IPC message |m|.
253 virtual void Write(IPC::Message
* m
) const = 0;
255 // Reads from the given IPC message |m|.
256 virtual bool Read(const IPC::Message
* m
, PickleIterator
* iter
) = 0;
258 // Logs this permission.
259 virtual void Log(std::string
* log
) const = 0;
262 // Returns the localized permission message associated with this api.
263 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
264 PermissionMessage
GetMessage_() const;
267 const APIPermissionInfo
* const info_
;
271 // The APIPermissionInfo is an immutable class that describes a single
272 // named permission (API permission).
273 // There is one instance per permission.
274 class APIPermissionInfo
{
279 // Indicates if the permission implies full access (native code).
280 kFlagImpliesFullAccess
= 1 << 0,
282 // Indicates if the permission implies full URL access.
283 kFlagImpliesFullURLAccess
= 1 << 1,
285 // Indicates that extensions cannot specify the permission as optional.
286 kFlagCannotBeOptional
= 1 << 3,
288 // Indicates that the permission is internal to the extensions
289 // system and cannot be specified in the "permissions" list.
290 kFlagInternal
= 1 << 4,
293 typedef APIPermission
* (*APIPermissionConstructor
)(const APIPermissionInfo
*);
295 typedef std::set
<APIPermission::ID
> IDSet
;
297 ~APIPermissionInfo();
299 // Creates a APIPermission instance.
300 APIPermission
* CreateAPIPermission() const;
302 int flags() const { return flags_
; }
304 APIPermission::ID
id() const { return id_
; }
306 // Returns the message id associated with this permission.
307 PermissionMessage::ID
message_id() const {
311 // Returns the name of this permission.
312 const char* name() const { return name_
; }
314 // Returns true if this permission implies full access (e.g., native code).
315 bool implies_full_access() const {
316 return (flags_
& kFlagImpliesFullAccess
) != 0;
319 // Returns true if this permission implies full URL access.
320 bool implies_full_url_access() const {
321 return (flags_
& kFlagImpliesFullURLAccess
) != 0;
324 // Returns true if this permission can be added and removed via the
325 // optional permissions extension API.
326 bool supports_optional() const {
327 return (flags_
& kFlagCannotBeOptional
) == 0;
330 // Returns true if this permission is internal rather than a
331 // "permissions" list entry.
332 bool is_internal() const {
333 return (flags_
& kFlagInternal
) != 0;
337 // Instances should only be constructed from within a PermissionsProvider.
338 friend class ChromeAPIPermissions
;
339 friend class ExtensionsAPIPermissions
;
340 // Implementations of APIPermission will want to get the permission message,
341 // but this class's implementation should be hidden from everyone else.
342 friend class APIPermission
;
344 // This exists to allow aggregate initialization, so that default values
345 // for flags, etc. can be omitted.
346 // TODO(yoz): Simplify the way initialization is done. APIPermissionInfo
347 // should be the simple data struct.
349 APIPermission::ID id
;
353 PermissionMessage::ID message_id
;
354 APIPermissionInfo::APIPermissionConstructor constructor
;
357 explicit APIPermissionInfo(const InitInfo
& info
);
359 // Returns the localized permission message associated with this api.
360 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
361 PermissionMessage
GetMessage_() const;
363 const APIPermission::ID id_
;
364 const char* const name_
;
366 const int l10n_message_id_
;
367 const PermissionMessage::ID message_id_
;
368 const APIPermissionConstructor api_permission_constructor_
;
371 } // namespace extensions
373 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_