Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / extensions / common / permissions / api_permission.h
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.
4
5 #ifndef EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_
6 #define EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
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"
17
18 namespace IPC {
19 class Message;
20 }
21
22 namespace extensions {
23
24 class APIPermissionInfo;
25 class ChromeAPIPermissions;
26
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.
30 class APIPermission {
31  public:
32   enum ID {
33     // Error codes.
34     kInvalid = -2,
35     kUnknown = -1,
36
37     // Real permissions.
38     kAccessibilityFeaturesModify,
39     kAccessibilityFeaturesRead,
40     kAccessibilityPrivate,
41     kActiveTab,
42     kActivityLogPrivate,
43     kAlarms,
44     kAlphaEnabled,
45     kAlwaysOnTopWindows,
46     kAppView,
47     kAudio,
48     kAudioCapture,
49     kAutomation,
50     kAutoTestPrivate,
51     kBackground,
52     kBluetoothPrivate,
53     kBookmark,
54     kBookmarkManagerPrivate,
55     kBrailleDisplayPrivate,
56     kBrowsingData,
57     kCast,
58     kCastStreaming,
59     kChromeosInfoPrivate,
60     kClipboardRead,
61     kClipboardWrite,
62     kCloudPrintPrivate,
63     kCommandLinePrivate,
64     kContentSettings,
65     kContextMenus,
66     kCookie,
67     kCopresence,
68     kCopresencePrivate,
69     kDiagnostics,
70     kDial,
71     kDebugger,
72     kDeclarative,
73     kDeclarativeContent,
74     kDeclarativeWebRequest,
75     kDesktopCapture,
76     kDeveloperPrivate,
77     kDevtools,
78     kDns,
79     kDownloads,
80     kDownloadsInternal,
81     kDownloadsOpen,
82     kDownloadsShelf,
83     kEasyUnlockPrivate,
84     kEchoPrivate,
85     kEmbeddedExtensionOptions,
86     kEnterprisePlatformKeys,
87     kEnterprisePlatformKeysPrivate,
88     kExperienceSamplingPrivate,
89     kExperimental,
90     kFeedbackPrivate,
91     kFileBrowserHandler,
92     kFileBrowserHandlerInternal,
93     kFileBrowserPrivate,
94     kFileSystem,
95     kFileSystemDirectory,
96     kFileSystemProvider,
97     kFileSystemRetainEntries,
98     kFileSystemWrite,
99     kFileSystemWriteDirectory,
100     kFontSettings,
101     kFullscreen,
102     kGcdPrivate,
103     kGcm,
104     kGeolocation,
105     kHid,
106     kHistory,
107     kHomepage,
108     kHotwordPrivate,
109     kIdentity,
110     kIdentityEmail,
111     kIdentityPrivate,
112     kIdltest,
113     kIdle,
114     kInfobars,
115     kInput,
116     kInputMethodPrivate,
117     kLedger,
118     kLocation,
119     kLogPrivate,
120     kManagement,
121     kMediaGalleries,
122     kMediaGalleriesPrivate,
123     kMediaPlayerPrivate,
124     kMetricsPrivate,
125     kMDns,
126     kMusicManagerPrivate,
127     kNativeMessaging,
128     kNetworkingPrivate,
129     kNotificationProvider,
130     kNotifications,
131     kOverrideEscFullscreen,
132     kPageCapture,
133     kPointerLock,
134     kPlugin,
135     kPower,
136     kPreferencesPrivate,
137     kPrincipalsPrivate,
138     kPrivacy,
139     kProcesses,
140     kProxy,
141     kPushMessaging,
142     kImageWriterPrivate,
143     kReadingListPrivate,
144     kRtcPrivate,
145     kSearchProvider,
146     kSerial,
147     kSessions,
148     kSignedInDevices,
149     kSocket,
150     kStartupPages,
151     kStorage,
152     kStreamsPrivate,
153     kSyncFileSystem,
154     kSyncedNotificationsPrivate,
155     kSystemPrivate,
156     kSystemDisplay,
157     kSystemStorage,
158     kTab,
159     kTabCapture,
160     kTabCaptureForTab,
161     kTerminalPrivate,
162     kTopSites,
163     kTts,
164     kTtsEngine,
165     kUnlimitedStorage,
166     kUsb,
167     kUsbDevice,
168     kVideoCapture,
169     kVirtualKeyboardPrivate,
170     kWallpaper,
171     kWallpaperPrivate,
172     kWebcamPrivate,
173     kWebConnectable,  // for externally_connectable manifest key
174     kWebNavigation,
175     kWebRequest,
176     kWebRequestBlocking,
177     kWebrtcAudioPrivate,
178     kWebrtcLoggingPrivate,
179     kWebstorePrivate,
180     kWebView,
181     kWindowShape,
182     kScreenlockPrivate,
183     kSystemCpu,
184     kSystemMemory,
185     kSystemNetwork,
186     kSystemInfoCpu,
187     kSystemInfoMemory,
188     kFirstRunPrivate,
189     kBrowser,
190     kEnumBoundary
191   };
192
193   struct CheckParam {
194   };
195
196   explicit APIPermission(const APIPermissionInfo* info);
197
198   virtual ~APIPermission();
199
200   // Returns the id of this permission.
201   ID id() const;
202
203   // Returns the name of this permission.
204   const char* name() const;
205
206   // Returns the APIPermission of this permission.
207   const APIPermissionInfo* info() const {
208     return info_;
209   }
210
211   // Returns true if this permission has any PermissionMessages.
212   virtual bool HasMessages() const = 0;
213
214   // Returns the localized permission messages of this permission.
215   virtual PermissionMessages GetMessages() const = 0;
216
217   // Returns true if the given permission is allowed.
218   virtual bool Check(const CheckParam* param) const = 0;
219
220   // Returns true if |rhs| is a subset of this.
221   virtual bool Contains(const APIPermission* rhs) const = 0;
222
223   // Returns true if |rhs| is equal to this.
224   virtual bool Equal(const APIPermission* rhs) const = 0;
225
226   // Parses the APIPermission from |value|. Returns false if an error happens
227   // and optionally set |error| if |error| is not NULL. If |value| represents
228   // multiple permissions, some are invalid, and |unhandled_permissions| is
229   // not NULL, the invalid ones are put into |unhandled_permissions| and the
230   // function returns true.
231   virtual bool FromValue(const base::Value* value,
232                          std::string* error,
233                          std::vector<std::string>* unhandled_permissions) = 0;
234
235   // Stores this into a new created |value|.
236   virtual scoped_ptr<base::Value> ToValue() const = 0;
237
238   // Clones this.
239   virtual APIPermission* Clone() const = 0;
240
241   // Returns a new API permission which equals this - |rhs|.
242   virtual APIPermission* Diff(const APIPermission* rhs) const = 0;
243
244   // Returns a new API permission which equals the union of this and |rhs|.
245   virtual APIPermission* Union(const APIPermission* rhs) const = 0;
246
247   // Returns a new API permission which equals the intersect of this and |rhs|.
248   virtual APIPermission* Intersect(const APIPermission* rhs) const = 0;
249
250   // IPC functions
251   // Writes this into the given IPC message |m|.
252   virtual void Write(IPC::Message* m) const = 0;
253
254   // Reads from the given IPC message |m|.
255   virtual bool Read(const IPC::Message* m, PickleIterator* iter) = 0;
256
257   // Logs this permission.
258   virtual void Log(std::string* log) const = 0;
259
260  protected:
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;
264
265  private:
266   const APIPermissionInfo* const info_;
267 };
268
269
270 // The APIPermissionInfo is an immutable class that describes a single
271 // named permission (API permission).
272 // There is one instance per permission.
273 class APIPermissionInfo {
274  public:
275   enum Flag {
276     kFlagNone = 0,
277
278     // Indicates if the permission implies full access (native code).
279     kFlagImpliesFullAccess = 1 << 0,
280
281     // Indicates if the permission implies full URL access.
282     kFlagImpliesFullURLAccess = 1 << 1,
283
284     // Indicates that extensions cannot specify the permission as optional.
285     kFlagCannotBeOptional = 1 << 3,
286
287     // Indicates that the permission is internal to the extensions
288     // system and cannot be specified in the "permissions" list.
289     kFlagInternal = 1 << 4,
290   };
291
292   typedef APIPermission* (*APIPermissionConstructor)(const APIPermissionInfo*);
293
294   typedef std::set<APIPermission::ID> IDSet;
295
296   ~APIPermissionInfo();
297
298   // Creates a APIPermission instance.
299   APIPermission* CreateAPIPermission() const;
300
301   int flags() const { return flags_; }
302
303   APIPermission::ID id() const { return id_; }
304
305   // Returns the message id associated with this permission.
306   PermissionMessage::ID message_id() const {
307     return message_id_;
308   }
309
310   // Returns the name of this permission.
311   const char* name() const { return name_; }
312
313   // Returns true if this permission implies full access (e.g., native code).
314   bool implies_full_access() const {
315     return (flags_ & kFlagImpliesFullAccess) != 0;
316   }
317
318   // Returns true if this permission implies full URL access.
319   bool implies_full_url_access() const {
320     return (flags_ & kFlagImpliesFullURLAccess) != 0;
321   }
322
323   // Returns true if this permission can be added and removed via the
324   // optional permissions extension API.
325   bool supports_optional() const {
326     return (flags_ & kFlagCannotBeOptional) == 0;
327   }
328
329   // Returns true if this permission is internal rather than a
330   // "permissions" list entry.
331   bool is_internal() const {
332     return (flags_ & kFlagInternal) != 0;
333   }
334
335  private:
336   // Instances should only be constructed from within a PermissionsProvider.
337   friend class ChromeAPIPermissions;
338   friend class ExtensionsAPIPermissions;
339   // Implementations of APIPermission will want to get the permission message,
340   // but this class's implementation should be hidden from everyone else.
341   friend class APIPermission;
342
343   // This exists to allow aggregate initialization, so that default values
344   // for flags, etc. can be omitted.
345   // TODO(yoz): Simplify the way initialization is done. APIPermissionInfo
346   // should be the simple data struct.
347   struct InitInfo {
348     APIPermission::ID id;
349     const char* name;
350     int flags;
351     int l10n_message_id;
352     PermissionMessage::ID message_id;
353     APIPermissionInfo::APIPermissionConstructor constructor;
354   };
355
356   explicit APIPermissionInfo(const InitInfo& info);
357
358   // Returns the localized permission message associated with this api.
359   // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
360   PermissionMessage GetMessage_() const;
361
362   const APIPermission::ID id_;
363   const char* const name_;
364   const int flags_;
365   const int l10n_message_id_;
366   const PermissionMessage::ID message_id_;
367   const APIPermissionConstructor api_permission_constructor_;
368 };
369
370 }  // namespace extensions
371
372 #endif  // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_