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"
21 namespace extensions
{
23 class PermissionIDSet
;
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.
32 // The IDs of all permissions available to apps. Add as many permissions here
33 // as needed to generate meaningful permission messages. Add the rules for the
34 // messages to ChromePermissionMessageProvider.
35 // Do not reorder this enumeration or remove any entries. If you need to add a
36 // new entry, add it just prior to kEnumBoundary, and ensure to update the
37 // "ExtensionPermission3" enum in tools/metrics/histograms/histograms.xml
38 // (by running update_extension_permission.py).
39 // TODO(sashab): Move this to a more central location, and rename it to
46 // Actual permission IDs. Not all of these are valid permissions on their
47 // own; some are just needed by various manifest permissions to represent
48 // their permission message rule combinations.
49 kAccessibilityFeaturesModify
,
50 kAccessibilityFeaturesRead
,
51 kAccessibilityPrivate
,
67 kBookmarkManagerPrivate
,
68 kBrailleDisplayPrivate
,
78 kCommandsAccessibility
,
91 kDeclarativeWebRequest
,
93 kDesktopCapturePrivate
,
104 kEmbeddedExtensionOptions
,
105 kEnterprisePlatformKeys
,
106 kEnterprisePlatformKeysPrivate
,
107 kExperienceSamplingPrivate
,
110 kExternallyConnectableAllUrls
,
113 kFileBrowserHandlerInternal
,
116 kFileSystemDirectory
,
118 kFileSystemRequestFileSystem
,
119 kFileSystemRetainEntries
,
121 kFileSystemWriteDirectory
,
138 kInlineInstallPrivate
,
142 kLauncherSearchProvider
,
151 kMusicManagerPrivate
,
155 kNotificationProvider
,
157 kOverrideEscFullscreen
,
173 kSearchEnginesPrivate
,
198 kVirtualKeyboardPrivate
,
203 kWebConnectable
, // for externally_connectable manifest key
208 kWebrtcDesktopCapturePrivate
,
209 kWebrtcLoggingPrivate
,
211 kWebstoreWidgetPrivate
,
228 kMediaGalleriesAllGalleriesCopyTo
,
229 kMediaGalleriesAllGalleriesDelete
,
230 kMediaGalleriesAllGalleriesRead
,
232 kOverrideBookmarksUI
,
236 kSocketSpecificHosts
,
237 kDeleted_UsbDeviceList
,
238 kUsbDeviceUnknownProduct
,
239 kUsbDeviceUnknownVendor
,
242 kLanguageSettingsPrivate
,
243 kEnterpriseDeviceAttributes
,
244 // Last entry: Add new entries above and ensure to update the
245 // "ExtensionPermission3" enum in tools/metrics/histograms/histograms.xml
246 // (by running update_extension_permission.py).
253 explicit APIPermission(const APIPermissionInfo
* info
);
255 virtual ~APIPermission();
257 // Returns the id of this permission.
260 // Returns the name of this permission.
261 const char* name() const;
263 // Returns the APIPermission of this permission.
264 const APIPermissionInfo
* info() const {
268 // The set of permissions an app/extension with this API permission has. These
269 // permissions are used by PermissionMessageProvider to generate meaningful
270 // permission messages for the app/extension.
272 // For simple API permissions, this will return a set containing only the ID
273 // of the permission. More complex permissions might have multiple IDs, one
274 // for each of the capabilities the API permission has (e.g. read, write and
275 // copy, in the case of the media gallery permission). Permissions that
276 // require parameters may also contain a parameter string (along with the
277 // permission's ID) which can be substituted into the permission message if a
278 // rule is defined to do so.
280 // Permissions with multiple values, such as host permissions, are represented
281 // by multiple entries in this set. Each permission in the subset has the same
282 // ID (e.g. kHostReadOnly) but a different parameter (e.g. google.com). These
283 // are grouped to form different kinds of permission messages (e.g. 'Access to
284 // 2 hosts') depending on the number that are in the set. The rules that
285 // define the grouping of related permissions with the same ID is defined in
286 // ChromePermissionMessageProvider.
287 virtual PermissionIDSet
GetPermissions() const = 0;
289 // Returns true if the given permission is allowed.
290 virtual bool Check(const CheckParam
* param
) const = 0;
292 // Returns true if |rhs| is a subset of this.
293 virtual bool Contains(const APIPermission
* rhs
) const = 0;
295 // Returns true if |rhs| is equal to this.
296 virtual bool Equal(const APIPermission
* rhs
) const = 0;
298 // Parses the APIPermission from |value|. Returns false if an error happens
299 // and optionally set |error| if |error| is not NULL. If |value| represents
300 // multiple permissions, some are invalid, and |unhandled_permissions| is
301 // not NULL, the invalid ones are put into |unhandled_permissions| and the
302 // function returns true.
303 virtual bool FromValue(const base::Value
* value
,
305 std::vector
<std::string
>* unhandled_permissions
) = 0;
307 // Stores this into a new created |value|.
308 virtual scoped_ptr
<base::Value
> ToValue() const = 0;
311 virtual APIPermission
* Clone() const = 0;
313 // Returns a new API permission which equals this - |rhs|.
314 virtual APIPermission
* Diff(const APIPermission
* rhs
) const = 0;
316 // Returns a new API permission which equals the union of this and |rhs|.
317 virtual APIPermission
* Union(const APIPermission
* rhs
) const = 0;
319 // Returns a new API permission which equals the intersect of this and |rhs|.
320 virtual APIPermission
* Intersect(const APIPermission
* rhs
) const = 0;
323 // Writes this into the given IPC message |m|.
324 virtual void Write(IPC::Message
* m
) const = 0;
326 // Reads from the given IPC message |m|.
327 virtual bool Read(const IPC::Message
* m
, base::PickleIterator
* iter
) = 0;
329 // Logs this permission.
330 virtual void Log(std::string
* log
) const = 0;
333 const APIPermissionInfo
* const info_
;
337 // The APIPermissionInfo is an immutable class that describes a single
338 // named permission (API permission).
339 // There is one instance per permission.
340 class APIPermissionInfo
{
345 // Indicates if the permission implies full access (native code).
346 kFlagImpliesFullAccess
= 1 << 0,
348 // Indicates if the permission implies full URL access.
349 kFlagImpliesFullURLAccess
= 1 << 1,
351 // Indicates that extensions cannot specify the permission as optional.
352 kFlagCannotBeOptional
= 1 << 3,
354 // Indicates that the permission is internal to the extensions
355 // system and cannot be specified in the "permissions" list.
356 kFlagInternal
= 1 << 4,
358 // Indicates that the permission may be granted to web contents by
359 // extensions using the content_capabilities manifest feature.
360 kFlagSupportsContentCapabilities
= 1 << 5,
363 typedef APIPermission
* (*APIPermissionConstructor
)(const APIPermissionInfo
*);
365 typedef std::set
<APIPermission::ID
> IDSet
;
367 ~APIPermissionInfo();
369 // Creates a APIPermission instance.
370 APIPermission
* CreateAPIPermission() const;
372 int flags() const { return flags_
; }
374 APIPermission::ID
id() const { return id_
; }
376 // Returns the name of this permission.
377 const char* name() const { return name_
; }
379 // Returns true if this permission implies full access (e.g., native code).
380 bool implies_full_access() const {
381 return (flags_
& kFlagImpliesFullAccess
) != 0;
384 // Returns true if this permission implies full URL access.
385 bool implies_full_url_access() const {
386 return (flags_
& kFlagImpliesFullURLAccess
) != 0;
389 // Returns true if this permission can be added and removed via the
390 // optional permissions extension API.
391 bool supports_optional() const {
392 return (flags_
& kFlagCannotBeOptional
) == 0;
395 // Returns true if this permission is internal rather than a
396 // "permissions" list entry.
397 bool is_internal() const {
398 return (flags_
& kFlagInternal
) != 0;
401 // Returns true if this permission can be granted to web contents by an
402 // extension through the content_capabilities manifest feature.
403 bool supports_content_capabilities() const {
404 return (flags_
& kFlagSupportsContentCapabilities
) != 0;
408 // Instances should only be constructed from within a PermissionsProvider.
409 friend class ChromeAPIPermissions
;
410 friend class ExtensionsAPIPermissions
;
411 // Implementations of APIPermission will want to get the permission message,
412 // but this class's implementation should be hidden from everyone else.
413 friend class APIPermission
;
415 // This exists to allow aggregate initialization, so that default values
416 // for flags, etc. can be omitted.
417 // TODO(yoz): Simplify the way initialization is done. APIPermissionInfo
418 // should be the simple data struct.
420 APIPermission::ID id
;
423 APIPermissionInfo::APIPermissionConstructor constructor
;
426 explicit APIPermissionInfo(const InitInfo
& info
);
428 const APIPermission::ID id_
;
429 const char* const name_
;
431 const APIPermissionConstructor api_permission_constructor_
;
434 } // namespace extensions
436 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_