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/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/extensions/api/api_resource_controller.h" | 9 #include "chrome/browser/extensions/api/api_resource_controller.h" |
10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 Socket* socket = NULL; | 113 Socket* socket = NULL; |
114 if (socket_type_ == kSocketTypeTCP) { | 114 if (socket_type_ == kSocketTypeTCP) { |
115 socket = new TCPSocket(event_notifier_); | 115 socket = new TCPSocket(event_notifier_); |
116 } else if (socket_type_== kSocketTypeUDP) { | 116 } else if (socket_type_== kSocketTypeUDP) { |
117 socket = new UDPSocket(event_notifier_); | 117 socket = new UDPSocket(event_notifier_); |
118 } | 118 } |
119 DCHECK(socket); | 119 DCHECK(socket); |
120 | 120 |
121 DictionaryValue* result = new DictionaryValue(); | 121 DictionaryValue* result = new DictionaryValue(); |
122 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket)); | 122 result->SetInteger(kSocketIdKey, controller()->AddAPIResource(socket)); |
123 result_.reset(result); | 123 SetSingleResult(result); |
124 } | 124 } |
125 | 125 |
126 bool SocketDestroyFunction::Prepare() { | 126 bool SocketDestroyFunction::Prepare() { |
127 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 127 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
128 return true; | 128 return true; |
129 } | 129 } |
130 | 130 |
131 void SocketDestroyFunction::Work() { | 131 void SocketDestroyFunction::Work() { |
132 if (!controller()->RemoveSocket(socket_id_)) | 132 if (!controller()->RemoveSocket(socket_id_)) |
133 error_ = kSocketNotFoundError; | 133 error_ = kSocketNotFoundError; |
(...skipping 13 matching lines...) Expand all Loading... |
147 } | 147 } |
148 | 148 |
149 void SocketConnectFunction::AsyncWorkStart() { | 149 void SocketConnectFunction::AsyncWorkStart() { |
150 StartDnsLookup(hostname_); | 150 StartDnsLookup(hostname_); |
151 } | 151 } |
152 | 152 |
153 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { | 153 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { |
154 if (lookup_result == net::OK) { | 154 if (lookup_result == net::OK) { |
155 StartConnect(); | 155 StartConnect(); |
156 } else { | 156 } else { |
157 result_.reset(Value::CreateIntegerValue(lookup_result)); | 157 SetSingleResult(Value::CreateIntegerValue(lookup_result)); |
158 AsyncWorkCompleted(); | 158 AsyncWorkCompleted(); |
159 } | 159 } |
160 } | 160 } |
161 | 161 |
162 void SocketConnectFunction::StartConnect() { | 162 void SocketConnectFunction::StartConnect() { |
163 Socket* socket = controller()->GetSocket(socket_id_); | 163 Socket* socket = controller()->GetSocket(socket_id_); |
164 if (!socket) { | 164 if (!socket) { |
165 error_ = kSocketNotFoundError; | 165 error_ = kSocketNotFoundError; |
166 OnConnect(-1); | 166 OnConnect(-1); |
167 return; | 167 return; |
168 } | 168 } |
169 | 169 |
170 socket->Connect(resolved_address_, port_, | 170 socket->Connect(resolved_address_, port_, |
171 base::Bind(&SocketConnectFunction::OnConnect, this)); | 171 base::Bind(&SocketConnectFunction::OnConnect, this)); |
172 } | 172 } |
173 | 173 |
174 void SocketConnectFunction::OnConnect(int result) { | 174 void SocketConnectFunction::OnConnect(int result) { |
175 result_.reset(Value::CreateIntegerValue(result)); | 175 SetSingleResult(Value::CreateIntegerValue(result)); |
176 AsyncWorkCompleted(); | 176 AsyncWorkCompleted(); |
177 } | 177 } |
178 | 178 |
179 bool SocketDisconnectFunction::Prepare() { | 179 bool SocketDisconnectFunction::Prepare() { |
180 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 180 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
181 return true; | 181 return true; |
182 } | 182 } |
183 | 183 |
184 void SocketDisconnectFunction::Work() { | 184 void SocketDisconnectFunction::Work() { |
185 Socket* socket = controller()->GetSocket(socket_id_); | 185 Socket* socket = controller()->GetSocket(socket_id_); |
186 if (socket) | 186 if (socket) |
187 socket->Disconnect(); | 187 socket->Disconnect(); |
188 else | 188 else |
189 error_ = kSocketNotFoundError; | 189 error_ = kSocketNotFoundError; |
190 result_.reset(Value::CreateNullValue()); | 190 SetSingleResult(Value::CreateNullValue()); |
191 } | 191 } |
192 | 192 |
193 bool SocketBindFunction::Prepare() { | 193 bool SocketBindFunction::Prepare() { |
194 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 194 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
195 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); | 195 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); |
196 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); | 196 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); |
197 return true; | 197 return true; |
198 } | 198 } |
199 | 199 |
200 void SocketBindFunction::Work() { | 200 void SocketBindFunction::Work() { |
201 int result = -1; | 201 int result = -1; |
202 Socket* socket = controller()->GetSocket(socket_id_); | 202 Socket* socket = controller()->GetSocket(socket_id_); |
203 if (socket) | 203 if (socket) |
204 result = socket->Bind(address_, port_); | 204 result = socket->Bind(address_, port_); |
205 else | 205 else |
206 error_ = kSocketNotFoundError; | 206 error_ = kSocketNotFoundError; |
207 | 207 |
208 result_.reset(Value::CreateIntegerValue(result)); | 208 SetSingleResult(Value::CreateIntegerValue(result)); |
209 } | 209 } |
210 | 210 |
211 SocketReadFunction::SocketReadFunction() | 211 SocketReadFunction::SocketReadFunction() |
212 : params_(NULL) { | 212 : params_(NULL) { |
213 } | 213 } |
214 | 214 |
215 SocketReadFunction::~SocketReadFunction() {} | 215 SocketReadFunction::~SocketReadFunction() {} |
216 | 216 |
217 bool SocketReadFunction::Prepare() { | 217 bool SocketReadFunction::Prepare() { |
218 params_ = api::experimental_socket::Read::Params::Create(*args_); | 218 params_ = api::experimental_socket::Read::Params::Create(*args_); |
(...skipping 19 matching lines...) Expand all Loading... |
238 result->SetInteger(kResultCodeKey, bytes_read); | 238 result->SetInteger(kResultCodeKey, bytes_read); |
239 if (bytes_read > 0) { | 239 if (bytes_read > 0) { |
240 result->Set(kDataKey, | 240 result->Set(kDataKey, |
241 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 241 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
242 bytes_read)); | 242 bytes_read)); |
243 } else { | 243 } else { |
244 // BinaryValue does not support NULL buffer. Workaround it with new char[1]. | 244 // BinaryValue does not support NULL buffer. Workaround it with new char[1]. |
245 // http://crbug.com/127630 | 245 // http://crbug.com/127630 |
246 result->Set(kDataKey, base::BinaryValue::Create(new char[1], 0)); | 246 result->Set(kDataKey, base::BinaryValue::Create(new char[1], 0)); |
247 } | 247 } |
248 result_.reset(result); | 248 SetSingleResult(result); |
249 | 249 |
250 AsyncWorkCompleted(); | 250 AsyncWorkCompleted(); |
251 } | 251 } |
252 | 252 |
253 SocketWriteFunction::SocketWriteFunction() | 253 SocketWriteFunction::SocketWriteFunction() |
254 : socket_id_(0), | 254 : socket_id_(0), |
255 io_buffer_(NULL), | 255 io_buffer_(NULL), |
256 io_buffer_size_(0) { | 256 io_buffer_size_(0) { |
257 } | 257 } |
258 | 258 |
(...skipping 18 matching lines...) Expand all Loading... |
277 return; | 277 return; |
278 } | 278 } |
279 | 279 |
280 socket->Write(io_buffer_, io_buffer_size_, | 280 socket->Write(io_buffer_, io_buffer_size_, |
281 base::Bind(&SocketWriteFunction::OnCompleted, this)); | 281 base::Bind(&SocketWriteFunction::OnCompleted, this)); |
282 } | 282 } |
283 | 283 |
284 void SocketWriteFunction::OnCompleted(int bytes_written) { | 284 void SocketWriteFunction::OnCompleted(int bytes_written) { |
285 DictionaryValue* result = new DictionaryValue(); | 285 DictionaryValue* result = new DictionaryValue(); |
286 result->SetInteger(kBytesWrittenKey, bytes_written); | 286 result->SetInteger(kBytesWrittenKey, bytes_written); |
287 result_.reset(result); | 287 SetSingleResult(result); |
288 | 288 |
289 AsyncWorkCompleted(); | 289 AsyncWorkCompleted(); |
290 } | 290 } |
291 | 291 |
292 SocketRecvFromFunction::SocketRecvFromFunction() | 292 SocketRecvFromFunction::SocketRecvFromFunction() |
293 : params_(NULL) { | 293 : params_(NULL) { |
294 } | 294 } |
295 | 295 |
296 SocketRecvFromFunction::~SocketRecvFromFunction() {} | 296 SocketRecvFromFunction::~SocketRecvFromFunction() {} |
297 | 297 |
(...skipping 25 matching lines...) Expand all Loading... |
323 result->Set(kDataKey, | 323 result->Set(kDataKey, |
324 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 324 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
325 bytes_read)); | 325 bytes_read)); |
326 } else { | 326 } else { |
327 // BinaryValue does not support NULL buffer. Workaround it with new char[1]. | 327 // BinaryValue does not support NULL buffer. Workaround it with new char[1]. |
328 // http://crbug.com/127630 | 328 // http://crbug.com/127630 |
329 result->Set(kDataKey, base::BinaryValue::Create(new char[1], 0)); | 329 result->Set(kDataKey, base::BinaryValue::Create(new char[1], 0)); |
330 } | 330 } |
331 result->SetString(kAddressKey, address); | 331 result->SetString(kAddressKey, address); |
332 result->SetInteger(kPortKey, port); | 332 result->SetInteger(kPortKey, port); |
333 result_.reset(result); | 333 SetSingleResult(result); |
334 | 334 |
335 AsyncWorkCompleted(); | 335 AsyncWorkCompleted(); |
336 } | 336 } |
337 | 337 |
338 SocketSendToFunction::SocketSendToFunction() | 338 SocketSendToFunction::SocketSendToFunction() |
339 : socket_id_(0), | 339 : socket_id_(0), |
340 io_buffer_(NULL), | 340 io_buffer_(NULL), |
341 io_buffer_size_(0) { | 341 io_buffer_size_(0) { |
342 } | 342 } |
343 | 343 |
(...skipping 12 matching lines...) Expand all Loading... |
356 } | 356 } |
357 | 357 |
358 void SocketSendToFunction::AsyncWorkStart() { | 358 void SocketSendToFunction::AsyncWorkStart() { |
359 StartDnsLookup(hostname_); | 359 StartDnsLookup(hostname_); |
360 } | 360 } |
361 | 361 |
362 void SocketSendToFunction::AfterDnsLookup(int lookup_result) { | 362 void SocketSendToFunction::AfterDnsLookup(int lookup_result) { |
363 if (lookup_result == net::OK) { | 363 if (lookup_result == net::OK) { |
364 StartSendTo(); | 364 StartSendTo(); |
365 } else { | 365 } else { |
366 result_.reset(Value::CreateIntegerValue(lookup_result)); | 366 SetSingleResult(Value::CreateIntegerValue(lookup_result)); |
367 AsyncWorkCompleted(); | 367 AsyncWorkCompleted(); |
368 } | 368 } |
369 } | 369 } |
370 | 370 |
371 void SocketSendToFunction::StartSendTo() { | 371 void SocketSendToFunction::StartSendTo() { |
372 Socket* socket = controller()->GetSocket(socket_id_); | 372 Socket* socket = controller()->GetSocket(socket_id_); |
373 if (!socket) { | 373 if (!socket) { |
374 error_ = kSocketNotFoundError; | 374 error_ = kSocketNotFoundError; |
375 OnCompleted(-1); | 375 OnCompleted(-1); |
376 return; | 376 return; |
377 } | 377 } |
378 | 378 |
379 socket->SendTo(io_buffer_, io_buffer_size_, resolved_address_, port_, | 379 socket->SendTo(io_buffer_, io_buffer_size_, resolved_address_, port_, |
380 base::Bind(&SocketSendToFunction::OnCompleted, this)); | 380 base::Bind(&SocketSendToFunction::OnCompleted, this)); |
381 } | 381 } |
382 | 382 |
383 void SocketSendToFunction::OnCompleted(int bytes_written) { | 383 void SocketSendToFunction::OnCompleted(int bytes_written) { |
384 DictionaryValue* result = new DictionaryValue(); | 384 DictionaryValue* result = new DictionaryValue(); |
385 result->SetInteger(kBytesWrittenKey, bytes_written); | 385 result->SetInteger(kBytesWrittenKey, bytes_written); |
386 result_.reset(result); | 386 SetSingleResult(result); |
387 | 387 |
388 AsyncWorkCompleted(); | 388 AsyncWorkCompleted(); |
389 } | 389 } |
390 | 390 |
391 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() | 391 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() |
392 : params_(NULL) { | 392 : params_(NULL) { |
393 } | 393 } |
394 | 394 |
395 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} | 395 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} |
396 | 396 |
397 bool SocketSetKeepAliveFunction::Prepare() { | 397 bool SocketSetKeepAliveFunction::Prepare() { |
398 params_ = api::experimental_socket::SetKeepAlive::Params::Create(*args_); | 398 params_ = api::experimental_socket::SetKeepAlive::Params::Create(*args_); |
399 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 399 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
400 return true; | 400 return true; |
401 } | 401 } |
402 | 402 |
403 void SocketSetKeepAliveFunction::Work() { | 403 void SocketSetKeepAliveFunction::Work() { |
404 bool result = false; | 404 bool result = false; |
405 Socket* socket = controller()->GetSocket(params_->socket_id); | 405 Socket* socket = controller()->GetSocket(params_->socket_id); |
406 if (socket) { | 406 if (socket) { |
407 int delay = 0; | 407 int delay = 0; |
408 if (params_->delay.get()) | 408 if (params_->delay.get()) |
409 delay = *params_->delay; | 409 delay = *params_->delay; |
410 result = socket->SetKeepAlive(params_->enable, delay); | 410 result = socket->SetKeepAlive(params_->enable, delay); |
411 } else { | 411 } else { |
412 error_ = kSocketNotFoundError; | 412 error_ = kSocketNotFoundError; |
413 } | 413 } |
414 result_.reset(Value::CreateBooleanValue(result)); | 414 SetSingleResult(Value::CreateBooleanValue(result)); |
415 } | 415 } |
416 | 416 |
417 SocketSetNoDelayFunction::SocketSetNoDelayFunction() | 417 SocketSetNoDelayFunction::SocketSetNoDelayFunction() |
418 : params_(NULL) { | 418 : params_(NULL) { |
419 } | 419 } |
420 | 420 |
421 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} | 421 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} |
422 | 422 |
423 bool SocketSetNoDelayFunction::Prepare() { | 423 bool SocketSetNoDelayFunction::Prepare() { |
424 params_ = api::experimental_socket::SetNoDelay::Params::Create(*args_); | 424 params_ = api::experimental_socket::SetNoDelay::Params::Create(*args_); |
425 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 425 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
426 return true; | 426 return true; |
427 } | 427 } |
428 | 428 |
429 void SocketSetNoDelayFunction::Work() { | 429 void SocketSetNoDelayFunction::Work() { |
430 bool result = false; | 430 bool result = false; |
431 Socket* socket = controller()->GetSocket(params_->socket_id); | 431 Socket* socket = controller()->GetSocket(params_->socket_id); |
432 if (socket) | 432 if (socket) |
433 result = socket->SetNoDelay(params_->no_delay); | 433 result = socket->SetNoDelay(params_->no_delay); |
434 else | 434 else |
435 error_ = kSocketNotFoundError; | 435 error_ = kSocketNotFoundError; |
436 result_.reset(Value::CreateBooleanValue(result)); | 436 SetSingleResult(Value::CreateBooleanValue(result)); |
437 } | 437 } |
438 | 438 |
439 } // namespace extensions | 439 } // namespace extensions |
OLD | NEW |