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