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