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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 } | 70 } |
70 | 71 |
71 int SocketAsyncApiFunction::AddSocket(Socket* socket) { | 72 int SocketAsyncApiFunction::AddSocket(Socket* socket) { |
72 return manager_->Add(socket); | 73 return manager_->Add(socket); |
73 } | 74 } |
74 | 75 |
75 Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { | 76 Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { |
76 return manager_->Get(extension_->id(), api_resource_id); | 77 return manager_->Get(extension_->id(), api_resource_id); |
77 } | 78 } |
78 | 79 |
80 void SocketAsyncApiFunction::SetSocket(int api_resource_id, | |
81 Socket* socket) { | |
82 manager_->Set(extension_->id(), api_resource_id, socket); | |
83 } | |
84 | |
79 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { | 85 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { |
80 return manager_->GetResourceIds(extension_->id()); | 86 return manager_->GetResourceIds(extension_->id()); |
81 } | 87 } |
82 | 88 |
83 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { | 89 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { |
84 manager_->Remove(extension_->id(), api_resource_id); | 90 manager_->Remove(extension_->id(), api_resource_id); |
85 } | 91 } |
86 | 92 |
87 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() | 93 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() |
88 : io_thread_(g_browser_process->io_thread()), | 94 : 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) { | 238 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { |
233 if (lookup_result == net::OK) { | 239 if (lookup_result == net::OK) { |
234 StartConnect(); | 240 StartConnect(); |
235 } else { | 241 } else { |
236 SetResult(new base::FundamentalValue(lookup_result)); | 242 SetResult(new base::FundamentalValue(lookup_result)); |
237 AsyncWorkCompleted(); | 243 AsyncWorkCompleted(); |
238 } | 244 } |
239 } | 245 } |
240 | 246 |
241 void SocketConnectFunction::StartConnect() { | 247 void SocketConnectFunction::StartConnect() { |
248 socket_->SetHostname(hostname_); | |
242 socket_->Connect(resolved_address_, port_, | 249 socket_->Connect(resolved_address_, port_, |
243 base::Bind(&SocketConnectFunction::OnConnect, this)); | 250 base::Bind(&SocketConnectFunction::OnConnect, this)); |
244 } | 251 } |
245 | 252 |
246 void SocketConnectFunction::OnConnect(int result) { | 253 void SocketConnectFunction::OnConnect(int result) { |
247 SetResult(new base::FundamentalValue(result)); | 254 SetResult(new base::FundamentalValue(result)); |
248 AsyncWorkCompleted(); | 255 AsyncWorkCompleted(); |
249 } | 256 } |
250 | 257 |
251 bool SocketDisconnectFunction::Prepare() { | 258 bool SocketDisconnectFunction::Prepare() { |
(...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
889 SetResult(new base::FundamentalValue(result)); | 896 SetResult(new base::FundamentalValue(result)); |
890 return; | 897 return; |
891 } | 898 } |
892 | 899 |
893 base::ListValue* values = new base::ListValue(); | 900 base::ListValue* values = new base::ListValue(); |
894 values->AppendStrings((std::vector<std::string>&) | 901 values->AppendStrings((std::vector<std::string>&) |
895 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); | 902 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); |
896 SetResult(values); | 903 SetResult(values); |
897 } | 904 } |
898 | 905 |
906 SocketSecureFunction::SocketSecureFunction() {} | |
907 SocketSecureFunction::~SocketSecureFunction() {} | |
908 | |
909 bool SocketSecureFunction::Prepare() { | |
910 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
911 params_ = api::socket::Secure::Params::Create(*args_); | |
912 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
913 url_request_getter_ = GetProfile()->GetRequestContext(); | |
914 return true; | |
915 } | |
916 | |
917 void SocketSecureFunction::Work() { | |
rpaquay
2013/12/09 23:02:03
Using "Work" is slightly different than the conven
lally
2013/12/12 02:31:39
Done.
| |
918 base::Value *result; | |
919 async_completed_needs_invocation_ = false; | |
920 const char * error_str = TLSSocket::SecureTCPSocket( | |
rpaquay
2013/12/09 23:02:03
There are issues with the way error handling is do
lally
2013/12/12 02:31:39
Done.
| |
921 GetSocket(params_->socket_id), | |
922 GetProfile(), | |
923 url_request_getter_, | |
924 extension_id(), | |
925 params_->options.get(), | |
926 base::Bind(&SocketSecureFunction::TlsConnectDone, this), | |
927 &result); | |
928 if (error_str) { | |
929 error_ = error_str; | |
930 } | |
931 SetResult(result); | |
932 | |
933 if (async_completed_needs_invocation_) { | |
934 AsyncWorkCompleted(); | |
935 async_completed_needs_invocation_ = false; | |
936 } else { | |
937 async_completed_needs_invocation_ = true; | |
938 } | |
939 } | |
940 | |
941 // Override the regular implementation, which would call AsyncWorkCompleted | |
942 // immediately after Work(). | |
943 void SocketSecureFunction::AsyncWorkStart() { | |
944 Work(); | |
945 } | |
946 | |
947 void SocketSecureFunction::TlsConnectDone(Socket *sock, int result) { | |
948 if (sock) { | |
949 SetSocket(params_->socket_id, sock); | |
950 } else { | |
951 RemoveSocket(params_->socket_id); | |
952 } | |
953 | |
954 if (async_completed_needs_invocation_) { | |
rpaquay
2013/12/09 23:02:03
As mentioned above, the code here should be someth
lally
2013/12/12 02:31:39
Done.
| |
955 AsyncWorkCompleted(); | |
956 async_completed_needs_invocation_ = false; | |
957 } else { | |
958 async_completed_needs_invocation_ = true; | |
959 } | |
960 } | |
961 | |
899 } // namespace extensions | 962 } // namespace extensions |
OLD | NEW |