| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/socket/socket_api.h" | 5 #include "chrome/browser/extensions/api/socket/socket_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/common/extensions/permissions/socket_permission.h" | 8 #include "chrome/common/extensions/permissions/socket_permission.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 const char kSocketIdKey[] = "socketId"; | 30 const char kSocketIdKey[] = "socketId"; |
| 31 const char kTCPOption[] = "tcp"; | 31 const char kTCPOption[] = "tcp"; |
| 32 const char kUDPOption[] = "udp"; | 32 const char kUDPOption[] = "udp"; |
| 33 const char kUnknown[] = "unknown"; | 33 const char kUnknown[] = "unknown"; |
| 34 | 34 |
| 35 const char kSocketNotFoundError[] = "Socket not found"; | 35 const char kSocketNotFoundError[] = "Socket not found"; |
| 36 const char kSocketTypeInvalidError[] = "Socket type is not supported"; | 36 const char kSocketTypeInvalidError[] = "Socket type is not supported"; |
| 37 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 37 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
| 38 const char kPermissionError[] = "Caller does not have permission"; | 38 const char kPermissionError[] = "Caller does not have permission"; |
| 39 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 39 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
| 40 const char kTCPSocketBindError[] = |
| 41 "TCP socket does not support bind. For TCP server please use listen."; |
| 40 | 42 |
| 41 SocketAsyncApiFunction::SocketAsyncApiFunction() | 43 SocketAsyncApiFunction::SocketAsyncApiFunction() |
| 42 : manager_(NULL) { | 44 : manager_(NULL) { |
| 43 } | 45 } |
| 44 | 46 |
| 45 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 47 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
| 46 } | 48 } |
| 47 | 49 |
| 48 bool SocketAsyncApiFunction::PrePrepare() { | 50 bool SocketAsyncApiFunction::PrePrepare() { |
| 49 manager_ = ExtensionSystem::Get(profile())->socket_manager(); | 51 manager_ = ExtensionSystem::Get(profile())->socket_manager(); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 264 |
| 263 if (socket->GetSocketType() == Socket::TYPE_UDP) { | 265 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 264 SocketPermission::CheckParam param( | 266 SocketPermission::CheckParam param( |
| 265 SocketPermissionData::UDP_BIND, address_, port_); | 267 SocketPermissionData::UDP_BIND, address_, port_); |
| 266 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | 268 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 267 ¶m)) { | 269 ¶m)) { |
| 268 error_ = kPermissionError; | 270 error_ = kPermissionError; |
| 269 SetResult(Value::CreateIntegerValue(result)); | 271 SetResult(Value::CreateIntegerValue(result)); |
| 270 return; | 272 return; |
| 271 } | 273 } |
| 274 } else if (socket->GetSocketType() == Socket::TYPE_TCP) { |
| 275 error_ = kTCPSocketBindError; |
| 276 SetResult(Value::CreateIntegerValue(result)); |
| 277 return; |
| 272 } | 278 } |
| 273 | 279 |
| 274 result = socket->Bind(address_, port_); | 280 result = socket->Bind(address_, port_); |
| 275 SetResult(Value::CreateIntegerValue(result)); | 281 SetResult(Value::CreateIntegerValue(result)); |
| 276 } | 282 } |
| 277 | 283 |
| 284 SocketListenFunction::SocketListenFunction() |
| 285 : params_(NULL) { |
| 286 } |
| 287 |
| 288 SocketListenFunction::~SocketListenFunction() {} |
| 289 |
| 290 bool SocketListenFunction::Prepare() { |
| 291 params_ = api::experimental_socket::Listen::Params::Create(*args_); |
| 292 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 293 return true; |
| 294 } |
| 295 |
| 296 void SocketListenFunction::Work() { |
| 297 int result = -1; |
| 298 net::IPEndPoint bindAddress; |
| 299 |
| 300 Socket* socket = GetSocket(params_->socket_id); |
| 301 if (socket) { |
| 302 SocketPermission::CheckParam param( |
| 303 SocketPermissionData::TCP_LISTEN, params_->address, params_->port); |
| 304 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 305 ¶m)) { |
| 306 error_ = kPermissionError; |
| 307 SetResult(Value::CreateIntegerValue(result)); |
| 308 return; |
| 309 } |
| 310 |
| 311 result = socket->Listen(params_->address, params_->port, params_->backlog, |
| 312 &error_); |
| 313 } else { |
| 314 error_ = kSocketNotFoundError; |
| 315 } |
| 316 |
| 317 SetResult(Value::CreateIntegerValue(result)); |
| 318 } |
| 319 |
| 320 SocketAcceptFunction::SocketAcceptFunction() |
| 321 : params_(NULL) { |
| 322 } |
| 323 |
| 324 SocketAcceptFunction::~SocketAcceptFunction() {} |
| 325 |
| 326 bool SocketAcceptFunction::Prepare() { |
| 327 params_ = api::experimental_socket::Accept::Params::Create(*args_); |
| 328 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 329 return true; |
| 330 } |
| 331 |
| 332 void SocketAcceptFunction::AsyncWorkStart() { |
| 333 Socket* socket = GetSocket(params_->socket_id); |
| 334 if (socket) { |
| 335 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); |
| 336 } else { |
| 337 error_ = kSocketNotFoundError; |
| 338 OnAccept(-1, NULL); |
| 339 } |
| 340 } |
| 341 |
| 342 void SocketAcceptFunction::OnAccept(int result_code, |
| 343 net::TCPClientSocket *socket) { |
| 344 DCHECK(socket); |
| 345 // TODO(justinlin): This socket won't have an event notifier, but it's not |
| 346 // used for anything right now. |
| 347 Socket *client_socket = new TCPSocket(socket, extension_id(), NULL, true); |
| 348 |
| 349 DictionaryValue* result = new DictionaryValue(); |
| 350 result->SetInteger(kResultCodeKey, result_code); |
| 351 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); |
| 352 SetResult(result); |
| 353 |
| 354 AsyncWorkCompleted(); |
| 355 } |
| 356 |
| 278 SocketReadFunction::SocketReadFunction() | 357 SocketReadFunction::SocketReadFunction() |
| 279 : params_(NULL) { | 358 : params_(NULL) { |
| 280 } | 359 } |
| 281 | 360 |
| 282 SocketReadFunction::~SocketReadFunction() {} | 361 SocketReadFunction::~SocketReadFunction() {} |
| 283 | 362 |
| 284 bool SocketReadFunction::Prepare() { | 363 bool SocketReadFunction::Prepare() { |
| 285 params_ = api::socket::Read::Params::Create(*args_); | 364 params_ = api::socket::Read::Params::Create(*args_); |
| 286 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 365 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 287 return true; | 366 return true; |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 info->name = i->name; | 691 info->name = i->name; |
| 613 info->address = net::IPAddressToString(i->address); | 692 info->address = net::IPAddressToString(i->address); |
| 614 create_arg.push_back(info); | 693 create_arg.push_back(info); |
| 615 } | 694 } |
| 616 | 695 |
| 617 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 696 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
| 618 SendResponse(true); | 697 SendResponse(true); |
| 619 } | 698 } |
| 620 | 699 |
| 621 } // namespace extensions | 700 } // namespace extensions |
| OLD | NEW |