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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 144 |
145 void SocketCreateFunction::Work() { | 145 void SocketCreateFunction::Work() { |
146 Socket* socket = NULL; | 146 Socket* socket = NULL; |
147 if (socket_type_ == kSocketTypeTCP) { | 147 if (socket_type_ == kSocketTypeTCP) { |
148 socket = new TCPSocket(extension_->id()); | 148 socket = new TCPSocket(extension_->id()); |
149 } else if (socket_type_== kSocketTypeUDP) { | 149 } else if (socket_type_== kSocketTypeUDP) { |
150 socket = new UDPSocket(extension_->id()); | 150 socket = new UDPSocket(extension_->id()); |
151 } | 151 } |
152 DCHECK(socket); | 152 DCHECK(socket); |
153 | 153 |
154 DictionaryValue* result = new DictionaryValue(); | 154 base::DictionaryValue* result = new base::DictionaryValue(); |
155 result->SetInteger(kSocketIdKey, manager_->Add(socket)); | 155 result->SetInteger(kSocketIdKey, manager_->Add(socket)); |
156 SetResult(result); | 156 SetResult(result); |
157 } | 157 } |
158 | 158 |
159 bool SocketDestroyFunction::Prepare() { | 159 bool SocketDestroyFunction::Prepare() { |
160 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 160 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
161 return true; | 161 return true; |
162 } | 162 } |
163 | 163 |
164 void SocketDestroyFunction::Work() { | 164 void SocketDestroyFunction::Work() { |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 if (socket) { | 341 if (socket) { |
342 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); | 342 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); |
343 } else { | 343 } else { |
344 error_ = kSocketNotFoundError; | 344 error_ = kSocketNotFoundError; |
345 OnAccept(-1, NULL); | 345 OnAccept(-1, NULL); |
346 } | 346 } |
347 } | 347 } |
348 | 348 |
349 void SocketAcceptFunction::OnAccept(int result_code, | 349 void SocketAcceptFunction::OnAccept(int result_code, |
350 net::TCPClientSocket *socket) { | 350 net::TCPClientSocket *socket) { |
351 DictionaryValue* result = new DictionaryValue(); | 351 base::DictionaryValue* result = new base::DictionaryValue(); |
352 result->SetInteger(kResultCodeKey, result_code); | 352 result->SetInteger(kResultCodeKey, result_code); |
353 if (socket) { | 353 if (socket) { |
354 Socket *client_socket = new TCPSocket(socket, extension_id(), true); | 354 Socket *client_socket = new TCPSocket(socket, extension_id(), true); |
355 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); | 355 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); |
356 } | 356 } |
357 SetResult(result); | 357 SetResult(result); |
358 | 358 |
359 AsyncWorkCompleted(); | 359 AsyncWorkCompleted(); |
360 } | 360 } |
361 | 361 |
(...skipping 14 matching lines...) Expand all Loading... |
376 OnCompleted(-1, NULL); | 376 OnCompleted(-1, NULL); |
377 return; | 377 return; |
378 } | 378 } |
379 | 379 |
380 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096, | 380 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096, |
381 base::Bind(&SocketReadFunction::OnCompleted, this)); | 381 base::Bind(&SocketReadFunction::OnCompleted, this)); |
382 } | 382 } |
383 | 383 |
384 void SocketReadFunction::OnCompleted(int bytes_read, | 384 void SocketReadFunction::OnCompleted(int bytes_read, |
385 scoped_refptr<net::IOBuffer> io_buffer) { | 385 scoped_refptr<net::IOBuffer> io_buffer) { |
386 DictionaryValue* result = new DictionaryValue(); | 386 base::DictionaryValue* result = new base::DictionaryValue(); |
387 result->SetInteger(kResultCodeKey, bytes_read); | 387 result->SetInteger(kResultCodeKey, bytes_read); |
388 if (bytes_read > 0) { | 388 if (bytes_read > 0) { |
389 result->Set(kDataKey, | 389 result->Set(kDataKey, |
390 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 390 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
391 bytes_read)); | 391 bytes_read)); |
392 } else { | 392 } else { |
393 result->Set(kDataKey, new base::BinaryValue()); | 393 result->Set(kDataKey, new base::BinaryValue()); |
394 } | 394 } |
395 SetResult(result); | 395 SetResult(result); |
396 | 396 |
(...skipping 25 matching lines...) Expand all Loading... |
422 error_ = kSocketNotFoundError; | 422 error_ = kSocketNotFoundError; |
423 OnCompleted(-1); | 423 OnCompleted(-1); |
424 return; | 424 return; |
425 } | 425 } |
426 | 426 |
427 socket->Write(io_buffer_, io_buffer_size_, | 427 socket->Write(io_buffer_, io_buffer_size_, |
428 base::Bind(&SocketWriteFunction::OnCompleted, this)); | 428 base::Bind(&SocketWriteFunction::OnCompleted, this)); |
429 } | 429 } |
430 | 430 |
431 void SocketWriteFunction::OnCompleted(int bytes_written) { | 431 void SocketWriteFunction::OnCompleted(int bytes_written) { |
432 DictionaryValue* result = new DictionaryValue(); | 432 base::DictionaryValue* result = new base::DictionaryValue(); |
433 result->SetInteger(kBytesWrittenKey, bytes_written); | 433 result->SetInteger(kBytesWrittenKey, bytes_written); |
434 SetResult(result); | 434 SetResult(result); |
435 | 435 |
436 AsyncWorkCompleted(); | 436 AsyncWorkCompleted(); |
437 } | 437 } |
438 | 438 |
439 SocketRecvFromFunction::SocketRecvFromFunction() {} | 439 SocketRecvFromFunction::SocketRecvFromFunction() {} |
440 | 440 |
441 SocketRecvFromFunction::~SocketRecvFromFunction() {} | 441 SocketRecvFromFunction::~SocketRecvFromFunction() {} |
442 | 442 |
(...skipping 12 matching lines...) Expand all Loading... |
455 } | 455 } |
456 | 456 |
457 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, | 457 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, |
458 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); | 458 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); |
459 } | 459 } |
460 | 460 |
461 void SocketRecvFromFunction::OnCompleted(int bytes_read, | 461 void SocketRecvFromFunction::OnCompleted(int bytes_read, |
462 scoped_refptr<net::IOBuffer> io_buffer, | 462 scoped_refptr<net::IOBuffer> io_buffer, |
463 const std::string& address, | 463 const std::string& address, |
464 int port) { | 464 int port) { |
465 DictionaryValue* result = new DictionaryValue(); | 465 base::DictionaryValue* result = new base::DictionaryValue(); |
466 result->SetInteger(kResultCodeKey, bytes_read); | 466 result->SetInteger(kResultCodeKey, bytes_read); |
467 if (bytes_read > 0) { | 467 if (bytes_read > 0) { |
468 result->Set(kDataKey, | 468 result->Set(kDataKey, |
469 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 469 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
470 bytes_read)); | 470 bytes_read)); |
471 } else { | 471 } else { |
472 result->Set(kDataKey, new base::BinaryValue()); | 472 result->Set(kDataKey, new base::BinaryValue()); |
473 } | 473 } |
474 result->SetString(kAddressKey, address); | 474 result->SetString(kAddressKey, address); |
475 result->SetInteger(kPortKey, port); | 475 result->SetInteger(kPortKey, port); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 AsyncWorkCompleted(); | 534 AsyncWorkCompleted(); |
535 } | 535 } |
536 } | 536 } |
537 | 537 |
538 void SocketSendToFunction::StartSendTo() { | 538 void SocketSendToFunction::StartSendTo() { |
539 socket_->SendTo(io_buffer_, io_buffer_size_, resolved_address_, port_, | 539 socket_->SendTo(io_buffer_, io_buffer_size_, resolved_address_, port_, |
540 base::Bind(&SocketSendToFunction::OnCompleted, this)); | 540 base::Bind(&SocketSendToFunction::OnCompleted, this)); |
541 } | 541 } |
542 | 542 |
543 void SocketSendToFunction::OnCompleted(int bytes_written) { | 543 void SocketSendToFunction::OnCompleted(int bytes_written) { |
544 DictionaryValue* result = new DictionaryValue(); | 544 base::DictionaryValue* result = new base::DictionaryValue(); |
545 result->SetInteger(kBytesWrittenKey, bytes_written); | 545 result->SetInteger(kBytesWrittenKey, bytes_written); |
546 SetResult(result); | 546 SetResult(result); |
547 | 547 |
548 AsyncWorkCompleted(); | 548 AsyncWorkCompleted(); |
549 } | 549 } |
550 | 550 |
551 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() {} | 551 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() {} |
552 | 552 |
553 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} | 553 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} |
554 | 554 |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
877 return; | 877 return; |
878 } | 878 } |
879 | 879 |
880 base::ListValue* values = new base::ListValue(); | 880 base::ListValue* values = new base::ListValue(); |
881 values->AppendStrings((std::vector<std::string>&) | 881 values->AppendStrings((std::vector<std::string>&) |
882 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); | 882 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); |
883 SetResult(values); | 883 SetResult(values); |
884 } | 884 } |
885 | 885 |
886 } // namespace extensions | 886 } // namespace extensions |
OLD | NEW |