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[] = "peer"; |
| 34 const char kLocalAddressKey[] = "local"; |
31 | 35 |
32 const char kSocketNotFoundError[] = "Socket not found"; | 36 const char kSocketNotFoundError[] = "Socket not found"; |
33 const char kSocketTypeInvalidError[] = "Socket type is not supported"; | 37 const char kSocketTypeInvalidError[] = "Socket type is not supported"; |
34 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 38 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
35 | 39 |
36 SocketAsyncApiFunction::SocketAsyncApiFunction() | 40 SocketAsyncApiFunction::SocketAsyncApiFunction() |
37 : manager_(NULL) { | 41 : manager_(NULL) { |
38 } | 42 } |
39 | 43 |
40 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 44 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 void SocketSetNoDelayFunction::Work() { | 443 void SocketSetNoDelayFunction::Work() { |
440 bool result = false; | 444 bool result = false; |
441 Socket* socket = manager_->Get(params_->socket_id); | 445 Socket* socket = manager_->Get(params_->socket_id); |
442 if (socket) | 446 if (socket) |
443 result = socket->SetNoDelay(params_->no_delay); | 447 result = socket->SetNoDelay(params_->no_delay); |
444 else | 448 else |
445 error_ = kSocketNotFoundError; | 449 error_ = kSocketNotFoundError; |
446 SetResult(Value::CreateBooleanValue(result)); | 450 SetResult(Value::CreateBooleanValue(result)); |
447 } | 451 } |
448 | 452 |
| 453 SocketGetInfoFunction::SocketGetInfoFunction() |
| 454 : params_(NULL) { |
| 455 } |
| 456 |
| 457 SocketGetInfoFunction::~SocketGetInfoFunction() {} |
| 458 |
| 459 bool SocketGetInfoFunction::Prepare() { |
| 460 params_ = api::experimental_socket::GetInfo::Params::Create(*args_); |
| 461 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 462 return true; |
| 463 } |
| 464 |
| 465 void SocketGetInfoFunction::Work() { |
| 466 DictionaryValue* result = new DictionaryValue(); |
| 467 Socket* socket = manager_->Get(params_->socket_id); |
| 468 if (socket) { |
| 469 std::string type = (socket->IsTCPSocket() ? kTCPOption : kUDPOption); |
| 470 result->Set(kSocketTypeKey, Value::CreateStringValue(type)); |
| 471 result->Set(kIsConnectedKey, |
| 472 Value::CreateBooleanValue(socket->IsConnected())); |
| 473 |
| 474 net::IPEndPoint peerAddress; |
| 475 if (socket->GetPeerAddress(&peerAddress)) |
| 476 result->Set(kPeerAddressKey, |
| 477 Value::CreateStringValue(peerAddress.ToString())); |
| 478 |
| 479 net::IPEndPoint localAddress; |
| 480 if (socket->GetLocalAddress(&localAddress)) |
| 481 result->Set(kLocalAddressKey, |
| 482 Value::CreateStringValue(localAddress.ToString())); |
| 483 |
| 484 } else { |
| 485 error_ = kSocketNotFoundError; |
| 486 } |
| 487 SetResult(result); |
| 488 } |
| 489 |
449 } // namespace extensions | 490 } // namespace extensions |
OLD | NEW |