Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / permissions / api_permission.h
blob07d0631159b3387a5017838aafa534681072c45a
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_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/callback.h"
13 #include "base/pickle.h"
14 #include "chrome/common/extensions/permissions/permission_message.h"
16 namespace base {
17 class Value;
20 namespace IPC {
21 class Message;
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.
32 class APIPermission {
33 public:
34 enum ID {
35 // Error codes.
36 kInvalid = -2,
37 kUnknown = -1,
39 // Real permissions.
40 kActiveTab,
41 kAlarms,
42 kAppCurrentWindowInternal,
43 kAppNotifications,
44 kAppRuntime,
45 kAppWindow,
46 kAudioCapture,
47 kBackground,
48 kBluetooth,
49 kBookmark,
50 kBookmarkManagerPrivate,
51 kBrowsingData,
52 kChromeosInfoPrivate,
53 kClipboardRead,
54 kClipboardWrite,
55 kCloudPrintPrivate,
56 kContentSettings,
57 kContextMenus,
58 kCookie,
59 kDebugger,
60 kDeclarative,
61 kDeclarativeWebRequest,
62 kDevtools,
63 kDownloads,
64 kEchoPrivate,
65 kExperimental,
66 kFileBrowserHandler,
67 kFileBrowserHandlerInternal,
68 kFileBrowserPrivate,
69 kFileSystem,
70 kFileSystemWrite,
71 kFontSettings,
72 kGeolocation,
73 kHistory,
74 kIdle,
75 kInput,
76 kInputMethodPrivate,
77 kManagedModePrivate,
78 kManagement,
79 kMediaGalleries,
80 kMediaGalleriesRead,
81 kMediaGalleriesAllAutoDetected,
82 kMediaGalleriesPrivate,
83 kMediaPlayerPrivate,
84 kMetricsPrivate,
85 kNotification,
86 kPageCapture,
87 kPlugin,
88 kPrivacy,
89 kProxy,
90 kPushMessaging,
91 kRtcPrivate,
92 kSerial,
93 kSocket,
94 kStorage,
95 kSyncFileSystem,
96 kSystemPrivate,
97 kTab,
98 kTabCapture,
99 kTerminalPrivate,
100 kTopSites,
101 kTts,
102 kTtsEngine,
103 kUnlimitedStorage,
104 kUsb,
105 kVideoCapture,
106 kWallpaperPrivate,
107 kWebNavigation,
108 kWebRequest,
109 kWebRequestBlocking,
110 kWebRequestInternal,
111 kWebSocketProxyPrivate,
112 kWebstorePrivate,
113 kWebView,
114 kEnumBoundary
117 struct CheckParam {
120 explicit APIPermission(const APIPermissionInfo* info);
122 virtual ~APIPermission();
124 // Returns the id of this permission.
125 ID id() const;
127 // Returns the name of this permission.
128 const char* name() const;
130 // Returns the APIPermission of this permission.
131 const APIPermissionInfo* info() const {
132 return info_;
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;
156 // Clones this.
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;
168 // IPC functions
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;
178 protected:
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;
183 private:
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 {
192 public:
193 enum Flag {
194 kFlagNone = 0,
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 {
221 return message_id_;
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;
243 private:
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,
252 const char* name,
253 int l10n_message_id,
254 PermissionMessage::ID message_id,
255 int flags,
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_;
267 const int flags_;
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_