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_SET_H_
6#define EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_
7
8
9#include "extensions/common/permissions/api_permission.h"
10#include "extensions/common/permissions/base_set_operators.h"
11
12namespace base {
13class ListValue;
14}  // namespace base
15
16namespace extensions {
17
18class Extension;
19class APIPermissionSet;
20
21template<>
22struct BaseSetOperatorsTraits<APIPermissionSet> {
23  typedef APIPermission ElementType;
24  typedef APIPermission::ID ElementIDType;
25};
26
27class APIPermissionSet : public BaseSetOperators<APIPermissionSet> {
28 public:
29  enum ParseSource {
30    // Don't allow internal permissions to be parsed (e.g. entries in the
31    // "permissions" list in a manifest).
32    kDisallowInternalPermissions,
33
34    // Allow internal permissions to be parsed (e.g. from the "api" field of a
35    // permissions list in the prefs).
36    kAllowInternalPermissions,
37  };
38
39  void insert(APIPermission::ID id);
40
41  // Insert |permission| into the APIPermissionSet. The APIPermissionSet will
42  // take the ownership of |permission|,
43  void insert(APIPermission* permission);
44
45  // Parses permissions from |permissions| and adds the parsed permissions to
46  // |api_permissions|. If |source| is kDisallowInternalPermissions, treat
47  // permissions with kFlagInternal as errors. If |unhandled_permissions| is
48  // not NULL, the names of all permissions that couldn't be parsed will be
49  // added to this vector. If |error| is NULL, parsing will continue with the
50  // next permission if invalid data is detected. If |error| is not NULL, it
51  // will be set to an error message and false is returned when an invalid
52  // permission is found.
53  static bool ParseFromJSON(
54      const base::ListValue* permissions,
55      ParseSource source,
56      APIPermissionSet* api_permissions,
57      base::string16* error,
58      std::vector<std::string>* unhandled_permissions);
59
60  void AddImpliedPermissions();
61};
62
63}  // namespace extensions
64
65#endif  // EXTENSIONS_COMMON_PERMISSIONS_API_PERMISSION_SET_H_
66