Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(616)

Side by Side Diff: chrome/browser/extensions/api/socket/socket_api.cc

Issue 76403004: An implementation of chrome.socket.secure(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Double. Spaces removed. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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, Socket* socket) {
84 manager_->Set(extension_->id(), api_resource_id, socket);
85 }
86
79 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { 87 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() {
80 return manager_->GetResourceIds(extension_->id()); 88 return manager_->GetResourceIds(extension_->id());
81 } 89 }
82 90
83 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { 91 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) {
84 manager_->Remove(extension_->id(), api_resource_id); 92 manager_->Remove(extension_->id(), api_resource_id);
85 } 93 }
86 94
87 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() 95 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction()
88 : io_thread_(g_browser_process->io_thread()), 96 : io_thread_(g_browser_process->io_thread()),
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 204
197 void SocketConnectFunction::AsyncWorkStart() { 205 void SocketConnectFunction::AsyncWorkStart() {
198 socket_ = GetSocket(socket_id_); 206 socket_ = GetSocket(socket_id_);
199 if (!socket_) { 207 if (!socket_) {
200 error_ = kSocketNotFoundError; 208 error_ = kSocketNotFoundError;
201 SetResult(new base::FundamentalValue(-1)); 209 SetResult(new base::FundamentalValue(-1));
202 AsyncWorkCompleted(); 210 AsyncWorkCompleted();
203 return; 211 return;
204 } 212 }
205 213
214 socket_->set_hostname(hostname_);
215
206 SocketPermissionRequest::OperationType operation_type; 216 SocketPermissionRequest::OperationType operation_type;
207 switch (socket_->GetSocketType()) { 217 switch (socket_->GetSocketType()) {
208 case Socket::TYPE_TCP: 218 case Socket::TYPE_TCP:
209 operation_type = SocketPermissionRequest::TCP_CONNECT; 219 operation_type = SocketPermissionRequest::TCP_CONNECT;
210 break; 220 break;
211 case Socket::TYPE_UDP: 221 case Socket::TYPE_UDP:
212 operation_type = SocketPermissionRequest::UDP_SEND_TO; 222 operation_type = SocketPermissionRequest::UDP_SEND_TO;
213 break; 223 break;
214 default: 224 default:
215 NOTREACHED() << "Unknown socket type."; 225 NOTREACHED() << "Unknown socket type.";
(...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
924
925 Socket* socket = GetSocket(params_->socket_id);
926 if (!socket) {
927 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT));
928 error_ = kSocketNotFoundError;
929 AsyncWorkCompleted();
930 return;
931 }
932
933 // Make sure that the socket is neither UDP nor already TLS. Also make
934 // sure that it's a client stream.
935 if (socket->GetSocketType() != Socket::TYPE_TCP ||
936 static_cast<TCPSocket*>(socket)->ClientStream() == NULL) {
937 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT));
938 error_ = kSecureSocketTypeError;
939 AsyncWorkCompleted();
940 return;
941 }
942
943 if (!socket->IsConnected()) {
944 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT));
945 error_ = kSocketNotConnectedError;
946 AsyncWorkCompleted();
947 return;
948 }
949
950 Profile* profile = GetProfile();
951 DCHECK(profile);
952
953 scoped_refptr<net::SSLConfigService> config_service(
954 profile->GetSSLConfigService());
955
956 TLSSocket::UpgradeSocketToTLS(
957 socket,
958 config_service,
959 url_request_getter_,
960 extension_id(),
961 params_->options.get(),
962 base::Bind(&SocketSecureFunction::TlsConnectDone, this));
963 }
964
965 void SocketSecureFunction::TlsConnectDone(scoped_ptr<TLSSocket> socket,
966 int result) {
967 // |socket| can only be non-null if |result| == net::OK.
968 DCHECK(result == net::OK || socket == NULL);
969
970 if (socket && result == net::OK) {
971 SetSocket(params_->socket_id, socket.release());
972 } else {
973 RemoveSocket(params_->socket_id);
974 error_ = net::ErrorToString(result);
975 }
976
977 results_ = api::socket::Secure::Results::Create(result);
978 AsyncWorkCompleted();
979 }
980
899 } // namespace extensions 981 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698