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/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 9 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
10 #include "chrome/browser/extensions/api/socket/socket.h" | 10 #include "chrome/browser/extensions/api/socket/socket.h" |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 int result = -1; | 217 int result = -1; |
218 Socket* socket = manager_->Get(socket_id_); | 218 Socket* socket = manager_->Get(socket_id_); |
219 if (socket) | 219 if (socket) |
220 result = socket->Bind(address_, port_); | 220 result = socket->Bind(address_, port_); |
221 else | 221 else |
222 error_ = kSocketNotFoundError; | 222 error_ = kSocketNotFoundError; |
223 | 223 |
224 SetResult(Value::CreateIntegerValue(result)); | 224 SetResult(Value::CreateIntegerValue(result)); |
225 } | 225 } |
226 | 226 |
227 bool SocketListenFunction::Prepare() { | |
228 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | |
miket_OOO
2012/08/31 23:18:01
You can do an EXTENSION_FUNCTION_VALIDATE(params_.
justinlin
2012/09/11 04:32:32
Done.
| |
229 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &backlog_)); | |
230 return true; | |
231 } | |
232 | |
233 void SocketListenFunction::Work() { | |
234 int result = -1; | |
235 Socket* socket = manager_->Get(socket_id_); | |
236 if (socket) | |
237 result = socket->Listen(backlog_); | |
238 else | |
239 error_ = kSocketNotFoundError; | |
240 | |
241 SetResult(Value::CreateIntegerValue(result)); | |
242 } | |
243 | |
244 SocketAcceptFunction::SocketAcceptFunction() | |
245 : params_(NULL), | |
246 event_notifier_(NULL), | |
247 src_id_(-1) { | |
248 | |
249 } | |
miket_OOO
2012/08/31 23:18:01
vertical whitespace
justinlin
2012/09/11 04:32:32
Done.
| |
250 SocketAcceptFunction::~SocketAcceptFunction() {} | |
251 | |
252 bool SocketAcceptFunction::Prepare() { | |
253 params_ = api::socket::Accept::Params::Create(*args_); | |
254 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
255 | |
256 event_notifier_ = CreateEventNotifier(src_id_); | |
257 return true; | |
258 } | |
259 | |
260 void SocketAcceptFunction::Work() { | |
261 Socket* socket = manager_->Get(params_->socket_id); | |
262 if (socket) { | |
263 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); | |
264 } else { | |
265 error_ = kSocketNotFoundError; | |
266 } | |
267 } | |
268 | |
269 void SocketAcceptFunction::OnAccept(int result_code, | |
270 net::TCPClientSocket *socket) { | |
271 DCHECK(socket); | |
272 Socket *client_socket = new TCPSocket(event_notifier_, socket); | |
273 | |
274 DictionaryValue* result = new DictionaryValue(); | |
275 result->SetInteger(kResultCodeKey, result_code); | |
276 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); | |
miket_OOO
2012/08/31 23:18:01
You might want to add a note to http://code.google
justinlin
2012/09/11 04:32:32
I'll keep an eye out for that issue. I'm not entir
| |
277 SetResult(result); | |
278 } | |
279 | |
227 SocketReadFunction::SocketReadFunction() | 280 SocketReadFunction::SocketReadFunction() |
228 : params_(NULL) { | 281 : params_(NULL) { |
229 } | 282 } |
230 | 283 |
231 SocketReadFunction::~SocketReadFunction() {} | 284 SocketReadFunction::~SocketReadFunction() {} |
232 | 285 |
233 bool SocketReadFunction::Prepare() { | 286 bool SocketReadFunction::Prepare() { |
234 params_ = api::socket::Read::Params::Create(*args_); | 287 params_ = api::socket::Read::Params::Create(*args_); |
235 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 288 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
236 return true; | 289 return true; |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
491 new std::string(localAddress.ToStringWithoutPort())); | 544 new std::string(localAddress.ToStringWithoutPort())); |
492 info.local_port.reset(new int(localAddress.port())); | 545 info.local_port.reset(new int(localAddress.port())); |
493 } | 546 } |
494 } else { | 547 } else { |
495 error_ = kSocketNotFoundError; | 548 error_ = kSocketNotFoundError; |
496 } | 549 } |
497 SetResult(info.ToValue().release()); | 550 SetResult(info.ToValue().release()); |
498 } | 551 } |
499 | 552 |
500 } // namespace extensions | 553 } // namespace extensions |
OLD | NEW |