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" |
25 #include "net/base/net_log.h" | 26 #include "net/base/net_log.h" |
26 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
| 28 #include "net/url_request/url_request_context_getter.h" |
27 | 29 |
28 namespace extensions { | 30 namespace extensions { |
29 | 31 |
30 using content::SocketPermissionRequest; | 32 using content::SocketPermissionRequest; |
31 | 33 |
32 const char kAddressKey[] = "address"; | 34 const char kAddressKey[] = "address"; |
33 const char kPortKey[] = "port"; | 35 const char kPortKey[] = "port"; |
34 const char kBytesWrittenKey[] = "bytesWritten"; | 36 const char kBytesWrittenKey[] = "bytesWritten"; |
35 const char kDataKey[] = "data"; | 37 const char kDataKey[] = "data"; |
36 const char kResultCodeKey[] = "resultCode"; | 38 const char kResultCodeKey[] = "resultCode"; |
37 const char kSocketIdKey[] = "socketId"; | 39 const char kSocketIdKey[] = "socketId"; |
38 | 40 |
39 const char kSocketNotFoundError[] = "Socket not found"; | 41 const char kSocketNotFoundError[] = "Socket not found"; |
40 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 42 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
41 const char kPermissionError[] = "App does not have permission"; | 43 const char kPermissionError[] = "App does not have permission"; |
42 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 44 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
43 const char kTCPSocketBindError[] = | 45 const char kTCPSocketBindError[] = |
44 "TCP socket does not support bind. For TCP server please use listen."; | 46 "TCP socket does not support bind. For TCP server please use listen."; |
45 const char kMulticastSocketTypeError[] = | 47 const char kMulticastSocketTypeError[] = |
46 "Only UDP socket supports multicast."; | 48 "Only UDP socket supports multicast."; |
47 const char kWildcardAddress[] = "*"; | 49 const char kWildcardAddress[] = "*"; |
48 const int kWildcardPort = 0; | 50 const int kWildcardPort = 0; |
| 51 const char kSecureSocketTypeError[] = "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::ReplaceSocket(int api_resource_id, |
| 84 Socket* socket) { |
| 85 manager_->Replace(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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 205 |
197 void SocketConnectFunction::AsyncWorkStart() { | 206 void SocketConnectFunction::AsyncWorkStart() { |
198 socket_ = GetSocket(socket_id_); | 207 socket_ = GetSocket(socket_id_); |
199 if (!socket_) { | 208 if (!socket_) { |
200 error_ = kSocketNotFoundError; | 209 error_ = kSocketNotFoundError; |
201 SetResult(new base::FundamentalValue(-1)); | 210 SetResult(new base::FundamentalValue(-1)); |
202 AsyncWorkCompleted(); | 211 AsyncWorkCompleted(); |
203 return; | 212 return; |
204 } | 213 } |
205 | 214 |
| 215 socket_->set_hostname(hostname_); |
| 216 |
206 SocketPermissionRequest::OperationType operation_type; | 217 SocketPermissionRequest::OperationType operation_type; |
207 switch (socket_->GetSocketType()) { | 218 switch (socket_->GetSocketType()) { |
208 case Socket::TYPE_TCP: | 219 case Socket::TYPE_TCP: |
209 operation_type = SocketPermissionRequest::TCP_CONNECT; | 220 operation_type = SocketPermissionRequest::TCP_CONNECT; |
210 break; | 221 break; |
211 case Socket::TYPE_UDP: | 222 case Socket::TYPE_UDP: |
212 operation_type = SocketPermissionRequest::UDP_SEND_TO; | 223 operation_type = SocketPermissionRequest::UDP_SEND_TO; |
213 break; | 224 break; |
214 default: | 225 default: |
215 NOTREACHED() << "Unknown socket type."; | 226 NOTREACHED() << "Unknown socket type."; |
(...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
889 SetResult(new base::FundamentalValue(result)); | 900 SetResult(new base::FundamentalValue(result)); |
890 return; | 901 return; |
891 } | 902 } |
892 | 903 |
893 base::ListValue* values = new base::ListValue(); | 904 base::ListValue* values = new base::ListValue(); |
894 values->AppendStrings((std::vector<std::string>&) | 905 values->AppendStrings((std::vector<std::string>&) |
895 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); | 906 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); |
896 SetResult(values); | 907 SetResult(values); |
897 } | 908 } |
898 | 909 |
| 910 SocketSecureFunction::SocketSecureFunction() {} |
| 911 SocketSecureFunction::~SocketSecureFunction() {} |
| 912 |
| 913 bool SocketSecureFunction::Prepare() { |
| 914 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 915 params_ = api::socket::Secure::Params::Create(*args_); |
| 916 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 917 url_request_getter_ = GetProfile()->GetRequestContext(); |
| 918 return true; |
| 919 } |
| 920 |
| 921 // Override the regular implementation, which would call AsyncWorkCompleted |
| 922 // immediately after Work(). |
| 923 void SocketSecureFunction::AsyncWorkStart() { |
| 924 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 925 |
| 926 Socket* socket = GetSocket(params_->socket_id); |
| 927 if (!socket) { |
| 928 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 929 error_ = kSocketNotFoundError; |
| 930 AsyncWorkCompleted(); |
| 931 return; |
| 932 } |
| 933 |
| 934 // Make sure that the socket is neither UDP nor already TLS. Also make |
| 935 // sure that it's a client stream. |
| 936 if (socket->GetSocketType() != Socket::TYPE_TCP || |
| 937 static_cast<TCPSocket*>(socket)->ClientStream() == NULL) { |
| 938 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 939 error_ = kSecureSocketTypeError; |
| 940 AsyncWorkCompleted(); |
| 941 return; |
| 942 } |
| 943 |
| 944 if (!socket->IsConnected()) { |
| 945 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT)); |
| 946 error_ = kSocketNotConnectedError; |
| 947 AsyncWorkCompleted(); |
| 948 return; |
| 949 } |
| 950 |
| 951 Profile* profile = GetProfile(); |
| 952 DCHECK(profile); |
| 953 |
| 954 scoped_refptr<net::SSLConfigService> config_service( |
| 955 profile->GetSSLConfigService()); |
| 956 |
| 957 TLSSocket::UpgradeSocketToTLS( |
| 958 socket, |
| 959 config_service, |
| 960 url_request_getter_, |
| 961 extension_id(), |
| 962 params_->options.get(), |
| 963 base::Bind(&SocketSecureFunction::TlsConnectDone, this)); |
| 964 } |
| 965 |
| 966 void SocketSecureFunction::TlsConnectDone(scoped_ptr<TLSSocket> socket, |
| 967 int result) { |
| 968 // |socket| can only be non-null if |result| == net::OK. |
| 969 DCHECK(result == net::OK || socket == NULL); |
| 970 |
| 971 if (socket && result == net::OK) { |
| 972 ReplaceSocket(params_->socket_id, socket.release()); |
| 973 } else { |
| 974 RemoveSocket(params_->socket_id); |
| 975 error_ = net::ErrorToString(result); |
| 976 } |
| 977 |
| 978 results_ = api::socket::Secure::Results::Create(result); |
| 979 AsyncWorkCompleted(); |
| 980 } |
| 981 |
899 } // namespace extensions | 982 } // namespace extensions |
OLD | NEW |