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
,
53 kBookmarkManagerPrivate
,
54 kBrailleDisplayPrivate
,
71 kDeclarativeWebRequest
,
81 kEnterprisePlatformKeys
,
82 kEnterprisePlatformKeysPrivate
,
86 kFileBrowserHandlerInternal
,
91 kFileSystemRetainEntries
,
93 kFileSystemWriteDirectory
,
114 kMediaGalleriesPrivate
,
118 kMusicManagerPrivate
,
122 kOverrideEscFullscreen
,
145 kSyncedNotificationsPrivate
,
160 kVirtualKeyboardPrivate
,
164 kWebConnectable
, // for externally_connectable manifest key
169 kWebrtcLoggingPrivate
,
187 explicit APIPermission(const APIPermissionInfo
* info
);
189 virtual ~APIPermission();
191 // Returns the id of this permission.
194 // Returns the name of this permission.
195 const char* name() const;
197 // Returns the APIPermission of this permission.
198 const APIPermissionInfo
* info() const {
202 // Returns true if this permission has any PermissionMessages.
203 virtual bool HasMessages() const = 0;
205 // Returns the localized permission messages of this permission.
206 virtual PermissionMessages
GetMessages() const = 0;
208 // Returns true if the given permission is allowed.
209 virtual bool Check(const CheckParam
* param
) const = 0;
211 // Returns true if |rhs| is a subset of this.
212 virtual bool Contains(const APIPermission
* rhs
) const = 0;
214 // Returns true if |rhs| is equal to this.
215 virtual bool Equal(const APIPermission
* rhs
) const = 0;
217 // Parses the APIPermission from |value|. Returns false if an error happens
218 // and optionally set |error| if |error| is not NULL. If |value| represents
219 // multiple permissions, some are invalid, and |unhandled_permissions| is
220 // not NULL, the invalid ones are put into |unhandled_permissions| and the
221 // function returns true.
222 virtual bool FromValue(const base::Value
* value
,
224 std::vector
<std::string
>* unhandled_permissions
) = 0;
226 // Stores this into a new created |value|.
227 virtual scoped_ptr
<base::Value
> ToValue() const = 0;
230 virtual APIPermission
* Clone() const = 0;
232 // Returns a new API permission which equals this - |rhs|.
233 virtual APIPermission
* Diff(const APIPermission
* rhs
) const = 0;
235 // Returns a new API permission which equals the union of this and |rhs|.
236 virtual APIPermission
* Union(const APIPermission
* rhs
) const = 0;
238 // Returns a new API permission which equals the intersect of this and |rhs|.
239 virtual APIPermission
* Intersect(const APIPermission
* rhs
) const = 0;
242 // Writes this into the given IPC message |m|.
243 virtual void Write(IPC::Message
* m
) const = 0;
245 // Reads from the given IPC message |m|.
246 virtual bool Read(const IPC::Message
* m
, PickleIterator
* iter
) = 0;
248 // Logs this permission.
249 virtual void Log(std::string
* log
) const = 0;
252 // Returns the localized permission message associated with this api.
253 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
254 PermissionMessage
GetMessage_() const;
257 const APIPermissionInfo
* const info_
;
261 // The APIPermissionInfo is an immutable class that describes a single
262 // named permission (API permission).
263 // There is one instance per permission.
264 class APIPermissionInfo
{
269 // Indicates if the permission implies full access (native code).
270 kFlagImpliesFullAccess
= 1 << 0,
272 // Indicates if the permission implies full URL access.
273 kFlagImpliesFullURLAccess
= 1 << 1,
275 // Indicates that extensions cannot specify the permission as optional.
276 kFlagCannotBeOptional
= 1 << 3,
278 // Indicates that the permission is internal to the extensions
279 // system and cannot be specified in the "permissions" list.
280 kFlagInternal
= 1 << 4,
283 typedef APIPermission
* (*APIPermissionConstructor
)(const APIPermissionInfo
*);
285 typedef std::set
<APIPermission::ID
> IDSet
;
287 ~APIPermissionInfo();
289 // Creates a APIPermission instance.
290 APIPermission
* CreateAPIPermission() const;
292 int flags() const { return flags_
; }
294 APIPermission::ID
id() const { return id_
; }
296 // Returns the message id associated with this permission.
297 PermissionMessage::ID
message_id() const {
301 // Returns the name of this permission.
302 const char* name() const { return name_
; }
304 // Returns true if this permission implies full access (e.g., native code).
305 bool implies_full_access() const {
306 return (flags_
& kFlagImpliesFullAccess
) != 0;
309 // Returns true if this permission implies full URL access.
310 bool implies_full_url_access() const {
311 return (flags_
& kFlagImpliesFullURLAccess
) != 0;
314 // Returns true if this permission can be added and removed via the
315 // optional permissions extension API.
316 bool supports_optional() const {
317 return (flags_
& kFlagCannotBeOptional
) == 0;
320 // Returns true if this permission is internal rather than a
321 // "permissions" list entry.
322 bool is_internal() const {
323 return (flags_
& kFlagInternal
) != 0;
327 // Instances should only be constructed from within a PermissionsProvider.
328 friend class ChromeAPIPermissions
;
329 friend class ExtensionsAPIPermissions
;
330 // Implementations of APIPermission will want to get the permission message,
331 // but this class's implementation should be hidden from everyone else.
332 friend class APIPermission
;
334 // This exists to allow aggregate initialization, so that default values
335 // for flags, etc. can be omitted.
336 // TODO(yoz): Simplify the way initialization is done. APIPermissionInfo
337 // should be the simple data struct.
339 APIPermission::ID id
;
343 PermissionMessage::ID message_id
;
344 APIPermissionInfo::APIPermissionConstructor constructor
;
347 explicit APIPermissionInfo(const InitInfo
& info
);
349 // Returns the localized permission message associated with this api.
350 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
351 PermissionMessage
GetMessage_() const;
353 const APIPermission::ID id_
;
354 const char* const name_
;
356 const int l10n_message_id_
;
357 const PermissionMessage::ID message_id_
;
358 const APIPermissionConstructor api_permission_constructor_
;
361 } // namespace extensions
363 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_