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