blob: 980be54e7b36da8c4486bf29c2b7fe92a5b37b97 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#ifndef CHROME_COMMON_EXTENSIONS_PERMISSIONS_API_PERMISSION_H_
6#define CHROME_COMMON_EXTENSIONS_PERMISSIONS_API_PERMISSION_H_
7
8#include <map>
9#include <set>
10#include <string>
11
12#include "base/callback.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000013#include "base/memory/scoped_ptr.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "base/pickle.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010015#include "base/values.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "chrome/common/extensions/permissions/permission_message.h"
17
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018namespace IPC {
19class Message;
20}
21
22namespace extensions {
23
24class APIPermissionInfo;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010025class ChromeAPIPermissions;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000026
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.
30class APIPermission {
31 public:
32 enum ID {
33 // Error codes.
34 kInvalid = -2,
35 kUnknown = -1,
36
37 // Real permissions.
38 kActiveTab,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010039 kActivityLogPrivate,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000040 kAdView,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041 kAlarms,
42 kAppCurrentWindowInternal,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000043 kAppRuntime,
44 kAppWindow,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010045 kAudio,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000046 kAudioCapture,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000047 kAutoTestPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000048 kBackground,
49 kBluetooth,
50 kBookmark,
51 kBookmarkManagerPrivate,
52 kBrowsingData,
53 kChromeosInfoPrivate,
54 kClipboardRead,
55 kClipboardWrite,
56 kCloudPrintPrivate,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010057 kCommandLinePrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000058 kContentSettings,
59 kContextMenus,
60 kCookie,
Ben Murdocheb525c52013-07-10 11:40:50 +010061 kDiagnostics,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000062 kDial,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000063 kDebugger,
64 kDeclarative,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000065 kDeclarativeContent,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000066 kDeclarativeWebRequest,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000067 kDeveloperPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068 kDevtools,
69 kDownloads,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000070 kDownloadsInternal,
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010071 kDownloadsOpen,
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010072 kDownloadsShelf,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000073 kEchoPrivate,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010074 kEnterprisePlatformKeysPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000075 kExperimental,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010076 kFeedbackPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000077 kFileBrowserHandler,
78 kFileBrowserHandlerInternal,
79 kFileBrowserPrivate,
80 kFileSystem,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010081 kFileSystemRetainEntries,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000082 kFileSystemWrite,
83 kFontSettings,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000084 kFullscreen,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000085 kGeolocation,
86 kHistory,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010087 kIdentity,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010088 kIdentityPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000089 kIdle,
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010090 kInfobars,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000091 kInput,
92 kInputMethodPrivate,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010093 kLocation,
Ben Murdochbb1529c2013-08-08 10:24:53 +010094 kLogPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000095 kManagement,
96 kMediaGalleries,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000097 kMediaGalleriesPrivate,
98 kMediaPlayerPrivate,
99 kMetricsPrivate,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100100 kMusicManagerPrivate,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000101 kNativeMessaging,
102 kNetworkingPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000103 kNotification,
104 kPageCapture,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000105 kPointerLock,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000106 kPlugin,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000107 kPower,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100108 kPreferencesPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000109 kPrivacy,
110 kProxy,
111 kPushMessaging,
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100112 kRecoveryPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000113 kRtcPrivate,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000114 kScreensaver,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000115 kSerial,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000116 kSessionRestore,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000117 kSocket,
118 kStorage,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000119 kStreamsPrivate,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000120 kSyncFileSystem,
121 kSystemPrivate,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000122 kSystemIndicator,
Ben Murdochca12bfa2013-07-23 11:17:05 +0100123 kSystemDisplay,
Ben Murdoch558790d2013-07-30 15:19:42 +0100124 kSystemStorage,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000125 kTab,
126 kTabCapture,
127 kTerminalPrivate,
128 kTopSites,
129 kTts,
130 kTtsEngine,
131 kUnlimitedStorage,
132 kUsb,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000133 kUsbDevice,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000134 kVideoCapture,
135 kWallpaperPrivate,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100136 kWebConnectable, // for externally_connectable manifest key
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000137 kWebNavigation,
138 kWebRequest,
139 kWebRequestBlocking,
140 kWebRequestInternal,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000141 kWebstorePrivate,
142 kWebView,
Ben Murdoch9ab55632013-07-18 11:57:30 +0100143 kSystemCpu,
Ben Murdocha3f7b4e2013-07-24 10:36:34 +0100144 kSystemMemory,
Ben Murdochbb1529c2013-08-08 10:24:53 +0100145 kSystemInfoCpu,
146 kSystemInfoMemory,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000147 kEnumBoundary
148 };
149
150 struct CheckParam {
151 };
152
153 explicit APIPermission(const APIPermissionInfo* info);
154
155 virtual ~APIPermission();
156
157 // Returns the id of this permission.
158 ID id() const;
159
160 // Returns the name of this permission.
161 const char* name() const;
162
163 // Returns the APIPermission of this permission.
164 const APIPermissionInfo* info() const {
165 return info_;
166 }
167
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000168 // Returns true if this permission cannot be found in the manifest.
169 virtual bool ManifestEntryForbidden() const;
170
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000171 // Returns true if this permission has any PermissionMessages.
172 virtual bool HasMessages() const = 0;
173
174 // Returns the localized permission messages of this permission.
175 virtual PermissionMessages GetMessages() const = 0;
176
177 // Returns true if the given permission is allowed.
178 virtual bool Check(const CheckParam* param) const = 0;
179
180 // Returns true if |rhs| is a subset of this.
181 virtual bool Contains(const APIPermission* rhs) const = 0;
182
183 // Returns true if |rhs| is equal to this.
184 virtual bool Equal(const APIPermission* rhs) const = 0;
185
186 // Parses the APIPermission from |value|. Returns false if error happens.
187 virtual bool FromValue(const base::Value* value) = 0;
188
189 // Stores this into a new created |value|.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000190 virtual scoped_ptr<base::Value> ToValue() const = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000191
192 // Clones this.
193 virtual APIPermission* Clone() const = 0;
194
195 // Returns a new API permission which equals this - |rhs|.
196 virtual APIPermission* Diff(const APIPermission* rhs) const = 0;
197
198 // Returns a new API permission which equals the union of this and |rhs|.
199 virtual APIPermission* Union(const APIPermission* rhs) const = 0;
200
201 // Returns a new API permission which equals the intersect of this and |rhs|.
202 virtual APIPermission* Intersect(const APIPermission* rhs) const = 0;
203
204 // IPC functions
205 // Writes this into the given IPC message |m|.
206 virtual void Write(IPC::Message* m) const = 0;
207
208 // Reads from the given IPC message |m|.
209 virtual bool Read(const IPC::Message* m, PickleIterator* iter) = 0;
210
211 // Logs this permission.
212 virtual void Log(std::string* log) const = 0;
213
214 protected:
215 // Returns the localized permission message associated with this api.
216 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
217 PermissionMessage GetMessage_() const;
218
219 private:
220 const APIPermissionInfo* const info_;
221};
222
223
224// The APIPermissionInfo is an immutable class that describes a single
225// named permission (API permission).
226// There is one instance per permission.
227class APIPermissionInfo {
228 public:
229 enum Flag {
230 kFlagNone = 0,
231
232 // Indicates if the permission implies full access (native code).
233 kFlagImpliesFullAccess = 1 << 0,
234
235 // Indicates if the permission implies full URL access.
236 kFlagImpliesFullURLAccess = 1 << 1,
237
238 // Indicates that extensions cannot specify the permission as optional.
Ben Murdochca12bfa2013-07-23 11:17:05 +0100239 kFlagCannotBeOptional = 1 << 3,
240
241 // Indicates that the permission is internal to the extensions
242 // system and cannot be specified in the "permissions" list.
243 kFlagInternal = 1 << 4,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000244 };
245
246 typedef APIPermission* (*APIPermissionConstructor)(const APIPermissionInfo*);
247
248 typedef std::set<APIPermission::ID> IDSet;
249
250 ~APIPermissionInfo();
251
252 // Creates a APIPermission instance.
253 APIPermission* CreateAPIPermission() const;
254
255 int flags() const { return flags_; }
256
257 APIPermission::ID id() const { return id_; }
258
259 // Returns the message id associated with this permission.
260 PermissionMessage::ID message_id() const {
261 return message_id_;
262 }
263
264 // Returns the name of this permission.
265 const char* name() const { return name_; }
266
267 // Returns true if this permission implies full access (e.g., native code).
268 bool implies_full_access() const {
269 return (flags_ & kFlagImpliesFullAccess) != 0;
270 }
271
272 // Returns true if this permission implies full URL access.
273 bool implies_full_url_access() const {
274 return (flags_ & kFlagImpliesFullURLAccess) != 0;
275 }
276
277 // Returns true if this permission can be added and removed via the
278 // optional permissions extension API.
279 bool supports_optional() const {
280 return (flags_ & kFlagCannotBeOptional) == 0;
281 }
282
Ben Murdochca12bfa2013-07-23 11:17:05 +0100283 // Returns true if this permission is internal rather than a
284 // "permissions" list entry.
285 bool is_internal() const {
286 return (flags_ & kFlagInternal) != 0;
287 }
288
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000289 private:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100290 // Instances should only be constructed from within a
291 // PermissionsInfo::Delegate.
292 friend class ChromeAPIPermissions;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000293 // Implementations of APIPermission will want to get the permission message,
294 // but this class's implementation should be hidden from everyone else.
295 friend class APIPermission;
296
297 explicit APIPermissionInfo(
298 APIPermission::ID id,
299 const char* name,
300 int l10n_message_id,
301 PermissionMessage::ID message_id,
302 int flags,
303 APIPermissionConstructor api_permission_constructor);
304
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000305 // Returns the localized permission message associated with this api.
306 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
307 PermissionMessage GetMessage_() const;
308
309 const APIPermission::ID id_;
310 const char* const name_;
311 const int flags_;
312 const int l10n_message_id_;
313 const PermissionMessage::ID message_id_;
314 const APIPermissionConstructor api_permission_constructor_;
315};
316
317} // namespace extensions
318
319#endif // CHROME_COMMON_EXTENSIONS_PERMISSIONS_API_PERMISSION_H_