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.
49 kBookmarkManagerPrivate,
50 kBrailleDisplayPrivate,
67 kDeclarativeWebRequest,
77 kEnterprisePlatformKeysPrivate,
81 kFileBrowserHandlerInternal,
86 kFileSystemRetainEntries,
88 kFileSystemWriteDirectory,
106 kMediaGalleriesPrivate,
110 kMusicManagerPrivate,
114 kOverrideEscFullscreen,
152 kVirtualKeyboardPrivate,
155 kWebConnectable, // for externally_connectable manifest key
161 kWebrtcLoggingPrivate,
177 explicit APIPermission(const APIPermissionInfo* info);
179 virtual ~APIPermission();
181 // Returns the id of this permission.
184 // Returns the name of this permission.
185 const char* name() const;
187 // Returns the APIPermission of this permission.
188 const APIPermissionInfo* info() const {
192 // Returns true if this permission has any PermissionMessages.
193 virtual bool HasMessages() const = 0;
195 // Returns the localized permission messages of this permission.
196 virtual PermissionMessages GetMessages() const = 0;
198 // Returns true if the given permission is allowed.
199 virtual bool Check(const CheckParam* param) const = 0;
201 // Returns true if |rhs| is a subset of this.
202 virtual bool Contains(const APIPermission* rhs) const = 0;
204 // Returns true if |rhs| is equal to this.
205 virtual bool Equal(const APIPermission* rhs) const = 0;
207 // Parses the APIPermission from |value|. Returns false if error happens.
208 virtual bool FromValue(const base::Value* value) = 0;
210 // Stores this into a new created |value|.
211 virtual scoped_ptr<base::Value> ToValue() const = 0;
214 virtual APIPermission* Clone() const = 0;
216 // Returns a new API permission which equals this - |rhs|.
217 virtual APIPermission* Diff(const APIPermission* rhs) const = 0;
219 // Returns a new API permission which equals the union of this and |rhs|.
220 virtual APIPermission* Union(const APIPermission* rhs) const = 0;
222 // Returns a new API permission which equals the intersect of this and |rhs|.
223 virtual APIPermission* Intersect(const APIPermission* rhs) const = 0;
226 // Writes this into the given IPC message |m|.
227 virtual void Write(IPC::Message* m) const = 0;
229 // Reads from the given IPC message |m|.
230 virtual bool Read(const IPC::Message* m, PickleIterator* iter) = 0;
232 // Logs this permission.
233 virtual void Log(std::string* log) const = 0;
236 // Returns the localized permission message associated with this api.
237 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
238 PermissionMessage GetMessage_() const;
241 const APIPermissionInfo* const info_;
245 // The APIPermissionInfo is an immutable class that describes a single
246 // named permission (API permission).
247 // There is one instance per permission.
248 class APIPermissionInfo {
253 // Indicates if the permission implies full access (native code).
254 kFlagImpliesFullAccess = 1 << 0,
256 // Indicates if the permission implies full URL access.
257 kFlagImpliesFullURLAccess = 1 << 1,
259 // Indicates that extensions cannot specify the permission as optional.
260 kFlagCannotBeOptional = 1 << 3,
262 // Indicates that the permission is internal to the extensions
263 // system and cannot be specified in the "permissions" list.
264 kFlagInternal = 1 << 4,
267 typedef APIPermission* (*APIPermissionConstructor)(const APIPermissionInfo*);
269 typedef std::set<APIPermission::ID> IDSet;
271 ~APIPermissionInfo();
273 // Creates a APIPermission instance.
274 APIPermission* CreateAPIPermission() const;
276 int flags() const { return flags_; }
278 APIPermission::ID id() const { return id_; }
280 // Returns the message id associated with this permission.
281 PermissionMessage::ID message_id() const {
285 // Returns the name of this permission.
286 const char* name() const { return name_; }
288 // Returns true if this permission implies full access (e.g., native code).
289 bool implies_full_access() const {
290 return (flags_ & kFlagImpliesFullAccess) != 0;
293 // Returns true if this permission implies full URL access.
294 bool implies_full_url_access() const {
295 return (flags_ & kFlagImpliesFullURLAccess) != 0;
298 // Returns true if this permission can be added and removed via the
299 // optional permissions extension API.
300 bool supports_optional() const {
301 return (flags_ & kFlagCannotBeOptional) == 0;
304 // Returns true if this permission is internal rather than a
305 // "permissions" list entry.
306 bool is_internal() const {
307 return (flags_ & kFlagInternal) != 0;
311 // Instances should only be constructed from within a
312 // PermissionsInfo::Delegate.
313 friend class ChromeAPIPermissions;
314 // Implementations of APIPermission will want to get the permission message,
315 // but this class's implementation should be hidden from everyone else.
316 friend class APIPermission;
318 explicit APIPermissionInfo(
319 APIPermission::ID id,
322 PermissionMessage::ID message_id,
324 APIPermissionConstructor api_permission_constructor);
326 // Returns the localized permission message associated with this api.
327 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
328 PermissionMessage GetMessage_() const;
330 const APIPermission::ID id_;
331 const char* const name_;
333 const int l10n_message_id_;
334 const PermissionMessage::ID message_id_;
335 const APIPermissionConstructor api_permission_constructor_;
338 } // namespace extensions
340 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_