Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / permissions / api_permission.h
blob98801e61bb542d7aede22154cdc07090151a1a23
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 kPointerLock,
162 kPlatformKeys,
163 kPlugin,
164 kPower,
165 kPreferencesPrivate,
166 kPrincipalsPrivate,
167 kPrinterProvider,
168 kPrivacy,
169 kProcesses,
170 kProxy,
171 kImageWriterPrivate,
172 kReadingListPrivate,
173 kRtcPrivate,
174 kSearchProvider,
175 kSearchEnginesPrivate,
176 kSerial,
177 kSessions,
178 kSettingsPrivate,
179 kSignedInDevices,
180 kSocket,
181 kStartupPages,
182 kStorage,
183 kStreamsPrivate,
184 kSyncFileSystem,
185 kSystemPrivate,
186 kSystemDisplay,
187 kSystemStorage,
188 kTab,
189 kTabCapture,
190 kTabCaptureForTab,
191 kTerminalPrivate,
192 kTopSites,
193 kTts,
194 kTtsEngine,
195 kUnlimitedStorage,
196 kU2fDevices,
197 kUsb,
198 kUsbDevice,
199 kVideoCapture,
200 kVirtualKeyboardPrivate,
201 kVpnProvider,
202 kWallpaper,
203 kWallpaperPrivate,
204 kWebcamPrivate,
205 kWebConnectable, // for externally_connectable manifest key
206 kWebNavigation,
207 kWebRequest,
208 kWebRequestBlocking,
209 kWebrtcAudioPrivate,
210 kWebrtcDesktopCapturePrivate,
211 kWebrtcLoggingPrivate,
212 kWebstorePrivate,
213 kWebstoreWidgetPrivate,
214 kWebView,
215 kWindowShape,
216 kScreenlockPrivate,
217 kSystemCpu,
218 kSystemMemory,
219 kSystemNetwork,
220 kSystemInfoCpu,
221 kSystemInfoMemory,
222 kBluetooth,
223 kBluetoothDevices,
224 kFavicon,
225 kFullAccess,
226 kHostReadOnly,
227 kHostReadWrite,
228 kHostsAll,
229 kHostsAllReadOnly,
230 kMediaGalleriesAllGalleriesCopyTo,
231 kMediaGalleriesAllGalleriesDelete,
232 kMediaGalleriesAllGalleriesRead,
233 kNetworkState,
234 kOverrideBookmarksUI,
235 kShouldWarnAllHosts,
236 kSocketAnyHost,
237 kSocketDomainHosts,
238 kSocketSpecificHosts,
239 kUsbDeviceList,
240 kUsbDeviceUnknownProduct,
241 kUsbDeviceUnknownVendor,
242 kUsersPrivate,
243 kPasswordsPrivate,
244 kLanguageSettingsPrivate,
245 // Last entry: Add new entries above and ensure to update the
246 // "ExtensionPermission3" enum in tools/metrics/histograms/histograms.xml
247 // (by running update_extension_permission.py).
248 kEnumBoundary
251 struct CheckParam {
254 explicit APIPermission(const APIPermissionInfo* info);
256 virtual ~APIPermission();
258 // Returns the id of this permission.
259 ID id() const;
261 // Returns the name of this permission.
262 const char* name() const;
264 // Returns the APIPermission of this permission.
265 const APIPermissionInfo* info() const {
266 return info_;
269 // The set of permissions an app/extension with this API permission has. These
270 // permissions are used by PermissionMessageProvider to generate meaningful
271 // permission messages for the app/extension.
273 // For simple API permissions, this will return a set containing only the ID
274 // of the permission. More complex permissions might have multiple IDs, one
275 // for each of the capabilities the API permission has (e.g. read, write and
276 // copy, in the case of the media gallery permission). Permissions that
277 // require parameters may also contain a parameter string (along with the
278 // permission's ID) which can be substituted into the permission message if a
279 // rule is defined to do so.
281 // Permissions with multiple values, such as host permissions, are represented
282 // by multiple entries in this set. Each permission in the subset has the same
283 // ID (e.g. kHostReadOnly) but a different parameter (e.g. google.com). These
284 // are grouped to form different kinds of permission messages (e.g. 'Access to
285 // 2 hosts') depending on the number that are in the set. The rules that
286 // define the grouping of related permissions with the same ID is defined in
287 // ChromePermissionMessageProvider.
288 virtual PermissionIDSet GetPermissions() const = 0;
290 // Returns true if this permission has any PermissionMessages.
291 // TODO(sashab): Deprecate this in favor of GetPermissions() above.
292 virtual bool HasMessages() const = 0;
294 // Returns the localized permission messages of this permission.
295 // TODO(sashab): Deprecate this in favor of GetPermissions() above.
296 virtual PermissionMessages GetMessages() const = 0;
298 // Returns true if the given permission is allowed.
299 virtual bool Check(const CheckParam* param) const = 0;
301 // Returns true if |rhs| is a subset of this.
302 virtual bool Contains(const APIPermission* rhs) const = 0;
304 // Returns true if |rhs| is equal to this.
305 virtual bool Equal(const APIPermission* rhs) const = 0;
307 // Parses the APIPermission from |value|. Returns false if an error happens
308 // and optionally set |error| if |error| is not NULL. If |value| represents
309 // multiple permissions, some are invalid, and |unhandled_permissions| is
310 // not NULL, the invalid ones are put into |unhandled_permissions| and the
311 // function returns true.
312 virtual bool FromValue(const base::Value* value,
313 std::string* error,
314 std::vector<std::string>* unhandled_permissions) = 0;
316 // Stores this into a new created |value|.
317 virtual scoped_ptr<base::Value> ToValue() const = 0;
319 // Clones this.
320 virtual APIPermission* Clone() const = 0;
322 // Returns a new API permission which equals this - |rhs|.
323 virtual APIPermission* Diff(const APIPermission* rhs) const = 0;
325 // Returns a new API permission which equals the union of this and |rhs|.
326 virtual APIPermission* Union(const APIPermission* rhs) const = 0;
328 // Returns a new API permission which equals the intersect of this and |rhs|.
329 virtual APIPermission* Intersect(const APIPermission* rhs) const = 0;
331 // IPC functions
332 // Writes this into the given IPC message |m|.
333 virtual void Write(IPC::Message* m) const = 0;
335 // Reads from the given IPC message |m|.
336 virtual bool Read(const IPC::Message* m, base::PickleIterator* iter) = 0;
338 // Logs this permission.
339 virtual void Log(std::string* log) const = 0;
341 protected:
342 // Returns the localized permission message associated with this api.
343 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
344 PermissionMessage GetMessage_() const;
346 private:
347 const APIPermissionInfo* const info_;
351 // The APIPermissionInfo is an immutable class that describes a single
352 // named permission (API permission).
353 // There is one instance per permission.
354 class APIPermissionInfo {
355 public:
356 enum Flag {
357 kFlagNone = 0,
359 // Indicates if the permission implies full access (native code).
360 kFlagImpliesFullAccess = 1 << 0,
362 // Indicates if the permission implies full URL access.
363 kFlagImpliesFullURLAccess = 1 << 1,
365 // Indicates that extensions cannot specify the permission as optional.
366 kFlagCannotBeOptional = 1 << 3,
368 // Indicates that the permission is internal to the extensions
369 // system and cannot be specified in the "permissions" list.
370 kFlagInternal = 1 << 4,
372 // Indicates that the permission may be granted to web contents by
373 // extensions using the content_capabilities manifest feature.
374 kFlagSupportsContentCapabilities = 1 << 5,
377 typedef APIPermission* (*APIPermissionConstructor)(const APIPermissionInfo*);
379 typedef std::set<APIPermission::ID> IDSet;
381 ~APIPermissionInfo();
383 // Creates a APIPermission instance.
384 APIPermission* CreateAPIPermission() const;
386 int flags() const { return flags_; }
388 APIPermission::ID id() const { return id_; }
390 // Returns the message id associated with this permission.
391 PermissionMessage::ID message_id() const {
392 return message_id_;
395 // Returns the name of this permission.
396 const char* name() const { return name_; }
398 // Returns true if this permission implies full access (e.g., native code).
399 bool implies_full_access() const {
400 return (flags_ & kFlagImpliesFullAccess) != 0;
403 // Returns true if this permission implies full URL access.
404 bool implies_full_url_access() const {
405 return (flags_ & kFlagImpliesFullURLAccess) != 0;
408 // Returns true if this permission can be added and removed via the
409 // optional permissions extension API.
410 bool supports_optional() const {
411 return (flags_ & kFlagCannotBeOptional) == 0;
414 // Returns true if this permission is internal rather than a
415 // "permissions" list entry.
416 bool is_internal() const {
417 return (flags_ & kFlagInternal) != 0;
420 // Returns true if this permission can be granted to web contents by an
421 // extension through the content_capabilities manifest feature.
422 bool supports_content_capabilities() const {
423 return (flags_ & kFlagSupportsContentCapabilities) != 0;
426 private:
427 // Instances should only be constructed from within a PermissionsProvider.
428 friend class ChromeAPIPermissions;
429 friend class ExtensionsAPIPermissions;
430 // Implementations of APIPermission will want to get the permission message,
431 // but this class's implementation should be hidden from everyone else.
432 friend class APIPermission;
434 // This exists to allow aggregate initialization, so that default values
435 // for flags, etc. can be omitted.
436 // TODO(yoz): Simplify the way initialization is done. APIPermissionInfo
437 // should be the simple data struct.
438 struct InitInfo {
439 APIPermission::ID id;
440 const char* name;
441 int flags;
442 int l10n_message_id;
443 PermissionMessage::ID message_id;
444 APIPermissionInfo::APIPermissionConstructor constructor;
447 explicit APIPermissionInfo(const InitInfo& info);
449 // Returns the localized permission message associated with this api.
450 // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
451 PermissionMessage GetMessage_() const;
453 const APIPermission::ID id_;
454 const char* const name_;
455 const int flags_;
456 const int l10n_message_id_;
457 const PermissionMessage::ID message_id_;
458 const APIPermissionConstructor api_permission_constructor_;
461 } // namespace extensions
463 #endif // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_