Index: chrome/browser/extensions/api/socket/socket_api.cc |
diff --git a/chrome/browser/extensions/api/socket/socket_api.cc b/chrome/browser/extensions/api/socket/socket_api.cc |
index b9bd48f47afc791144d1b1cf651c4283b5eec70b..951f60005d772158d1fd1507a95b2453c27103ae 100644 |
--- a/chrome/browser/extensions/api/socket/socket_api.cc |
+++ b/chrome/browser/extensions/api/socket/socket_api.cc |
@@ -453,4 +453,48 @@ void SocketSetNoDelayFunction::Work() { |
SetResult(Value::CreateBooleanValue(result)); |
} |
+SocketGetInfoFunction::SocketGetInfoFunction() |
+ : params_(NULL) {} |
+ |
+SocketGetInfoFunction::~SocketGetInfoFunction() {} |
+ |
+bool SocketGetInfoFunction::Prepare() { |
+ params_ = api::socket::GetInfo::Params::Create(*args_); |
+ EXTENSION_FUNCTION_VALIDATE(params_.get()); |
+ return true; |
+} |
+ |
+void SocketGetInfoFunction::Work() { |
+ api::socket::SocketInfo info; |
+ Socket* socket = manager_->Get(params_->socket_id); |
+ if (socket) { |
+ // This represents what we know about the socket, and does not call through |
+ // to the system. |
+ info.socket_type = (socket->IsTCPSocket() ? kTCPOption : kUDPOption); |
+ info.connected = socket->IsConnected(); |
+ |
+ // Grab the peer address as known by the OS. This and the call below will |
+ // always succeed while the socket is connected, even if the socket has |
+ // been remotely closed by the peer; only reading the socket will reveal |
+ // that it should be closed locally. |
+ net::IPEndPoint peerAddress; |
+ if (socket->GetPeerAddress(&peerAddress)) { |
+ info.peer_address.reset( |
+ new std::string(peerAddress.ToStringWithoutPort())); |
+ info.peer_port.reset(new int(peerAddress.port())); |
+ } |
+ |
+ // Grab the local address as known by the OS. |
+ net::IPEndPoint localAddress; |
+ if (socket->GetLocalAddress(&localAddress)) { |
+ info.local_address.reset( |
+ new std::string(localAddress.ToStringWithoutPort())); |
+ info.local_port.reset(new int(localAddress.port())); |
+ } |
+ } else { |
+ error_ = kSocketNotFoundError; |
+ } |
+ SetResult(info.ToValue().release()); |
+} |
+ |
} // namespace extensions |