| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/socket/socket_api.h" | 5 #include "extensions/browser/api/socket/socket_api.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 83 |
| 84 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { | 84 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { |
| 85 return manager_->GetResourceIds(extension_->id()); | 85 return manager_->GetResourceIds(extension_->id()); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { | 88 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { |
| 89 manager_->Remove(extension_->id(), api_resource_id); | 89 manager_->Remove(extension_->id(), api_resource_id); |
| 90 } | 90 } |
| 91 | 91 |
| 92 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() | 92 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() |
| 93 : resource_context_(NULL), | 93 : resource_context_(nullptr), |
| 94 request_handle_(new net::HostResolver::RequestHandle), | 94 request_handle_(new net::HostResolver::RequestHandle), |
| 95 addresses_(new net::AddressList) {} | 95 addresses_(new net::AddressList) { |
| 96 } |
| 96 | 97 |
| 97 SocketExtensionWithDnsLookupFunction::~SocketExtensionWithDnsLookupFunction() {} | 98 SocketExtensionWithDnsLookupFunction::~SocketExtensionWithDnsLookupFunction() {} |
| 98 | 99 |
| 99 bool SocketExtensionWithDnsLookupFunction::PrePrepare() { | 100 bool SocketExtensionWithDnsLookupFunction::PrePrepare() { |
| 100 if (!SocketAsyncApiFunction::PrePrepare()) | 101 if (!SocketAsyncApiFunction::PrePrepare()) |
| 101 return false; | 102 return false; |
| 102 resource_context_ = browser_context()->GetResourceContext(); | 103 resource_context_ = browser_context()->GetResourceContext(); |
| 103 return resource_context_ != NULL; | 104 return resource_context_ != nullptr; |
| 104 } | 105 } |
| 105 | 106 |
| 106 void SocketExtensionWithDnsLookupFunction::StartDnsLookup( | 107 void SocketExtensionWithDnsLookupFunction::StartDnsLookup( |
| 107 const std::string& hostname) { | 108 const std::string& hostname) { |
| 108 net::HostResolver* host_resolver = | 109 net::HostResolver* host_resolver = |
| 109 HostResolverWrapper::GetInstance()->GetHostResolver(resource_context_); | 110 HostResolverWrapper::GetInstance()->GetHostResolver(resource_context_); |
| 110 DCHECK(host_resolver); | 111 DCHECK(host_resolver); |
| 111 | 112 |
| 112 // Yes, we are passing zero as the port. There are some interesting but not | 113 // Yes, we are passing zero as the port. There are some interesting but not |
| 113 // presently relevant reasons why HostResolver asks for the port of the | 114 // presently relevant reasons why HostResolver asks for the port of the |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 break; | 157 break; |
| 157 case extensions::core_api::socket::SOCKET_TYPE_NONE: | 158 case extensions::core_api::socket::SOCKET_TYPE_NONE: |
| 158 NOTREACHED(); | 159 NOTREACHED(); |
| 159 break; | 160 break; |
| 160 } | 161 } |
| 161 | 162 |
| 162 return true; | 163 return true; |
| 163 } | 164 } |
| 164 | 165 |
| 165 void SocketCreateFunction::Work() { | 166 void SocketCreateFunction::Work() { |
| 166 Socket* socket = NULL; | 167 Socket* socket = nullptr; |
| 167 if (socket_type_ == kSocketTypeTCP) { | 168 if (socket_type_ == kSocketTypeTCP) { |
| 168 socket = new TCPSocket(extension_->id()); | 169 socket = new TCPSocket(extension_->id()); |
| 169 } else if (socket_type_ == kSocketTypeUDP) { | 170 } else if (socket_type_ == kSocketTypeUDP) { |
| 170 socket = new UDPSocket(extension_->id()); | 171 socket = new UDPSocket(extension_->id()); |
| 171 } | 172 } |
| 172 DCHECK(socket); | 173 DCHECK(socket); |
| 173 | 174 |
| 174 base::DictionaryValue* result = new base::DictionaryValue(); | 175 base::DictionaryValue* result = new base::DictionaryValue(); |
| 175 result->SetInteger(kSocketIdKey, AddSocket(socket)); | 176 result->SetInteger(kSocketIdKey, AddSocket(socket)); |
| 176 SetResult(result); | 177 SetResult(result); |
| 177 } | 178 } |
| 178 | 179 |
| 179 bool SocketDestroyFunction::Prepare() { | 180 bool SocketDestroyFunction::Prepare() { |
| 180 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 181 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 181 return true; | 182 return true; |
| 182 } | 183 } |
| 183 | 184 |
| 184 void SocketDestroyFunction::Work() { RemoveSocket(socket_id_); } | 185 void SocketDestroyFunction::Work() { RemoveSocket(socket_id_); } |
| 185 | 186 |
| 186 SocketConnectFunction::SocketConnectFunction() | 187 SocketConnectFunction::SocketConnectFunction() |
| 187 : socket_id_(0), hostname_(), port_(0), socket_(NULL) {} | 188 : socket_id_(0), hostname_(), port_(0), socket_(nullptr) { |
| 189 } |
| 188 | 190 |
| 189 SocketConnectFunction::~SocketConnectFunction() {} | 191 SocketConnectFunction::~SocketConnectFunction() {} |
| 190 | 192 |
| 191 bool SocketConnectFunction::Prepare() { | 193 bool SocketConnectFunction::Prepare() { |
| 192 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 194 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 193 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); | 195 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); |
| 194 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); | 196 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); |
| 195 return true; | 197 return true; |
| 196 } | 198 } |
| 197 | 199 |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 349 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 348 return true; | 350 return true; |
| 349 } | 351 } |
| 350 | 352 |
| 351 void SocketAcceptFunction::AsyncWorkStart() { | 353 void SocketAcceptFunction::AsyncWorkStart() { |
| 352 Socket* socket = GetSocket(params_->socket_id); | 354 Socket* socket = GetSocket(params_->socket_id); |
| 353 if (socket) { | 355 if (socket) { |
| 354 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); | 356 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); |
| 355 } else { | 357 } else { |
| 356 error_ = kSocketNotFoundError; | 358 error_ = kSocketNotFoundError; |
| 357 OnAccept(-1, NULL); | 359 OnAccept(-1, nullptr); |
| 358 } | 360 } |
| 359 } | 361 } |
| 360 | 362 |
| 361 void SocketAcceptFunction::OnAccept(int result_code, | 363 void SocketAcceptFunction::OnAccept(int result_code, |
| 362 net::TCPClientSocket* socket) { | 364 net::TCPClientSocket* socket) { |
| 363 base::DictionaryValue* result = new base::DictionaryValue(); | 365 base::DictionaryValue* result = new base::DictionaryValue(); |
| 364 result->SetInteger(kResultCodeKey, result_code); | 366 result->SetInteger(kResultCodeKey, result_code); |
| 365 if (socket) { | 367 if (socket) { |
| 366 Socket* client_socket = new TCPSocket(socket, extension_id(), true); | 368 Socket* client_socket = new TCPSocket(socket, extension_id(), true); |
| 367 result->SetInteger(kSocketIdKey, AddSocket(client_socket)); | 369 result->SetInteger(kSocketIdKey, AddSocket(client_socket)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 378 bool SocketReadFunction::Prepare() { | 380 bool SocketReadFunction::Prepare() { |
| 379 params_ = core_api::socket::Read::Params::Create(*args_); | 381 params_ = core_api::socket::Read::Params::Create(*args_); |
| 380 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 382 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 381 return true; | 383 return true; |
| 382 } | 384 } |
| 383 | 385 |
| 384 void SocketReadFunction::AsyncWorkStart() { | 386 void SocketReadFunction::AsyncWorkStart() { |
| 385 Socket* socket = GetSocket(params_->socket_id); | 387 Socket* socket = GetSocket(params_->socket_id); |
| 386 if (!socket) { | 388 if (!socket) { |
| 387 error_ = kSocketNotFoundError; | 389 error_ = kSocketNotFoundError; |
| 388 OnCompleted(-1, NULL); | 390 OnCompleted(-1, nullptr); |
| 389 return; | 391 return; |
| 390 } | 392 } |
| 391 | 393 |
| 392 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096, | 394 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096, |
| 393 base::Bind(&SocketReadFunction::OnCompleted, this)); | 395 base::Bind(&SocketReadFunction::OnCompleted, this)); |
| 394 } | 396 } |
| 395 | 397 |
| 396 void SocketReadFunction::OnCompleted(int bytes_read, | 398 void SocketReadFunction::OnCompleted(int bytes_read, |
| 397 scoped_refptr<net::IOBuffer> io_buffer) { | 399 scoped_refptr<net::IOBuffer> io_buffer) { |
| 398 base::DictionaryValue* result = new base::DictionaryValue(); | 400 base::DictionaryValue* result = new base::DictionaryValue(); |
| 399 result->SetInteger(kResultCodeKey, bytes_read); | 401 result->SetInteger(kResultCodeKey, bytes_read); |
| 400 if (bytes_read > 0) { | 402 if (bytes_read > 0) { |
| 401 result->Set(kDataKey, | 403 result->Set(kDataKey, |
| 402 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 404 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
| 403 bytes_read)); | 405 bytes_read)); |
| 404 } else { | 406 } else { |
| 405 result->Set(kDataKey, new base::BinaryValue()); | 407 result->Set(kDataKey, new base::BinaryValue()); |
| 406 } | 408 } |
| 407 SetResult(result); | 409 SetResult(result); |
| 408 | 410 |
| 409 AsyncWorkCompleted(); | 411 AsyncWorkCompleted(); |
| 410 } | 412 } |
| 411 | 413 |
| 412 SocketWriteFunction::SocketWriteFunction() | 414 SocketWriteFunction::SocketWriteFunction() |
| 413 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0) {} | 415 : socket_id_(0), io_buffer_(nullptr), io_buffer_size_(0) { |
| 416 } |
| 414 | 417 |
| 415 SocketWriteFunction::~SocketWriteFunction() {} | 418 SocketWriteFunction::~SocketWriteFunction() {} |
| 416 | 419 |
| 417 bool SocketWriteFunction::Prepare() { | 420 bool SocketWriteFunction::Prepare() { |
| 418 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 421 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 419 base::BinaryValue* data = NULL; | 422 base::BinaryValue* data = nullptr; |
| 420 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 423 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
| 421 | 424 |
| 422 io_buffer_size_ = data->GetSize(); | 425 io_buffer_size_ = data->GetSize(); |
| 423 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 426 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
| 424 return true; | 427 return true; |
| 425 } | 428 } |
| 426 | 429 |
| 427 void SocketWriteFunction::AsyncWorkStart() { | 430 void SocketWriteFunction::AsyncWorkStart() { |
| 428 Socket* socket = GetSocket(socket_id_); | 431 Socket* socket = GetSocket(socket_id_); |
| 429 | 432 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 453 bool SocketRecvFromFunction::Prepare() { | 456 bool SocketRecvFromFunction::Prepare() { |
| 454 params_ = core_api::socket::RecvFrom::Params::Create(*args_); | 457 params_ = core_api::socket::RecvFrom::Params::Create(*args_); |
| 455 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 458 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 456 return true; | 459 return true; |
| 457 } | 460 } |
| 458 | 461 |
| 459 void SocketRecvFromFunction::AsyncWorkStart() { | 462 void SocketRecvFromFunction::AsyncWorkStart() { |
| 460 Socket* socket = GetSocket(params_->socket_id); | 463 Socket* socket = GetSocket(params_->socket_id); |
| 461 if (!socket) { | 464 if (!socket) { |
| 462 error_ = kSocketNotFoundError; | 465 error_ = kSocketNotFoundError; |
| 463 OnCompleted(-1, NULL, std::string(), 0); | 466 OnCompleted(-1, nullptr, std::string(), 0); |
| 464 return; | 467 return; |
| 465 } | 468 } |
| 466 | 469 |
| 467 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, | 470 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, |
| 468 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); | 471 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); |
| 469 } | 472 } |
| 470 | 473 |
| 471 void SocketRecvFromFunction::OnCompleted(int bytes_read, | 474 void SocketRecvFromFunction::OnCompleted(int bytes_read, |
| 472 scoped_refptr<net::IOBuffer> io_buffer, | 475 scoped_refptr<net::IOBuffer> io_buffer, |
| 473 const std::string& address, | 476 const std::string& address, |
| 474 int port) { | 477 int port) { |
| 475 base::DictionaryValue* result = new base::DictionaryValue(); | 478 base::DictionaryValue* result = new base::DictionaryValue(); |
| 476 result->SetInteger(kResultCodeKey, bytes_read); | 479 result->SetInteger(kResultCodeKey, bytes_read); |
| 477 if (bytes_read > 0) { | 480 if (bytes_read > 0) { |
| 478 result->Set(kDataKey, | 481 result->Set(kDataKey, |
| 479 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 482 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
| 480 bytes_read)); | 483 bytes_read)); |
| 481 } else { | 484 } else { |
| 482 result->Set(kDataKey, new base::BinaryValue()); | 485 result->Set(kDataKey, new base::BinaryValue()); |
| 483 } | 486 } |
| 484 result->SetString(kAddressKey, address); | 487 result->SetString(kAddressKey, address); |
| 485 result->SetInteger(kPortKey, port); | 488 result->SetInteger(kPortKey, port); |
| 486 SetResult(result); | 489 SetResult(result); |
| 487 | 490 |
| 488 AsyncWorkCompleted(); | 491 AsyncWorkCompleted(); |
| 489 } | 492 } |
| 490 | 493 |
| 491 SocketSendToFunction::SocketSendToFunction() | 494 SocketSendToFunction::SocketSendToFunction() |
| 492 : socket_id_(0), | 495 : socket_id_(0), |
| 493 io_buffer_(NULL), | 496 io_buffer_(nullptr), |
| 494 io_buffer_size_(0), | 497 io_buffer_size_(0), |
| 495 port_(0), | 498 port_(0), |
| 496 socket_(NULL) {} | 499 socket_(nullptr) { |
| 500 } |
| 497 | 501 |
| 498 SocketSendToFunction::~SocketSendToFunction() {} | 502 SocketSendToFunction::~SocketSendToFunction() {} |
| 499 | 503 |
| 500 bool SocketSendToFunction::Prepare() { | 504 bool SocketSendToFunction::Prepare() { |
| 501 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 505 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 502 base::BinaryValue* data = NULL; | 506 base::BinaryValue* data = nullptr; |
| 503 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 507 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
| 504 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); | 508 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); |
| 505 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); | 509 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); |
| 506 | 510 |
| 507 io_buffer_size_ = data->GetSize(); | 511 io_buffer_size_ = data->GetSize(); |
| 508 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 512 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
| 509 return true; | 513 return true; |
| 510 } | 514 } |
| 511 | 515 |
| 512 void SocketSendToFunction::AsyncWorkStart() { | 516 void SocketSendToFunction::AsyncWorkStart() { |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 921 Socket* socket = GetSocket(params_->socket_id); | 925 Socket* socket = GetSocket(params_->socket_id); |
| 922 if (!socket) { | 926 if (!socket) { |
| 923 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); | 927 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 924 error_ = kSocketNotFoundError; | 928 error_ = kSocketNotFoundError; |
| 925 AsyncWorkCompleted(); | 929 AsyncWorkCompleted(); |
| 926 return; | 930 return; |
| 927 } | 931 } |
| 928 | 932 |
| 929 // Make sure that the socket is a TCP client socket. | 933 // Make sure that the socket is a TCP client socket. |
| 930 if (socket->GetSocketType() != Socket::TYPE_TCP || | 934 if (socket->GetSocketType() != Socket::TYPE_TCP || |
| 931 static_cast<TCPSocket*>(socket)->ClientStream() == NULL) { | 935 static_cast<TCPSocket*>(socket)->ClientStream() == nullptr) { |
| 932 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); | 936 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 933 error_ = kSecureSocketTypeError; | 937 error_ = kSecureSocketTypeError; |
| 934 AsyncWorkCompleted(); | 938 AsyncWorkCompleted(); |
| 935 return; | 939 return; |
| 936 } | 940 } |
| 937 | 941 |
| 938 if (!socket->IsConnected()) { | 942 if (!socket->IsConnected()) { |
| 939 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); | 943 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 940 error_ = kSocketNotConnectedError; | 944 error_ = kSocketNotConnectedError; |
| 941 AsyncWorkCompleted(); | 945 AsyncWorkCompleted(); |
| 942 return; | 946 return; |
| 943 } | 947 } |
| 944 | 948 |
| 945 net::URLRequestContext* url_request_context = | 949 net::URLRequestContext* url_request_context = |
| 946 url_request_getter_->GetURLRequestContext(); | 950 url_request_getter_->GetURLRequestContext(); |
| 947 | 951 |
| 948 TLSSocket::UpgradeSocketToTLS( | 952 TLSSocket::UpgradeSocketToTLS( |
| 949 socket, | 953 socket, |
| 950 url_request_context->ssl_config_service(), | 954 url_request_context->ssl_config_service(), |
| 951 url_request_context->cert_verifier(), | 955 url_request_context->cert_verifier(), |
| 952 url_request_context->transport_security_state(), | 956 url_request_context->transport_security_state(), |
| 953 extension_id(), | 957 extension_id(), |
| 954 params_->options.get(), | 958 params_->options.get(), |
| 955 base::Bind(&SocketSecureFunction::TlsConnectDone, this)); | 959 base::Bind(&SocketSecureFunction::TlsConnectDone, this)); |
| 956 } | 960 } |
| 957 | 961 |
| 958 void SocketSecureFunction::TlsConnectDone(scoped_ptr<TLSSocket> socket, | 962 void SocketSecureFunction::TlsConnectDone(scoped_ptr<TLSSocket> socket, |
| 959 int result) { | 963 int result) { |
| 960 // if an error occurred, socket MUST be NULL. | 964 // if an error occurred, socket MUST be NULL. |
| 961 DCHECK(result == net::OK || socket == NULL); | 965 DCHECK(result == net::OK || socket == nullptr); |
| 962 | 966 |
| 963 if (socket && result == net::OK) { | 967 if (socket && result == net::OK) { |
| 964 ReplaceSocket(params_->socket_id, socket.release()); | 968 ReplaceSocket(params_->socket_id, socket.release()); |
| 965 } else { | 969 } else { |
| 966 RemoveSocket(params_->socket_id); | 970 RemoveSocket(params_->socket_id); |
| 967 error_ = net::ErrorToString(result); | 971 error_ = net::ErrorToString(result); |
| 968 } | 972 } |
| 969 | 973 |
| 970 results_ = core_api::socket::Secure::Results::Create(result); | 974 results_ = core_api::socket::Secure::Results::Create(result); |
| 971 AsyncWorkCompleted(); | 975 AsyncWorkCompleted(); |
| 972 } | 976 } |
| 973 | 977 |
| 974 } // namespace extensions | 978 } // namespace extensions |
| OLD | NEW |