Chromium Code Reviews| 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/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 12 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
| 13 #include "chrome/browser/extensions/api/socket/socket.h" | 13 #include "chrome/browser/extensions/api/socket/socket.h" |
| 14 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | 14 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| 15 #include "chrome/browser/extensions/api/socket/tls_socket.h" | |
| 15 #include "chrome/browser/extensions/api/socket/udp_socket.h" | 16 #include "chrome/browser/extensions/api/socket/udp_socket.h" |
| 16 #include "chrome/browser/extensions/extension_system.h" | 17 #include "chrome/browser/extensions/extension_system.h" |
| 17 #include "chrome/browser/io_thread.h" | 18 #include "chrome/browser/io_thread.h" |
| 18 #include "chrome/common/extensions/permissions/socket_permission.h" | 19 #include "chrome/common/extensions/permissions/socket_permission.h" |
| 19 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/permissions/permissions_data.h" | 21 #include "extensions/common/permissions/permissions_data.h" |
| 21 #include "net/base/host_port_pair.h" | 22 #include "net/base/host_port_pair.h" |
| 22 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
| 23 #include "net/base/ip_endpoint.h" | 24 #include "net/base/ip_endpoint.h" |
| 24 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 39 const char kSocketNotFoundError[] = "Socket not found"; | 40 const char kSocketNotFoundError[] = "Socket not found"; |
| 40 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 41 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
| 41 const char kPermissionError[] = "App does not have permission"; | 42 const char kPermissionError[] = "App does not have permission"; |
| 42 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 43 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
| 43 const char kTCPSocketBindError[] = | 44 const char kTCPSocketBindError[] = |
| 44 "TCP socket does not support bind. For TCP server please use listen."; | 45 "TCP socket does not support bind. For TCP server please use listen."; |
| 45 const char kMulticastSocketTypeError[] = | 46 const char kMulticastSocketTypeError[] = |
| 46 "Only UDP socket supports multicast."; | 47 "Only UDP socket supports multicast."; |
| 47 const char kWildcardAddress[] = "*"; | 48 const char kWildcardAddress[] = "*"; |
| 48 const int kWildcardPort = 0; | 49 const int kWildcardPort = 0; |
| 50 const char kSecureSocketTypeError[] = | |
| 51 "Only TCP sockets are supported for TLS."; | |
| 52 const char kSocketNotConnectedError[] = "Socket not connected"; | |
| 49 | 53 |
| 50 SocketAsyncApiFunction::SocketAsyncApiFunction() { | 54 SocketAsyncApiFunction::SocketAsyncApiFunction() { |
| 51 } | 55 } |
| 52 | 56 |
| 53 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 57 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
| 54 } | 58 } |
| 55 | 59 |
| 56 bool SocketAsyncApiFunction::PrePrepare() { | 60 bool SocketAsyncApiFunction::PrePrepare() { |
| 57 manager_ = CreateSocketResourceManager(); | 61 manager_ = CreateSocketResourceManager(); |
| 58 return manager_->SetProfile(GetProfile()); | 62 return manager_->SetProfile(GetProfile()); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 69 } | 73 } |
| 70 | 74 |
| 71 int SocketAsyncApiFunction::AddSocket(Socket* socket) { | 75 int SocketAsyncApiFunction::AddSocket(Socket* socket) { |
| 72 return manager_->Add(socket); | 76 return manager_->Add(socket); |
| 73 } | 77 } |
| 74 | 78 |
| 75 Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { | 79 Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { |
| 76 return manager_->Get(extension_->id(), api_resource_id); | 80 return manager_->Get(extension_->id(), api_resource_id); |
| 77 } | 81 } |
| 78 | 82 |
| 83 void SocketAsyncApiFunction::SetSocket(int api_resource_id, | |
| 84 Socket* socket) { | |
| 85 manager_->Set(extension_->id(), api_resource_id, socket); | |
| 86 } | |
| 87 | |
| 79 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { | 88 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { |
| 80 return manager_->GetResourceIds(extension_->id()); | 89 return manager_->GetResourceIds(extension_->id()); |
| 81 } | 90 } |
| 82 | 91 |
| 83 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { | 92 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { |
| 84 manager_->Remove(extension_->id(), api_resource_id); | 93 manager_->Remove(extension_->id(), api_resource_id); |
| 85 } | 94 } |
| 86 | 95 |
| 87 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() | 96 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() |
| 88 : io_thread_(g_browser_process->io_thread()), | 97 : io_thread_(g_browser_process->io_thread()), |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { | 241 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { |
| 233 if (lookup_result == net::OK) { | 242 if (lookup_result == net::OK) { |
| 234 StartConnect(); | 243 StartConnect(); |
| 235 } else { | 244 } else { |
| 236 SetResult(new base::FundamentalValue(lookup_result)); | 245 SetResult(new base::FundamentalValue(lookup_result)); |
| 237 AsyncWorkCompleted(); | 246 AsyncWorkCompleted(); |
| 238 } | 247 } |
| 239 } | 248 } |
| 240 | 249 |
| 241 void SocketConnectFunction::StartConnect() { | 250 void SocketConnectFunction::StartConnect() { |
| 251 socket_->set_hostname(hostname_); | |
| 242 socket_->Connect(resolved_address_, port_, | 252 socket_->Connect(resolved_address_, port_, |
| 243 base::Bind(&SocketConnectFunction::OnConnect, this)); | 253 base::Bind(&SocketConnectFunction::OnConnect, this)); |
| 244 } | 254 } |
| 245 | 255 |
| 246 void SocketConnectFunction::OnConnect(int result) { | 256 void SocketConnectFunction::OnConnect(int result) { |
| 247 SetResult(new base::FundamentalValue(result)); | 257 SetResult(new base::FundamentalValue(result)); |
| 248 AsyncWorkCompleted(); | 258 AsyncWorkCompleted(); |
| 249 } | 259 } |
| 250 | 260 |
| 251 bool SocketDisconnectFunction::Prepare() { | 261 bool SocketDisconnectFunction::Prepare() { |
| (...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 889 SetResult(new base::FundamentalValue(result)); | 899 SetResult(new base::FundamentalValue(result)); |
| 890 return; | 900 return; |
| 891 } | 901 } |
| 892 | 902 |
| 893 base::ListValue* values = new base::ListValue(); | 903 base::ListValue* values = new base::ListValue(); |
| 894 values->AppendStrings((std::vector<std::string>&) | 904 values->AppendStrings((std::vector<std::string>&) |
| 895 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); | 905 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); |
| 896 SetResult(values); | 906 SetResult(values); |
| 897 } | 907 } |
| 898 | 908 |
| 909 SocketSecureFunction::SocketSecureFunction() {} | |
| 910 SocketSecureFunction::~SocketSecureFunction() {} | |
| 911 | |
| 912 bool SocketSecureFunction::Prepare() { | |
| 913 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 914 params_ = api::socket::Secure::Params::Create(*args_); | |
| 915 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 916 url_request_getter_ = GetProfile()->GetRequestContext(); | |
| 917 return true; | |
| 918 } | |
| 919 | |
| 920 // Override the regular implementation, which would call AsyncWorkCompleted | |
| 921 // immediately after Work(). | |
| 922 void SocketSecureFunction::AsyncWorkStart() { | |
| 923 int result = net::ERR_INVALID_ARGUMENT; | |
| 924 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 925 | |
| 926 Socket* socket = GetSocket(params_->socket_id); | |
| 927 Profile* profile = GetProfile(); | |
|
rpaquay
2013/12/16 20:18:46
nit: move this closer to first usage.
lally
2013/12/16 22:20:07
Done.
| |
| 928 DCHECK(profile); | |
| 929 | |
| 930 if (!socket) { | |
| 931 SetResult(new base::FundamentalValue(result)); | |
| 932 error_ = kSocketNotFoundError; | |
| 933 AsyncWorkCompleted(); | |
| 934 return; | |
| 935 } | |
| 936 | |
| 937 TCPSocket* tcp_socket = static_cast<TCPSocket*>(socket); | |
| 938 if (socket->GetSocketType() != Socket::TYPE_TCP | |
| 939 || tcp_socket->ClientStream() == NULL) { | |
| 940 SetResult(new base::FundamentalValue(result)); | |
| 941 error_ = kSecureSocketTypeError; | |
| 942 AsyncWorkCompleted(); | |
| 943 return; | |
| 944 } | |
| 945 | |
| 946 if (!socket->IsConnected()) { | |
| 947 SetResult(new base::FundamentalValue(result)); | |
| 948 error_ = kSocketNotConnectedError; | |
| 949 AsyncWorkCompleted(); | |
| 950 return; | |
| 951 } | |
| 952 | |
| 953 TLSSocket::SecureTCPSocket( | |
| 954 socket, profile, url_request_getter_, extension_id(), | |
| 955 params_->options.get(), base::Bind(&SocketSecureFunction::TlsConnectDone, | |
| 956 this)); | |
| 957 } | |
| 958 | |
| 959 void SocketSecureFunction::TlsConnectDone(TLSSocket* sock, int result) { | |
| 960 SetResult(new base::FundamentalValue(result)); | |
|
rpaquay
2013/12/16 20:18:46
remove this line (see comment below)
lally
2013/12/16 22:20:07
Done.
| |
| 961 if (sock) { | |
| 962 SetSocket(params_->socket_id, sock); | |
| 963 } else { | |
| 964 RemoveSocket(params_->socket_id); | |
| 965 } | |
| 966 | |
| 967 if (result != net::OK) { | |
| 968 error_ = net::ErrorToString(result); | |
| 969 results_ = api::socket::Secure::Results::Create(result); | |
|
rpaquay
2013/12/16 20:18:46
move this out of the "if" block.
lally
2013/12/16 22:20:07
Done.
| |
| 970 } | |
| 971 AsyncWorkCompleted(); | |
| 972 } | |
| 973 | |
| 899 } // namespace extensions | 974 } // namespace extensions |
| OLD | NEW |