• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CHROME_COMMON_EXTENSIONS_PERMISSIONS_SOCKET_PERMISSION_ENTRY_H_
5 #define CHROME_COMMON_EXTENSIONS_PERMISSIONS_SOCKET_PERMISSION_ENTRY_H_
6 
7 #include <string>
8 #include <vector>
9 
10 #include "content/public/common/socket_permission_request.h"
11 #include "ipc/ipc_param_traits.h"
12 
13 template <class T> struct FuzzTraits;
14 
15 namespace extensions {
16 
17 // Internal representation of a socket permission for a specific operation, such
18 // as UDP "bind", host 127.0.0.1, port *.
19 class SocketPermissionEntry {
20  public:
21   enum HostType {
22     ANY_HOST,
23     HOSTS_IN_DOMAINS,
24     SPECIFIC_HOSTS,
25   };
26 
27   SocketPermissionEntry();
28   ~SocketPermissionEntry();
29 
30   // operators <, == are needed by container std::set and algorithms
31   // std::set_includes and std::set_differences.
32   bool operator<(const SocketPermissionEntry& rhs) const;
33   bool operator==(const SocketPermissionEntry& rhs) const;
34 
35   bool Check(const content::SocketPermissionRequest& request) const;
36 
37   // Parse a host:port pattern for a given operation type.
38   //   <pattern> := '' |
39   //                <host> |
40   //                ':' <port> |
41   //                <host> ':' <port> |
42   //
43   //   <host> := '*' |
44   //             '*.' <anychar except '/' and '*'>+ |
45   //             <anychar except '/' and '*'>+
46   //
47   //   <port> := '*' |
48   //             <port number between 0 and 65535>)
49   static bool ParseHostPattern(
50       content::SocketPermissionRequest::OperationType type,
51       const std::string& pattern,
52       SocketPermissionEntry* entry);
53 
54   static bool ParseHostPattern(
55       content::SocketPermissionRequest::OperationType type,
56       const std::vector<std::string>& pattern_tokens,
57       SocketPermissionEntry* entry);
58 
59   // Returns true if the permission type can be bound to a host or port.
60   bool IsAddressBoundType() const;
61 
62   std::string GetHostPatternAsString() const;
63   HostType GetHostType() const;
64 
pattern()65   const content::SocketPermissionRequest& pattern() const { return pattern_; }
match_subdomains()66   bool match_subdomains() const { return match_subdomains_; }
67 
68  private:
69   // Friend so ParamTraits can serialize us.
70   friend struct IPC::ParamTraits<SocketPermissionEntry>;
71   friend struct FuzzTraits<SocketPermissionEntry>;
72 
73   // The permission type, host and port.
74   content::SocketPermissionRequest pattern_;
75 
76   // True if there was a wildcard in the host name.
77   bool match_subdomains_;
78 };
79 
80 }  // namespace extensions
81 
82 #endif  // CHROME_COMMON_EXTENSIONS_PERMISSIONS_SOCKET_PERMISSION_ENTRY_H_
83