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