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[] = | |
52 "Only TCP sockets are supported for TLS."; | |
53 const char kSocketNotConnectedError[] = "Socket not connected"; | |
49 | 54 |
50 SocketAsyncApiFunction::SocketAsyncApiFunction() { | 55 SocketAsyncApiFunction::SocketAsyncApiFunction() { |
51 } | 56 } |
52 | 57 |
53 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 58 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
54 } | 59 } |
55 | 60 |
56 bool SocketAsyncApiFunction::PrePrepare() { | 61 bool SocketAsyncApiFunction::PrePrepare() { |
57 manager_ = CreateSocketResourceManager(); | 62 manager_ = CreateSocketResourceManager(); |
58 return manager_->SetProfile(GetProfile()); | 63 return manager_->SetProfile(GetProfile()); |
(...skipping 10 matching lines...) Expand all Loading... | |
69 } | 74 } |
70 | 75 |
71 int SocketAsyncApiFunction::AddSocket(Socket* socket) { | 76 int SocketAsyncApiFunction::AddSocket(Socket* socket) { |
72 return manager_->Add(socket); | 77 return manager_->Add(socket); |
73 } | 78 } |
74 | 79 |
75 Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { | 80 Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { |
76 return manager_->Get(extension_->id(), api_resource_id); | 81 return manager_->Get(extension_->id(), api_resource_id); |
77 } | 82 } |
78 | 83 |
84 void SocketAsyncApiFunction::SetSocket(int api_resource_id, | |
85 Socket* socket) { | |
86 manager_->Set(extension_->id(), api_resource_id, socket); | |
87 } | |
88 | |
79 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { | 89 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { |
80 return manager_->GetResourceIds(extension_->id()); | 90 return manager_->GetResourceIds(extension_->id()); |
81 } | 91 } |
82 | 92 |
83 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { | 93 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { |
84 manager_->Remove(extension_->id(), api_resource_id); | 94 manager_->Remove(extension_->id(), api_resource_id); |
85 } | 95 } |
86 | 96 |
87 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() | 97 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() |
88 : io_thread_(g_browser_process->io_thread()), | 98 : 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) { | 242 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { |
233 if (lookup_result == net::OK) { | 243 if (lookup_result == net::OK) { |
234 StartConnect(); | 244 StartConnect(); |
235 } else { | 245 } else { |
236 SetResult(new base::FundamentalValue(lookup_result)); | 246 SetResult(new base::FundamentalValue(lookup_result)); |
237 AsyncWorkCompleted(); | 247 AsyncWorkCompleted(); |
238 } | 248 } |
239 } | 249 } |
240 | 250 |
241 void SocketConnectFunction::StartConnect() { | 251 void SocketConnectFunction::StartConnect() { |
252 socket_->set_hostname(hostname_); | |
242 socket_->Connect(resolved_address_, port_, | 253 socket_->Connect(resolved_address_, port_, |
243 base::Bind(&SocketConnectFunction::OnConnect, this)); | 254 base::Bind(&SocketConnectFunction::OnConnect, this)); |
244 } | 255 } |
245 | 256 |
246 void SocketConnectFunction::OnConnect(int result) { | 257 void SocketConnectFunction::OnConnect(int result) { |
247 SetResult(new base::FundamentalValue(result)); | 258 SetResult(new base::FundamentalValue(result)); |
248 AsyncWorkCompleted(); | 259 AsyncWorkCompleted(); |
249 } | 260 } |
250 | 261 |
251 bool SocketDisconnectFunction::Prepare() { | 262 bool SocketDisconnectFunction::Prepare() { |
(...skipping 637 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 int result = net::ERR_INVALID_ARGUMENT; | |
Ryan Sleevi
2013/12/17 00:37:40
You never assign to result, but I think it makes t
lally
2014/01/09 18:47:16
Done.
| |
925 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
Ryan Sleevi
2013/12/17 00:37:40
style nit: Normally we put pre-condition checks fi
lally
2014/01/09 18:47:16
Done.
| |
926 | |
927 Socket* socket = GetSocket(params_->socket_id); | |
928 if (!socket) { | |
929 SetResult(new base::FundamentalValue(result)); | |
930 error_ = kSocketNotFoundError; | |
931 AsyncWorkCompleted(); | |
932 return; | |
933 } | |
934 | |
935 TCPSocket* tcp_socket = static_cast<TCPSocket*>(socket); | |
936 if (socket->GetSocketType() != Socket::TYPE_TCP | |
937 || tcp_socket->ClientStream() == NULL) { | |
Ryan Sleevi
2013/12/17 00:37:40
style: Wrap after binary operators, not before ( h
lally
2014/01/09 18:47:16
Done.
| |
938 SetResult(new base::FundamentalValue(result)); | |
939 error_ = kSecureSocketTypeError; | |
940 AsyncWorkCompleted(); | |
941 return; | |
942 } | |
943 | |
944 if (!socket->IsConnected()) { | |
945 SetResult(new base::FundamentalValue(result)); | |
946 error_ = kSocketNotConnectedError; | |
947 AsyncWorkCompleted(); | |
948 return; | |
949 } | |
950 | |
951 Profile* profile = GetProfile(); | |
952 DCHECK(profile); | |
953 | |
954 TLSSocket::SecureTCPSocket( | |
955 socket, profile, url_request_getter_, extension_id(), | |
956 params_->options.get(), base::Bind(&SocketSecureFunction::TlsConnectDone, | |
957 this)); | |
958 } | |
959 | |
960 void SocketSecureFunction::TlsConnectDone(TLSSocket* sock, int result) { | |
961 if (sock) { | |
962 SetSocket(params_->socket_id, sock); | |
963 } else { | |
964 RemoveSocket(params_->socket_id); | |
Ryan Sleevi
2013/12/17 00:37:40
naming: sock -> socket
DESIGN: I find this condit
lally
2014/01/09 18:47:16
Ah, fair enough. socket can only be non-null if r
| |
965 } | |
966 | |
967 results_ = api::socket::Secure::Results::Create(result); | |
968 if (result != net::OK) { | |
969 error_ = net::ErrorToString(result); | |
970 } | |
971 url_request_getter_->Release(); | |
972 AsyncWorkCompleted(); | |
973 } | |
974 | |
899 } // namespace extensions | 975 } // namespace extensions |
OLD | NEW |