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 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 error_ = kPermissionError; | 268 error_ = kPermissionError; |
| 269 SetResult(Value::CreateIntegerValue(result)); | 269 SetResult(Value::CreateIntegerValue(result)); |
| 270 return; | 270 return; |
| 271 } | 271 } |
| 272 } | 272 } |
| 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::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 Socket* socket = manager_->Get(extension_id(), params_->socket_id); | |
|
Peng
2012/09/12 19:49:49
GetSocket()?
justinlin
2012/09/13 08:21:12
Done.
| |
| 293 if (socket) { | |
| 294 result = socket->Listen(params_->backlog, &error_); | |
| 295 } else { | |
| 296 error_ = kSocketNotFoundError; | |
| 297 } | |
| 298 | |
| 299 SetResult(Value::CreateIntegerValue(result)); | |
| 300 } | |
| 301 | |
| 302 SocketAcceptFunction::SocketAcceptFunction() | |
| 303 : params_(NULL) { | |
| 304 } | |
| 305 | |
| 306 SocketAcceptFunction::~SocketAcceptFunction() {} | |
| 307 | |
| 308 bool SocketAcceptFunction::Prepare() { | |
| 309 params_ = api::socket::Accept::Params::Create(*args_); | |
| 310 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 311 return true; | |
| 312 } | |
| 313 | |
| 314 void SocketAcceptFunction::AsyncWorkStart() { | |
| 315 Socket* socket = manager_->Get(extension_id(), params_->socket_id); | |
|
Peng
2012/09/12 19:49:49
ditto
justinlin
2012/09/13 08:21:12
Done.
| |
| 316 if (socket) { | |
| 317 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); | |
| 318 } else { | |
| 319 error_ = kSocketNotFoundError; | |
| 320 OnAccept(-1, NULL); | |
| 321 } | |
| 322 } | |
| 323 | |
| 324 void SocketAcceptFunction::OnAccept(int result_code, | |
| 325 net::TCPClientSocket *socket) { | |
| 326 DCHECK(socket); | |
| 327 // TODO(justinlin): This socket won't have an event notifier, but it's not | |
| 328 // used for anything right now. | |
| 329 Socket *client_socket = new TCPSocket(socket, extension_id(), NULL, true); | |
| 330 | |
| 331 DictionaryValue* result = new DictionaryValue(); | |
| 332 result->SetInteger(kResultCodeKey, result_code); | |
| 333 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); | |
| 334 SetResult(result); | |
| 335 | |
| 336 AsyncWorkCompleted(); | |
| 337 } | |
| 338 | |
| 278 SocketReadFunction::SocketReadFunction() | 339 SocketReadFunction::SocketReadFunction() |
| 279 : params_(NULL) { | 340 : params_(NULL) { |
| 280 } | 341 } |
| 281 | 342 |
| 282 SocketReadFunction::~SocketReadFunction() {} | 343 SocketReadFunction::~SocketReadFunction() {} |
| 283 | 344 |
| 284 bool SocketReadFunction::Prepare() { | 345 bool SocketReadFunction::Prepare() { |
| 285 params_ = api::socket::Read::Params::Create(*args_); | 346 params_ = api::socket::Read::Params::Create(*args_); |
| 286 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 347 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 287 return true; | 348 return true; |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 612 info->name = i->name; | 673 info->name = i->name; |
| 613 info->address = net::IPAddressToString(i->address); | 674 info->address = net::IPAddressToString(i->address); |
| 614 create_arg.push_back(info); | 675 create_arg.push_back(info); |
| 615 } | 676 } |
| 616 | 677 |
| 617 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 678 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
| 618 SendResponse(true); | 679 SendResponse(true); |
| 619 } | 680 } |
| 620 | 681 |
| 621 } // namespace extensions | 682 } // namespace extensions |
| OLD | NEW |