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 10 matching lines...) Expand all Loading... | |
21 namespace extensions { | 21 namespace extensions { |
22 | 22 |
23 const char kAddressKey[] = "address"; | 23 const char kAddressKey[] = "address"; |
24 const char kPortKey[] = "port"; | 24 const char kPortKey[] = "port"; |
25 const char kBytesWrittenKey[] = "bytesWritten"; | 25 const char kBytesWrittenKey[] = "bytesWritten"; |
26 const char kDataKey[] = "data"; | 26 const char kDataKey[] = "data"; |
27 const char kResultCodeKey[] = "resultCode"; | 27 const char kResultCodeKey[] = "resultCode"; |
28 const char kSocketIdKey[] = "socketId"; | 28 const char kSocketIdKey[] = "socketId"; |
29 const char kTCPOption[] = "tcp"; | 29 const char kTCPOption[] = "tcp"; |
30 const char kUDPOption[] = "udp"; | 30 const char kUDPOption[] = "udp"; |
31 const char kSocketTypeKey[] = "socketType"; | |
32 const char kIsConnectedKey[] = "connected"; | |
33 const char kPeerAddressKey[] = "peerAddress"; | |
34 const char kPeerPortKey[] = "peerPort"; | |
35 const char kLocalAddressKey[] = "localAddress"; | |
36 const char kLocalPortKey[] = "localPort"; | |
31 | 37 |
32 const char kSocketNotFoundError[] = "Socket not found"; | 38 const char kSocketNotFoundError[] = "Socket not found"; |
33 const char kSocketTypeInvalidError[] = "Socket type is not supported"; | 39 const char kSocketTypeInvalidError[] = "Socket type is not supported"; |
34 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 40 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
35 | 41 |
36 SocketAsyncApiFunction::SocketAsyncApiFunction() | 42 SocketAsyncApiFunction::SocketAsyncApiFunction() |
37 : manager_(NULL) { | 43 : manager_(NULL) { |
38 } | 44 } |
39 | 45 |
40 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 46 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 void SocketSetNoDelayFunction::Work() { | 446 void SocketSetNoDelayFunction::Work() { |
441 bool result = false; | 447 bool result = false; |
442 Socket* socket = manager_->Get(params_->socket_id); | 448 Socket* socket = manager_->Get(params_->socket_id); |
443 if (socket) | 449 if (socket) |
444 result = socket->SetNoDelay(params_->no_delay); | 450 result = socket->SetNoDelay(params_->no_delay); |
445 else | 451 else |
446 error_ = kSocketNotFoundError; | 452 error_ = kSocketNotFoundError; |
447 SetResult(Value::CreateBooleanValue(result)); | 453 SetResult(Value::CreateBooleanValue(result)); |
448 } | 454 } |
449 | 455 |
456 SocketGetInfoFunction::SocketGetInfoFunction() | |
457 : params_(NULL) { | |
458 } | |
459 | |
460 SocketGetInfoFunction::~SocketGetInfoFunction() {} | |
461 | |
462 bool SocketGetInfoFunction::Prepare() { | |
463 params_ = api::experimental_socket::GetInfo::Params::Create(*args_); | |
464 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
465 return true; | |
466 } | |
467 | |
468 void SocketGetInfoFunction::Work() { | |
469 DictionaryValue* result = new DictionaryValue(); | |
470 Socket* socket = manager_->Get(params_->socket_id); | |
471 if (socket) { | |
472 std::string type = (socket->IsTCPSocket() ? kTCPOption : kUDPOption); | |
473 result->Set(kSocketTypeKey, Value::CreateStringValue(type)); | |
benwells
2012/07/26 04:18:37
I think there is a nicer way to set this return va
thorogood
2012/07/26 06:19:50
I've had a go at using the generated type. How I r
| |
474 result->Set(kIsConnectedKey, | |
475 Value::CreateBooleanValue(socket->IsConnected())); | |
476 | |
477 net::IPEndPoint peerAddress; | |
478 if (socket->GetPeerAddress(&peerAddress)) { | |
479 result->Set(kPeerAddressKey, | |
480 Value::CreateStringValue(peerAddress.ToStringWithoutPort())); | |
481 result->Set(kPeerPortKey, | |
482 Value::CreateIntegerValue(peerAddress.port())); | |
483 } | |
484 | |
485 net::IPEndPoint localAddress; | |
486 if (socket->GetLocalAddress(&localAddress)) { | |
487 result->Set(kLocalAddressKey, | |
488 Value::CreateStringValue(localAddress.ToStringWithoutPort())); | |
489 result->Set(kLocalPortKey, | |
490 Value::CreateIntegerValue(localAddress.port())); | |
491 } | |
492 } else { | |
493 error_ = kSocketNotFoundError; | |
494 } | |
495 SetResult(result); | |
496 } | |
497 | |
450 } // namespace extensions | 498 } // namespace extensions |
OLD | NEW |