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 "chrome/common/extensions/api/experimental.socket.h" |
13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
14 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
15 | 16 |
16 namespace extensions { | 17 namespace extensions { |
17 | 18 |
18 const char kAddressKey[] = "address"; | 19 const char kAddressKey[] = "address"; |
19 const char kPortKey[] = "port"; | 20 const char kPortKey[] = "port"; |
20 const char kBytesWrittenKey[] = "bytesWritten"; | 21 const char kBytesWrittenKey[] = "bytesWritten"; |
21 const char kDataKey[] = "data"; | 22 const char kDataKey[] = "data"; |
22 const char kResultCodeKey[] = "resultCode"; | 23 const char kResultCodeKey[] = "resultCode"; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 Socket* socket = controller()->GetSocket(socket_id_); | 135 Socket* socket = controller()->GetSocket(socket_id_); |
135 if (socket) | 136 if (socket) |
136 result = socket->Bind(address_, port_); | 137 result = socket->Bind(address_, port_); |
137 else | 138 else |
138 error_ = kSocketNotFoundError; | 139 error_ = kSocketNotFoundError; |
139 | 140 |
140 result_.reset(Value::CreateIntegerValue(result)); | 141 result_.reset(Value::CreateIntegerValue(result)); |
141 } | 142 } |
142 | 143 |
143 bool SocketReadFunction::Prepare() { | 144 bool SocketReadFunction::Prepare() { |
144 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 145 scoped_ptr<api::experimental_socket::Read::Params> params( |
| 146 api::experimental_socket::Read::Params::Create(*args_)); |
| 147 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 148 socket_id_ = params->socket_id; |
| 149 buffer_size_ = params->buffer_size.get() ? *params->buffer_size : 4096; |
145 return true; | 150 return true; |
146 } | 151 } |
147 | 152 |
148 void SocketReadFunction::AsyncWorkStart() { | 153 void SocketReadFunction::AsyncWorkStart() { |
149 Socket* socket = controller()->GetSocket(socket_id_); | 154 Socket* socket = controller()->GetSocket(socket_id_); |
150 | 155 |
151 if (!socket) { | 156 if (!socket) { |
152 error_ = kSocketNotFoundError; | 157 error_ = kSocketNotFoundError; |
153 OnCompleted(-1, NULL); | 158 OnCompleted(-1, NULL); |
154 return; | 159 return; |
155 } | 160 } |
156 | 161 |
157 // TODO(miket): this is an arbitrary number. Can we come up with one that | 162 socket->Read(buffer_size_, |
158 // makes sense? | |
159 const int buffer_len = 2048; | |
160 socket->Read(buffer_len, | |
161 base::Bind(&SocketReadFunction::OnCompleted, this)); | 163 base::Bind(&SocketReadFunction::OnCompleted, this)); |
162 } | 164 } |
163 | 165 |
164 void SocketReadFunction::OnCompleted(int bytes_read, | 166 void SocketReadFunction::OnCompleted(int bytes_read, |
165 scoped_refptr<net::IOBuffer> io_buffer) { | 167 scoped_refptr<net::IOBuffer> io_buffer) { |
166 // TODO(miket): the buffer-to-array functionality appears twice, once here | 168 // TODO(miket): the buffer-to-array functionality appears twice, once here |
167 // and once in socket.cc. When serial etc. is converted over, it'll appear | 169 // and once in socket.cc. When serial etc. is converted over, it'll appear |
168 // there, too. What's a good single place for it to live? Keep in mind that | 170 // there, too. What's a good single place for it to live? Keep in mind that |
169 // this is short-term code, to be replaced with ArrayBuffer code. | 171 // this is short-term code, to be replaced with ArrayBuffer code. |
170 DictionaryValue* result = new DictionaryValue(); | 172 DictionaryValue* result = new DictionaryValue(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 DictionaryValue* result = new DictionaryValue(); | 231 DictionaryValue* result = new DictionaryValue(); |
230 result->SetInteger(kBytesWrittenKey, bytes_written); | 232 result->SetInteger(kBytesWrittenKey, bytes_written); |
231 result_.reset(result); | 233 result_.reset(result); |
232 | 234 |
233 AsyncWorkCompleted(); | 235 AsyncWorkCompleted(); |
234 } | 236 } |
235 | 237 |
236 SocketRecvFromFunction::~SocketRecvFromFunction() {} | 238 SocketRecvFromFunction::~SocketRecvFromFunction() {} |
237 | 239 |
238 bool SocketRecvFromFunction::Prepare() { | 240 bool SocketRecvFromFunction::Prepare() { |
239 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 241 scoped_ptr<api::experimental_socket::RecvFrom::Params> params( |
| 242 api::experimental_socket::RecvFrom::Params::Create(*args_)); |
| 243 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 244 socket_id_ = params->socket_id; |
| 245 buffer_size_ = params->buffer_size.get() ? *params->buffer_size : 4096; |
240 return true; | 246 return true; |
241 } | 247 } |
242 | 248 |
243 void SocketRecvFromFunction::AsyncWorkStart() { | 249 void SocketRecvFromFunction::AsyncWorkStart() { |
244 Socket* socket = controller()->GetSocket(socket_id_); | 250 Socket* socket = controller()->GetSocket(socket_id_); |
245 // TODO(miket): this is an arbitrary number. Can we come up with one that | 251 // TODO(miket): this is an arbitrary number. Can we come up with one that |
246 // makes sense? | 252 // makes sense? |
247 if (!socket) { | 253 if (!socket) { |
248 error_ = kSocketNotFoundError; | 254 error_ = kSocketNotFoundError; |
249 OnCompleted(-1, NULL, std::string(), 0); | 255 OnCompleted(-1, NULL, std::string(), 0); |
250 return; | 256 return; |
251 } | 257 } |
252 | 258 |
253 const int buffer_len = 2048; | 259 socket->RecvFrom(buffer_size_, |
254 socket->RecvFrom(buffer_len, | |
255 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); | 260 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); |
256 } | 261 } |
257 | 262 |
258 void SocketRecvFromFunction::OnCompleted(int bytes_read, | 263 void SocketRecvFromFunction::OnCompleted(int bytes_read, |
259 scoped_refptr<net::IOBuffer> io_buffer, | 264 scoped_refptr<net::IOBuffer> io_buffer, |
260 const std::string& address, | 265 const std::string& address, |
261 int port) { | 266 int port) { |
262 // TODO(miket): the buffer-to-array functionality appears twice, once here | 267 // TODO(miket): the buffer-to-array functionality appears twice, once here |
263 // and once in socket.cc. When serial etc. is converted over, it'll appear | 268 // and once in socket.cc. When serial etc. is converted over, it'll appear |
264 // there, too. What's a good single place for it to live? Keep in mind that | 269 // there, too. What's a good single place for it to live? Keep in mind that |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 | 332 |
328 void SocketSendToFunction::OnCompleted(int bytes_written) { | 333 void SocketSendToFunction::OnCompleted(int bytes_written) { |
329 DictionaryValue* result = new DictionaryValue(); | 334 DictionaryValue* result = new DictionaryValue(); |
330 result->SetInteger(kBytesWrittenKey, bytes_written); | 335 result->SetInteger(kBytesWrittenKey, bytes_written); |
331 result_.reset(result); | 336 result_.reset(result); |
332 | 337 |
333 AsyncWorkCompleted(); | 338 AsyncWorkCompleted(); |
334 } | 339 } |
335 | 340 |
336 } // namespace extensions | 341 } // namespace extensions |
OLD | NEW |