Upstream version 7.35.139.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     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     kSystemDisplay,
143     kSystemStorage,
144     kTab,
145     kTabCapture,
146     kTabCaptureForTab,
147     kTerminalPrivate,
148     kTopSites,
149     kTts,
150     kTtsEngine,
151     kUnlimitedStorage,
152     kUsb,
153     kUsbDevice,
154     kVideoCapture,
155     kVirtualKeyboardPrivate,
156     kWallpaper,
157     kWallpaperPrivate,
158     kWebConnectable,  // for externally_connectable manifest key
159     kWebNavigation,
160     kWebRequest,
161     kWebRequestBlocking,
162     kWebRequestInternal,
163     kWebrtcAudioPrivate,
164     kWebrtcLoggingPrivate,
165     kWebstorePrivate,
166     kWebView,
167     kWindowShape,
168     kScreenlockPrivate,
169     kSystemCpu,
170     kSystemMemory,
171     kSystemNetwork,
172     kSystemInfoCpu,
173     kSystemInfoMemory,
174     kFirstRunPrivate,
175     kEnumBoundary
176   };
177
178   struct CheckParam {
179   };
180
181   explicit APIPermission(const APIPermissionInfo* info);
182
183   virtual ~APIPermission();
184
185   // Returns the id of this permission.
186   ID id() const;
187
188   // Returns the name of this permission.
189   const char* name() const;
190
191   // Returns the APIPermission of this permission.
192   const APIPermissionInfo* info() const {
193     return info_;
194   }
195
196   // Returns true if this permission has any PermissionMessages.
197   virtual bool HasMessages() const = 0;
198
199   // Returns the localized permission messages of this permission.
200   virtual PermissionMessages GetMessages() const = 0;
201
202   // Returns true if the given permission is allowed.
203   virtual bool Check(const CheckParam* param) const = 0;
204
205   // Returns true if |rhs| is a subset of this.
206   virtual bool Contains(const APIPermission* rhs) const = 0;
207
208   // Returns true if |rhs| is equal to this.
209   virtual bool Equal(const APIPermission* rhs) const = 0;
210
211   // Parses the APIPermission from |value|. Returns false if an error happens
212   // and optionally set |error| if |error| is not NULL.
213   virtual bool FromValue(const base::Value* value, std::string* error) = 0;
214
215   // Stores this into a new created |value|.
216   virtual scoped_ptr<base::Value> ToValue() const = 0;
217
218   // Clones this.
219   virtual APIPermission* Clone() const = 0;
220
221   // Returns a new API permission which equals this - |rhs|.
222   virtual APIPermission* Diff(const APIPermission* rhs) const = 0;
223
224   // Returns a new API permission which equals the union of this and |rhs|.
225   virtual APIPermission* Union(const APIPermission* rhs) const = 0;
226
227   // Returns a new API permission which equals the intersect of this and |rhs|.
228   virtual APIPermission* Intersect(const APIPermission* rhs) const = 0;
229
230   // IPC functions
231   // Writes this into the given IPC message |m|.
232   virtual void Write(IPC::Message* m) const = 0;
233
234   // Reads from the given IPC message |m|.
235   virtual bool Read(const IPC::Message* m, PickleIterator* iter) = 0;
236
237   // Logs this permission.
238   virtual void Log(std::string* log) const = 0;
239
240  protected:
241   // Returns the localized permission message associated with this api.
242   // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
243   PermissionMessage GetMessage_() const;
244
245  private:
246   const APIPermissionInfo* const info_;
247 };
248
249
250 // The APIPermissionInfo is an immutable class that describes a single
251 // named permission (API permission).
252 // There is one instance per permission.
253 class APIPermissionInfo {
254  public:
255   enum Flag {
256     kFlagNone = 0,
257
258     // Indicates if the permission implies full access (native code).
259     kFlagImpliesFullAccess = 1 << 0,
260
261     // Indicates if the permission implies full URL access.
262     kFlagImpliesFullURLAccess = 1 << 1,
263
264     // Indicates that extensions cannot specify the permission as optional.
265     kFlagCannotBeOptional = 1 << 3,
266
267     // Indicates that the permission is internal to the extensions
268     // system and cannot be specified in the "permissions" list.
269     kFlagInternal = 1 << 4,
270   };
271
272   typedef APIPermission* (*APIPermissionConstructor)(const APIPermissionInfo*);
273
274   typedef std::set<APIPermission::ID> IDSet;
275
276   ~APIPermissionInfo();
277
278   // Creates a APIPermission instance.
279   APIPermission* CreateAPIPermission() const;
280
281   int flags() const { return flags_; }
282
283   APIPermission::ID id() const { return id_; }
284
285   // Returns the message id associated with this permission.
286   PermissionMessage::ID message_id() const {
287     return message_id_;
288   }
289
290   // Returns the name of this permission.
291   const char* name() const { return name_; }
292
293   // Returns true if this permission implies full access (e.g., native code).
294   bool implies_full_access() const {
295     return (flags_ & kFlagImpliesFullAccess) != 0;
296   }
297
298   // Returns true if this permission implies full URL access.
299   bool implies_full_url_access() const {
300     return (flags_ & kFlagImpliesFullURLAccess) != 0;
301   }
302
303   // Returns true if this permission can be added and removed via the
304   // optional permissions extension API.
305   bool supports_optional() const {
306     return (flags_ & kFlagCannotBeOptional) == 0;
307   }
308
309   // Returns true if this permission is internal rather than a
310   // "permissions" list entry.
311   bool is_internal() const {
312     return (flags_ & kFlagInternal) != 0;
313   }
314
315  private:
316   // Instances should only be constructed from within a PermissionsProvider.
317   friend class ChromeAPIPermissions;
318   // Implementations of APIPermission will want to get the permission message,
319   // but this class's implementation should be hidden from everyone else.
320   friend class APIPermission;
321
322   explicit APIPermissionInfo(
323       APIPermission::ID id,
324       const char* name,
325       int l10n_message_id,
326       PermissionMessage::ID message_id,
327       int flags,
328       APIPermissionConstructor api_permission_constructor);
329
330   // Returns the localized permission message associated with this api.
331   // Use GetMessage_ to avoid name conflict with macro GetMessage on Windows.
332   PermissionMessage GetMessage_() const;
333
334   const APIPermission::ID id_;
335   const char* const name_;
336   const int flags_;
337   const int l10n_message_id_;
338   const PermissionMessage::ID message_id_;
339   const APIPermissionConstructor api_permission_constructor_;
340 };
341
342 }  // namespace extensions
343
344 #endif  // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_H_