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/common/extensions/extension.h" | 8 #include "chrome/common/extensions/extension.h" |
9 #include "chrome/common/extensions/permissions/socket_permission.h" | 9 #include "chrome/common/extensions/permissions/socket_permission.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 const char kDataKey[] = "data"; | 31 const char kDataKey[] = "data"; |
32 const char kResultCodeKey[] = "resultCode"; | 32 const char kResultCodeKey[] = "resultCode"; |
33 const char kSocketIdKey[] = "socketId"; | 33 const char kSocketIdKey[] = "socketId"; |
34 | 34 |
35 const char kSocketNotFoundError[] = "Socket not found"; | 35 const char kSocketNotFoundError[] = "Socket not found"; |
36 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 36 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
37 const char kPermissionError[] = "App does not have permission"; | 37 const char kPermissionError[] = "App does not have permission"; |
38 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 38 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
39 const char kTCPSocketBindError[] = | 39 const char kTCPSocketBindError[] = |
40 "TCP socket does not support bind. For TCP server please use listen."; | 40 "TCP socket does not support bind. For TCP server please use listen."; |
| 41 const char kMulticastSocketTypeError[] = |
| 42 "Only UDP socket supports multicast."; |
| 43 const char kWildcardArress[] = "*"; |
| 44 const int kWildcardPortNumber = 0; |
41 | 45 |
42 SocketAsyncApiFunction::SocketAsyncApiFunction() | 46 SocketAsyncApiFunction::SocketAsyncApiFunction() |
43 : manager_(NULL) { | 47 : manager_(NULL) { |
44 } | 48 } |
45 | 49 |
46 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 50 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
47 } | 51 } |
48 | 52 |
49 bool SocketAsyncApiFunction::PrePrepare() { | 53 bool SocketAsyncApiFunction::PrePrepare() { |
50 manager_ = ExtensionSystem::Get(profile())->socket_manager(); | 54 manager_ = ExtensionSystem::Get(profile())->socket_manager(); |
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 make_linked_ptr(new api::socket::NetworkInterface); | 677 make_linked_ptr(new api::socket::NetworkInterface); |
674 info->name = i->name; | 678 info->name = i->name; |
675 info->address = net::IPAddressToString(i->address); | 679 info->address = net::IPAddressToString(i->address); |
676 create_arg.push_back(info); | 680 create_arg.push_back(info); |
677 } | 681 } |
678 | 682 |
679 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 683 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
680 SendResponse(true); | 684 SendResponse(true); |
681 } | 685 } |
682 | 686 |
| 687 SocketJoinGroupFunction::SocketJoinGroupFunction() |
| 688 : params_(NULL) {} |
| 689 |
| 690 SocketJoinGroupFunction::~SocketJoinGroupFunction() {} |
| 691 |
| 692 bool SocketJoinGroupFunction::Prepare() { |
| 693 params_ = api::socket::JoinGroup::Params::Create(*args_); |
| 694 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 695 return true; |
| 696 } |
| 697 |
| 698 void SocketJoinGroupFunction::Work() { |
| 699 int result = -1; |
| 700 Socket* socket = GetSocket(params_->socket_id); |
| 701 if (!socket) { |
| 702 error_ = kSocketNotFoundError; |
| 703 SetResult(Value::CreateIntegerValue(result)); |
| 704 return; |
| 705 } |
| 706 |
| 707 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 708 SocketPermission::CheckParam param( |
| 709 SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP, |
| 710 kWildcardArress, |
| 711 kWildcardPortNumber); |
| 712 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 713 ¶m)) { |
| 714 error_ = kPermissionError; |
| 715 SetResult(Value::CreateIntegerValue(result)); |
| 716 return; |
| 717 } |
| 718 } else { |
| 719 error_ = kMulticastSocketTypeError; |
| 720 SetResult(Value::CreateIntegerValue(result)); |
| 721 return; |
| 722 } |
| 723 |
| 724 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address); |
| 725 if (result != 0) { |
| 726 error_ = net::ErrorToString(result); |
| 727 } |
| 728 SetResult(Value::CreateIntegerValue(result)); |
| 729 } |
| 730 |
| 731 |
| 732 SocketLeaveGroupFunction::SocketLeaveGroupFunction() |
| 733 : params_(NULL) {} |
| 734 |
| 735 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} |
| 736 |
| 737 bool SocketLeaveGroupFunction::Prepare() { |
| 738 params_ = api::socket::LeaveGroup::Params::Create(*args_); |
| 739 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 740 return true; |
| 741 } |
| 742 |
| 743 void SocketLeaveGroupFunction::Work() { |
| 744 int result = -1; |
| 745 Socket* socket = GetSocket(params_->socket_id); |
| 746 |
| 747 if (!socket) { |
| 748 error_ = kSocketNotFoundError; |
| 749 SetResult(Value::CreateIntegerValue(result)); |
| 750 return; |
| 751 } |
| 752 |
| 753 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 754 SocketPermission::CheckParam param( |
| 755 SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP, |
| 756 kWildcardArress, |
| 757 kWildcardPortNumber); |
| 758 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 759 ¶m)) { |
| 760 error_ = kPermissionError; |
| 761 SetResult(Value::CreateIntegerValue(result)); |
| 762 return; |
| 763 } |
| 764 } else { |
| 765 error_ = kMulticastSocketTypeError; |
| 766 SetResult(Value::CreateIntegerValue(result)); |
| 767 return; |
| 768 } |
| 769 |
| 770 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address); |
| 771 if (result != 0) |
| 772 error_ = net::ErrorToString(result); |
| 773 SetResult(Value::CreateIntegerValue(result)); |
| 774 } |
| 775 |
| 776 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() |
| 777 : params_(NULL) {} |
| 778 |
| 779 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {} |
| 780 |
| 781 bool SocketSetMulticastTimeToLiveFunction::Prepare() { |
| 782 params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_); |
| 783 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 784 return true; |
| 785 } |
| 786 void SocketSetMulticastTimeToLiveFunction::Work() { |
| 787 int result = -1; |
| 788 Socket* socket = GetSocket(params_->socket_id); |
| 789 if (!socket) { |
| 790 error_ = kSocketNotFoundError; |
| 791 SetResult(Value::CreateIntegerValue(result)); |
| 792 return; |
| 793 } |
| 794 |
| 795 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 796 result = static_cast<UDPSocket*>(socket)-> |
| 797 SetMulticastTimeToLive(params_->ttl); |
| 798 if (result != 0) |
| 799 error_ = net::ErrorToString(result); |
| 800 SetResult(Value::CreateIntegerValue(result)); |
| 801 } else { |
| 802 error_ = kMulticastSocketTypeError; |
| 803 } |
| 804 SetResult(Value::CreateIntegerValue(result)); |
| 805 } |
| 806 |
| 807 SocketSetMulticastLoopbackModeFunction::SocketSetMulticastLoopbackModeFunction() |
| 808 : params_(NULL) {} |
| 809 |
| 810 SocketSetMulticastLoopbackModeFunction:: |
| 811 ~SocketSetMulticastLoopbackModeFunction() {} |
| 812 |
| 813 bool SocketSetMulticastLoopbackModeFunction::Prepare() { |
| 814 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_); |
| 815 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 816 return true; |
| 817 } |
| 818 |
| 819 void SocketSetMulticastLoopbackModeFunction::Work() { |
| 820 int result = -1; |
| 821 Socket* socket = GetSocket(params_->socket_id); |
| 822 if (!socket) { |
| 823 error_ = kSocketNotFoundError; |
| 824 SetResult(Value::CreateIntegerValue(result)); |
| 825 return; |
| 826 } |
| 827 |
| 828 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 829 result = static_cast<UDPSocket*>(socket)-> |
| 830 SetMulticastLoopbackMode(params_->enabled); |
| 831 if (result != 0) |
| 832 error_ = net::ErrorToString(result); |
| 833 } else { |
| 834 error_ = kMulticastSocketTypeError; |
| 835 } |
| 836 SetResult(Value::CreateIntegerValue(result)); |
| 837 } |
| 838 |
| 839 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() |
| 840 : params_(NULL) {} |
| 841 |
| 842 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {} |
| 843 |
| 844 bool SocketGetJoinedGroupsFunction::Prepare() { |
| 845 params_ = api::socket::GetJoinedGroups::Params::Create(*args_); |
| 846 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 847 return true; |
| 848 } |
| 849 |
| 850 void SocketGetJoinedGroupsFunction::Work() { |
| 851 int result = -1; |
| 852 Socket* socket = GetSocket(params_->socket_id); |
| 853 if (!socket) { |
| 854 error_ = kSocketNotFoundError; |
| 855 SetResult(Value::CreateIntegerValue(result)); |
| 856 return; |
| 857 } |
| 858 |
| 859 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 860 SocketPermission::CheckParam param( |
| 861 SocketPermissionRequest::UDP_SEND_TO, |
| 862 kWildcardArress, |
| 863 kWildcardPortNumber); |
| 864 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 865 ¶m)) { |
| 866 error_ = kPermissionError; |
| 867 SetResult(Value::CreateIntegerValue(result)); |
| 868 return; |
| 869 } |
| 870 } else { |
| 871 error_ = kMulticastSocketTypeError; |
| 872 SetResult(Value::CreateIntegerValue(result)); |
| 873 return; |
| 874 } |
| 875 |
| 876 base::hash_set<std::string> groups; |
| 877 result = static_cast<UDPSocket*>(socket)->GetJoinedGroups(&groups); |
| 878 if (result != 0) |
| 879 error_ = net::ErrorToString(result); |
| 880 base::ListValue* list = new base::ListValue(); |
| 881 for (base::hash_set<std::string>::iterator it = groups.begin(); |
| 882 it != groups.end(); ++it) { |
| 883 list->AppendString(*it); |
| 884 } |
| 885 SetResult(list); |
| 886 } |
| 887 |
683 } // namespace extensions | 888 } // namespace extensions |
OLD | NEW |