Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(890)

Unified Diff: chrome/browser/extensions/api/socket/socket_api.cc

Issue 10790137: Adds socket.getInfo to the socket API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Better comments, nit fix Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 93ba29c1e7fa20a0746b0c6d77cb653e6bb08c3d..f55f68de5eefc88037f3c66d593abefb4deab66c 100644
--- a/chrome/browser/extensions/api/socket/socket_api.cc
+++ b/chrome/browser/extensions/api/socket/socket_api.cc
@@ -447,4 +447,48 @@ void SocketSetNoDelayFunction::Work() {
SetResult(Value::CreateBooleanValue(result));
}
+SocketGetInfoFunction::SocketGetInfoFunction()
+ : params_(NULL) {}
+
+SocketGetInfoFunction::~SocketGetInfoFunction() {}
+
+bool SocketGetInfoFunction::Prepare() {
+ params_ = api::experimental_socket::GetInfo::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(params_.get());
+ return true;
+}
+
+void SocketGetInfoFunction::Work() {
+ api::experimental_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

Powered by Google App Engine
This is Rietveld 408576698