Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / common / permissions / api_permission.h
blob8fbc63631a051abde48104b009dc752e8e260029
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/pickle.h"
16 #include "base/values.h"
17 #include "extensions/common/permissions/permission_message.h"
19 namespace IPC {
20 class Message;
23 namespace extensions {
25 class PermissionIDSet;
26 class APIPermissionInfo;
27 class ChromeAPIPermissions;
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 // The IDs of all permissions available to apps. Add as many permissions here
35 // as needed to generate meaningful permission messages. Add the rules for the
36 // messages to ChromePermissionMessageProvider.
37 // Do not reorder this enumeration or remove any entries. If you need to add a
38 // new entry, add it just prior to kEnumBoundary, and ensure to update the
39 // "ExtensionPermission3" enum in tools/metrics/histograms/histograms.xml
40 // (by running update_extension_permission.py).
41 // TODO(sashab): Move this to a more central location, and rename it to
42 // PermissionID.
43 enum ID {
44 // Error codes.
45 kInvalid,
46 kUnknown,
48 // Actual permission IDs. Not all of these are valid permissions on their
49 // own; some are just needed by various manifest permissions to represent
50 // their permission message rule combinations.
51 kAccessibilityFeaturesModify,
52 kAccessibilityFeaturesRead,
53 kAccessibilityPrivate,
54 kActiveTab,
55 kActivityLogPrivate,
56 kAlarms,
57 kAlphaEnabled,
58 kAlwaysOnTopWindows,
59 kAppView,
60 kAudio,
61 kAudioCapture,
62 kAudioModem,
63 kAutofillPrivate,
64 kAutomation,
65 kAutoTestPrivate,
66 kBackground,
67 kBluetoothPrivate,
68 kBookmark,
69 kBookmarkManagerPrivate,
70 kBrailleDisplayPrivate,
71 kBrowser,
72 kBrowsingData,
73 kCast,
74 kCastStreaming,
75 kChromeosInfoPrivate,
76 kClipboardRead,
77 kClipboardWrite,
78 kCloudPrintPrivate,
79 kCommandLinePrivate,
80 kCommandsAccessibility,
81 kContentSettings,
82 kContextMenus,
83 kCookie,
84 kCopresence,
85 kCopresencePrivate,
86 kCryptotokenPrivate,
87 kDataReductionProxy,
88 kDiagnostics,
89 kDial,
90 kDebugger,
91 kDeclarative,
92 kDeclarativeContent,
93 kDeclarativeWebRequest,
94 kDesktopCapture,
95 kDesktopCapturePrivate,
96 kDeveloperPrivate,
97 kDevtools,
98 kDns,
99 kDocumentScan,
100 kDownloads,
101 kDownloadsInternal,
102 kDownloadsOpen,
103 kDownloadsShelf,
104 kEasyUnlockPrivate,
105 kEchoPrivate,
106 kEmbeddedExtensionOptions,
107 kEnterprisePlatformKeys,
108 kEnterprisePlatformKeysPrivate,
109 kExperienceSamplingPrivate,
110 kExperimental,
111 kExtensionView,
112 kExternallyConnectableAllUrls,
113 kFeedbackPrivate,
114 kFileBrowserHandler,
115 kFileBrowserHandlerInternal,
116 kFileManagerPrivate,
117 kFileSystem,
118 kFileSystemDirectory,
119 kFileSystemProvider,
120 kFileSystemRequestFileSystem,
121 kFileSystemRetainEntries,
122 kFileSystemWrite,
123 kFileSystemWriteDirectory,
124 kFirstRunPrivate,
125 kFontSettings,
126 kFullscreen,
127 kGcdPrivate,
128 kGcm,
129 kGeolocation,
130 kHid,
131 kHistory,
132 kHomepage,
133 kHotwordPrivate,
134 kIdentity,
135 kIdentityEmail,
136 kIdentityPrivate,
137 kIdltest,
138 kIdle,
139 kImeWindowEnabled,
140 kInlineInstallPrivate,
141 kInput,
142 kInputMethodPrivate,
143 kInterceptAllKeys,
144 kLauncherSearchProvider,
145 kLocation,
146 kLogPrivate,
147 kManagement,
148 kMediaGalleries,
149 kMediaPlayerPrivate,
150 kMediaRouterPrivate,
151 kMetricsPrivate,
152 kMDns,
153 kMusicManagerPrivate,
154 kNativeMessaging,
155 kNetworkingConfig,
156 kNetworkingPrivate,
157 kNotificationProvider,
158 kNotifications,
159 kOverrideEscFullscreen,
160 kPageCapture,
161 kPasswordsPrivate,
162 kPointerLock,
163 kPlatformKeys,
164 kPlugin,
165 kPower,
166 kPreferencesPrivate,
167 kPrincipalsPrivate,
168 kPrinterProvider,
169 kPrivacy,
170 kProcesses,
171 kProxy,
172 kImageWriterPrivate,
173 kReadingListPrivate,
174 kRtcPrivate,
175 kSearchProvider,
176 kSearchEnginesPrivate,
177 kSerial,
178 kSessions,
179 kSettingsPrivate,
180 kSignedInDevices,
181 kSocket,
182 kStartupPages,
183 kStorage,
184 kStreamsPrivate,
185 kSyncFileSystem,
186 kSystemPrivate,
187 kSystemDisplay,
188 kSystemStorage,
189 kTab,
190 kTabCapture,
191 kTabCaptureForTab,
192 kTerminalPrivate,
193 kTopSites,
194 kTts,
195 kTtsEngine,
196 kUnlimitedStorage,
197 kU2fDevices,
198 kUsb,
199 kUsbDevice,
200 kVideoCapture,
201 kVirtualKeyboardPrivate,
202 kVpnProvider,
203 kWallpaper,
204 kWallpaperPrivate,
205 kWebcamPrivate,
206 kWebConnectable, // for externally_connectable manifest key
207 kWebNavigation,
208 kWebRequest,
209 kWebRequestBlocking,
210 kWebrtcAudioPrivate,
211 kWebrtcDesktopCapturePrivate,
212 kWebrtcLoggingPrivate,
213 kWebstorePrivate,
214 kWebstoreWidgetPrivate,
215 kWebView,
216 kWindowShape,
217 kScreenlockPrivate,
218 kSystemCpu,
219 kSystemMemory,
220 kSystemNetwork,
221 kSystemInfoCpu,
222 kSystemInfoMemory,
223 kBluetooth,
224 kBluetoothDevices,
225 kFavicon,
226 kFullAccess,
227 kHostReadOnly,
228 kHostReadWrite,
229 kHostsAll,
230 kHostsAllReadOnly,
231 kMediaGalleriesAllGalleriesCopyTo,
232 kMediaGalleriesAllGalleriesDelete,
233 kMediaGalleriesAllGalleriesRead,
234 kNetworkState,
235 kOverrideBookmarksUI,
236 kShouldWarnAllHosts,
237 kSocketAnyHost,
238 kSocketDomainHosts,
239 kSocketSpecificHosts,
240 kUsbDeviceList,
241 kUsbDeviceUnknownProduct,
242 kUsbDeviceUnknownVendor,
243 // Last entry: Add new entries above and ensure to update the
244 // "ExtensionPermission3" enum in tools/metrics/histograms/histograms.xml
245 // (by running update_extension_permission.py).
246 kEnumBoundary
249 struct CheckParam {
252 explicit APIPermission(const APIPermissionInfo* info);
254 virtual ~APIPermission();
256 // Returns the id of this permission.
257 ID id() const;
259 // Returns the name of this permission.
260 const char* name() const;
262 // Returns the APIPermission of this permission.
263 const APIPermissionInfo* info() const {
264 return info_;
267 // The set of permissions an app/extension with this API permission has. These
268 // permissions are used by PermissionMessageProvider to generate meaningful
269 // permission messages for the app/extension.
271 // For simple API permissions, this will return a set containing only the ID
272 // of the permission. More complex permissions might have multiple IDs, one
273 // for each of the capabilities the API permission has (e.g. read, write and
274 // copy, in the case of the media gallery permission). Permissions that
275 // require parameters may also contain a parameter string (along with the
276 // permission's ID) which can be substituted into the permission message if a
277 // rule is defined to do so.
279 // Permissions with multiple values, such as host permissions, are represented
280 // by multiple entries in this set. Each permission in the subset has the same
281 // ID (e.g. kHostReadOnly) but a different parameter (e.g. google.com). These
282 // are grouped to form different kinds of permission messages (e.g. 'Access to
283 // 2 hosts') depending on the number that are in the set. The rules that
284 // define the grouping of related permissions with the same ID is defined in
285 // ChromePermissionMessageProvider.
286 virtual PermissionIDSet GetPermissions() const = 0;
288 // Returns true if this permission has any PermissionMessages.
289 // TODO(sashab): Deprecate this in favor of GetPermissions() above.
290 virtual bool HasMessages() const = 0;
292 // Returns the localized permission messages of this permission.
293 // TODO(sashab): Deprecate this in favor of GetPermissions() above.
294 virtual PermissionMessages GetMessages() const = 0;
296 // Returns true if the given permission is allowed.
297 virtual bool Check(const CheckParam* param) const = 0;
299 // Returns true if |rhs| is a subset of this.
300 virtual bool Contains(const APIPermission* rhs) const = 0;
302 // Returns true if |rhs| is equal to this.
303 virtual bool Equal(const APIPermission* rhs) const = 0;
305 // Parses the APIPermission from |value|. Returns false if an error happens
306 // and optionally set |error| if |error| is not NULL. If |value| represents
307 // multiple permissions, some are invalid, and |unhandled_permissions| is
308 // not NULL, the invalid ones are put into |unhandled_permissions| and the
309 // function returns true.
310 virtual bool FromValue(const base::Value* value,
311 std::string* error,
312 std::vector<std::string>* unhandled_permissions) = 0;
314 // Stores this into a new created |value|.
315 virtual scoped_ptr<base::Value> ToValue() const = 0;
317 // Clones this.
318 virtual APIPermission* Clone() const = 0;
320 // Returns a new API permission which equals this - |rhs|.
321 virtual APIPermission* Diff(const APIPermission* rhs) const = 0;
323 // Returns a new API permission which equals the union of this and |rhs|.
324 virtual APIPermission* Union(const APIPermission* rhs) const = 0;
326 // Returns a new API permission which equals the intersect of this and |rhs|.
327 virtual APIPermission* Intersect(const APIPermission* rhs) const = 0;
329 // IPC functions
330 // Writes this into the given IPC message |m|.
331 virtual void Write(IPC::Message* m) const = 0;
333 // Reads from the given IPC message |m|.
334 virtual bool Read(const IPC::Message* m, PickleIterator* iter) = 0;
336 // Logs this permission.
337 virtual void Log(std::string* log) const = 0;
339 protected:
340 // Returns the localized permission message associated with this api.
341 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
342 PermissionMessage GetMessage_() const;
344 private:
345 const APIPermissionInfo* const info_;
349 // The APIPermissionInfo is an immutable class that describes a single
350 // named permission (API permission).
351 // There is one instance per permission.
352 class APIPermissionInfo {
353 public:
354 enum Flag {
355 kFlagNone = 0,
357 // Indicates if the permission implies full access (native code).
358 kFlagImpliesFullAccess = 1 << 0,
360 // Indicates if the permission implies full URL access.
361 kFlagImpliesFullURLAccess = 1 << 1,
363 // Indicates that extensions cannot specify the permission as optional.
364 kFlagCannotBeOptional = 1 << 3,
366 // Indicates that the permission is internal to the extensions
367 // system and cannot be specified in the "permissions" list.
368 kFlagInternal = 1 << 4,
370 // Indicates that the permission may be granted to web contents by
371 // extensions using the content_capabilities manifest feature.
372 kFlagSupportsContentCapabilities = 1 << 5,
375 typedef APIPermission* (*APIPermissionConstructor)(const APIPermissionInfo*);
377 typedef std::set<APIPermission::ID> IDSet;
379 ~APIPermissionInfo();
381 // Creates a APIPermission instance.
382 APIPermission* CreateAPIPermission() const;
384 int flags() const { return flags_; }
386 APIPermission::ID id() const { return id_; }
388 // Returns the message id associated with this permission.
389 PermissionMessage::ID message_id() const {
390 return message_id_;
393 // Returns the name of this permission.
394 const char* name() const { return name_; }
396 // Returns true if this permission implies full access (e.g., native code).
397 bool implies_full_access() const {
398 return (flags_ & kFlagImpliesFullAccess) != 0;
401 // Returns true if this permission implies full URL access.
402 bool implies_full_url_access() const {
403 return (flags_ & kFlagImpliesFullURLAccess) != 0;
406 // Returns true if this permission can be added and removed via the
407 // optional permissions extension API.
408 bool supports_optional() const {
409 return (flags_ & kFlagCannotBeOptional) == 0;
412 // Returns true if this permission is internal rather than a
413 // "permissions" list entry.
414 bool is_internal() const {
415 return (flags_ & kFlagInternal) != 0;
418 // Returns true if this permission can be granted to web contents by an
419 // extension through the content_capabilities manifest feature.
420 bool supports_content_capabilities() const {
421 return (flags_ & kFlagSupportsContentCapabilities) != 0;
424 private:
425 // Instances should only be constructed from within a PermissionsProvider.
426 friend class ChromeAPIPermissions;
427 friend class ExtensionsAPIPermissions;
428 // Implementations of APIPermission will want to get the permission message,
429 // but this class's implementation should be hidden from everyone else.
430 friend class APIPermission;
432 // This exists to allow aggregate initialization, so that default values
433 // for flags, etc. can be omitted.
434 // TODO(yoz): Simplify the way initialization is done. APIPermissionInfo
435 // should be the simple data struct.
436 struct InitInfo {
437 APIPermission::ID id;
438 const char* name;
439 int flags;
440 int l10n_message_id;
441 PermissionMessage::ID message_id;
442 APIPermissionInfo::APIPermissionConstructor constructor;
445 explicit APIPermissionInfo(const InitInfo& info);
447 // Returns the localized permission message associated with this api.
448 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
449 PermissionMessage GetMessage_() const;
451 const APIPermission::ID id_;
452 const char* const name_;
453 const int flags_;
454 const int l10n_message_id_;
455 const PermissionMessage::ID message_id_;
456 const APIPermissionConstructor api_permission_constructor_;
459 } // namespace extensions
461 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_