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

Unified Diff: chrome/browser/extensions/api/socket/socket_api.cc

Issue 10095020: Allow socket API to send binary data. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch Set 1 Created 8 years, 8 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 side-by-side diff with in-line comments
Download patch
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..ddbb9b3bd58f773de9ecee3b3d38d7a2dc267119 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;
jeremya 2012/04/16 22:28:27 FWIW, some noodling around with getsockopt(SO_RCVB
+ 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,19 +155,44 @@ 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) {
jeremya 2012/04/16 22:28:27 https://memegen.googleplex.com/5604701
+ io_buffer_ = new net::IOBufferWithSize(size);
+ unsigned char* data_buffer =
miket_OOO 2012/04/16 20:15:23 Note reinterpret_cast. I think this is defensible;
asargent_no_longer_on_chrome 2012/04/16 22:51:13 Seems ok, but I think it might be better to use ui
miket_OOO 2012/04/16 23:15:53 Done.
+ reinterpret_cast<unsigned char*>(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);
+ unsigned char truncated_int = static_cast<unsigned char>(int_value);
+ *data_buffer++ = truncated_int;
+ }
+ }
return true;
}
void SocketWriteFunction::Work() {
int bytes_written = -1;
Socket* socket = controller()->GetSocket(socket_id_);
- if (socket)
- bytes_written = socket->Write(message_);
- else
+ if (socket) {
+ bytes_written = socket->Write(io_buffer_, io_buffer_->size());
+ } else {
asargent_no_longer_on_chrome 2012/04/16 22:51:13 nit: looks like elsewhere in this file you use the
miket_OOO 2012/04/16 23:15:53 Right! I was blindly listening to the lint checker
error_ = kSocketNotFoundError;
+ }
DictionaryValue* result = new DictionaryValue();
result->SetInteger(kBytesWrittenKey, bytes_written);

Powered by Google App Engine
This is Rietveld 408576698