Home | History | Annotate | Download | only in permissions
      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 "base/memory/scoped_ptr.h"
     10 #include "chrome/common/extensions/permissions/api_permission.h"
     11 #include "content/public/common/socket_permission_request.h"
     12 
     13 namespace extensions {
     14 
     15 // A pattern that can be used to match socket permission.
     16 //   <socket-permission-pattern>
     17 //          := <op> |
     18 //             <op> ':' <host> |
     19 //             <op> ':' ':' <port> |
     20 //             <op> ':' <host> ':' <port> |
     21 //             'udp-multicast-membership'
     22 //   <op>   := 'tcp-connect' |
     23 //             'tcp-listen' |
     24 //             'udp-bind' |
     25 //             'udp-send-to'
     26 //   <host> := '*' |
     27 //             '*.' <anychar except '/' and '*'>+ |
     28 //             <anychar except '/' and '*'>+
     29 //   <port> := '*' |
     30 //             <port number between 0 and 65535>)
     31 // The multicast membership permission implies a permission to any address.
     32 class SocketPermissionData {
     33  public:
     34   enum HostType {
     35     ANY_HOST,
     36     HOSTS_IN_DOMAINS,
     37     SPECIFIC_HOSTS,
     38   };
     39 
     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   HostType GetHostType() const;
     59   const std::string GetHost() const;
     60 
     61   const content::SocketPermissionRequest& pattern() const { return pattern_; }
     62   const bool& match_subdomains() const { return match_subdomains_; }
     63 
     64   // These accessors are provided for IPC_STRUCT_TRAITS_MEMBER.  Please
     65   // think twice before using them for anything else.
     66   content::SocketPermissionRequest& pattern();
     67   bool& match_subdomains();
     68 
     69   // TODO(bryeung): SocketPermissionData should be encoded as a base::Value
     70   // instead of a string.  Until that is done, expose these methods for
     71   // testing.
     72   bool ParseForTest(const std::string& permission) { return Parse(permission); }
     73   const std::string& GetAsStringForTest() const { return GetAsString(); }
     74 
     75  private:
     76   bool Parse(const std::string& permission);
     77   const std::string& GetAsString() const;
     78   void Reset();
     79 
     80   content::SocketPermissionRequest pattern_;
     81   bool match_subdomains_;
     82   mutable std::string spec_;
     83 };
     84 
     85 }  // namespace extensions
     86 
     87 #endif  // CHROME_COMMON_EXTENSIONS_PERMISSIONS_SOCKET_PERMISSION_DATA_H_
     88