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