Chromium Code Reviews| 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 108ab87cc4bf757fa3ece1bc497b2d24c0a88bf5..4fd664655419f166d9742474f767fc757dc5c292 100644 |
| --- a/chrome/browser/extensions/api/socket/socket_api.cc |
| +++ b/chrome/browser/extensions/api/socket/socket_api.cc |
| @@ -38,6 +38,10 @@ const char kPermissionError[] = "App 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."; |
| +const char kMulticastSocketTypeError[] = |
| + "Only UDP socket supports multicast."; |
| +const char kWildcardArress[] = "*"; |
| +const int kWildcardPortNumber = 0; |
| SocketAsyncApiFunction::SocketAsyncApiFunction() |
| : manager_(NULL) { |
| @@ -381,6 +385,7 @@ void SocketReadFunction::OnCompleted(int bytes_read, |
| result->Set(kDataKey, |
| base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
| bytes_read)); |
| + |
| } else { |
| result->Set(kDataKey, new base::BinaryValue()); |
| } |
| @@ -680,4 +685,209 @@ void SocketGetNetworkListFunction::SendResponseOnUIThread( |
| SendResponse(true); |
| } |
| +SocketJoinGroupFunction::SocketJoinGroupFunction() |
| + : params_(NULL) {} |
|
mmenke
2013/04/12 21:07:41
4 space indent. Comment applies to whole file.
Bei Zhang
2013/04/15 22:30:26
Done.
|
| + |
| +SocketJoinGroupFunction::~SocketJoinGroupFunction() {} |
| + |
| +bool SocketJoinGroupFunction::Prepare() { |
| + params_ = api::socket::JoinGroup::Params::Create(*args_); |
| + EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| + return true; |
| +} |
| + |
| +void SocketJoinGroupFunction::Work() { |
| + int result = -1; |
| + Socket* socket = GetSocket(params_->socket_id); |
| + if (!socket) { |
| + error_ = kSocketNotFoundError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + |
| + if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| + SocketPermission::CheckParam param( |
| + SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP, |
| + kWildcardArress, |
| + kWildcardPortNumber); |
| + if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| + ¶m)) { |
|
mmenke
2013/04/12 21:07:41
Should either be aligned with APIPermission::kSock
Bei Zhang
2013/04/15 22:30:26
Done.
|
| + error_ = kPermissionError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + } else { |
| + error_ = kMulticastSocketTypeError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + |
| + result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address); |
| + if (result != 0) { |
| + error_ = net::ErrorToString(result); |
| + } |
| + SetResult(Value::CreateIntegerValue(result)); |
| +} |
| + |
| + |
| +SocketLeaveGroupFunction::SocketLeaveGroupFunction() |
| + : params_(NULL) {} |
| + |
| +SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} |
| + |
| +bool SocketLeaveGroupFunction::Prepare() { |
| + params_ = api::socket::LeaveGroup::Params::Create(*args_); |
| + EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| + return true; |
| +} |
| + |
| +void SocketLeaveGroupFunction::Work() { |
| + int result = -1; |
| + Socket* socket = GetSocket(params_->socket_id); |
| + |
| + if (!socket) { |
| + error_ = kSocketNotFoundError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + |
| + if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| + SocketPermission::CheckParam param( |
| + SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP, |
| + kWildcardArress, |
| + kWildcardPortNumber); |
| + if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| + ¶m)) { |
| + error_ = kPermissionError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + } else { |
| + error_ = kMulticastSocketTypeError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + |
| + result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address); |
| + if (result != 0) { |
| + error_ = net::ErrorToString(result); |
| + } |
| + SetResult(Value::CreateIntegerValue(result)); |
| +} |
| + |
| +SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() |
| + : params_(NULL) {} |
| + |
| +SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {} |
| + |
| +bool SocketSetMulticastTimeToLiveFunction::Prepare() { |
| + params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_); |
| + EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| + return true; |
| +} |
| +void SocketSetMulticastTimeToLiveFunction::Work() { |
| + int result = -1; |
| + Socket* socket = GetSocket(params_->socket_id); |
| + if (!socket) { |
| + error_ = kSocketNotFoundError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + |
| + if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| + result = static_cast<UDPSocket*>(socket)-> |
| + SetMulticastTimeToLive(params_->ttl); |
| + if (result != 0) { |
| + error_ = net::ErrorToString(result); |
| + } |
| + SetResult(Value::CreateIntegerValue(result)); |
| + } else { |
| + error_ = kMulticastSocketTypeError; |
| + } |
| + SetResult(Value::CreateIntegerValue(result)); |
| +} |
| + |
| +SocketSetMulticastLoopbackModeFunction::SocketSetMulticastLoopbackModeFunction() |
| + : params_(NULL) {} |
| + |
| +SocketSetMulticastLoopbackModeFunction:: |
| + ~SocketSetMulticastLoopbackModeFunction() {} |
| + |
| +bool SocketSetMulticastLoopbackModeFunction::Prepare() { |
| + params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_); |
| + EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| + return true; |
| +} |
| + |
| +void SocketSetMulticastLoopbackModeFunction::Work() { |
| + int result = -1; |
| + Socket* socket = GetSocket(params_->socket_id); |
| + if (!socket) { |
| + error_ = kSocketNotFoundError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + |
| + if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| + result = static_cast<UDPSocket*>(socket)-> |
| + SetMulticastLoopbackMode(params_->enabled); |
| + if (result != 0) { |
| + error_ = net::ErrorToString(result); |
| + } |
| + } else { |
| + error_ = kMulticastSocketTypeError; |
| + } |
| + SetResult(Value::CreateIntegerValue(result)); |
| +} |
| + |
| +SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() |
| + : params_(NULL) {} |
| + |
| +SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {} |
| + |
| +bool SocketGetJoinedGroupsFunction::Prepare() { |
| + params_ = api::socket::GetJoinedGroups::Params::Create(*args_); |
| + EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| + return true; |
| +} |
| + |
| +void SocketGetJoinedGroupsFunction::Work() { |
| + int result = -1; |
| + Socket* socket = GetSocket(params_->socket_id); |
| + if (!socket) { |
| + error_ = kSocketNotFoundError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + |
| + if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| + SocketPermission::CheckParam param( |
| + SocketPermissionRequest::UDP_SEND_TO, |
| + kWildcardArress, |
| + kWildcardPortNumber); |
| + if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| + ¶m)) { |
| + error_ = kPermissionError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + } else { |
| + error_ = kMulticastSocketTypeError; |
| + SetResult(Value::CreateIntegerValue(result)); |
| + return; |
| + } |
| + |
| + base::hash_set<std::string> groups; |
| + result = static_cast<UDPSocket*>(socket)-> |
| + GetJoinedGroups(groups); |
| + if (result != 0) { |
| + error_ = net::ErrorToString(result); |
| + } |
| + base::ListValue* list = new base::ListValue(); |
| + for (base::hash_set<std::string>::iterator it = groups.begin(); |
| + it != groups.end(); ++it) { |
| + list->AppendString(*it); |
| + } |
| + SetResult(list); |
| +} |
| } // namespace extensions |
|
mmenke
2013/04/12 21:07:41
Line break before end of namespace.
Bei Zhang
2013/04/15 22:30:26
Done.
|