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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 result->SetInteger(kSocketIdKey, manager_->Add(socket)); | 140 result->SetInteger(kSocketIdKey, manager_->Add(socket)); |
| 141 SetResult(result); | 141 SetResult(result); |
| 142 } | 142 } |
| 143 | 143 |
| 144 bool SocketDestroyFunction::Prepare() { | 144 bool SocketDestroyFunction::Prepare() { |
| 145 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 145 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 146 return true; | 146 return true; |
| 147 } | 147 } |
| 148 | 148 |
| 149 void SocketDestroyFunction::Work() { | 149 void SocketDestroyFunction::Work() { |
| 150 manager_->Remove(socket_id_); | 150 manager_->Remove(socket_id_); |
|
Peng
2012/09/11 14:01:18
Could we check if the listen port is release after
justinlin
2012/09/12 07:29:42
I think the only way is to bind() again, but there
Peng
2012/09/12 19:49:49
That's fine. I think we could file an issue to rem
| |
| 151 } | 151 } |
| 152 | 152 |
| 153 SocketConnectFunction::SocketConnectFunction() | 153 SocketConnectFunction::SocketConnectFunction() |
| 154 : socket_id_(0), | 154 : socket_id_(0), |
| 155 port_(0) { | 155 port_(0) { |
| 156 } | 156 } |
| 157 | 157 |
| 158 SocketConnectFunction::~SocketConnectFunction() { | 158 SocketConnectFunction::~SocketConnectFunction() { |
| 159 } | 159 } |
| 160 | 160 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 error_ = kPermissionError; | 258 error_ = kPermissionError; |
| 259 SetResult(Value::CreateIntegerValue(result)); | 259 SetResult(Value::CreateIntegerValue(result)); |
| 260 return; | 260 return; |
| 261 } | 261 } |
| 262 } | 262 } |
| 263 | 263 |
| 264 result = socket->Bind(address_, port_); | 264 result = socket->Bind(address_, port_); |
| 265 SetResult(Value::CreateIntegerValue(result)); | 265 SetResult(Value::CreateIntegerValue(result)); |
| 266 } | 266 } |
| 267 | 267 |
| 268 SocketListenFunction::SocketListenFunction() | |
| 269 : params_(NULL) { | |
| 270 } | |
| 271 | |
| 272 SocketListenFunction::~SocketListenFunction() {} | |
| 273 | |
| 274 bool SocketListenFunction::Prepare() { | |
| 275 params_ = api::socket::Listen::Params::Create(*args_); | |
| 276 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 277 return true; | |
| 278 } | |
| 279 | |
| 280 void SocketListenFunction::Work() { | |
| 281 int result = -1; | |
| 282 Socket* socket = manager_->Get(params_->socket_id); | |
| 283 if (socket) { | |
| 284 result = socket->Listen(params_->backlog, error_); | |
| 285 } else { | |
| 286 error_ = kSocketNotFoundError; | |
| 287 } | |
| 288 | |
| 289 SetResult(Value::CreateIntegerValue(result)); | |
| 290 } | |
| 291 | |
| 292 SocketAcceptFunction::SocketAcceptFunction() | |
| 293 : params_(NULL) { | |
| 294 } | |
| 295 | |
| 296 SocketAcceptFunction::~SocketAcceptFunction() {} | |
| 297 | |
| 298 bool SocketAcceptFunction::Prepare() { | |
| 299 params_ = api::socket::Accept::Params::Create(*args_); | |
| 300 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 301 return true; | |
| 302 } | |
| 303 | |
| 304 void SocketAcceptFunction::AsyncWorkStart() { | |
| 305 Socket* socket = manager_->Get(params_->socket_id); | |
| 306 if (socket) { | |
| 307 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); | |
| 308 } else { | |
| 309 error_ = kSocketNotFoundError; | |
| 310 OnAccept(-1, NULL); | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 void SocketAcceptFunction::OnAccept(int result_code, | |
| 315 net::TCPClientSocket *socket) { | |
| 316 DCHECK(socket); | |
| 317 // TODO(justinlin): This socket won't have an event notifier, but it's not | |
| 318 // used for anything right now. | |
| 319 Socket *client_socket = new TCPSocket(socket, NULL, true); | |
| 320 | |
| 321 DictionaryValue* result = new DictionaryValue(); | |
| 322 result->SetInteger(kResultCodeKey, result_code); | |
| 323 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); | |
| 324 SetResult(result); | |
| 325 | |
| 326 AsyncWorkCompleted(); | |
| 327 } | |
| 328 | |
| 268 SocketReadFunction::SocketReadFunction() | 329 SocketReadFunction::SocketReadFunction() |
| 269 : params_(NULL) { | 330 : params_(NULL) { |
| 270 } | 331 } |
| 271 | 332 |
| 272 SocketReadFunction::~SocketReadFunction() {} | 333 SocketReadFunction::~SocketReadFunction() {} |
| 273 | 334 |
| 274 bool SocketReadFunction::Prepare() { | 335 bool SocketReadFunction::Prepare() { |
| 275 params_ = api::socket::Read::Params::Create(*args_); | 336 params_ = api::socket::Read::Params::Create(*args_); |
| 276 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 337 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 277 return true; | 338 return true; |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 new std::string(localAddress.ToStringWithoutPort())); | 617 new std::string(localAddress.ToStringWithoutPort())); |
| 557 info.local_port.reset(new int(localAddress.port())); | 618 info.local_port.reset(new int(localAddress.port())); |
| 558 } | 619 } |
| 559 } else { | 620 } else { |
| 560 error_ = kSocketNotFoundError; | 621 error_ = kSocketNotFoundError; |
| 561 } | 622 } |
| 562 SetResult(info.ToValue().release()); | 623 SetResult(info.ToValue().release()); |
| 563 } | 624 } |
| 564 | 625 |
| 565 } // namespace extensions | 626 } // namespace extensions |
| OLD | NEW |