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