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.
48 kBookmarkManagerPrivate
,
49 kBrailleDisplayPrivate
,
66 kDeclarativeWebRequest
,
76 kEnterprisePlatformKeysPrivate
,
80 kFileBrowserHandlerInternal
,
85 kFileSystemRetainEntries
,
87 kFileSystemWriteDirectory
,
107 kMediaGalleriesPrivate
,
111 kMusicManagerPrivate
,
115 kOverrideEscFullscreen
,
153 kVirtualKeyboardPrivate
,
156 kWebConnectable
, // for externally_connectable manifest key
162 kWebrtcLoggingPrivate
,
178 explicit APIPermission(const APIPermissionInfo
* info
);
180 virtual ~APIPermission();
182 // Returns the id of this permission.
185 // Returns the name of this permission.
186 const char* name() const;
188 // Returns the APIPermission of this permission.
189 const APIPermissionInfo
* info() const {
193 // Returns true if this permission has any PermissionMessages.
194 virtual bool HasMessages() const = 0;
196 // Returns the localized permission messages of this permission.
197 virtual PermissionMessages
GetMessages() const = 0;
199 // Returns true if the given permission is allowed.
200 virtual bool Check(const CheckParam
* param
) const = 0;
202 // Returns true if |rhs| is a subset of this.
203 virtual bool Contains(const APIPermission
* rhs
) const = 0;
205 // Returns true if |rhs| is equal to this.
206 virtual bool Equal(const APIPermission
* rhs
) const = 0;
208 // Parses the APIPermission from |value|. Returns false if an error happens
209 // and optionally set |error| if |error| is not NULL.
210 virtual bool FromValue(const base::Value
* value
, std::string
* error
) = 0;
212 // Stores this into a new created |value|.
213 virtual scoped_ptr
<base::Value
> ToValue() const = 0;
216 virtual APIPermission
* Clone() const = 0;
218 // Returns a new API permission which equals this - |rhs|.
219 virtual APIPermission
* Diff(const APIPermission
* rhs
) const = 0;
221 // Returns a new API permission which equals the union of this and |rhs|.
222 virtual APIPermission
* Union(const APIPermission
* rhs
) const = 0;
224 // Returns a new API permission which equals the intersect of this and |rhs|.
225 virtual APIPermission
* Intersect(const APIPermission
* rhs
) const = 0;
228 // Writes this into the given IPC message |m|.
229 virtual void Write(IPC::Message
* m
) const = 0;
231 // Reads from the given IPC message |m|.
232 virtual bool Read(const IPC::Message
* m
, PickleIterator
* iter
) = 0;
234 // Logs this permission.
235 virtual void Log(std::string
* log
) const = 0;
238 // Returns the localized permission message associated with this api.
239 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
240 PermissionMessage
GetMessage_() const;
243 const APIPermissionInfo
* const info_
;
247 // The APIPermissionInfo is an immutable class that describes a single
248 // named permission (API permission).
249 // There is one instance per permission.
250 class APIPermissionInfo
{
255 // Indicates if the permission implies full access (native code).
256 kFlagImpliesFullAccess
= 1 << 0,
258 // Indicates if the permission implies full URL access.
259 kFlagImpliesFullURLAccess
= 1 << 1,
261 // Indicates that extensions cannot specify the permission as optional.
262 kFlagCannotBeOptional
= 1 << 3,
264 // Indicates that the permission is internal to the extensions
265 // system and cannot be specified in the "permissions" list.
266 kFlagInternal
= 1 << 4,
269 typedef APIPermission
* (*APIPermissionConstructor
)(const APIPermissionInfo
*);
271 typedef std::set
<APIPermission::ID
> IDSet
;
273 ~APIPermissionInfo();
275 // Creates a APIPermission instance.
276 APIPermission
* CreateAPIPermission() const;
278 int flags() const { return flags_
; }
280 APIPermission::ID
id() const { return id_
; }
282 // Returns the message id associated with this permission.
283 PermissionMessage::ID
message_id() const {
287 // Returns the name of this permission.
288 const char* name() const { return name_
; }
290 // Returns true if this permission implies full access (e.g., native code).
291 bool implies_full_access() const {
292 return (flags_
& kFlagImpliesFullAccess
) != 0;
295 // Returns true if this permission implies full URL access.
296 bool implies_full_url_access() const {
297 return (flags_
& kFlagImpliesFullURLAccess
) != 0;
300 // Returns true if this permission can be added and removed via the
301 // optional permissions extension API.
302 bool supports_optional() const {
303 return (flags_
& kFlagCannotBeOptional
) == 0;
306 // Returns true if this permission is internal rather than a
307 // "permissions" list entry.
308 bool is_internal() const {
309 return (flags_
& kFlagInternal
) != 0;
313 // Instances should only be constructed from within a PermissionsProvider.
314 friend class ChromeAPIPermissions
;
315 // Implementations of APIPermission will want to get the permission message,
316 // but this class's implementation should be hidden from everyone else.
317 friend class APIPermission
;
319 explicit APIPermissionInfo(
320 APIPermission::ID id
,
323 PermissionMessage::ID message_id
,
325 APIPermissionConstructor api_permission_constructor
);
327 // Returns the localized permission message associated with this api.
328 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
329 PermissionMessage
GetMessage_() const;
331 const APIPermission::ID id_
;
332 const char* const name_
;
334 const int l10n_message_id_
;
335 const PermissionMessage::ID message_id_
;
336 const APIPermissionConstructor api_permission_constructor_
;
339 } // namespace extensions
341 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_