| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/extensions/api/api_resource_controller.h" | 9 #include "chrome/browser/extensions/api/api_resource_controller.h" |
| 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
| 11 #include "chrome/browser/extensions/api/socket/socket.h" | 11 #include "chrome/browser/extensions/api/socket/socket.h" |
| 12 #include "chrome/browser/extensions/api/socket/ssl_socket.h" |
| 12 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | 13 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| 13 #include "chrome/browser/extensions/api/socket/udp_socket.h" | 14 #include "chrome/browser/extensions/api/socket/udp_socket.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/io_thread.h" | 16 #include "chrome/browser/io_thread.h" |
| 16 #include "net/base/host_port_pair.h" | 17 #include "net/base/host_port_pair.h" |
| 17 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 18 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_log.h" | 21 #include "net/base/net_log.h" |
| 21 | 22 |
| 22 namespace extensions { | 23 namespace extensions { |
| 23 | 24 |
| 24 const char kAddressKey[] = "address"; | 25 const char kAddressKey[] = "address"; |
| 25 const char kPortKey[] = "port"; | 26 const char kPortKey[] = "port"; |
| 26 const char kBytesWrittenKey[] = "bytesWritten"; | 27 const char kBytesWrittenKey[] = "bytesWritten"; |
| 27 const char kDataKey[] = "data"; | 28 const char kDataKey[] = "data"; |
| 28 const char kResultCodeKey[] = "resultCode"; | 29 const char kResultCodeKey[] = "resultCode"; |
| 29 const char kSocketIdKey[] = "socketId"; | 30 const char kSocketIdKey[] = "socketId"; |
| 30 const char kTCPOption[] = "tcp"; | 31 const char kTCPOption[] = "tcp"; |
| 31 const char kUDPOption[] = "udp"; | 32 const char kUDPOption[] = "udp"; |
| 33 const char kSSLOption[] = "ssl"; |
| 32 | 34 |
| 33 const char kSocketNotFoundError[] = "Socket not found"; | 35 const char kSocketNotFoundError[] = "Socket not found"; |
| 34 const char kSocketTypeInvalidError[] = "Socket type is not supported"; | 36 const char kSocketTypeInvalidError[] = "Socket type is not supported"; |
| 35 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 37 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
| 36 | 38 |
| 37 void SocketExtensionFunction::Work() { | 39 void SocketExtensionFunction::Work() { |
| 38 } | 40 } |
| 39 | 41 |
| 40 bool SocketExtensionFunction::Respond() { | 42 bool SocketExtensionFunction::Respond() { |
| 41 return error_.empty(); | 43 return error_.empty(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 | 93 |
| 92 SocketCreateFunction::~SocketCreateFunction() {} | 94 SocketCreateFunction::~SocketCreateFunction() {} |
| 93 | 95 |
| 94 bool SocketCreateFunction::Prepare() { | 96 bool SocketCreateFunction::Prepare() { |
| 95 std::string socket_type_string; | 97 std::string socket_type_string; |
| 96 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type_string)); | 98 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type_string)); |
| 97 if (socket_type_string == kTCPOption) { | 99 if (socket_type_string == kTCPOption) { |
| 98 socket_type_ = kSocketTypeTCP; | 100 socket_type_ = kSocketTypeTCP; |
| 99 } else if (socket_type_string == kUDPOption) { | 101 } else if (socket_type_string == kUDPOption) { |
| 100 socket_type_ = kSocketTypeUDP; | 102 socket_type_ = kSocketTypeUDP; |
| 103 } else if (socket_type_string == kSSLOption) { |
| 104 socket_type_ = kSocketTypeSSL; |
| 101 } else { | 105 } else { |
| 102 error_ = kSocketTypeInvalidError; | 106 error_ = kSocketTypeInvalidError; |
| 103 return false; | 107 return false; |
| 104 } | 108 } |
| 105 | 109 |
| 106 src_id_ = ExtractSrcId(1); | 110 src_id_ = ExtractSrcId(1); |
| 107 event_notifier_ = CreateEventNotifier(src_id_); | 111 event_notifier_ = CreateEventNotifier(src_id_); |
| 108 | 112 |
| 109 return true; | 113 return true; |
| 110 } | 114 } |
| 111 | 115 |
| 112 void SocketCreateFunction::Work() { | 116 void SocketCreateFunction::Work() { |
| 113 Socket* socket = NULL; | 117 Socket* socket = NULL; |
| 114 if (socket_type_ == kSocketTypeTCP) { | 118 if (socket_type_ == kSocketTypeTCP) { |
| 115 socket = new TCPSocket(event_notifier_); | 119 socket = new TCPSocket(event_notifier_); |
| 116 } else if (socket_type_== kSocketTypeUDP) { | 120 } else if (socket_type_ == kSocketTypeUDP) { |
| 117 socket = new UDPSocket(event_notifier_); | 121 socket = new UDPSocket(event_notifier_); |
| 122 } else if (socket_type_ == kSocketTypeSSL) { |
| 123 socket = new SSLSocket(event_notifier_); |
| 118 } | 124 } |
| 119 DCHECK(socket); | 125 DCHECK(socket); |
| 120 | 126 |
| 121 DictionaryValue* result = new DictionaryValue(); | 127 DictionaryValue* result = new DictionaryValue(); |
| 122 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket)); | 128 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket)); |
| 123 result_.reset(result); | 129 result_.reset(result); |
| 124 } | 130 } |
| 125 | 131 |
| 126 bool SocketDestroyFunction::Prepare() { | 132 bool SocketDestroyFunction::Prepare() { |
| 127 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 133 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 | 175 |
| 170 socket->Connect(resolved_address_, port_, | 176 socket->Connect(resolved_address_, port_, |
| 171 base::Bind(&SocketConnectFunction::OnConnect, this)); | 177 base::Bind(&SocketConnectFunction::OnConnect, this)); |
| 172 } | 178 } |
| 173 | 179 |
| 174 void SocketConnectFunction::OnConnect(int result) { | 180 void SocketConnectFunction::OnConnect(int result) { |
| 175 result_.reset(Value::CreateIntegerValue(result)); | 181 result_.reset(Value::CreateIntegerValue(result)); |
| 176 AsyncWorkCompleted(); | 182 AsyncWorkCompleted(); |
| 177 } | 183 } |
| 178 | 184 |
| 185 bool SocketHandshakeFunction::Prepare() { |
| 186 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 187 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &ssl_hostname_)); |
| 188 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &ssl_port_)); |
| 189 return true; |
| 190 } |
| 191 |
| 192 void SocketHandshakeFunction::AsyncWorkStart() { |
| 193 Socket* socket = controller()->GetSocket(socket_id_); |
| 194 if (!socket) { |
| 195 error_ = kSocketNotFoundError; |
| 196 OnCompleted(-1); |
| 197 return; |
| 198 } |
| 199 socket->PerformSSLHandshake(ssl_hostname_, |
| 200 ssl_port_, |
| 201 base::Bind(&SocketHandshakeFunction::OnCompleted, |
| 202 this)); |
| 203 } |
| 204 |
| 205 void SocketHandshakeFunction::OnCompleted(int result) { |
| 206 result_.reset(Value::CreateIntegerValue(result)); |
| 207 AsyncWorkCompleted(); |
| 208 } |
| 209 |
| 179 bool SocketDisconnectFunction::Prepare() { | 210 bool SocketDisconnectFunction::Prepare() { |
| 180 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 211 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 181 return true; | 212 return true; |
| 182 } | 213 } |
| 183 | 214 |
| 184 void SocketDisconnectFunction::Work() { | 215 void SocketDisconnectFunction::Work() { |
| 185 Socket* socket = controller()->GetSocket(socket_id_); | 216 Socket* socket = controller()->GetSocket(socket_id_); |
| 186 if (socket) | 217 if (socket) |
| 187 socket->Disconnect(); | 218 socket->Disconnect(); |
| 188 else | 219 else |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 bool result = false; | 461 bool result = false; |
| 431 Socket* socket = controller()->GetSocket(params_->socket_id); | 462 Socket* socket = controller()->GetSocket(params_->socket_id); |
| 432 if (socket) | 463 if (socket) |
| 433 result = socket->SetNoDelay(params_->no_delay); | 464 result = socket->SetNoDelay(params_->no_delay); |
| 434 else | 465 else |
| 435 error_ = kSocketNotFoundError; | 466 error_ = kSocketNotFoundError; |
| 436 result_.reset(Value::CreateBooleanValue(result)); | 467 result_.reset(Value::CreateBooleanValue(result)); |
| 437 } | 468 } |
| 438 | 469 |
| 439 } // namespace extensions | 470 } // namespace extensions |
| OLD | NEW |