OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/socket/socket_api.h" | 5 #include "extensions/browser/api/socket/socket_api.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 socket->Read(params_->buffer_size.get() ? *params_->buffer_size : 4096, | 512 socket->Read(params_->buffer_size.get() ? *params_->buffer_size : 4096, |
513 base::Bind(&SocketReadFunction::OnCompleted, this)); | 513 base::Bind(&SocketReadFunction::OnCompleted, this)); |
514 } | 514 } |
515 | 515 |
516 void SocketReadFunction::OnCompleted(int bytes_read, | 516 void SocketReadFunction::OnCompleted(int bytes_read, |
517 scoped_refptr<net::IOBuffer> io_buffer, | 517 scoped_refptr<net::IOBuffer> io_buffer, |
518 bool socket_destroying) { | 518 bool socket_destroying) { |
519 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 519 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
520 result->SetInteger(kResultCodeKey, bytes_read); | 520 result->SetInteger(kResultCodeKey, bytes_read); |
521 if (bytes_read > 0) { | 521 if (bytes_read > 0) { |
522 result->Set(kDataKey, | 522 result->Set(kDataKey, base::Value::CreateWithCopiedBuffer(io_buffer->data(), |
523 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 523 bytes_read)); |
524 bytes_read)); | |
525 } else { | 524 } else { |
526 result->Set(kDataKey, new base::Value(base::Value::Type::BINARY)); | 525 result->Set(kDataKey, new base::Value(base::Value::Type::BINARY)); |
527 } | 526 } |
528 SetResult(std::move(result)); | 527 SetResult(std::move(result)); |
529 | 528 |
530 AsyncWorkCompleted(); | 529 AsyncWorkCompleted(); |
531 } | 530 } |
532 | 531 |
533 SocketWriteFunction::SocketWriteFunction() | 532 SocketWriteFunction::SocketWriteFunction() |
534 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0) {} | 533 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0) {} |
535 | 534 |
536 SocketWriteFunction::~SocketWriteFunction() {} | 535 SocketWriteFunction::~SocketWriteFunction() {} |
537 | 536 |
538 bool SocketWriteFunction::Prepare() { | 537 bool SocketWriteFunction::Prepare() { |
539 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 538 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
540 base::BinaryValue* data = NULL; | 539 base::Value* data = NULL; |
541 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 540 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
542 | 541 |
543 io_buffer_size_ = data->GetSize(); | 542 io_buffer_size_ = data->GetSize(); |
544 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 543 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
545 return true; | 544 return true; |
546 } | 545 } |
547 | 546 |
548 void SocketWriteFunction::AsyncWorkStart() { | 547 void SocketWriteFunction::AsyncWorkStart() { |
549 Socket* socket = GetSocket(socket_id_); | 548 Socket* socket = GetSocket(socket_id_); |
550 | 549 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 } | 589 } |
591 | 590 |
592 void SocketRecvFromFunction::OnCompleted(int bytes_read, | 591 void SocketRecvFromFunction::OnCompleted(int bytes_read, |
593 scoped_refptr<net::IOBuffer> io_buffer, | 592 scoped_refptr<net::IOBuffer> io_buffer, |
594 bool socket_destroying, | 593 bool socket_destroying, |
595 const std::string& address, | 594 const std::string& address, |
596 uint16_t port) { | 595 uint16_t port) { |
597 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 596 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
598 result->SetInteger(kResultCodeKey, bytes_read); | 597 result->SetInteger(kResultCodeKey, bytes_read); |
599 if (bytes_read > 0) { | 598 if (bytes_read > 0) { |
600 result->Set(kDataKey, | 599 result->Set(kDataKey, base::Value::CreateWithCopiedBuffer(io_buffer->data(), |
601 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 600 bytes_read)); |
602 bytes_read)); | |
603 } else { | 601 } else { |
604 result->Set(kDataKey, new base::Value(base::Value::Type::BINARY)); | 602 result->Set(kDataKey, new base::Value(base::Value::Type::BINARY)); |
605 } | 603 } |
606 result->SetString(kAddressKey, address); | 604 result->SetString(kAddressKey, address); |
607 result->SetInteger(kPortKey, port); | 605 result->SetInteger(kPortKey, port); |
608 SetResult(std::move(result)); | 606 SetResult(std::move(result)); |
609 | 607 |
610 AsyncWorkCompleted(); | 608 AsyncWorkCompleted(); |
611 } | 609 } |
612 | 610 |
613 SocketSendToFunction::SocketSendToFunction() | 611 SocketSendToFunction::SocketSendToFunction() |
614 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0), port_(0) { | 612 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0), port_(0) { |
615 } | 613 } |
616 | 614 |
617 SocketSendToFunction::~SocketSendToFunction() {} | 615 SocketSendToFunction::~SocketSendToFunction() {} |
618 | 616 |
619 bool SocketSendToFunction::Prepare() { | 617 bool SocketSendToFunction::Prepare() { |
620 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 618 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
621 base::BinaryValue* data = NULL; | 619 base::Value* data = NULL; |
622 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 620 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
623 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); | 621 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); |
624 int port; | 622 int port; |
625 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port)); | 623 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port)); |
626 if (!IsPortValid(port)) { | 624 if (!IsPortValid(port)) { |
627 error_ = kPortInvalidError; | 625 error_ = kPortInvalidError; |
628 return false; | 626 return false; |
629 } | 627 } |
630 port_ = static_cast<uint16_t>(port); | 628 port_ = static_cast<uint16_t>(port); |
631 | 629 |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1089 } else { | 1087 } else { |
1090 RemoveSocket(params_->socket_id); | 1088 RemoveSocket(params_->socket_id); |
1091 error_ = net::ErrorToString(result); | 1089 error_ = net::ErrorToString(result); |
1092 } | 1090 } |
1093 | 1091 |
1094 results_ = api::socket::Secure::Results::Create(result); | 1092 results_ = api::socket::Secure::Results::Create(result); |
1095 AsyncWorkCompleted(); | 1093 AsyncWorkCompleted(); |
1096 } | 1094 } |
1097 | 1095 |
1098 } // namespace extensions | 1096 } // namespace extensions |
OLD | NEW |