| 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 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 } else if (socket->GetSocketType() == Socket::TYPE_TCP) { | 281 } else if (socket->GetSocketType() == Socket::TYPE_TCP) { |
| 282 error_ = kTCPSocketBindError; | 282 error_ = kTCPSocketBindError; |
| 283 SetResult(Value::CreateIntegerValue(result)); | 283 SetResult(Value::CreateIntegerValue(result)); |
| 284 return; | 284 return; |
| 285 } | 285 } |
| 286 | 286 |
| 287 result = socket->Bind(address_, port_); | 287 result = socket->Bind(address_, port_); |
| 288 SetResult(Value::CreateIntegerValue(result)); | 288 SetResult(Value::CreateIntegerValue(result)); |
| 289 } | 289 } |
| 290 | 290 |
| 291 SocketListenFunction::SocketListenFunction() | 291 SocketListenFunction::SocketListenFunction() {} |
| 292 : params_(NULL) { | |
| 293 } | |
| 294 | 292 |
| 295 SocketListenFunction::~SocketListenFunction() {} | 293 SocketListenFunction::~SocketListenFunction() {} |
| 296 | 294 |
| 297 bool SocketListenFunction::Prepare() { | 295 bool SocketListenFunction::Prepare() { |
| 298 params_ = api::socket::Listen::Params::Create(*args_); | 296 params_ = api::socket::Listen::Params::Create(*args_); |
| 299 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 297 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 300 return true; | 298 return true; |
| 301 } | 299 } |
| 302 | 300 |
| 303 void SocketListenFunction::Work() { | 301 void SocketListenFunction::Work() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 321 params_->port, | 319 params_->port, |
| 322 params_->backlog.get() ? *params_->backlog.get() : 5, | 320 params_->backlog.get() ? *params_->backlog.get() : 5, |
| 323 &error_); | 321 &error_); |
| 324 } else { | 322 } else { |
| 325 error_ = kSocketNotFoundError; | 323 error_ = kSocketNotFoundError; |
| 326 } | 324 } |
| 327 | 325 |
| 328 SetResult(Value::CreateIntegerValue(result)); | 326 SetResult(Value::CreateIntegerValue(result)); |
| 329 } | 327 } |
| 330 | 328 |
| 331 SocketAcceptFunction::SocketAcceptFunction() | 329 SocketAcceptFunction::SocketAcceptFunction() {} |
| 332 : params_(NULL) { | |
| 333 } | |
| 334 | 330 |
| 335 SocketAcceptFunction::~SocketAcceptFunction() {} | 331 SocketAcceptFunction::~SocketAcceptFunction() {} |
| 336 | 332 |
| 337 bool SocketAcceptFunction::Prepare() { | 333 bool SocketAcceptFunction::Prepare() { |
| 338 params_ = api::socket::Accept::Params::Create(*args_); | 334 params_ = api::socket::Accept::Params::Create(*args_); |
| 339 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 335 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 340 return true; | 336 return true; |
| 341 } | 337 } |
| 342 | 338 |
| 343 void SocketAcceptFunction::AsyncWorkStart() { | 339 void SocketAcceptFunction::AsyncWorkStart() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 356 result->SetInteger(kResultCodeKey, result_code); | 352 result->SetInteger(kResultCodeKey, result_code); |
| 357 if (socket) { | 353 if (socket) { |
| 358 Socket *client_socket = new TCPSocket(socket, extension_id(), true); | 354 Socket *client_socket = new TCPSocket(socket, extension_id(), true); |
| 359 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); | 355 result->SetInteger(kSocketIdKey, manager_->Add(client_socket)); |
| 360 } | 356 } |
| 361 SetResult(result); | 357 SetResult(result); |
| 362 | 358 |
| 363 AsyncWorkCompleted(); | 359 AsyncWorkCompleted(); |
| 364 } | 360 } |
| 365 | 361 |
| 366 SocketReadFunction::SocketReadFunction() | 362 SocketReadFunction::SocketReadFunction() {} |
| 367 : params_(NULL) { | |
| 368 } | |
| 369 | 363 |
| 370 SocketReadFunction::~SocketReadFunction() {} | 364 SocketReadFunction::~SocketReadFunction() {} |
| 371 | 365 |
| 372 bool SocketReadFunction::Prepare() { | 366 bool SocketReadFunction::Prepare() { |
| 373 params_ = api::socket::Read::Params::Create(*args_); | 367 params_ = api::socket::Read::Params::Create(*args_); |
| 374 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 368 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 375 return true; | 369 return true; |
| 376 } | 370 } |
| 377 | 371 |
| 378 void SocketReadFunction::AsyncWorkStart() { | 372 void SocketReadFunction::AsyncWorkStart() { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 } | 429 } |
| 436 | 430 |
| 437 void SocketWriteFunction::OnCompleted(int bytes_written) { | 431 void SocketWriteFunction::OnCompleted(int bytes_written) { |
| 438 DictionaryValue* result = new DictionaryValue(); | 432 DictionaryValue* result = new DictionaryValue(); |
| 439 result->SetInteger(kBytesWrittenKey, bytes_written); | 433 result->SetInteger(kBytesWrittenKey, bytes_written); |
| 440 SetResult(result); | 434 SetResult(result); |
| 441 | 435 |
| 442 AsyncWorkCompleted(); | 436 AsyncWorkCompleted(); |
| 443 } | 437 } |
| 444 | 438 |
| 445 SocketRecvFromFunction::SocketRecvFromFunction() | 439 SocketRecvFromFunction::SocketRecvFromFunction() {} |
| 446 : params_(NULL) { | |
| 447 } | |
| 448 | 440 |
| 449 SocketRecvFromFunction::~SocketRecvFromFunction() {} | 441 SocketRecvFromFunction::~SocketRecvFromFunction() {} |
| 450 | 442 |
| 451 bool SocketRecvFromFunction::Prepare() { | 443 bool SocketRecvFromFunction::Prepare() { |
| 452 params_ = api::socket::RecvFrom::Params::Create(*args_); | 444 params_ = api::socket::RecvFrom::Params::Create(*args_); |
| 453 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 445 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 454 return true; | 446 return true; |
| 455 } | 447 } |
| 456 | 448 |
| 457 void SocketRecvFromFunction::AsyncWorkStart() { | 449 void SocketRecvFromFunction::AsyncWorkStart() { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 } | 541 } |
| 550 | 542 |
| 551 void SocketSendToFunction::OnCompleted(int bytes_written) { | 543 void SocketSendToFunction::OnCompleted(int bytes_written) { |
| 552 DictionaryValue* result = new DictionaryValue(); | 544 DictionaryValue* result = new DictionaryValue(); |
| 553 result->SetInteger(kBytesWrittenKey, bytes_written); | 545 result->SetInteger(kBytesWrittenKey, bytes_written); |
| 554 SetResult(result); | 546 SetResult(result); |
| 555 | 547 |
| 556 AsyncWorkCompleted(); | 548 AsyncWorkCompleted(); |
| 557 } | 549 } |
| 558 | 550 |
| 559 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() | 551 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() {} |
| 560 : params_(NULL) { | |
| 561 } | |
| 562 | 552 |
| 563 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} | 553 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} |
| 564 | 554 |
| 565 bool SocketSetKeepAliveFunction::Prepare() { | 555 bool SocketSetKeepAliveFunction::Prepare() { |
| 566 params_ = api::socket::SetKeepAlive::Params::Create(*args_); | 556 params_ = api::socket::SetKeepAlive::Params::Create(*args_); |
| 567 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 557 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 568 return true; | 558 return true; |
| 569 } | 559 } |
| 570 | 560 |
| 571 void SocketSetKeepAliveFunction::Work() { | 561 void SocketSetKeepAliveFunction::Work() { |
| 572 bool result = false; | 562 bool result = false; |
| 573 Socket* socket = GetSocket(params_->socket_id); | 563 Socket* socket = GetSocket(params_->socket_id); |
| 574 if (socket) { | 564 if (socket) { |
| 575 int delay = 0; | 565 int delay = 0; |
| 576 if (params_->delay.get()) | 566 if (params_->delay.get()) |
| 577 delay = *params_->delay; | 567 delay = *params_->delay; |
| 578 result = socket->SetKeepAlive(params_->enable, delay); | 568 result = socket->SetKeepAlive(params_->enable, delay); |
| 579 } else { | 569 } else { |
| 580 error_ = kSocketNotFoundError; | 570 error_ = kSocketNotFoundError; |
| 581 } | 571 } |
| 582 SetResult(Value::CreateBooleanValue(result)); | 572 SetResult(Value::CreateBooleanValue(result)); |
| 583 } | 573 } |
| 584 | 574 |
| 585 SocketSetNoDelayFunction::SocketSetNoDelayFunction() | 575 SocketSetNoDelayFunction::SocketSetNoDelayFunction() {} |
| 586 : params_(NULL) { | |
| 587 } | |
| 588 | 576 |
| 589 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} | 577 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} |
| 590 | 578 |
| 591 bool SocketSetNoDelayFunction::Prepare() { | 579 bool SocketSetNoDelayFunction::Prepare() { |
| 592 params_ = api::socket::SetNoDelay::Params::Create(*args_); | 580 params_ = api::socket::SetNoDelay::Params::Create(*args_); |
| 593 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 581 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 594 return true; | 582 return true; |
| 595 } | 583 } |
| 596 | 584 |
| 597 void SocketSetNoDelayFunction::Work() { | 585 void SocketSetNoDelayFunction::Work() { |
| 598 bool result = false; | 586 bool result = false; |
| 599 Socket* socket = GetSocket(params_->socket_id); | 587 Socket* socket = GetSocket(params_->socket_id); |
| 600 if (socket) | 588 if (socket) |
| 601 result = socket->SetNoDelay(params_->no_delay); | 589 result = socket->SetNoDelay(params_->no_delay); |
| 602 else | 590 else |
| 603 error_ = kSocketNotFoundError; | 591 error_ = kSocketNotFoundError; |
| 604 SetResult(Value::CreateBooleanValue(result)); | 592 SetResult(Value::CreateBooleanValue(result)); |
| 605 } | 593 } |
| 606 | 594 |
| 607 SocketGetInfoFunction::SocketGetInfoFunction() | 595 SocketGetInfoFunction::SocketGetInfoFunction() {} |
| 608 : params_(NULL) {} | |
| 609 | 596 |
| 610 SocketGetInfoFunction::~SocketGetInfoFunction() {} | 597 SocketGetInfoFunction::~SocketGetInfoFunction() {} |
| 611 | 598 |
| 612 bool SocketGetInfoFunction::Prepare() { | 599 bool SocketGetInfoFunction::Prepare() { |
| 613 params_ = api::socket::GetInfo::Params::Create(*args_); | 600 params_ = api::socket::GetInfo::Params::Create(*args_); |
| 614 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 601 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 615 return true; | 602 return true; |
| 616 } | 603 } |
| 617 | 604 |
| 618 void SocketGetInfoFunction::Work() { | 605 void SocketGetInfoFunction::Work() { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 make_linked_ptr(new api::socket::NetworkInterface); | 679 make_linked_ptr(new api::socket::NetworkInterface); |
| 693 info->name = i->name; | 680 info->name = i->name; |
| 694 info->address = net::IPAddressToString(i->address); | 681 info->address = net::IPAddressToString(i->address); |
| 695 create_arg.push_back(info); | 682 create_arg.push_back(info); |
| 696 } | 683 } |
| 697 | 684 |
| 698 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 685 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
| 699 SendResponse(true); | 686 SendResponse(true); |
| 700 } | 687 } |
| 701 | 688 |
| 702 SocketJoinGroupFunction::SocketJoinGroupFunction() | 689 SocketJoinGroupFunction::SocketJoinGroupFunction() {} |
| 703 : params_(NULL) {} | |
| 704 | 690 |
| 705 SocketJoinGroupFunction::~SocketJoinGroupFunction() {} | 691 SocketJoinGroupFunction::~SocketJoinGroupFunction() {} |
| 706 | 692 |
| 707 bool SocketJoinGroupFunction::Prepare() { | 693 bool SocketJoinGroupFunction::Prepare() { |
| 708 params_ = api::socket::JoinGroup::Params::Create(*args_); | 694 params_ = api::socket::JoinGroup::Params::Create(*args_); |
| 709 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 695 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 710 return true; | 696 return true; |
| 711 } | 697 } |
| 712 | 698 |
| 713 void SocketJoinGroupFunction::Work() { | 699 void SocketJoinGroupFunction::Work() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 737 return; | 723 return; |
| 738 } | 724 } |
| 739 | 725 |
| 740 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address); | 726 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address); |
| 741 if (result != 0) { | 727 if (result != 0) { |
| 742 error_ = net::ErrorToString(result); | 728 error_ = net::ErrorToString(result); |
| 743 } | 729 } |
| 744 SetResult(Value::CreateIntegerValue(result)); | 730 SetResult(Value::CreateIntegerValue(result)); |
| 745 } | 731 } |
| 746 | 732 |
| 747 | 733 SocketLeaveGroupFunction::SocketLeaveGroupFunction() {} |
| 748 SocketLeaveGroupFunction::SocketLeaveGroupFunction() | |
| 749 : params_(NULL) {} | |
| 750 | 734 |
| 751 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} | 735 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} |
| 752 | 736 |
| 753 bool SocketLeaveGroupFunction::Prepare() { | 737 bool SocketLeaveGroupFunction::Prepare() { |
| 754 params_ = api::socket::LeaveGroup::Params::Create(*args_); | 738 params_ = api::socket::LeaveGroup::Params::Create(*args_); |
| 755 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 739 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 756 return true; | 740 return true; |
| 757 } | 741 } |
| 758 | 742 |
| 759 void SocketLeaveGroupFunction::Work() { | 743 void SocketLeaveGroupFunction::Work() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 783 SetResult(Value::CreateIntegerValue(result)); | 767 SetResult(Value::CreateIntegerValue(result)); |
| 784 return; | 768 return; |
| 785 } | 769 } |
| 786 | 770 |
| 787 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address); | 771 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address); |
| 788 if (result != 0) | 772 if (result != 0) |
| 789 error_ = net::ErrorToString(result); | 773 error_ = net::ErrorToString(result); |
| 790 SetResult(Value::CreateIntegerValue(result)); | 774 SetResult(Value::CreateIntegerValue(result)); |
| 791 } | 775 } |
| 792 | 776 |
| 793 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() | 777 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() {} |
| 794 : params_(NULL) {} | |
| 795 | 778 |
| 796 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {} | 779 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {} |
| 797 | 780 |
| 798 bool SocketSetMulticastTimeToLiveFunction::Prepare() { | 781 bool SocketSetMulticastTimeToLiveFunction::Prepare() { |
| 799 params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_); | 782 params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_); |
| 800 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 783 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 801 return true; | 784 return true; |
| 802 } | 785 } |
| 803 void SocketSetMulticastTimeToLiveFunction::Work() { | 786 void SocketSetMulticastTimeToLiveFunction::Work() { |
| 804 int result = -1; | 787 int result = -1; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 815 return; | 798 return; |
| 816 } | 799 } |
| 817 | 800 |
| 818 result = static_cast<UDPSocket*>(socket)->SetMulticastTimeToLive( | 801 result = static_cast<UDPSocket*>(socket)->SetMulticastTimeToLive( |
| 819 params_->ttl); | 802 params_->ttl); |
| 820 if (result != 0) | 803 if (result != 0) |
| 821 error_ = net::ErrorToString(result); | 804 error_ = net::ErrorToString(result); |
| 822 SetResult(Value::CreateIntegerValue(result)); | 805 SetResult(Value::CreateIntegerValue(result)); |
| 823 } | 806 } |
| 824 | 807 |
| 825 SocketSetMulticastLoopbackModeFunction::SocketSetMulticastLoopbackModeFunction() | 808 SocketSetMulticastLoopbackModeFunction:: |
| 826 : params_(NULL) {} | 809 SocketSetMulticastLoopbackModeFunction() {} |
| 827 | 810 |
| 828 SocketSetMulticastLoopbackModeFunction:: | 811 SocketSetMulticastLoopbackModeFunction:: |
| 829 ~SocketSetMulticastLoopbackModeFunction() {} | 812 ~SocketSetMulticastLoopbackModeFunction() {} |
| 830 | 813 |
| 831 bool SocketSetMulticastLoopbackModeFunction::Prepare() { | 814 bool SocketSetMulticastLoopbackModeFunction::Prepare() { |
| 832 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_); | 815 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_); |
| 833 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 816 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 834 return true; | 817 return true; |
| 835 } | 818 } |
| 836 | 819 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 849 return; | 832 return; |
| 850 } | 833 } |
| 851 | 834 |
| 852 result = static_cast<UDPSocket*>(socket)-> | 835 result = static_cast<UDPSocket*>(socket)-> |
| 853 SetMulticastLoopbackMode(params_->enabled); | 836 SetMulticastLoopbackMode(params_->enabled); |
| 854 if (result != 0) | 837 if (result != 0) |
| 855 error_ = net::ErrorToString(result); | 838 error_ = net::ErrorToString(result); |
| 856 SetResult(Value::CreateIntegerValue(result)); | 839 SetResult(Value::CreateIntegerValue(result)); |
| 857 } | 840 } |
| 858 | 841 |
| 859 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() | 842 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() {} |
| 860 : params_(NULL) {} | |
| 861 | 843 |
| 862 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {} | 844 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {} |
| 863 | 845 |
| 864 bool SocketGetJoinedGroupsFunction::Prepare() { | 846 bool SocketGetJoinedGroupsFunction::Prepare() { |
| 865 params_ = api::socket::GetJoinedGroups::Params::Create(*args_); | 847 params_ = api::socket::GetJoinedGroups::Params::Create(*args_); |
| 866 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 848 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 867 return true; | 849 return true; |
| 868 } | 850 } |
| 869 | 851 |
| 870 void SocketGetJoinedGroupsFunction::Work() { | 852 void SocketGetJoinedGroupsFunction::Work() { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 895 return; | 877 return; |
| 896 } | 878 } |
| 897 | 879 |
| 898 base::ListValue* values = new base::ListValue(); | 880 base::ListValue* values = new base::ListValue(); |
| 899 values->AppendStrings((std::vector<std::string>&) | 881 values->AppendStrings((std::vector<std::string>&) |
| 900 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); | 882 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); |
| 901 SetResult(values); | 883 SetResult(values); |
| 902 } | 884 } |
| 903 | 885 |
| 904 } // namespace extensions | 886 } // namespace extensions |
| OLD | NEW |