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

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: An integration test, and some nits fixed. 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[] =
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
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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 206
197 void SocketConnectFunction::AsyncWorkStart() { 207 void SocketConnectFunction::AsyncWorkStart() {
198 socket_ = GetSocket(socket_id_); 208 socket_ = GetSocket(socket_id_);
199 if (!socket_) { 209 if (!socket_) {
200 error_ = kSocketNotFoundError; 210 error_ = kSocketNotFoundError;
201 SetResult(new base::FundamentalValue(-1)); 211 SetResult(new base::FundamentalValue(-1));
202 AsyncWorkCompleted(); 212 AsyncWorkCompleted();
203 return; 213 return;
204 } 214 }
205 215
216 socket_->set_hostname(hostname_);
217
206 SocketPermissionRequest::OperationType operation_type; 218 SocketPermissionRequest::OperationType operation_type;
207 switch (socket_->GetSocketType()) { 219 switch (socket_->GetSocketType()) {
208 case Socket::TYPE_TCP: 220 case Socket::TYPE_TCP:
209 operation_type = SocketPermissionRequest::TCP_CONNECT; 221 operation_type = SocketPermissionRequest::TCP_CONNECT;
210 break; 222 break;
211 case Socket::TYPE_UDP: 223 case Socket::TYPE_UDP:
212 operation_type = SocketPermissionRequest::UDP_SEND_TO; 224 operation_type = SocketPermissionRequest::UDP_SEND_TO;
213 break; 225 break;
214 default: 226 default:
215 NOTREACHED() << "Unknown socket type."; 227 NOTREACHED() << "Unknown socket type.";
(...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 SetResult(new base::FundamentalValue(result)); 901 SetResult(new base::FundamentalValue(result));
890 return; 902 return;
891 } 903 }
892 904
893 base::ListValue* values = new base::ListValue(); 905 base::ListValue* values = new base::ListValue();
894 values->AppendStrings((std::vector<std::string>&) 906 values->AppendStrings((std::vector<std::string>&)
895 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); 907 static_cast<UDPSocket*>(socket)->GetJoinedGroups());
896 SetResult(values); 908 SetResult(values);
897 } 909 }
898 910
911 SocketSecureFunction::SocketSecureFunction() {}
912 SocketSecureFunction::~SocketSecureFunction() {}
913
914 bool SocketSecureFunction::Prepare() {
915 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
916 params_ = api::socket::Secure::Params::Create(*args_);
917 EXTENSION_FUNCTION_VALIDATE(params_.get());
918 url_request_getter_ = GetProfile()->GetRequestContext();
919 return true;
920 }
921
922 // Override the regular implementation, which would call AsyncWorkCompleted
923 // immediately after Work().
924 void SocketSecureFunction::AsyncWorkStart() {
925 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
926
927 Socket* socket = GetSocket(params_->socket_id);
928 if (!socket) {
929 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT));
930 error_ = kSocketNotFoundError;
931 AsyncWorkCompleted();
932 return;
933 }
934
935 // Make sure that the socket is neither UDP nor already TLS. Also make
936 // sure that it's a client stream.
937 if (socket->GetSocketType() != Socket::TYPE_TCP ||
938 static_cast<TCPSocket*>(socket)->ClientStream() == NULL) {
939 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT));
940 error_ = kSecureSocketTypeError;
941 AsyncWorkCompleted();
942 return;
943 }
944
945 if (!socket->IsConnected()) {
946 SetResult(new base::FundamentalValue(net::ERR_INVALID_ARGUMENT));
947 error_ = kSocketNotConnectedError;
948 AsyncWorkCompleted();
949 return;
950 }
951
952 Profile* profile = GetProfile();
Ryan Sleevi 2014/02/04 22:28:14 I'm quite surprised this is safe. GetProfile() was
lally 2014/02/11 22:05:35 This isn't the normal GetProfile(). It's implemen
953 DCHECK(profile);
954
955 TLSSocket::UpgradeSocketToTLS(
956 socket, profile, url_request_getter_, extension_id(),
957 params_->options.get(), base::Bind(&SocketSecureFunction::TlsConnectDone,
958 this));
959 }
960
961 void SocketSecureFunction::TlsConnectDone(
962 scoped_ptr<TLSSocket> socket, int result) {
963 // |socket| can only be non-null if |result| == net::OK.
964 DCHECK(result == net::OK || socket == NULL);
965
966 if (socket && result == net::OK) {
967 SetSocket(params_->socket_id, socket.release());
968 } else {
969 RemoveSocket(params_->socket_id);
970 error_ = net::ErrorToString(result);
971 }
972
973 results_ = api::socket::Secure::Results::Create(result);
974 url_request_getter_->Release();
Ryan Sleevi 2014/02/04 22:28:14 Why are you manually releasing here? Won't this ca
lally 2014/02/11 22:05:35 Changed. Now passing ownership to UpgradeSocketTo
975 AsyncWorkCompleted();
976 }
977
899 } // namespace extensions 978 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698