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

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: Broke apart local/peer into localAddress,localPort peerAddress,peerPort, and updated the docs 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..8a12fd1e84a656c8bfa0b034ab0209231677f68c 100644
--- a/chrome/browser/extensions/api/socket/socket_api.cc
+++ b/chrome/browser/extensions/api/socket/socket_api.cc
@@ -28,6 +28,12 @@ const char kResultCodeKey[] = "resultCode";
const char kSocketIdKey[] = "socketId";
const char kTCPOption[] = "tcp";
const char kUDPOption[] = "udp";
+const char kSocketTypeKey[] = "socketType";
+const char kIsConnectedKey[] = "connected";
+const char kPeerAddressKey[] = "peerAddress";
+const char kPeerPortKey[] = "peerPort";
+const char kLocalAddressKey[] = "localAddress";
+const char kLocalPortKey[] = "localPort";
const char kSocketNotFoundError[] = "Socket not found";
const char kSocketTypeInvalidError[] = "Socket type is not supported";
@@ -447,4 +453,46 @@ 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() {
+ DictionaryValue* result = new DictionaryValue();
+ Socket* socket = manager_->Get(params_->socket_id);
+ if (socket) {
+ std::string type = (socket->IsTCPSocket() ? kTCPOption : kUDPOption);
+ result->Set(kSocketTypeKey, Value::CreateStringValue(type));
benwells 2012/07/26 04:18:37 I think there is a nicer way to set this return va
thorogood 2012/07/26 06:19:50 I've had a go at using the generated type. How I r
+ result->Set(kIsConnectedKey,
+ Value::CreateBooleanValue(socket->IsConnected()));
+
+ net::IPEndPoint peerAddress;
+ if (socket->GetPeerAddress(&peerAddress)) {
+ result->Set(kPeerAddressKey,
+ Value::CreateStringValue(peerAddress.ToStringWithoutPort()));
+ result->Set(kPeerPortKey,
+ Value::CreateIntegerValue(peerAddress.port()));
+ }
+
+ net::IPEndPoint localAddress;
+ if (socket->GetLocalAddress(&localAddress)) {
+ result->Set(kLocalAddressKey,
+ Value::CreateStringValue(localAddress.ToStringWithoutPort()));
+ result->Set(kLocalPortKey,
+ Value::CreateIntegerValue(localAddress.port()));
+ }
+ } else {
+ error_ = kSocketNotFoundError;
+ }
+ SetResult(result);
+}
+
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698