| 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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 | 609 |
| 610 SocketGetInfoFunction::~SocketGetInfoFunction() {} | 610 SocketGetInfoFunction::~SocketGetInfoFunction() {} |
| 611 | 611 |
| 612 bool SocketGetInfoFunction::Prepare() { | 612 bool SocketGetInfoFunction::Prepare() { |
| 613 params_ = api::socket::GetInfo::Params::Create(*args_); | 613 params_ = api::socket::GetInfo::Params::Create(*args_); |
| 614 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 614 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 615 return true; | 615 return true; |
| 616 } | 616 } |
| 617 | 617 |
| 618 void SocketGetInfoFunction::Work() { | 618 void SocketGetInfoFunction::Work() { |
| 619 Socket* socket = GetSocket(params_->socket_id); |
| 620 if (!socket) { |
| 621 error_ = kSocketNotFoundError; |
| 622 return; |
| 623 } |
| 624 |
| 619 api::socket::SocketInfo info; | 625 api::socket::SocketInfo info; |
| 620 Socket* socket = GetSocket(params_->socket_id); | 626 // This represents what we know about the socket, and does not call through |
| 621 if (socket) { | 627 // to the system. |
| 622 // This represents what we know about the socket, and does not call through | 628 if (socket->GetSocketType() == Socket::TYPE_TCP) |
| 623 // to the system. | 629 info.socket_type = extensions::api::socket::SOCKET_TYPE_TCP; |
| 624 if (socket->GetSocketType() == Socket::TYPE_TCP) | 630 else |
| 625 info.socket_type = extensions::api::socket::SOCKET_TYPE_TCP; | 631 info.socket_type = extensions::api::socket::SOCKET_TYPE_UDP; |
| 626 else | 632 info.connected = socket->IsConnected(); |
| 627 info.socket_type = extensions::api::socket::SOCKET_TYPE_UDP; | |
| 628 info.connected = socket->IsConnected(); | |
| 629 | 633 |
| 630 // Grab the peer address as known by the OS. This and the call below will | 634 // Grab the peer address as known by the OS. This and the call below will |
| 631 // always succeed while the socket is connected, even if the socket has | 635 // always succeed while the socket is connected, even if the socket has |
| 632 // been remotely closed by the peer; only reading the socket will reveal | 636 // been remotely closed by the peer; only reading the socket will reveal |
| 633 // that it should be closed locally. | 637 // that it should be closed locally. |
| 634 net::IPEndPoint peerAddress; | 638 net::IPEndPoint peerAddress; |
| 635 if (socket->GetPeerAddress(&peerAddress)) { | 639 if (socket->GetPeerAddress(&peerAddress)) { |
| 636 info.peer_address.reset( | 640 info.peer_address.reset( |
| 637 new std::string(peerAddress.ToStringWithoutPort())); | 641 new std::string(peerAddress.ToStringWithoutPort())); |
| 638 info.peer_port.reset(new int(peerAddress.port())); | 642 info.peer_port.reset(new int(peerAddress.port())); |
| 639 } | 643 } |
| 640 | 644 |
| 641 // Grab the local address as known by the OS. | 645 // Grab the local address as known by the OS. |
| 642 net::IPEndPoint localAddress; | 646 net::IPEndPoint localAddress; |
| 643 if (socket->GetLocalAddress(&localAddress)) { | 647 if (socket->GetLocalAddress(&localAddress)) { |
| 644 info.local_address.reset( | 648 info.local_address.reset( |
| 645 new std::string(localAddress.ToStringWithoutPort())); | 649 new std::string(localAddress.ToStringWithoutPort())); |
| 646 info.local_port.reset(new int(localAddress.port())); | 650 info.local_port.reset(new int(localAddress.port())); |
| 647 } | |
| 648 } else { | |
| 649 error_ = kSocketNotFoundError; | |
| 650 } | 651 } |
| 652 |
| 651 SetResult(info.ToValue().release()); | 653 SetResult(info.ToValue().release()); |
| 652 } | 654 } |
| 653 | 655 |
| 654 bool SocketGetNetworkListFunction::RunImpl() { | 656 bool SocketGetNetworkListFunction::RunImpl() { |
| 655 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | 657 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, |
| 656 base::Bind(&SocketGetNetworkListFunction::GetNetworkListOnFileThread, | 658 base::Bind(&SocketGetNetworkListFunction::GetNetworkListOnFileThread, |
| 657 this)); | 659 this)); |
| 658 return true; | 660 return true; |
| 659 } | 661 } |
| 660 | 662 |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 893 return; | 895 return; |
| 894 } | 896 } |
| 895 | 897 |
| 896 base::ListValue* values = new base::ListValue(); | 898 base::ListValue* values = new base::ListValue(); |
| 897 values->AppendStrings((std::vector<std::string>&) | 899 values->AppendStrings((std::vector<std::string>&) |
| 898 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); | 900 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); |
| 899 SetResult(values); | 901 SetResult(values); |
| 900 } | 902 } |
| 901 | 903 |
| 902 } // namespace extensions | 904 } // namespace extensions |
| OLD | NEW |