| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 SocketCreateFunction::~SocketCreateFunction() {} | 94 SocketCreateFunction::~SocketCreateFunction() {} |
| 93 | 95 |
| 94 bool SocketCreateFunction::Prepare() { | 96 bool SocketCreateFunction::Prepare() { |
| 95 params_ = api::experimental_socket::Create::Params::Create(*args_); | 97 params_ = api::experimental_socket::Create::Params::Create(*args_); |
| 96 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 98 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 97 | 99 |
| 98 if (params_->type == kTCPOption) { | 100 if (params_->type == kTCPOption) { |
| 99 socket_type_ = kSocketTypeTCP; | 101 socket_type_ = kSocketTypeTCP; |
| 100 } else if (params_->type == kUDPOption) { | 102 } else if (params_->type == kUDPOption) { |
| 101 socket_type_ = kSocketTypeUDP; | 103 socket_type_ = kSocketTypeUDP; |
| 104 } else if (params_->type == kSSLOption) { |
| 105 socket_type_ = kSocketTypeSSL; |
| 102 } else { | 106 } else { |
| 103 error_ = kSocketTypeInvalidError; | 107 error_ = kSocketTypeInvalidError; |
| 104 return false; | 108 return false; |
| 105 } | 109 } |
| 106 if (params_->options.get()) { | 110 if (params_->options.get()) { |
| 107 scoped_ptr<DictionaryValue> options = params_->options->ToValue(); | 111 scoped_ptr<DictionaryValue> options = params_->options->ToValue(); |
| 108 src_id_ = ExtractSrcId(options.get()); | 112 src_id_ = ExtractSrcId(options.get()); |
| 109 event_notifier_ = CreateEventNotifier(src_id_); | 113 event_notifier_ = CreateEventNotifier(src_id_); |
| 110 } | 114 } |
| 111 | 115 |
| 112 return true; | 116 return true; |
| 113 } | 117 } |
| 114 | 118 |
| 115 void SocketCreateFunction::Work() { | 119 void SocketCreateFunction::Work() { |
| 116 Socket* socket = NULL; | 120 Socket* socket = NULL; |
| 117 if (socket_type_ == kSocketTypeTCP) { | 121 if (socket_type_ == kSocketTypeTCP) { |
| 118 socket = new TCPSocket(event_notifier_); | 122 socket = new TCPSocket(event_notifier_); |
| 119 } else if (socket_type_== kSocketTypeUDP) { | 123 } else if (socket_type_ == kSocketTypeUDP) { |
| 120 socket = new UDPSocket(event_notifier_); | 124 socket = new UDPSocket(event_notifier_); |
| 125 } else if (socket_type_ == kSocketTypeSSL) { |
| 126 socket = new SSLSocket(event_notifier_); |
| 121 } | 127 } |
| 122 DCHECK(socket); | 128 DCHECK(socket); |
| 123 | 129 |
| 124 DictionaryValue* result = new DictionaryValue(); | 130 DictionaryValue* result = new DictionaryValue(); |
| 125 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket)); | 131 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket)); |
| 126 result_.reset(result); | 132 result_.reset(result); |
| 127 } | 133 } |
| 128 | 134 |
| 129 bool SocketDestroyFunction::Prepare() { | 135 bool SocketDestroyFunction::Prepare() { |
| 130 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 136 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 | 178 |
| 173 socket->Connect(resolved_address_, port_, | 179 socket->Connect(resolved_address_, port_, |
| 174 base::Bind(&SocketConnectFunction::OnConnect, this)); | 180 base::Bind(&SocketConnectFunction::OnConnect, this)); |
| 175 } | 181 } |
| 176 | 182 |
| 177 void SocketConnectFunction::OnConnect(int result) { | 183 void SocketConnectFunction::OnConnect(int result) { |
| 178 result_.reset(Value::CreateIntegerValue(result)); | 184 result_.reset(Value::CreateIntegerValue(result)); |
| 179 AsyncWorkCompleted(); | 185 AsyncWorkCompleted(); |
| 180 } | 186 } |
| 181 | 187 |
| 188 bool SocketHandshakeFunction::Prepare() { |
| 189 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 190 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &ssl_hostname_)); |
| 191 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &ssl_port_)); |
| 192 return true; |
| 193 } |
| 194 |
| 195 void SocketHandshakeFunction::AsyncWorkStart() { |
| 196 Socket* socket = controller()->GetSocket(socket_id_); |
| 197 if (!socket) { |
| 198 error_ = kSocketNotFoundError; |
| 199 OnCompleted(-1); |
| 200 return; |
| 201 } |
| 202 socket->PerformSSLHandshake(ssl_hostname_, |
| 203 ssl_port_, |
| 204 base::Bind(&SocketHandshakeFunction::OnCompleted, |
| 205 this)); |
| 206 } |
| 207 |
| 208 void SocketHandshakeFunction::OnCompleted(int result) { |
| 209 result_.reset(Value::CreateIntegerValue(result)); |
| 210 AsyncWorkCompleted(); |
| 211 } |
| 212 |
| 182 bool SocketDisconnectFunction::Prepare() { | 213 bool SocketDisconnectFunction::Prepare() { |
| 183 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 214 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 184 return true; | 215 return true; |
| 185 } | 216 } |
| 186 | 217 |
| 187 void SocketDisconnectFunction::Work() { | 218 void SocketDisconnectFunction::Work() { |
| 188 Socket* socket = controller()->GetSocket(socket_id_); | 219 Socket* socket = controller()->GetSocket(socket_id_); |
| 189 if (socket) | 220 if (socket) |
| 190 socket->Disconnect(); | 221 socket->Disconnect(); |
| 191 else | 222 else |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 bool result = false; | 464 bool result = false; |
| 434 Socket* socket = controller()->GetSocket(params_->socket_id); | 465 Socket* socket = controller()->GetSocket(params_->socket_id); |
| 435 if (socket) | 466 if (socket) |
| 436 result = socket->SetNoDelay(params_->no_delay); | 467 result = socket->SetNoDelay(params_->no_delay); |
| 437 else | 468 else |
| 438 error_ = kSocketNotFoundError; | 469 error_ = kSocketNotFoundError; |
| 439 result_.reset(Value::CreateBooleanValue(result)); | 470 result_.reset(Value::CreateBooleanValue(result)); |
| 440 } | 471 } |
| 441 | 472 |
| 442 } // namespace extensions | 473 } // namespace extensions |
| OLD | NEW |