1// Copyright (c) 2012 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#ifndef CHROME_COMMON_EXTENSIONS_PERMISSIONS_SOCKET_PERMISSION_DATA_H_
5#define CHROME_COMMON_EXTENSIONS_PERMISSIONS_SOCKET_PERMISSION_DATA_H_
6
7#include <string>
8
9#include "chrome/common/extensions/permissions/socket_permission_entry.h"
10#include "extensions/common/permissions/api_permission.h"
11#include "ipc/ipc_param_traits.h"
12
13template <class T> struct FuzzTraits;
14
15namespace extensions {
16
17// A pattern that can be used to match socket permission.
18//   <socket-permission-pattern>
19//          := <op> |
20//             <op> ':' <host> |
21//             <op> ':' ':' <port> |
22//             <op> ':' <host> ':' <port> |
23//             'udp-multicast-membership'
24//   <op>   := 'tcp-connect' |
25//             'tcp-listen' |
26//             'udp-bind' |
27//             'udp-send-to' |
28//             'udp-multicast-membership' |
29//             'resolve-host' |
30//             'resolve-proxy' |
31//             'network-state'
32//   <host> := '*' |
33//             '*.' <anychar except '/' and '*'>+ |
34//             <anychar except '/' and '*'>+
35//   <port> := '*' |
36//             <port number between 0 and 65535>)
37// The multicast membership permission implies a permission to any address.
38class SocketPermissionData {
39 public:
40  SocketPermissionData();
41  ~SocketPermissionData();
42
43  // operators <, == are needed by container std::set and algorithms
44  // std::set_includes and std::set_differences.
45  bool operator<(const SocketPermissionData& rhs) const;
46  bool operator==(const SocketPermissionData& rhs) const;
47
48  // Check if |param| (which must be a SocketPermissionData::CheckParam)
49  // matches the spec of |this|.
50  bool Check(const APIPermission::CheckParam* param) const;
51
52  // Convert |this| into a base::Value.
53  scoped_ptr<base::Value> ToValue() const;
54
55  // Populate |this| from a base::Value.
56  bool FromValue(const base::Value* value);
57
58  // TODO(bryeung): SocketPermissionData should be encoded as a base::Value
59  // instead of a string.  Until that is done, expose these methods for
60  // testing.
61  bool ParseForTest(const std::string& permission) { return Parse(permission); }
62  const std::string& GetAsStringForTest() const { return GetAsString(); }
63
64  const SocketPermissionEntry& entry() const { return entry_; }
65
66 private:
67  // Friend so ParamTraits can serialize us.
68  friend struct IPC::ParamTraits<SocketPermissionData>;
69  friend struct FuzzTraits<SocketPermissionData>;
70
71  SocketPermissionEntry& entry();
72
73  bool Parse(const std::string& permission);
74  const std::string& GetAsString() const;
75  void Reset();
76
77  SocketPermissionEntry entry_;
78  mutable std::string spec_;
79};
80
81}  // namespace extensions
82
83#endif  // CHROME_COMMON_EXTENSIONS_PERMISSIONS_SOCKET_PERMISSION_DATA_H_
84