Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "chrome/common/extensions/permissions/socket_permission_data.h" | |
| 6 | |
| 7 #include <cstdlib> | |
| 8 #include <sstream> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/string_number_conversions.h" | |
| 13 #include "base/string_split.h" | |
| 14 #include "base/string_util.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 using extensions::SocketPermissionData; | |
| 19 | |
| 20 const char kInvalid[] = "invalid"; | |
| 21 const char kTCPConnect[] = "tcp-connect"; | |
| 22 const char kTCPListen[] = "tcp-listen"; | |
| 23 const char kUDPBind[] = "udp-bind"; | |
| 24 const char kUDPSendTo[] = "udp-send-to"; | |
| 25 | |
| 26 SocketPermissionData::OperationType StringToType(const std::string& s) { | |
| 27 if (s == kTCPConnect) | |
| 28 return SocketPermissionData::TCP_CONNECT; | |
| 29 if (s == kTCPListen) | |
| 30 return SocketPermissionData::TCP_LISTEN; | |
| 31 if (s == kUDPBind) | |
| 32 return SocketPermissionData::UDP_BIND; | |
| 33 if (s == kUDPSendTo) | |
| 34 return SocketPermissionData::UDP_SEND_TO; | |
| 35 return SocketPermissionData::NONE; | |
| 36 } | |
| 37 | |
| 38 const char* TypeToString(SocketPermissionData::OperationType type) { | |
| 39 switch (type) { | |
| 40 case SocketPermissionData::TCP_CONNECT: | |
| 41 return kTCPConnect; | |
| 42 case SocketPermissionData::TCP_LISTEN: | |
| 43 return kTCPListen; | |
| 44 case SocketPermissionData::UDP_BIND: | |
| 45 return kUDPBind; | |
| 46 case SocketPermissionData::UDP_SEND_TO: | |
| 47 return kUDPSendTo; | |
| 48 default: | |
| 49 return kInvalid; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 } | |
| 54 | |
| 55 namespace extensions { | |
| 56 | |
| 57 SocketPermissionData::SocketPermissionData() | |
| 58 : type_(NONE), | |
| 59 match_subdomains_(true), | |
| 60 port_(0) { | |
| 61 } | |
| 62 | |
| 63 SocketPermissionData::~SocketPermissionData() { | |
| 64 } | |
| 65 | |
| 66 bool SocketPermissionData::operator<(const SocketPermissionData& rhs) const { | |
| 67 if (type_ < rhs.type_) | |
| 68 return true; | |
| 69 if (type_ > rhs.type_) | |
| 70 return false; | |
| 71 | |
| 72 if (host_ < rhs.host_) | |
| 73 return true; | |
| 74 if (host_ > rhs.host_) | |
| 75 return false; | |
| 76 | |
| 77 if (match_subdomains_ < rhs.match_subdomains_) | |
| 78 return true; | |
| 79 if (match_subdomains_ > rhs.match_subdomains_) | |
| 80 return false; | |
| 81 | |
| 82 if (port_ < rhs.port_) | |
| 83 return true; | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 bool SocketPermissionData::operator==(const SocketPermissionData& rhs) const { | |
| 88 return (type_ == rhs.type_) && (host_ == rhs.host_) && | |
| 89 (match_subdomains_ == rhs.match_subdomains_) && | |
| 90 (port_ == rhs.port_); | |
| 91 } | |
| 92 | |
| 93 bool SocketPermissionData::Match( | |
| 94 OperationType type, const std::string& host, int port) const { | |
| 95 if (type_ != type) | |
| 96 return false; | |
| 97 | |
| 98 std::string lhost = StringToLowerASCII(host); | |
| 99 if (host_ != lhost) { | |
| 100 if (!match_subdomains_) | |
| 101 return false; | |
| 102 | |
| 103 // host should equal to one or more chars + "." + host_. | |
|
miket_OOO
2012/08/07 23:02:47
either "be equal to" or just "equal."
Peng
2012/08/08 15:40:07
Done.
| |
| 104 if (lhost.length() < host_.length() + 2) | |
| 105 return false; | |
| 106 | |
| 107 if (lhost.compare(lhost.length() - host_.length(), | |
| 108 host_.length(), host_) != 0) | |
| 109 return false; | |
| 110 } | |
| 111 | |
| 112 if (port_ != port && port_ != 0) | |
| 113 return false; | |
| 114 | |
| 115 return true; | |
| 116 } | |
| 117 | |
| 118 bool SocketPermissionData::Parse(const std::string& permission) { | |
| 119 // Reset | |
| 120 *this = SocketPermissionData(); | |
| 121 | |
| 122 std::vector<std::string> tokens; | |
| 123 base::SplitString(permission, ':', &tokens); | |
| 124 | |
| 125 if (tokens.empty() || tokens.size() > 3) | |
| 126 return false; | |
| 127 | |
| 128 type_ = StringToType(tokens[0]); | |
| 129 if (type_ == NONE) | |
| 130 return false; | |
| 131 | |
| 132 if (tokens.size() == 1) | |
| 133 return true; | |
| 134 | |
| 135 // The first component can optionally be '*' to match all subdomains. | |
| 136 host_ = StringToLowerASCII(tokens[1]); | |
| 137 if (!host_.empty()) { | |
| 138 std::vector<std::string> host_components; | |
| 139 base::SplitString(host_, '.', &host_components); | |
| 140 DCHECK(!host_components.empty()); | |
| 141 | |
| 142 if (host_components[0] == "*" || host_components[0].empty()) { | |
| 143 host_components.erase(host_components.begin(), | |
| 144 host_components.begin() + 1); | |
| 145 } else { | |
| 146 match_subdomains_ = false; | |
| 147 } | |
| 148 host_ = JoinString(host_components, '.'); | |
| 149 } | |
| 150 | |
| 151 if (tokens.size() == 2) | |
| 152 return true; | |
| 153 | |
| 154 if (tokens.empty() || tokens[2] == "*") | |
| 155 return true; | |
| 156 | |
| 157 if (!tokens[2].empty()) { | |
| 158 if (!base::StringToInt(tokens[2], &port_) || | |
| 159 port_ < 0 || port_ > 65535) | |
|
miket_OOO
2012/08/07 23:02:47
Is port == 0 really a valid port? I'm concerned th
Peng
2012/08/08 15:40:07
Done.
| |
| 160 return false; | |
| 161 } | |
| 162 | |
| 163 return true; | |
| 164 } | |
| 165 | |
| 166 const std::string& SocketPermissionData::GetAsString() const { | |
| 167 if (!spec_.empty()) | |
| 168 return spec_; | |
| 169 | |
| 170 spec_.reserve(64); | |
| 171 spec_.append(TypeToString(type_)); | |
| 172 | |
| 173 if (match_subdomains_) { | |
| 174 spec_.append(":*"); | |
| 175 if (!host_.empty()) | |
| 176 spec_.append(".").append(host_); | |
| 177 } else { | |
| 178 spec_.append(":").append(host_); | |
| 179 } | |
| 180 | |
| 181 if (port_ == 0) | |
| 182 spec_.append(":*"); | |
| 183 else | |
| 184 spec_.append(":").append(base::IntToString(port_)); | |
| 185 | |
| 186 return spec_; | |
| 187 } | |
| 188 | |
| 189 } // namespace extensions | |
| OLD | NEW |