| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/socket/socket_api.h" | 5 #include "extensions/browser/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/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "net/base/ip_endpoint.h" | 25 #include "net/base/ip_endpoint.h" |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/base/net_log.h" | 27 #include "net/base/net_log.h" |
| 28 #include "net/base/net_util.h" | 28 #include "net/base/net_util.h" |
| 29 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
| 30 #include "net/url_request/url_request_context_getter.h" | 30 #include "net/url_request/url_request_context_getter.h" |
| 31 | 31 |
| 32 #if defined(OS_CHROMEOS) | 32 #if defined(OS_CHROMEOS) |
| 33 #include "base/command_line.h" | 33 #include "base/command_line.h" |
| 34 #include "chromeos/chromeos_switches.h" | 34 #include "chromeos/chromeos_switches.h" |
| 35 #include "chromeos/network/firewall_hole.h" | |
| 36 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
| 37 #endif // OS_CHROMEOS | 36 #endif // OS_CHROMEOS |
| 38 | 37 |
| 39 namespace extensions { | 38 namespace extensions { |
| 40 | 39 |
| 40 using content::BrowserThread; |
| 41 using content::SocketPermissionRequest; | 41 using content::SocketPermissionRequest; |
| 42 | 42 |
| 43 const char kAddressKey[] = "address"; | 43 const char kAddressKey[] = "address"; |
| 44 const char kPortKey[] = "port"; | 44 const char kPortKey[] = "port"; |
| 45 const char kBytesWrittenKey[] = "bytesWritten"; | 45 const char kBytesWrittenKey[] = "bytesWritten"; |
| 46 const char kDataKey[] = "data"; | 46 const char kDataKey[] = "data"; |
| 47 const char kResultCodeKey[] = "resultCode"; | 47 const char kResultCodeKey[] = "resultCode"; |
| 48 const char kSocketIdKey[] = "socketId"; | 48 const char kSocketIdKey[] = "socketId"; |
| 49 | 49 |
| 50 const char kSocketNotFoundError[] = "Socket not found"; | 50 const char kSocketNotFoundError[] = "Socket not found"; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 chromeos::switches::kEnableFirewallHolePunching)) { | 110 chromeos::switches::kEnableFirewallHolePunching)) { |
| 111 net::IPEndPoint local_address; | 111 net::IPEndPoint local_address; |
| 112 if (!socket->GetLocalAddress(&local_address)) { | 112 if (!socket->GetLocalAddress(&local_address)) { |
| 113 NOTREACHED() << "Cannot get address of recently bound socket."; | 113 NOTREACHED() << "Cannot get address of recently bound socket."; |
| 114 error_ = kFirewallFailure; | 114 error_ = kFirewallFailure; |
| 115 SetResult(new base::FundamentalValue(-1)); | 115 SetResult(new base::FundamentalValue(-1)); |
| 116 AsyncWorkCompleted(); | 116 AsyncWorkCompleted(); |
| 117 return; | 117 return; |
| 118 } | 118 } |
| 119 | 119 |
| 120 chromeos::FirewallHole::PortType port_type; | 120 AppFirewallHole::PortType type = socket->GetSocketType() == Socket::TYPE_TCP |
| 121 if (socket->GetSocketType() == Socket::TYPE_TCP) { | 121 ? AppFirewallHole::PortType::TCP |
| 122 port_type = chromeos::FirewallHole::PortType::TCP; | 122 : AppFirewallHole::PortType::UDP; |
| 123 } else { | |
| 124 port_type = chromeos::FirewallHole::PortType::UDP; | |
| 125 } | |
| 126 | 123 |
| 127 content::BrowserThread::PostTask( | 124 BrowserThread::PostTask( |
| 128 content::BrowserThread::UI, FROM_HERE, | 125 BrowserThread::UI, FROM_HERE, |
| 129 base::Bind( | 126 base::Bind(&SocketAsyncApiFunction::OpenFirewallHoleOnUIThread, this, |
| 130 &chromeos::FirewallHole::Open, port_type, local_address.port(), | 127 type, local_address.port(), socket_id)); |
| 131 "" /* all interfaces */, | |
| 132 base::Bind(&SocketAsyncApiFunction::OnFirewallHoleOpenedOnUIThread, | |
| 133 this, socket_id))); | |
| 134 return; | 128 return; |
| 135 } | 129 } |
| 136 #endif | 130 #endif |
| 137 AsyncWorkCompleted(); | 131 AsyncWorkCompleted(); |
| 138 } | 132 } |
| 139 | 133 |
| 140 #if defined(OS_CHROMEOS) | 134 #if defined(OS_CHROMEOS) |
| 141 | 135 |
| 142 void SocketAsyncApiFunction::OnFirewallHoleOpenedOnUIThread( | 136 void SocketAsyncApiFunction::OpenFirewallHoleOnUIThread( |
| 143 int socket_id, | 137 AppFirewallHole::PortType type, |
| 144 scoped_ptr<chromeos::FirewallHole> hole) { | 138 uint16_t port, |
| 145 content::BrowserThread::PostTask( | 139 int socket_id) { |
| 146 content::BrowserThread::IO, FROM_HERE, | 140 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 141 AppFirewallHoleManager* manager = |
| 142 AppFirewallHoleManager::Get(browser_context()); |
| 143 scoped_ptr<AppFirewallHole, BrowserThread::DeleteOnUIThread> hole( |
| 144 manager->Open(type, port, extension_id())); |
| 145 BrowserThread::PostTask( |
| 146 BrowserThread::IO, FROM_HERE, |
| 147 base::Bind(&SocketAsyncApiFunction::OnFirewallHoleOpened, this, socket_id, | 147 base::Bind(&SocketAsyncApiFunction::OnFirewallHoleOpened, this, socket_id, |
| 148 base::Passed(&hole))); | 148 base::Passed(&hole))); |
| 149 } | 149 } |
| 150 | 150 |
| 151 void SocketAsyncApiFunction::OnFirewallHoleOpened( | 151 void SocketAsyncApiFunction::OnFirewallHoleOpened( |
| 152 int socket_id, | 152 int socket_id, |
| 153 scoped_ptr<chromeos::FirewallHole> hole) { | 153 scoped_ptr<AppFirewallHole, BrowserThread::DeleteOnUIThread> hole) { |
| 154 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 154 if (!hole) { | 155 if (!hole) { |
| 155 error_ = kFirewallFailure; | 156 error_ = kFirewallFailure; |
| 156 SetResult(new base::FundamentalValue(-1)); | 157 SetResult(new base::FundamentalValue(-1)); |
| 157 AsyncWorkCompleted(); | 158 AsyncWorkCompleted(); |
| 158 return; | 159 return; |
| 159 } | 160 } |
| 160 | 161 |
| 161 Socket* socket = GetSocket(socket_id); | 162 Socket* socket = GetSocket(socket_id); |
| 162 if (!socket) { | 163 if (!socket) { |
| 163 error_ = kSocketNotFoundError; | 164 error_ = kSocketNotFoundError; |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 767 if (socket->GetLocalAddress(&localAddress)) { | 768 if (socket->GetLocalAddress(&localAddress)) { |
| 768 info.local_address.reset( | 769 info.local_address.reset( |
| 769 new std::string(localAddress.ToStringWithoutPort())); | 770 new std::string(localAddress.ToStringWithoutPort())); |
| 770 info.local_port.reset(new int(localAddress.port())); | 771 info.local_port.reset(new int(localAddress.port())); |
| 771 } | 772 } |
| 772 | 773 |
| 773 SetResult(info.ToValue().release()); | 774 SetResult(info.ToValue().release()); |
| 774 } | 775 } |
| 775 | 776 |
| 776 bool SocketGetNetworkListFunction::RunAsync() { | 777 bool SocketGetNetworkListFunction::RunAsync() { |
| 777 content::BrowserThread::PostTask( | 778 BrowserThread::PostTask( |
| 778 content::BrowserThread::FILE, | 779 BrowserThread::FILE, FROM_HERE, |
| 779 FROM_HERE, | |
| 780 base::Bind(&SocketGetNetworkListFunction::GetNetworkListOnFileThread, | 780 base::Bind(&SocketGetNetworkListFunction::GetNetworkListOnFileThread, |
| 781 this)); | 781 this)); |
| 782 return true; | 782 return true; |
| 783 } | 783 } |
| 784 | 784 |
| 785 void SocketGetNetworkListFunction::GetNetworkListOnFileThread() { | 785 void SocketGetNetworkListFunction::GetNetworkListOnFileThread() { |
| 786 net::NetworkInterfaceList interface_list; | 786 net::NetworkInterfaceList interface_list; |
| 787 if (GetNetworkList(&interface_list, | 787 if (GetNetworkList(&interface_list, |
| 788 net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { | 788 net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { |
| 789 content::BrowserThread::PostTask( | 789 BrowserThread::PostTask( |
| 790 content::BrowserThread::UI, | 790 BrowserThread::UI, FROM_HERE, |
| 791 FROM_HERE, | 791 base::Bind(&SocketGetNetworkListFunction::SendResponseOnUIThread, this, |
| 792 base::Bind(&SocketGetNetworkListFunction::SendResponseOnUIThread, | |
| 793 this, | |
| 794 interface_list)); | 792 interface_list)); |
| 795 return; | 793 return; |
| 796 } | 794 } |
| 797 | 795 |
| 798 content::BrowserThread::PostTask( | 796 BrowserThread::PostTask( |
| 799 content::BrowserThread::UI, | 797 BrowserThread::UI, FROM_HERE, |
| 800 FROM_HERE, | |
| 801 base::Bind(&SocketGetNetworkListFunction::HandleGetNetworkListError, | 798 base::Bind(&SocketGetNetworkListFunction::HandleGetNetworkListError, |
| 802 this)); | 799 this)); |
| 803 } | 800 } |
| 804 | 801 |
| 805 void SocketGetNetworkListFunction::HandleGetNetworkListError() { | 802 void SocketGetNetworkListFunction::HandleGetNetworkListError() { |
| 806 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 803 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 807 error_ = kNetworkListError; | 804 error_ = kNetworkListError; |
| 808 SendResponse(false); | 805 SendResponse(false); |
| 809 } | 806 } |
| 810 | 807 |
| 811 void SocketGetNetworkListFunction::SendResponseOnUIThread( | 808 void SocketGetNetworkListFunction::SendResponseOnUIThread( |
| 812 const net::NetworkInterfaceList& interface_list) { | 809 const net::NetworkInterfaceList& interface_list) { |
| 813 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 810 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 814 | 811 |
| 815 std::vector<linked_ptr<core_api::socket::NetworkInterface> > create_arg; | 812 std::vector<linked_ptr<core_api::socket::NetworkInterface> > create_arg; |
| 816 create_arg.reserve(interface_list.size()); | 813 create_arg.reserve(interface_list.size()); |
| 817 for (net::NetworkInterfaceList::const_iterator i = interface_list.begin(); | 814 for (net::NetworkInterfaceList::const_iterator i = interface_list.begin(); |
| 818 i != interface_list.end(); | 815 i != interface_list.end(); |
| 819 ++i) { | 816 ++i) { |
| 820 linked_ptr<core_api::socket::NetworkInterface> info = | 817 linked_ptr<core_api::socket::NetworkInterface> info = |
| 821 make_linked_ptr(new core_api::socket::NetworkInterface); | 818 make_linked_ptr(new core_api::socket::NetworkInterface); |
| 822 info->name = i->name; | 819 info->name = i->name; |
| 823 info->address = net::IPAddressToString(i->address); | 820 info->address = net::IPAddressToString(i->address); |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1023 SetResult(values); | 1020 SetResult(values); |
| 1024 } | 1021 } |
| 1025 | 1022 |
| 1026 SocketSecureFunction::SocketSecureFunction() { | 1023 SocketSecureFunction::SocketSecureFunction() { |
| 1027 } | 1024 } |
| 1028 | 1025 |
| 1029 SocketSecureFunction::~SocketSecureFunction() { | 1026 SocketSecureFunction::~SocketSecureFunction() { |
| 1030 } | 1027 } |
| 1031 | 1028 |
| 1032 bool SocketSecureFunction::Prepare() { | 1029 bool SocketSecureFunction::Prepare() { |
| 1033 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 1030 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1034 params_ = core_api::socket::Secure::Params::Create(*args_); | 1031 params_ = core_api::socket::Secure::Params::Create(*args_); |
| 1035 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 1032 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 1036 url_request_getter_ = browser_context()->GetRequestContext(); | 1033 url_request_getter_ = browser_context()->GetRequestContext(); |
| 1037 return true; | 1034 return true; |
| 1038 } | 1035 } |
| 1039 | 1036 |
| 1040 // Override the regular implementation, which would call AsyncWorkCompleted | 1037 // Override the regular implementation, which would call AsyncWorkCompleted |
| 1041 // immediately after Work(). | 1038 // immediately after Work(). |
| 1042 void SocketSecureFunction::AsyncWorkStart() { | 1039 void SocketSecureFunction::AsyncWorkStart() { |
| 1043 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 1040 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1044 | 1041 |
| 1045 Socket* socket = GetSocket(params_->socket_id); | 1042 Socket* socket = GetSocket(params_->socket_id); |
| 1046 if (!socket) { | 1043 if (!socket) { |
| 1047 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); | 1044 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 1048 error_ = kSocketNotFoundError; | 1045 error_ = kSocketNotFoundError; |
| 1049 AsyncWorkCompleted(); | 1046 AsyncWorkCompleted(); |
| 1050 return; | 1047 return; |
| 1051 } | 1048 } |
| 1052 | 1049 |
| 1053 // Make sure that the socket is a TCP client socket. | 1050 // Make sure that the socket is a TCP client socket. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1089 } else { | 1086 } else { |
| 1090 RemoveSocket(params_->socket_id); | 1087 RemoveSocket(params_->socket_id); |
| 1091 error_ = net::ErrorToString(result); | 1088 error_ = net::ErrorToString(result); |
| 1092 } | 1089 } |
| 1093 | 1090 |
| 1094 results_ = core_api::socket::Secure::Results::Create(result); | 1091 results_ = core_api::socket::Secure::Results::Create(result); |
| 1095 AsyncWorkCompleted(); | 1092 AsyncWorkCompleted(); |
| 1096 } | 1093 } |
| 1097 | 1094 |
| 1098 } // namespace extensions | 1095 } // namespace extensions |
| OLD | NEW |