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 kFileSystemRequestFileSystem
,
116 kFileSystemRetainEntries
,
118 kFileSystemWriteDirectory
,
135 kInlineInstallPrivate
,
139 kLauncherSearchProvider
,
148 kMusicManagerPrivate
,
152 kNotificationProvider
,
154 kOverrideEscFullscreen
,
194 kVirtualKeyboardPrivate
,
199 kWebConnectable
, // for externally_connectable manifest key
204 kWebrtcLoggingPrivate
,
215 // Permission message IDs that are not currently valid permissions on their
216 // own, but are needed by various manifest permissions to represent their
217 // permission message rule combinations.
218 // TODO(sashab): Move these in-line with the other permission IDs.
227 kMediaGalleriesAllGalleriesCopyTo
,
228 kMediaGalleriesAllGalleriesDelete
,
229 kMediaGalleriesAllGalleriesRead
,
231 kOverrideBookmarksUI
,
235 kSocketSpecificHosts
,
237 kUsbDeviceUnknownProduct
,
238 kUsbDeviceUnknownVendor
,
246 explicit APIPermission(const APIPermissionInfo
* info
);
248 virtual ~APIPermission();
250 // Returns the id of this permission.
253 // Returns the name of this permission.
254 const char* name() const;
256 // Returns the APIPermission of this permission.
257 const APIPermissionInfo
* info() const {
261 // The set of permissions an app/extension with this API permission has. These
262 // permissions are used by PermissionMessageProvider to generate meaningful
263 // permission messages for the app/extension.
265 // For simple API permissions, this will return a set containing only the ID
266 // of the permission. More complex permissions might have multiple IDs, one
267 // for each of the capabilities the API permission has (e.g. read, write and
268 // copy, in the case of the media gallery permission). Permissions that
269 // require parameters may also contain a parameter string (along with the
270 // permission's ID) which can be substituted into the permission message if a
271 // rule is defined to do so.
273 // Permissions with multiple values, such as host permissions, are represented
274 // by multiple entries in this set. Each permission in the subset has the same
275 // ID (e.g. kHostReadOnly) but a different parameter (e.g. google.com). These
276 // are grouped to form different kinds of permission messages (e.g. 'Access to
277 // 2 hosts') depending on the number that are in the set. The rules that
278 // define the grouping of related permissions with the same ID is defined in
279 // ChromePermissionMessageProvider.
280 virtual PermissionIDSet
GetPermissions() const = 0;
282 // Returns true if this permission has any PermissionMessages.
283 // TODO(sashab): Deprecate this in favor of GetPermissions() above.
284 virtual bool HasMessages() const = 0;
286 // Returns the localized permission messages of this permission.
287 // TODO(sashab): Deprecate this in favor of GetPermissions() above.
288 virtual PermissionMessages
GetMessages() const = 0;
290 // Returns true if the given permission is allowed.
291 virtual bool Check(const CheckParam
* param
) const = 0;
293 // Returns true if |rhs| is a subset of this.
294 virtual bool Contains(const APIPermission
* rhs
) const = 0;
296 // Returns true if |rhs| is equal to this.
297 virtual bool Equal(const APIPermission
* rhs
) const = 0;
299 // Parses the APIPermission from |value|. Returns false if an error happens
300 // and optionally set |error| if |error| is not NULL. If |value| represents
301 // multiple permissions, some are invalid, and |unhandled_permissions| is
302 // not NULL, the invalid ones are put into |unhandled_permissions| and the
303 // function returns true.
304 virtual bool FromValue(const base::Value
* value
,
306 std::vector
<std::string
>* unhandled_permissions
) = 0;
308 // Stores this into a new created |value|.
309 virtual scoped_ptr
<base::Value
> ToValue() const = 0;
312 virtual APIPermission
* Clone() const = 0;
314 // Returns a new API permission which equals this - |rhs|.
315 virtual APIPermission
* Diff(const APIPermission
* rhs
) const = 0;
317 // Returns a new API permission which equals the union of this and |rhs|.
318 virtual APIPermission
* Union(const APIPermission
* rhs
) const = 0;
320 // Returns a new API permission which equals the intersect of this and |rhs|.
321 virtual APIPermission
* Intersect(const APIPermission
* rhs
) const = 0;
324 // Writes this into the given IPC message |m|.
325 virtual void Write(IPC::Message
* m
) const = 0;
327 // Reads from the given IPC message |m|.
328 virtual bool Read(const IPC::Message
* m
, PickleIterator
* iter
) = 0;
330 // Logs this permission.
331 virtual void Log(std::string
* log
) const = 0;
334 // Returns the localized permission message associated with this api.
335 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
336 PermissionMessage
GetMessage_() const;
339 const APIPermissionInfo
* const info_
;
343 // The APIPermissionInfo is an immutable class that describes a single
344 // named permission (API permission).
345 // There is one instance per permission.
346 class APIPermissionInfo
{
351 // Indicates if the permission implies full access (native code).
352 kFlagImpliesFullAccess
= 1 << 0,
354 // Indicates if the permission implies full URL access.
355 kFlagImpliesFullURLAccess
= 1 << 1,
357 // Indicates that extensions cannot specify the permission as optional.
358 kFlagCannotBeOptional
= 1 << 3,
360 // Indicates that the permission is internal to the extensions
361 // system and cannot be specified in the "permissions" list.
362 kFlagInternal
= 1 << 4,
364 // Indicates that the permission may be granted to web contents by
365 // extensions using the content_capabilities manifest feature.
366 kFlagSupportsContentCapabilities
= 1 << 5,
369 typedef APIPermission
* (*APIPermissionConstructor
)(const APIPermissionInfo
*);
371 typedef std::set
<APIPermission::ID
> IDSet
;
373 ~APIPermissionInfo();
375 // Creates a APIPermission instance.
376 APIPermission
* CreateAPIPermission() const;
378 int flags() const { return flags_
; }
380 APIPermission::ID
id() const { return id_
; }
382 // Returns the message id associated with this permission.
383 PermissionMessage::ID
message_id() const {
387 // Returns the name of this permission.
388 const char* name() const { return name_
; }
390 // Returns true if this permission implies full access (e.g., native code).
391 bool implies_full_access() const {
392 return (flags_
& kFlagImpliesFullAccess
) != 0;
395 // Returns true if this permission implies full URL access.
396 bool implies_full_url_access() const {
397 return (flags_
& kFlagImpliesFullURLAccess
) != 0;
400 // Returns true if this permission can be added and removed via the
401 // optional permissions extension API.
402 bool supports_optional() const {
403 return (flags_
& kFlagCannotBeOptional
) == 0;
406 // Returns true if this permission is internal rather than a
407 // "permissions" list entry.
408 bool is_internal() const {
409 return (flags_
& kFlagInternal
) != 0;
412 // Returns true if this permission can be granted to web contents by an
413 // extension through the content_capabilities manifest feature.
414 bool supports_content_capabilities() const {
415 return (flags_
& kFlagSupportsContentCapabilities
) != 0;
419 // Instances should only be constructed from within a PermissionsProvider.
420 friend class ChromeAPIPermissions
;
421 friend class ExtensionsAPIPermissions
;
422 // Implementations of APIPermission will want to get the permission message,
423 // but this class's implementation should be hidden from everyone else.
424 friend class APIPermission
;
426 // This exists to allow aggregate initialization, so that default values
427 // for flags, etc. can be omitted.
428 // TODO(yoz): Simplify the way initialization is done. APIPermissionInfo
429 // should be the simple data struct.
431 APIPermission::ID id
;
435 PermissionMessage::ID message_id
;
436 APIPermissionInfo::APIPermissionConstructor constructor
;
439 explicit APIPermissionInfo(const InitInfo
& info
);
441 // Returns the localized permission message associated with this api.
442 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
443 PermissionMessage
GetMessage_() const;
445 const APIPermission::ID id_
;
446 const char* const name_
;
448 const int l10n_message_id_
;
449 const PermissionMessage::ID message_id_
;
450 const APIPermissionConstructor api_permission_constructor_
;
453 } // namespace extensions
455 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_