| Index: chrome/browser/extensions/api/socket/socket_api.cc
|
| diff --git a/chrome/browser/extensions/api/socket/socket_api.cc b/chrome/browser/extensions/api/socket/socket_api.cc
|
| index 1b0b1c4ebc638bcc3562e0e9d090954ca46116fb..08e17c97ae0421f099b2f7d6cd7b8dc0e551ffb6 100644
|
| --- a/chrome/browser/extensions/api/socket/socket_api.cc
|
| +++ b/chrome/browser/extensions/api/socket/socket_api.cc
|
| @@ -5,17 +5,17 @@
|
| #include "chrome/browser/extensions/api/socket/socket_api.h"
|
|
|
| #include "base/bind.h"
|
| -#include "base/values.h"
|
| #include "chrome/browser/extensions/api/api_resource_controller.h"
|
| #include "chrome/browser/extensions/api/socket/socket.h"
|
| #include "chrome/browser/extensions/api/socket/tcp_socket.h"
|
| #include "chrome/browser/extensions/api/socket/udp_socket.h"
|
| #include "chrome/browser/extensions/extension_service.h"
|
| +#include "net/base/io_buffer.h"
|
|
|
| namespace extensions {
|
|
|
| const char kBytesWrittenKey[] = "bytesWritten";
|
| -const char kMessageKey[] = "message";
|
| +const char kDataKey[] = "data";
|
| const char kSocketIdKey[] = "socketId";
|
| const char kTCPOption[] = "tcp";
|
| const char kUDPOption[] = "udp";
|
| @@ -125,13 +125,29 @@ bool SocketReadFunction::Prepare() {
|
| }
|
|
|
| void SocketReadFunction::Work() {
|
| - std::string message;
|
| + // TODO(miket): this is an arbitrary number. Can we come up with one that
|
| + // makes sense?
|
| + const int buffer_len = 2048;
|
| + scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(buffer_len));
|
| Socket* socket = controller()->GetSocket(socket_id_);
|
| + int bytes_read = -1;
|
| if (socket)
|
| - message = socket->Read();
|
| + bytes_read = socket->Read(io_buffer, buffer_len);
|
|
|
| + // TODO(miket): the buffer-to-array functionality appears twice, once here
|
| + // and once in socket.cc. When serial etc. is converted over, it'll appear
|
| + // there, too. What's a good single place for it to live? Keep in mind that
|
| + // this is short-term code, to be replaced with ArrayBuffer code.
|
| DictionaryValue* result = new DictionaryValue();
|
| - result->SetString(kMessageKey, message);
|
| + ListValue* data_value = new ListValue();
|
| + if (bytes_read > 0) {
|
| + size_t bytes_size = static_cast<size_t>(bytes_read);
|
| + const char* io_buffer_start = io_buffer->data();
|
| + for (size_t i = 0; i < bytes_size; ++i) {
|
| + data_value->Set(i, Value::CreateIntegerValue(io_buffer_start[i]));
|
| + }
|
| + }
|
| + result->Set(kDataKey, data_value);
|
| result_.reset(result);
|
| }
|
|
|
| @@ -139,9 +155,33 @@ bool SocketReadFunction::Respond() {
|
| return true;
|
| }
|
|
|
| +SocketWriteFunction::SocketWriteFunction()
|
| + : socket_id_(0),
|
| + io_buffer_(NULL) {
|
| +}
|
| +
|
| +SocketWriteFunction::~SocketWriteFunction() {
|
| +}
|
| +
|
| bool SocketWriteFunction::Prepare() {
|
| EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
|
| - EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &message_));
|
| + base::ListValue *data_list_value;
|
| + EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &data_list_value));
|
| +
|
| + size_t size = data_list_value->GetSize();
|
| + if (size != 0) {
|
| + io_buffer_ = new net::IOBufferWithSize(size);
|
| + uint8* data_buffer =
|
| + reinterpret_cast<uint8*>(io_buffer_->data());
|
| + for (size_t i = 0; i < size; ++i) {
|
| + int int_value = -1;
|
| + data_list_value->GetInteger(i, &int_value);
|
| + DCHECK(int_value < 256);
|
| + DCHECK(int_value >= 0);
|
| + uint8 truncated_int = static_cast<uint8>(int_value);
|
| + *data_buffer++ = truncated_int;
|
| + }
|
| + }
|
| return true;
|
| }
|
|
|
| @@ -149,8 +189,8 @@ void SocketWriteFunction::Work() {
|
| int bytes_written = -1;
|
| Socket* socket = controller()->GetSocket(socket_id_);
|
| if (socket)
|
| - bytes_written = socket->Write(message_);
|
| - else
|
| + bytes_written = socket->Write(io_buffer_, io_buffer_->size());
|
| + else
|
| error_ = kSocketNotFoundError;
|
|
|
| DictionaryValue* result = new DictionaryValue();
|
|
|