| 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/sockets_tcp/sockets_tcp_api.h" | 5 #include "extensions/browser/api/sockets_tcp/sockets_tcp_api.h" |
| 6 | 6 |
| 7 #include "content/public/browser/browser_context.h" | 7 #include "content/public/browser/browser_context.h" |
| 8 #include "content/public/common/socket_permission_request.h" | 8 #include "content/public/common/socket_permission_request.h" |
| 9 #include "extensions/browser/api/socket/tcp_socket.h" | 9 #include "extensions/browser/api/socket/tcp_socket.h" |
| 10 #include "extensions/browser/api/socket/tls_socket.h" | 10 #include "extensions/browser/api/socket/tls_socket.h" |
| 11 #include "extensions/browser/api/sockets_tcp/tcp_socket_event_dispatcher.h" | 11 #include "extensions/browser/api/sockets_tcp/tcp_socket_event_dispatcher.h" |
| 12 #include "extensions/common/api/sockets/sockets_manifest_data.h" | 12 #include "extensions/common/api/sockets/sockets_manifest_data.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/url_request/url_request_context.h" | 14 #include "net/url_request/url_request_context.h" |
| 15 #include "net/url_request/url_request_context_getter.h" | 15 #include "net/url_request/url_request_context_getter.h" |
| 16 | 16 |
| 17 using extensions::ResumableTCPSocket; | 17 using extensions::ResumableTCPSocket; |
| 18 using extensions::api::sockets_tcp::SocketInfo; | 18 using extensions::api::sockets_tcp::SocketInfo; |
| 19 using extensions::api::sockets_tcp::SocketProperties; | 19 using extensions::api::sockets_tcp::SocketProperties; |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const char kSocketNotFoundError[] = "Socket not found"; | 23 const char kSocketNotFoundError[] = "Socket not found"; |
| 24 const char kPermissionError[] = "Does not have permission"; | 24 const char kPermissionError[] = "Does not have permission"; |
| 25 const char kInvalidSocketStateError[] = | 25 const char kInvalidSocketStateError[] = |
| 26 "Socket must be a connected client TCP socket."; | 26 "Socket must be a connected client TCP socket."; |
| 27 const char kSocketNotConnectedError[] = "Socket not connected"; | 27 const char kSocketNotConnectedError[] = "Socket not connected"; |
| 28 | 28 |
| 29 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, | 29 SocketInfo CreateSocketInfo(int socket_id, ResumableTCPSocket* socket) { |
| 30 ResumableTCPSocket* socket) { | 30 SocketInfo socket_info; |
| 31 linked_ptr<SocketInfo> socket_info(new SocketInfo()); | |
| 32 // This represents what we know about the socket, and does not call through | 31 // This represents what we know about the socket, and does not call through |
| 33 // to the system. | 32 // to the system. |
| 34 socket_info->socket_id = socket_id; | 33 socket_info.socket_id = socket_id; |
| 35 if (!socket->name().empty()) { | 34 if (!socket->name().empty()) { |
| 36 socket_info->name.reset(new std::string(socket->name())); | 35 socket_info.name.reset(new std::string(socket->name())); |
| 37 } | 36 } |
| 38 socket_info->persistent = socket->persistent(); | 37 socket_info.persistent = socket->persistent(); |
| 39 if (socket->buffer_size() > 0) { | 38 if (socket->buffer_size() > 0) { |
| 40 socket_info->buffer_size.reset(new int(socket->buffer_size())); | 39 socket_info.buffer_size.reset(new int(socket->buffer_size())); |
| 41 } | 40 } |
| 42 socket_info->paused = socket->paused(); | 41 socket_info.paused = socket->paused(); |
| 43 socket_info->connected = socket->IsConnected(); | 42 socket_info.connected = socket->IsConnected(); |
| 44 | 43 |
| 45 // Grab the local address as known by the OS. | 44 // Grab the local address as known by the OS. |
| 46 net::IPEndPoint localAddress; | 45 net::IPEndPoint localAddress; |
| 47 if (socket->GetLocalAddress(&localAddress)) { | 46 if (socket->GetLocalAddress(&localAddress)) { |
| 48 socket_info->local_address.reset( | 47 socket_info.local_address.reset( |
| 49 new std::string(localAddress.ToStringWithoutPort())); | 48 new std::string(localAddress.ToStringWithoutPort())); |
| 50 socket_info->local_port.reset(new int(localAddress.port())); | 49 socket_info.local_port.reset(new int(localAddress.port())); |
| 51 } | 50 } |
| 52 | 51 |
| 53 // Grab the peer address as known by the OS. This and the call below will | 52 // Grab the peer address as known by the OS. This and the call below will |
| 54 // always succeed while the socket is connected, even if the socket has | 53 // always succeed while the socket is connected, even if the socket has |
| 55 // been remotely closed by the peer; only reading the socket will reveal | 54 // been remotely closed by the peer; only reading the socket will reveal |
| 56 // that it should be closed locally. | 55 // that it should be closed locally. |
| 57 net::IPEndPoint peerAddress; | 56 net::IPEndPoint peerAddress; |
| 58 if (socket->GetPeerAddress(&peerAddress)) { | 57 if (socket->GetPeerAddress(&peerAddress)) { |
| 59 socket_info->peer_address.reset( | 58 socket_info.peer_address.reset( |
| 60 new std::string(peerAddress.ToStringWithoutPort())); | 59 new std::string(peerAddress.ToStringWithoutPort())); |
| 61 socket_info->peer_port.reset(new int(peerAddress.port())); | 60 socket_info.peer_port.reset(new int(peerAddress.port())); |
| 62 } | 61 } |
| 63 | 62 |
| 64 return socket_info; | 63 return socket_info; |
| 65 } | 64 } |
| 66 | 65 |
| 67 void SetSocketProperties(ResumableTCPSocket* socket, | 66 void SetSocketProperties(ResumableTCPSocket* socket, |
| 68 SocketProperties* properties) { | 67 SocketProperties* properties) { |
| 69 if (properties->name.get()) { | 68 if (properties->name.get()) { |
| 70 socket->set_name(*properties->name.get()); | 69 socket->set_name(*properties->name.get()); |
| 71 } | 70 } |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 return true; | 413 return true; |
| 415 } | 414 } |
| 416 | 415 |
| 417 void SocketsTcpGetInfoFunction::Work() { | 416 void SocketsTcpGetInfoFunction::Work() { |
| 418 ResumableTCPSocket* socket = GetTcpSocket(params_->socket_id); | 417 ResumableTCPSocket* socket = GetTcpSocket(params_->socket_id); |
| 419 if (!socket) { | 418 if (!socket) { |
| 420 error_ = kSocketNotFoundError; | 419 error_ = kSocketNotFoundError; |
| 421 return; | 420 return; |
| 422 } | 421 } |
| 423 | 422 |
| 424 linked_ptr<sockets_tcp::SocketInfo> socket_info = | 423 sockets_tcp::SocketInfo socket_info = |
| 425 CreateSocketInfo(params_->socket_id, socket); | 424 CreateSocketInfo(params_->socket_id, socket); |
| 426 results_ = sockets_tcp::GetInfo::Results::Create(*socket_info); | 425 results_ = sockets_tcp::GetInfo::Results::Create(socket_info); |
| 427 } | 426 } |
| 428 | 427 |
| 429 SocketsTcpGetSocketsFunction::SocketsTcpGetSocketsFunction() {} | 428 SocketsTcpGetSocketsFunction::SocketsTcpGetSocketsFunction() {} |
| 430 | 429 |
| 431 SocketsTcpGetSocketsFunction::~SocketsTcpGetSocketsFunction() {} | 430 SocketsTcpGetSocketsFunction::~SocketsTcpGetSocketsFunction() {} |
| 432 | 431 |
| 433 bool SocketsTcpGetSocketsFunction::Prepare() { return true; } | 432 bool SocketsTcpGetSocketsFunction::Prepare() { return true; } |
| 434 | 433 |
| 435 void SocketsTcpGetSocketsFunction::Work() { | 434 void SocketsTcpGetSocketsFunction::Work() { |
| 436 std::vector<linked_ptr<sockets_tcp::SocketInfo> > socket_infos; | 435 std::vector<sockets_tcp::SocketInfo> socket_infos; |
| 437 base::hash_set<int>* resource_ids = GetSocketIds(); | 436 base::hash_set<int>* resource_ids = GetSocketIds(); |
| 438 if (resource_ids != NULL) { | 437 if (resource_ids != NULL) { |
| 439 for (base::hash_set<int>::iterator it = resource_ids->begin(); | 438 for (int socket_id : *resource_ids) { |
| 440 it != resource_ids->end(); | |
| 441 ++it) { | |
| 442 int socket_id = *it; | |
| 443 ResumableTCPSocket* socket = GetTcpSocket(socket_id); | 439 ResumableTCPSocket* socket = GetTcpSocket(socket_id); |
| 444 if (socket) { | 440 if (socket) { |
| 445 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); | 441 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); |
| 446 } | 442 } |
| 447 } | 443 } |
| 448 } | 444 } |
| 449 results_ = sockets_tcp::GetSockets::Results::Create(socket_infos); | 445 results_ = sockets_tcp::GetSockets::Results::Create(socket_infos); |
| 450 } | 446 } |
| 451 | 447 |
| 452 SocketsTcpSecureFunction::SocketsTcpSecureFunction() { | 448 SocketsTcpSecureFunction::SocketsTcpSecureFunction() { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 RemoveSocket(params_->socket_id); | 533 RemoveSocket(params_->socket_id); |
| 538 error_ = net::ErrorToString(result); | 534 error_ = net::ErrorToString(result); |
| 539 } | 535 } |
| 540 | 536 |
| 541 results_ = api::sockets_tcp::Secure::Results::Create(result); | 537 results_ = api::sockets_tcp::Secure::Results::Create(result); |
| 542 AsyncWorkCompleted(); | 538 AsyncWorkCompleted(); |
| 543 } | 539 } |
| 544 | 540 |
| 545 } // namespace api | 541 } // namespace api |
| 546 } // namespace extensions | 542 } // namespace extensions |
| OLD | NEW |