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_
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/pickle.h"
16 #include "base/values.h"
17 #include "extensions/common/permissions/permission_message.h"
23 namespace extensions
{
25 class PermissionIDSet
;
26 class APIPermissionInfo
;
27 class ChromeAPIPermissions
;
29 // APIPermission is for handling some complex permissions. Please refer to
30 // extensions::SocketPermission as an example.
31 // There is one instance per permission per loaded extension.
34 // The IDs of all permissions available to apps. Add as many permissions here
35 // as needed to generate meaningful permission messages. Add the rules for the
36 // messages to ChromePermissionMessageProvider.
37 // Remove permissions from this list if they have no longer have a
38 // corresponding API permission and no permission message.
39 // TODO(sashab): Move this to a more central location, and rename it to
47 kAccessibilityFeaturesModify
,
48 kAccessibilityFeaturesRead
,
49 kAccessibilityPrivate
,
64 kBookmarkManagerPrivate
,
65 kBrailleDisplayPrivate
,
75 kCommandsAccessibility
,
88 kDeclarativeWebRequest
,
90 kDesktopCapturePrivate
,
101 kEmbeddedExtensionOptions
,
102 kEnterprisePlatformKeys
,
103 kEnterprisePlatformKeysPrivate
,
104 kExperienceSamplingPrivate
,
107 kExternallyConnectableAllUrls
,
110 kFileBrowserHandlerInternal
,
113 kFileSystemDirectory
,
115 kFileSystemRetainEntries
,
117 kFileSystemWriteDirectory
,
134 kInlineInstallPrivate
,
145 kMusicManagerPrivate
,
149 kNotificationProvider
,
151 kOverrideEscFullscreen
,
191 kVirtualKeyboardPrivate
,
196 kWebConnectable
, // for externally_connectable manifest key
201 kWebrtcLoggingPrivate
,
212 // Permission message IDs that are not currently valid permissions on their
213 // own, but are needed by various manifest permissions to represent their
214 // permission message rule combinations.
215 // TODO(sashab): Move these in-line with the other permission IDs.
224 kMediaGalleriesAllGalleriesCopyTo
,
225 kMediaGalleriesAllGalleriesDelete
,
226 kMediaGalleriesAllGalleriesRead
,
228 kOverrideBookmarksUI
,
232 kSocketSpecificHosts
,
234 kUsbDeviceUnknownProduct
,
235 kUsbDeviceUnknownVendor
,
243 explicit APIPermission(const APIPermissionInfo
* info
);
245 virtual ~APIPermission();
247 // Returns the id of this permission.
250 // Returns the name of this permission.
251 const char* name() const;
253 // Returns the APIPermission of this permission.
254 const APIPermissionInfo
* info() const {
258 // The set of permissions an app/extension with this API permission has. These
259 // permissions are used by PermissionMessageProvider to generate meaningful
260 // permission messages for the app/extension.
262 // For simple API permissions, this will return a set containing only the ID
263 // of the permission. More complex permissions might have multiple IDs, one
264 // for each of the capabilities the API permission has (e.g. read, write and
265 // copy, in the case of the media gallery permission). Permissions that
266 // require parameters may also contain a parameter string (along with the
267 // permission's ID) which can be substituted into the permission message if a
268 // rule is defined to do so.
270 // Permissions with multiple values, such as host permissions, are represented
271 // by multiple entries in this set. Each permission in the subset has the same
272 // ID (e.g. kHostReadOnly) but a different parameter (e.g. google.com). These
273 // are grouped to form different kinds of permission messages (e.g. 'Access to
274 // 2 hosts') depending on the number that are in the set. The rules that
275 // define the grouping of related permissions with the same ID is defined in
276 // ChromePermissionMessageProvider.
277 virtual PermissionIDSet
GetPermissions() const = 0;
279 // Returns true if this permission has any PermissionMessages.
280 // TODO(sashab): Deprecate this in favor of GetPermissions() above.
281 virtual bool HasMessages() const = 0;
283 // Returns the localized permission messages of this permission.
284 // TODO(sashab): Deprecate this in favor of GetPermissions() above.
285 virtual PermissionMessages
GetMessages() const = 0;
287 // Returns true if the given permission is allowed.
288 virtual bool Check(const CheckParam
* param
) const = 0;
290 // Returns true if |rhs| is a subset of this.
291 virtual bool Contains(const APIPermission
* rhs
) const = 0;
293 // Returns true if |rhs| is equal to this.
294 virtual bool Equal(const APIPermission
* rhs
) const = 0;
296 // Parses the APIPermission from |value|. Returns false if an error happens
297 // and optionally set |error| if |error| is not NULL. If |value| represents
298 // multiple permissions, some are invalid, and |unhandled_permissions| is
299 // not NULL, the invalid ones are put into |unhandled_permissions| and the
300 // function returns true.
301 virtual bool FromValue(const base::Value
* value
,
303 std::vector
<std::string
>* unhandled_permissions
) = 0;
305 // Stores this into a new created |value|.
306 virtual scoped_ptr
<base::Value
> ToValue() const = 0;
309 virtual APIPermission
* Clone() const = 0;
311 // Returns a new API permission which equals this - |rhs|.
312 virtual APIPermission
* Diff(const APIPermission
* rhs
) const = 0;
314 // Returns a new API permission which equals the union of this and |rhs|.
315 virtual APIPermission
* Union(const APIPermission
* rhs
) const = 0;
317 // Returns a new API permission which equals the intersect of this and |rhs|.
318 virtual APIPermission
* Intersect(const APIPermission
* rhs
) const = 0;
321 // Writes this into the given IPC message |m|.
322 virtual void Write(IPC::Message
* m
) const = 0;
324 // Reads from the given IPC message |m|.
325 virtual bool Read(const IPC::Message
* m
, PickleIterator
* iter
) = 0;
327 // Logs this permission.
328 virtual void Log(std::string
* log
) const = 0;
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;
336 const APIPermissionInfo
* const info_
;
340 // The APIPermissionInfo is an immutable class that describes a single
341 // named permission (API permission).
342 // There is one instance per permission.
343 class APIPermissionInfo
{
348 // Indicates if the permission implies full access (native code).
349 kFlagImpliesFullAccess
= 1 << 0,
351 // Indicates if the permission implies full URL access.
352 kFlagImpliesFullURLAccess
= 1 << 1,
354 // Indicates that extensions cannot specify the permission as optional.
355 kFlagCannotBeOptional
= 1 << 3,
357 // Indicates that the permission is internal to the extensions
358 // system and cannot be specified in the "permissions" list.
359 kFlagInternal
= 1 << 4,
361 // Indicates that the permission may be granted to web contents by
362 // extensions using the content_capabilities manifest feature.
363 kFlagSupportsContentCapabilities
= 1 << 5,
366 typedef APIPermission
* (*APIPermissionConstructor
)(const APIPermissionInfo
*);
368 typedef std::set
<APIPermission::ID
> IDSet
;
370 ~APIPermissionInfo();
372 // Creates a APIPermission instance.
373 APIPermission
* CreateAPIPermission() const;
375 int flags() const { return flags_
; }
377 APIPermission::ID
id() const { return id_
; }
379 // Returns the message id associated with this permission.
380 PermissionMessage::ID
message_id() const {
384 // Returns the name of this permission.
385 const char* name() const { return name_
; }
387 // Returns true if this permission implies full access (e.g., native code).
388 bool implies_full_access() const {
389 return (flags_
& kFlagImpliesFullAccess
) != 0;
392 // Returns true if this permission implies full URL access.
393 bool implies_full_url_access() const {
394 return (flags_
& kFlagImpliesFullURLAccess
) != 0;
397 // Returns true if this permission can be added and removed via the
398 // optional permissions extension API.
399 bool supports_optional() const {
400 return (flags_
& kFlagCannotBeOptional
) == 0;
403 // Returns true if this permission is internal rather than a
404 // "permissions" list entry.
405 bool is_internal() const {
406 return (flags_
& kFlagInternal
) != 0;
409 // Returns true if this permission can be granted to web contents by an
410 // extension through the content_capabilities manifest feature.
411 bool supports_content_capabilities() const {
412 return (flags_
& kFlagSupportsContentCapabilities
) != 0;
416 // Instances should only be constructed from within a PermissionsProvider.
417 friend class ChromeAPIPermissions
;
418 friend class ExtensionsAPIPermissions
;
419 // Implementations of APIPermission will want to get the permission message,
420 // but this class's implementation should be hidden from everyone else.
421 friend class APIPermission
;
423 // This exists to allow aggregate initialization, so that default values
424 // for flags, etc. can be omitted.
425 // TODO(yoz): Simplify the way initialization is done. APIPermissionInfo
426 // should be the simple data struct.
428 APIPermission::ID id
;
432 PermissionMessage::ID message_id
;
433 APIPermissionInfo::APIPermissionConstructor constructor
;
436 explicit APIPermissionInfo(const InitInfo
& info
);
438 // Returns the localized permission message associated with this api.
439 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
440 PermissionMessage
GetMessage_() const;
442 const APIPermission::ID id_
;
443 const char* const name_
;
445 const int l10n_message_id_
;
446 const PermissionMessage::ID message_id_
;
447 const APIPermissionConstructor api_permission_constructor_
;
450 } // namespace extensions
452 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_