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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/values.h"
9 #include "chrome/browser/extensions/api/api_resource_controller.h" 8 #include "chrome/browser/extensions/api/api_resource_controller.h"
10 #include "chrome/browser/extensions/api/socket/socket.h" 9 #include "chrome/browser/extensions/api/socket/socket.h"
11 #include "chrome/browser/extensions/api/socket/tcp_socket.h" 10 #include "chrome/browser/extensions/api/socket/tcp_socket.h"
12 #include "chrome/browser/extensions/api/socket/udp_socket.h" 11 #include "chrome/browser/extensions/api/socket/udp_socket.h"
13 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
13 #include "net/base/io_buffer.h"
14 14
15 namespace extensions { 15 namespace extensions {
16 16
17 const char kBytesWrittenKey[] = "bytesWritten"; 17 const char kBytesWrittenKey[] = "bytesWritten";
18 const char kMessageKey[] = "message"; 18 const char kDataKey[] = "data";
19 const char kSocketIdKey[] = "socketId"; 19 const char kSocketIdKey[] = "socketId";
20 const char kTCPOption[] = "tcp"; 20 const char kTCPOption[] = "tcp";
21 const char kUDPOption[] = "udp"; 21 const char kUDPOption[] = "udp";
22 22
23 const char kSocketNotFoundError[] = "Socket not found"; 23 const char kSocketNotFoundError[] = "Socket not found";
24 24
25 SocketCreateFunction::SocketCreateFunction() 25 SocketCreateFunction::SocketCreateFunction()
26 : src_id_(-1), event_notifier_(NULL) { 26 : src_id_(-1), event_notifier_(NULL) {
27 } 27 }
28 28
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 bool SocketDisconnectFunction::Respond() { 118 bool SocketDisconnectFunction::Respond() {
119 return true; 119 return true;
120 } 120 }
121 121
122 bool SocketReadFunction::Prepare() { 122 bool SocketReadFunction::Prepare() {
123 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 123 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
124 return true; 124 return true;
125 } 125 }
126 126
127 void SocketReadFunction::Work() { 127 void SocketReadFunction::Work() {
128 std::string message; 128 // TODO(miket): this is an arbitrary number. Can we come up with one that
129 // makes sense?
130 const int buffer_len = 2048;
jeremya 2012/04/16 22:28:27 FWIW, some noodling around with getsockopt(SO_RCVB
131 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(buffer_len));
129 Socket* socket = controller()->GetSocket(socket_id_); 132 Socket* socket = controller()->GetSocket(socket_id_);
133 int bytes_read = -1;
130 if (socket) 134 if (socket)
131 message = socket->Read(); 135 bytes_read = socket->Read(io_buffer, buffer_len);
132 136
137 // TODO(miket): the buffer-to-array functionality appears twice, once here
138 // and once in socket.cc. When serial etc. is converted over, it'll appear
139 // there, too. What's a good single place for it to live? Keep in mind that
140 // this is short-term code, to be replaced with ArrayBuffer code.
133 DictionaryValue* result = new DictionaryValue(); 141 DictionaryValue* result = new DictionaryValue();
134 result->SetString(kMessageKey, message); 142 ListValue* data_value = new ListValue();
143 if (bytes_read > 0) {
144 size_t bytes_size = static_cast<size_t>(bytes_read);
145 const char* io_buffer_start = io_buffer->data();
146 for (size_t i = 0; i < bytes_size; ++i) {
147 data_value->Set(i, Value::CreateIntegerValue(io_buffer_start[i]));
148 }
149 }
150 result->Set(kDataKey, data_value);
135 result_.reset(result); 151 result_.reset(result);
136 } 152 }
137 153
138 bool SocketReadFunction::Respond() { 154 bool SocketReadFunction::Respond() {
139 return true; 155 return true;
140 } 156 }
141 157
158 SocketWriteFunction::SocketWriteFunction()
159 : socket_id_(0),
160 io_buffer_(NULL) {
161 }
162
163 SocketWriteFunction::~SocketWriteFunction() {
164 }
165
142 bool SocketWriteFunction::Prepare() { 166 bool SocketWriteFunction::Prepare() {
143 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 167 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
144 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &message_)); 168 base::ListValue *data_list_value;
169 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &data_list_value));
170
171 size_t size = data_list_value->GetSize();
172 if (size != 0) {
jeremya 2012/04/16 22:28:27 https://memegen.googleplex.com/5604701
173 io_buffer_ = new net::IOBufferWithSize(size);
174 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.
175 reinterpret_cast<unsigned char*>(io_buffer_->data());
176 for (size_t i = 0; i < size; ++i) {
177 int int_value = -1;
178 data_list_value->GetInteger(i, &int_value);
179 DCHECK(int_value < 256);
180 DCHECK(int_value >= 0);
181 unsigned char truncated_int = static_cast<unsigned char>(int_value);
182 *data_buffer++ = truncated_int;
183 }
184 }
145 return true; 185 return true;
146 } 186 }
147 187
148 void SocketWriteFunction::Work() { 188 void SocketWriteFunction::Work() {
149 int bytes_written = -1; 189 int bytes_written = -1;
150 Socket* socket = controller()->GetSocket(socket_id_); 190 Socket* socket = controller()->GetSocket(socket_id_);
151 if (socket) 191 if (socket) {
152 bytes_written = socket->Write(message_); 192 bytes_written = socket->Write(io_buffer_, io_buffer_->size());
153 else 193 } 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
154 error_ = kSocketNotFoundError; 194 error_ = kSocketNotFoundError;
195 }
155 196
156 DictionaryValue* result = new DictionaryValue(); 197 DictionaryValue* result = new DictionaryValue();
157 result->SetInteger(kBytesWrittenKey, bytes_written); 198 result->SetInteger(kBytesWrittenKey, bytes_written);
158 result_.reset(result); 199 result_.reset(result);
159 } 200 }
160 201
161 bool SocketWriteFunction::Respond() { 202 bool SocketWriteFunction::Respond() {
162 return true; 203 return true;
163 } 204 }
164 205
165 } // namespace extensions 206 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698