| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/socket/socket_api.h" | 5 #include "chrome/browser/extensions/api/socket/socket_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/common/extensions/extension.h" | 8 #include "chrome/common/extensions/extension.h" |
| 9 #include "chrome/common/extensions/permissions/socket_permission.h" | 9 #include "chrome/common/extensions/permissions/socket_permission.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 11 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
| 12 #include "chrome/browser/extensions/api/socket/socket.h" | 12 #include "chrome/browser/extensions/api/socket/socket.h" |
| 13 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | 13 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| 14 #include "chrome/browser/extensions/api/socket/udp_socket.h" | 14 #include "chrome/browser/extensions/api/socket/udp_socket.h" |
| 15 #include "chrome/browser/extensions/extension_system.h" | 15 #include "chrome/browser/extensions/extension_system.h" |
| 16 #include "chrome/browser/io_thread.h" | 16 #include "chrome/browser/io_thread.h" |
| 17 #include "net/base/host_port_pair.h" | 17 #include "net/base/host_port_pair.h" |
| 18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 19 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/base/net_log.h" | 21 #include "net/base/net_log.h" |
| 22 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 23 | 23 |
| 24 namespace extensions { | 24 namespace extensions { |
| 25 | 25 |
| 26 using content::SocketPermissionRequest; |
| 27 |
| 26 const char kAddressKey[] = "address"; | 28 const char kAddressKey[] = "address"; |
| 27 const char kPortKey[] = "port"; | 29 const char kPortKey[] = "port"; |
| 28 const char kBytesWrittenKey[] = "bytesWritten"; | 30 const char kBytesWrittenKey[] = "bytesWritten"; |
| 29 const char kDataKey[] = "data"; | 31 const char kDataKey[] = "data"; |
| 30 const char kResultCodeKey[] = "resultCode"; | 32 const char kResultCodeKey[] = "resultCode"; |
| 31 const char kSocketIdKey[] = "socketId"; | 33 const char kSocketIdKey[] = "socketId"; |
| 32 | 34 |
| 33 const char kSocketNotFoundError[] = "Socket not found"; | 35 const char kSocketNotFoundError[] = "Socket not found"; |
| 34 const char kSocketTypeInvalidError[] = "Socket type is not supported"; | 36 const char kSocketTypeInvalidError[] = "Socket type is not supported"; |
| 35 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 37 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 | 177 |
| 176 void SocketConnectFunction::AsyncWorkStart() { | 178 void SocketConnectFunction::AsyncWorkStart() { |
| 177 socket_ = GetSocket(socket_id_); | 179 socket_ = GetSocket(socket_id_); |
| 178 if (!socket_) { | 180 if (!socket_) { |
| 179 error_ = kSocketNotFoundError; | 181 error_ = kSocketNotFoundError; |
| 180 SetResult(Value::CreateIntegerValue(-1)); | 182 SetResult(Value::CreateIntegerValue(-1)); |
| 181 AsyncWorkCompleted(); | 183 AsyncWorkCompleted(); |
| 182 return; | 184 return; |
| 183 } | 185 } |
| 184 | 186 |
| 185 SocketPermissionData::OperationType operation_type; | 187 SocketPermissionRequest::OperationType operation_type; |
| 186 switch (socket_->GetSocketType()) { | 188 switch (socket_->GetSocketType()) { |
| 187 case Socket::TYPE_TCP: | 189 case Socket::TYPE_TCP: |
| 188 operation_type = SocketPermissionData::TCP_CONNECT; | 190 operation_type = SocketPermissionRequest::TCP_CONNECT; |
| 189 break; | 191 break; |
| 190 case Socket::TYPE_UDP: | 192 case Socket::TYPE_UDP: |
| 191 operation_type = SocketPermissionData::UDP_SEND_TO; | 193 operation_type = SocketPermissionRequest::UDP_SEND_TO; |
| 192 break; | 194 break; |
| 193 default: | 195 default: |
| 194 NOTREACHED() << "Unknown socket type."; | 196 NOTREACHED() << "Unknown socket type."; |
| 195 operation_type = SocketPermissionData::NONE; | 197 operation_type = SocketPermissionRequest::NONE; |
| 196 break; | 198 break; |
| 197 } | 199 } |
| 198 | 200 |
| 199 SocketPermission::CheckParam param(operation_type, hostname_, port_); | 201 SocketPermission::CheckParam param(operation_type, hostname_, port_); |
| 200 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | 202 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 201 ¶m)) { | 203 ¶m)) { |
| 202 error_ = kPermissionError; | 204 error_ = kPermissionError; |
| 203 SetResult(Value::CreateIntegerValue(-1)); | 205 SetResult(Value::CreateIntegerValue(-1)); |
| 204 AsyncWorkCompleted(); | 206 AsyncWorkCompleted(); |
| 205 return; | 207 return; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 Socket* socket = GetSocket(socket_id_); | 255 Socket* socket = GetSocket(socket_id_); |
| 254 | 256 |
| 255 if (!socket) { | 257 if (!socket) { |
| 256 error_ = kSocketNotFoundError; | 258 error_ = kSocketNotFoundError; |
| 257 SetResult(Value::CreateIntegerValue(result)); | 259 SetResult(Value::CreateIntegerValue(result)); |
| 258 return; | 260 return; |
| 259 } | 261 } |
| 260 | 262 |
| 261 if (socket->GetSocketType() == Socket::TYPE_UDP) { | 263 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 262 SocketPermission::CheckParam param( | 264 SocketPermission::CheckParam param( |
| 263 SocketPermissionData::UDP_BIND, address_, port_); | 265 SocketPermissionRequest::UDP_BIND, address_, port_); |
| 264 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | 266 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 265 ¶m)) { | 267 ¶m)) { |
| 266 error_ = kPermissionError; | 268 error_ = kPermissionError; |
| 267 SetResult(Value::CreateIntegerValue(result)); | 269 SetResult(Value::CreateIntegerValue(result)); |
| 268 return; | 270 return; |
| 269 } | 271 } |
| 270 } else if (socket->GetSocketType() == Socket::TYPE_TCP) { | 272 } else if (socket->GetSocketType() == Socket::TYPE_TCP) { |
| 271 error_ = kTCPSocketBindError; | 273 error_ = kTCPSocketBindError; |
| 272 SetResult(Value::CreateIntegerValue(result)); | 274 SetResult(Value::CreateIntegerValue(result)); |
| 273 return; | 275 return; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 293 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 295 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 294 return true; | 296 return true; |
| 295 } | 297 } |
| 296 | 298 |
| 297 void SocketListenFunction::Work() { | 299 void SocketListenFunction::Work() { |
| 298 int result = -1; | 300 int result = -1; |
| 299 | 301 |
| 300 Socket* socket = GetSocket(params_->socket_id); | 302 Socket* socket = GetSocket(params_->socket_id); |
| 301 if (socket) { | 303 if (socket) { |
| 302 SocketPermission::CheckParam param( | 304 SocketPermission::CheckParam param( |
| 303 SocketPermissionData::TCP_LISTEN, params_->address, params_->port); | 305 SocketPermissionRequest::TCP_LISTEN, params_->address, params_->port); |
| 304 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | 306 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 305 ¶m)) { | 307 ¶m)) { |
| 306 error_ = kPermissionError; | 308 error_ = kPermissionError; |
| 307 SetResult(Value::CreateIntegerValue(result)); | 309 SetResult(Value::CreateIntegerValue(result)); |
| 308 return; | 310 return; |
| 309 } | 311 } |
| 310 | 312 |
| 311 result = socket->Listen( | 313 result = socket->Listen( |
| 312 params_->address, | 314 params_->address, |
| 313 params_->port, | 315 params_->port, |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 void SocketSendToFunction::AsyncWorkStart() { | 513 void SocketSendToFunction::AsyncWorkStart() { |
| 512 socket_ = GetSocket(socket_id_); | 514 socket_ = GetSocket(socket_id_); |
| 513 if (!socket_) { | 515 if (!socket_) { |
| 514 error_ = kSocketNotFoundError; | 516 error_ = kSocketNotFoundError; |
| 515 SetResult(Value::CreateIntegerValue(-1)); | 517 SetResult(Value::CreateIntegerValue(-1)); |
| 516 AsyncWorkCompleted(); | 518 AsyncWorkCompleted(); |
| 517 return; | 519 return; |
| 518 } | 520 } |
| 519 | 521 |
| 520 if (socket_->GetSocketType() == Socket::TYPE_UDP) { | 522 if (socket_->GetSocketType() == Socket::TYPE_UDP) { |
| 521 SocketPermission::CheckParam param(SocketPermissionData::UDP_SEND_TO, | 523 SocketPermission::CheckParam param(SocketPermissionRequest::UDP_SEND_TO, |
| 522 hostname_, port_); | 524 hostname_, port_); |
| 523 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | 525 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 524 ¶m)) { | 526 ¶m)) { |
| 525 error_ = kPermissionError; | 527 error_ = kPermissionError; |
| 526 SetResult(Value::CreateIntegerValue(-1)); | 528 SetResult(Value::CreateIntegerValue(-1)); |
| 527 AsyncWorkCompleted(); | 529 AsyncWorkCompleted(); |
| 528 return; | 530 return; |
| 529 } | 531 } |
| 530 } | 532 } |
| 531 | 533 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 info->name = i->name; | 691 info->name = i->name; |
| 690 info->address = net::IPAddressToString(i->address); | 692 info->address = net::IPAddressToString(i->address); |
| 691 create_arg.push_back(info); | 693 create_arg.push_back(info); |
| 692 } | 694 } |
| 693 | 695 |
| 694 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 696 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
| 695 SendResponse(true); | 697 SendResponse(true); |
| 696 } | 698 } |
| 697 | 699 |
| 698 } // namespace extensions | 700 } // namespace extensions |
| OLD | NEW |