Chromium Code Reviews| 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 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 | 262 |
| 263 if (socket->GetSocketType() == Socket::TYPE_UDP) { | 263 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 264 SocketPermission::CheckParam param( | 264 SocketPermission::CheckParam param( |
| 265 SocketPermissionData::UDP_BIND, address_, port_); | 265 SocketPermissionData::UDP_BIND, address_, port_); |
| 266 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | 266 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 267 ¶m)) { | 267 ¶m)) { |
| 268 error_ = kPermissionError; | 268 error_ = kPermissionError; |
| 269 SetResult(Value::CreateIntegerValue(result)); | 269 SetResult(Value::CreateIntegerValue(result)); |
| 270 return; | 270 return; |
| 271 } | 271 } |
| 272 } | 272 } |
|
Peng
2012/09/17 15:02:48
As you implemented bind() for TCP socket. TCPSocke
justinlin
2012/09/20 23:11:23
Done.
| |
| 273 | 273 |
| 274 result = socket->Bind(address_, port_); | 274 result = socket->Bind(address_, port_); |
| 275 SetResult(Value::CreateIntegerValue(result)); | 275 SetResult(Value::CreateIntegerValue(result)); |
| 276 } | 276 } |
| 277 | 277 |
| 278 SocketListenFunction::SocketListenFunction() | |
| 279 : params_(NULL) { | |
| 280 } | |
| 281 | |
| 282 SocketListenFunction::~SocketListenFunction() {} | |
| 283 | |
| 284 bool SocketListenFunction::Prepare() { | |
| 285 params_ = api::experimental_socket::Listen::Params::Create(*args_); | |
| 286 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 287 return true; | |
| 288 } | |
| 289 | |
| 290 void SocketListenFunction::Work() { | |
| 291 int result = -1; | |
| 292 net::IPEndPoint bindAddress; | |
| 293 | |
| 294 Socket* socket = GetSocket(params_->socket_id); | |
| 295 if (socket) { | |
| 296 SocketPermission::CheckParam param( | |
| 297 SocketPermissionData::TCP_LISTEN, params_->address, params_->port); | |
| 298 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, | |
| 299 ¶m)) { | |
| 300 error_ = kPermissionError; | |
| 301 SetResult(Value::CreateIntegerValue(result)); | |
| 302 return; | |
| 303 } | |
| 304 | |
| 305 result = socket->Listen(params_->address, params_->port, params_->backlog, | |
| 306 &error_); | |
| 307 } else { | |
| 308 error_ = kSocketNotFoundError; | |
| 309 } | |
| 310 | |
| 311 SetResult(Value::CreateIntegerValue(result)); | |
| 312 } | |
| 313 | |
| 314 SocketAcceptFunction::SocketAcceptFunction() | |
| 315 : params_(NULL) { | |
| 316 } | |
| 317 | |
| 318 SocketAcceptFunction::~SocketAcceptFunction() {} | |
| 319 | |
| 320 bool SocketAcceptFunction::Prepare() { | |
| 321 params_ = api::experimental_socket::Accept::Params::Create(*args_); | |
| 322 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 323 return true; | |
| 324 } | |
| 325 | |
| 326 void SocketAcceptFunction::AsyncWorkStart() { | |
| 327 Socket* socket = GetSocket(params_->socket_id); | |
| 328 if (socket) { | |
| 329 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); | |
| 330 } else { | |
| 331 error_ = kSocketNotFoundError; | |
| 332 OnAccept(-1, NULL); | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 void SocketAcceptFunction::OnAccept(int result_code, | |
| 337 net::TCPClientSocket *socket) { | |
| 338 DCHECK(socket); | |
| 339 // TODO(justinlin): This socket won't have an event notifier, but it's not | |
| 340 // used for anything right now. | |
| 341 Socket *client_socket = new TCPSocket(socket, extension_id(), NULL, true); | |
| 342 | |
| 343 DictionaryValue* result = new DictionaryValue(); | |
| 344 result->SetInteger(kResultCodeKey, result_code); | |
| 345 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); | |
| 346 SetResult(result); | |
| 347 | |
| 348 AsyncWorkCompleted(); | |
| 349 } | |
| 350 | |
| 278 SocketReadFunction::SocketReadFunction() | 351 SocketReadFunction::SocketReadFunction() |
| 279 : params_(NULL) { | 352 : params_(NULL) { |
| 280 } | 353 } |
| 281 | 354 |
| 282 SocketReadFunction::~SocketReadFunction() {} | 355 SocketReadFunction::~SocketReadFunction() {} |
| 283 | 356 |
| 284 bool SocketReadFunction::Prepare() { | 357 bool SocketReadFunction::Prepare() { |
| 285 params_ = api::socket::Read::Params::Create(*args_); | 358 params_ = api::socket::Read::Params::Create(*args_); |
| 286 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 359 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 287 return true; | 360 return true; |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 612 info->name = i->name; | 685 info->name = i->name; |
| 613 info->address = net::IPAddressToString(i->address); | 686 info->address = net::IPAddressToString(i->address); |
| 614 create_arg.push_back(info); | 687 create_arg.push_back(info); |
| 615 } | 688 } |
| 616 | 689 |
| 617 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 690 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
| 618 SendResponse(true); | 691 SendResponse(true); |
| 619 } | 692 } |
| 620 | 693 |
| 621 } // namespace extensions | 694 } // namespace extensions |
| OLD | NEW |