| 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/extensions/api/api_resource_controller.h" | 8 #include "chrome/browser/extensions/api/api_resource_controller.h" |
| 9 #include "chrome/browser/extensions/api/socket/socket.h" | 9 #include "chrome/browser/extensions/api/socket/socket.h" |
| 10 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | 10 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
| 11 #include "chrome/browser/extensions/api/socket/udp_socket.h" | 11 #include "chrome/browser/extensions/api/socket/udp_socket.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 #include "net/base/ip_endpoint.h" |
| 14 | 15 |
| 15 namespace extensions { | 16 namespace extensions { |
| 16 | 17 |
| 18 const char kAddressKey[] = "address"; |
| 19 const char kPortKey[] = "port"; |
| 17 const char kBytesWrittenKey[] = "bytesWritten"; | 20 const char kBytesWrittenKey[] = "bytesWritten"; |
| 18 const char kDataKey[] = "data"; | 21 const char kDataKey[] = "data"; |
| 19 const char kResultCodeKey[] = "resultCode"; | 22 const char kResultCodeKey[] = "resultCode"; |
| 20 const char kSocketIdKey[] = "socketId"; | 23 const char kSocketIdKey[] = "socketId"; |
| 21 const char kTCPOption[] = "tcp"; | 24 const char kTCPOption[] = "tcp"; |
| 22 const char kUDPOption[] = "udp"; | 25 const char kUDPOption[] = "udp"; |
| 23 | 26 |
| 24 const char kSocketNotFoundError[] = "Socket not found"; | 27 const char kSocketNotFoundError[] = "Socket not found"; |
| 25 | 28 |
| 26 SocketCreateFunction::SocketCreateFunction() | 29 SocketCreateFunction::SocketCreateFunction() |
| 27 : src_id_(-1), event_notifier_(NULL) { | 30 : src_id_(-1), event_notifier_(NULL) { |
| 28 } | 31 } |
| 29 | 32 |
| 30 bool SocketCreateFunction::Prepare() { | 33 bool SocketCreateFunction::Prepare() { |
| 31 std::string socket_type_string; | 34 std::string socket_type_string; |
| 32 size_t argument_position = 0; | 35 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type_string)); |
| 33 EXTENSION_FUNCTION_VALIDATE(args_->GetString(argument_position++, | |
| 34 &socket_type_string)); | |
| 35 EXTENSION_FUNCTION_VALIDATE(args_->GetString(argument_position++, | |
| 36 &address_)); | |
| 37 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(argument_position++, | |
| 38 &port_)); | |
| 39 | |
| 40 if (socket_type_string == kTCPOption) | 36 if (socket_type_string == kTCPOption) |
| 41 socket_type_ = kSocketTypeTCP; | 37 socket_type_ = kSocketTypeTCP; |
| 42 else if (socket_type_string == kUDPOption) | 38 else if (socket_type_string == kUDPOption) |
| 43 socket_type_ = kSocketTypeUDP; | 39 socket_type_ = kSocketTypeUDP; |
| 44 else | 40 else |
| 45 return false; | 41 return false; |
| 46 | 42 |
| 47 src_id_ = ExtractSrcId(argument_position); | 43 src_id_ = ExtractSrcId(1); |
| 48 event_notifier_ = CreateEventNotifier(src_id_); | 44 event_notifier_ = CreateEventNotifier(src_id_); |
| 49 | 45 |
| 50 return true; | 46 return true; |
| 51 } | 47 } |
| 52 | 48 |
| 53 void SocketCreateFunction::Work() { | 49 void SocketCreateFunction::Work() { |
| 54 Socket* socket = NULL; | 50 Socket* socket = NULL; |
| 55 if (socket_type_ == kSocketTypeTCP) { | 51 if (socket_type_ == kSocketTypeTCP) { |
| 56 socket = new TCPSocket(address_, port_, event_notifier_); | 52 socket = new TCPSocket(event_notifier_); |
| 57 } else { | 53 } else { |
| 58 socket = new UDPSocket(address_, port_, event_notifier_); | 54 socket = new UDPSocket(event_notifier_); |
| 59 } | 55 } |
| 60 DCHECK(socket); | 56 DCHECK(socket); |
| 61 DCHECK(socket->IsValid()); | |
| 62 | 57 |
| 63 DictionaryValue* result = new DictionaryValue(); | 58 DictionaryValue* result = new DictionaryValue(); |
| 64 | 59 |
| 65 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket)); | 60 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket)); |
| 66 result_.reset(result); | 61 result_.reset(result); |
| 67 } | 62 } |
| 68 | 63 |
| 69 bool SocketCreateFunction::Respond() { | 64 bool SocketCreateFunction::Respond() { |
| 70 return true; | 65 return true; |
| 71 } | 66 } |
| 72 | 67 |
| 73 bool SocketDestroyFunction::Prepare() { | 68 bool SocketDestroyFunction::Prepare() { |
| 74 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 69 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 75 return true; | 70 return true; |
| 76 } | 71 } |
| 77 | 72 |
| 78 void SocketDestroyFunction::Work() { | 73 void SocketDestroyFunction::Work() { |
| 79 controller()->RemoveAPIResource(socket_id_); | 74 controller()->RemoveAPIResource(socket_id_); |
| 80 } | 75 } |
| 81 | 76 |
| 82 bool SocketDestroyFunction::Respond() { | 77 bool SocketDestroyFunction::Respond() { |
| 83 return true; | 78 return true; |
| 84 } | 79 } |
| 85 | 80 |
| 86 bool SocketConnectFunction::Prepare() { | 81 bool SocketConnectFunction::Prepare() { |
| 87 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 82 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 83 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); |
| 84 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); |
| 88 return true; | 85 return true; |
| 89 } | 86 } |
| 90 | 87 |
| 91 void SocketConnectFunction::Work() { | 88 void SocketConnectFunction::Work() { |
| 92 int result = -1; | 89 int result = -1; |
| 93 Socket* socket = controller()->GetSocket(socket_id_); | 90 Socket* socket = controller()->GetSocket(socket_id_); |
| 94 if (socket) | 91 if (socket) |
| 95 result = socket->Connect(); | 92 result = socket->Connect(address_, port_); |
| 96 else | 93 else |
| 97 error_ = kSocketNotFoundError; | 94 error_ = kSocketNotFoundError; |
| 98 result_.reset(Value::CreateIntegerValue(result)); | 95 result_.reset(Value::CreateIntegerValue(result)); |
| 99 } | 96 } |
| 100 | 97 |
| 101 bool SocketConnectFunction::Respond() { | 98 bool SocketConnectFunction::Respond() { |
| 102 return true; | 99 return true; |
| 103 } | 100 } |
| 104 | 101 |
| 105 bool SocketDisconnectFunction::Prepare() { | 102 bool SocketDisconnectFunction::Prepare() { |
| 106 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 103 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 107 return true; | 104 return true; |
| 108 } | 105 } |
| 109 | 106 |
| 110 void SocketDisconnectFunction::Work() { | 107 void SocketDisconnectFunction::Work() { |
| 111 Socket* socket = controller()->GetSocket(socket_id_); | 108 Socket* socket = controller()->GetSocket(socket_id_); |
| 112 if (socket) | 109 if (socket) |
| 113 socket->Disconnect(); | 110 socket->Disconnect(); |
| 114 else | 111 else |
| 115 error_ = kSocketNotFoundError; | 112 error_ = kSocketNotFoundError; |
| 116 result_.reset(Value::CreateNullValue()); | 113 result_.reset(Value::CreateNullValue()); |
| 117 } | 114 } |
| 118 | 115 |
| 119 bool SocketDisconnectFunction::Respond() { | 116 bool SocketDisconnectFunction::Respond() { |
| 120 return true; | 117 return true; |
| 121 } | 118 } |
| 122 | 119 |
| 120 |
| 121 bool SocketBindFunction::Prepare() { |
| 122 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 123 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); |
| 124 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); |
| 125 return true; |
| 126 } |
| 127 |
| 128 void SocketBindFunction::Work() { |
| 129 int result = -1; |
| 130 Socket* socket = controller()->GetSocket(socket_id_); |
| 131 if (socket) |
| 132 result = socket->Bind(address_, port_); |
| 133 else |
| 134 error_ = kSocketNotFoundError; |
| 135 |
| 136 result_.reset(Value::CreateIntegerValue(result)); |
| 137 } |
| 138 |
| 139 bool SocketBindFunction::Respond() { |
| 140 return true; |
| 141 } |
| 142 |
| 123 bool SocketReadFunction::Prepare() { | 143 bool SocketReadFunction::Prepare() { |
| 124 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 144 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 125 return true; | 145 return true; |
| 126 } | 146 } |
| 127 | 147 |
| 128 void SocketReadFunction::Work() { | 148 void SocketReadFunction::Work() { |
| 129 // TODO(miket): this is an arbitrary number. Can we come up with one that | 149 // TODO(miket): this is an arbitrary number. Can we come up with one that |
| 130 // makes sense? | 150 // makes sense? |
| 131 const int buffer_len = 2048; | 151 const int buffer_len = 2048; |
| 132 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(buffer_len)); | 152 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(buffer_len)); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 | 217 |
| 198 DictionaryValue* result = new DictionaryValue(); | 218 DictionaryValue* result = new DictionaryValue(); |
| 199 result->SetInteger(kBytesWrittenKey, bytes_written); | 219 result->SetInteger(kBytesWrittenKey, bytes_written); |
| 200 result_.reset(result); | 220 result_.reset(result); |
| 201 } | 221 } |
| 202 | 222 |
| 203 bool SocketWriteFunction::Respond() { | 223 bool SocketWriteFunction::Respond() { |
| 204 return true; | 224 return true; |
| 205 } | 225 } |
| 206 | 226 |
| 227 bool SocketRecvFromFunction::Prepare() { |
| 228 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 229 return true; |
| 230 } |
| 231 |
| 232 void SocketRecvFromFunction::Work() { |
| 233 // TODO(miket): this is an arbitrary number. Can we come up with one that |
| 234 // makes sense? |
| 235 const int buffer_len = 2048; |
| 236 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(buffer_len)); |
| 237 Socket* socket = controller()->GetSocket(socket_id_); |
| 238 int bytes_read = -1; |
| 239 std::string ip_address_str; |
| 240 int port = 0; |
| 241 if (socket) { |
| 242 bytes_read = socket->RecvFrom(io_buffer, buffer_len, &address_); |
| 243 } |
| 244 |
| 245 // TODO(miket): the buffer-to-array functionality appears twice, once here |
| 246 // and once in socket.cc. When serial etc. is converted over, it'll appear |
| 247 // there, too. What's a good single place for it to live? Keep in mind that |
| 248 // this is short-term code, to be replaced with ArrayBuffer code. |
| 249 DictionaryValue* result = new DictionaryValue(); |
| 250 ListValue* data_value = new ListValue(); |
| 251 if (bytes_read > 0) { |
| 252 Socket::IPEndPointToStringAndPort(address_, &ip_address_str, &port); |
| 253 size_t bytes_size = static_cast<size_t>(bytes_read); |
| 254 const char* io_buffer_start = io_buffer->data(); |
| 255 for (size_t i = 0; i < bytes_size; ++i) { |
| 256 data_value->Set(i, Value::CreateIntegerValue(io_buffer_start[i])); |
| 257 } |
| 258 } |
| 259 |
| 260 result->SetInteger(kResultCodeKey, bytes_read); |
| 261 result->Set(kDataKey, data_value); |
| 262 result->SetString(kAddressKey, ip_address_str); |
| 263 result->SetInteger(kPortKey, port); |
| 264 result_.reset(result); |
| 265 } |
| 266 |
| 267 bool SocketRecvFromFunction::Respond() { |
| 268 return true; |
| 269 } |
| 270 |
| 271 SocketSendToFunction::SocketSendToFunction() |
| 272 : socket_id_(0), |
| 273 io_buffer_(NULL) { |
| 274 } |
| 275 |
| 276 SocketSendToFunction::~SocketSendToFunction() { |
| 277 } |
| 278 |
| 279 bool SocketSendToFunction::Prepare() { |
| 280 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 281 base::ListValue *data_list_value; |
| 282 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &data_list_value)); |
| 283 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &address_)); |
| 284 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); |
| 285 |
| 286 size_t size = data_list_value->GetSize(); |
| 287 if (size != 0) { |
| 288 io_buffer_ = new net::IOBufferWithSize(size); |
| 289 uint8* data_buffer = |
| 290 reinterpret_cast<uint8*>(io_buffer_->data()); |
| 291 for (size_t i = 0; i < size; ++i) { |
| 292 int int_value = -1; |
| 293 data_list_value->GetInteger(i, &int_value); |
| 294 DCHECK(int_value < 256); |
| 295 DCHECK(int_value >= 0); |
| 296 uint8 truncated_int = static_cast<uint8>(int_value); |
| 297 *data_buffer++ = truncated_int; |
| 298 } |
| 299 } |
| 300 return true; |
| 301 } |
| 302 |
| 303 void SocketSendToFunction::Work() { |
| 304 int bytes_written = -1; |
| 305 Socket* socket = controller()->GetSocket(socket_id_); |
| 306 if (socket) { |
| 307 bytes_written = socket->SendTo(io_buffer_, io_buffer_->size(), address_, |
| 308 port_); |
| 309 } else { |
| 310 error_ = kSocketNotFoundError; |
| 311 } |
| 312 |
| 313 DictionaryValue* result = new DictionaryValue(); |
| 314 result->SetInteger(kBytesWrittenKey, bytes_written); |
| 315 result_.reset(result); |
| 316 } |
| 317 |
| 318 bool SocketSendToFunction::Respond() { |
| 319 return true; |
| 320 } |
| 321 |
| 207 } // namespace extensions | 322 } // namespace extensions |
| OLD | NEW |