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

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

Issue 10827390: Implement chrome.socket.bind/listen/accept for TCP server socket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add error if trying to bind TCP socket, remove TCP client socket bind. Created 8 years, 3 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 ce400e52555c92c8b066844f108681bec9f28690..342664e2db93baaa872a0a599d47aac7e4979f06 100644
--- a/chrome/browser/extensions/api/socket/socket_api.cc
+++ b/chrome/browser/extensions/api/socket/socket_api.cc
@@ -37,6 +37,8 @@ const char kSocketTypeInvalidError[] = "Socket type is not supported";
const char kDnsLookupFailedError[] = "DNS resolution failed";
const char kPermissionError[] = "Caller does not have permission";
const char kNetworkListError[] = "Network lookup failed or unsupported";
+const char kTCPSocketBindError[] =
+ "TCP socket does not support bind. For TCP server please use listen.";
SocketAsyncApiFunction::SocketAsyncApiFunction()
: manager_(NULL) {
@@ -269,12 +271,89 @@ void SocketBindFunction::Work() {
SetResult(Value::CreateIntegerValue(result));
return;
}
+ } else if (socket->GetSocketType() == Socket::TYPE_TCP) {
+ error_ = kTCPSocketBindError;
+ SetResult(Value::CreateIntegerValue(result));
+ return;
}
result = socket->Bind(address_, port_);
SetResult(Value::CreateIntegerValue(result));
}
+SocketListenFunction::SocketListenFunction()
+ : params_(NULL) {
+}
+
+SocketListenFunction::~SocketListenFunction() {}
+
+bool SocketListenFunction::Prepare() {
+ params_ = api::experimental_socket::Listen::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(params_.get());
+ return true;
+}
+
+void SocketListenFunction::Work() {
+ int result = -1;
+ net::IPEndPoint bindAddress;
+
+ Socket* socket = GetSocket(params_->socket_id);
+ if (socket) {
+ SocketPermission::CheckParam param(
+ SocketPermissionData::TCP_LISTEN, params_->address, params_->port);
+ if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket,
+ &param)) {
+ error_ = kPermissionError;
+ SetResult(Value::CreateIntegerValue(result));
+ return;
+ }
+
+ result = socket->Listen(params_->address, params_->port, params_->backlog,
+ &error_);
+ } else {
+ error_ = kSocketNotFoundError;
+ }
+
+ SetResult(Value::CreateIntegerValue(result));
+}
+
+SocketAcceptFunction::SocketAcceptFunction()
+ : params_(NULL) {
+}
+
+SocketAcceptFunction::~SocketAcceptFunction() {}
+
+bool SocketAcceptFunction::Prepare() {
+ params_ = api::experimental_socket::Accept::Params::Create(*args_);
+ EXTENSION_FUNCTION_VALIDATE(params_.get());
+ return true;
+}
+
+void SocketAcceptFunction::AsyncWorkStart() {
+ Socket* socket = GetSocket(params_->socket_id);
+ if (socket) {
+ socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this));
+ } else {
+ error_ = kSocketNotFoundError;
+ OnAccept(-1, NULL);
+ }
+}
+
+void SocketAcceptFunction::OnAccept(int result_code,
+ net::TCPClientSocket *socket) {
+ DCHECK(socket);
+ // TODO(justinlin): This socket won't have an event notifier, but it's not
+ // used for anything right now.
+ Socket *client_socket = new TCPSocket(socket, extension_id(), NULL, true);
+
+ DictionaryValue* result = new DictionaryValue();
+ result->SetInteger(kResultCodeKey, result_code);
+ result->SetInteger(kSocketIdKey, manager_->Add(client_socket));
+ SetResult(result);
+
+ AsyncWorkCompleted();
+}
+
SocketReadFunction::SocketReadFunction()
: params_(NULL) {
}

Powered by Google App Engine
This is Rietveld 408576698