1 // Copyright (c) 2012 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 CHROME_COMMON_EXTENSIONS_PERMISSIONS_API_PERMISSION_H_
6 #define CHROME_COMMON_EXTENSIONS_PERMISSIONS_API_PERMISSION_H_
12 #include "base/callback.h"
13 #include "base/pickle.h"
14 #include "chrome/common/extensions/permissions/permission_message.h"
24 namespace extensions
{
26 class APIPermissionInfo
;
27 class PermissionsInfo
;
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.
42 kAppCurrentWindowInternal
,
50 kBookmarkManagerPrivate
,
61 kDeclarativeWebRequest
,
67 kFileBrowserHandlerInternal
,
81 kMediaGalleriesAllAutoDetected
,
82 kMediaGalleriesPrivate
,
111 kWebSocketProxyPrivate
,
120 explicit APIPermission(const APIPermissionInfo
* info
);
122 virtual ~APIPermission();
124 // Returns the id of this permission.
127 // Returns the name of this permission.
128 const char* name() const;
130 // Returns the APIPermission of this permission.
131 const APIPermissionInfo
* info() const {
135 // Returns true if this permission has any PermissionMessages.
136 virtual bool HasMessages() const = 0;
138 // Returns the localized permission messages of this permission.
139 virtual PermissionMessages
GetMessages() const = 0;
141 // Returns true if the given permission is allowed.
142 virtual bool Check(const CheckParam
* param
) const = 0;
144 // Returns true if |rhs| is a subset of this.
145 virtual bool Contains(const APIPermission
* rhs
) const = 0;
147 // Returns true if |rhs| is equal to this.
148 virtual bool Equal(const APIPermission
* rhs
) const = 0;
150 // Parses the APIPermission from |value|. Returns false if error happens.
151 virtual bool FromValue(const base::Value
* value
) = 0;
153 // Stores this into a new created |value|.
154 virtual void ToValue(base::Value
** value
) const = 0;
157 virtual APIPermission
* Clone() const = 0;
159 // Returns a new API permission which equals this - |rhs|.
160 virtual APIPermission
* Diff(const APIPermission
* rhs
) const = 0;
162 // Returns a new API permission which equals the union of this and |rhs|.
163 virtual APIPermission
* Union(const APIPermission
* rhs
) const = 0;
165 // Returns a new API permission which equals the intersect of this and |rhs|.
166 virtual APIPermission
* Intersect(const APIPermission
* rhs
) const = 0;
169 // Writes this into the given IPC message |m|.
170 virtual void Write(IPC::Message
* m
) const = 0;
172 // Reads from the given IPC message |m|.
173 virtual bool Read(const IPC::Message
* m
, PickleIterator
* iter
) = 0;
175 // Logs this permission.
176 virtual void Log(std::string
* log
) const = 0;
179 // Returns the localized permission message associated with this api.
180 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
181 PermissionMessage
GetMessage_() const;
184 const APIPermissionInfo
* const info_
;
188 // The APIPermissionInfo is an immutable class that describes a single
189 // named permission (API permission).
190 // There is one instance per permission.
191 class APIPermissionInfo
{
196 // Indicates if the permission implies full access (native code).
197 kFlagImpliesFullAccess
= 1 << 0,
199 // Indicates if the permission implies full URL access.
200 kFlagImpliesFullURLAccess
= 1 << 1,
202 // Indicates that extensions cannot specify the permission as optional.
203 kFlagCannotBeOptional
= 1 << 3
206 typedef APIPermission
* (*APIPermissionConstructor
)(const APIPermissionInfo
*);
208 typedef std::set
<APIPermission::ID
> IDSet
;
210 ~APIPermissionInfo();
212 // Creates a APIPermission instance.
213 APIPermission
* CreateAPIPermission() const;
215 int flags() const { return flags_
; }
217 APIPermission::ID
id() const { return id_
; }
219 // Returns the message id associated with this permission.
220 PermissionMessage::ID
message_id() const {
224 // Returns the name of this permission.
225 const char* name() const { return name_
; }
227 // Returns true if this permission implies full access (e.g., native code).
228 bool implies_full_access() const {
229 return (flags_
& kFlagImpliesFullAccess
) != 0;
232 // Returns true if this permission implies full URL access.
233 bool implies_full_url_access() const {
234 return (flags_
& kFlagImpliesFullURLAccess
) != 0;
237 // Returns true if this permission can be added and removed via the
238 // optional permissions extension API.
239 bool supports_optional() const {
240 return (flags_
& kFlagCannotBeOptional
) == 0;
244 // Instances should only be constructed from within PermissionsInfo.
245 friend class PermissionsInfo
;
246 // Implementations of APIPermission will want to get the permission message,
247 // but this class's implementation should be hidden from everyone else.
248 friend class APIPermission
;
250 explicit APIPermissionInfo(
251 APIPermission::ID id
,
254 PermissionMessage::ID message_id
,
256 APIPermissionConstructor api_permission_constructor
);
258 // Register ALL the permissions!
259 static void RegisterAllPermissions(PermissionsInfo
* info
);
261 // Returns the localized permission message associated with this api.
262 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
263 PermissionMessage
GetMessage_() const;
265 const APIPermission::ID id_
;
266 const char* const name_
;
268 const int l10n_message_id_
;
269 const PermissionMessage::ID message_id_
;
270 const APIPermissionConstructor api_permission_constructor_
;
273 } // namespace extensions
275 #endif // CHROME_COMMON_EXTENSIONS_PERMISSIONS_API_PERMISSION_H_