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 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 } | 378 } |
375 | 379 |
376 void SocketReadFunction::OnCompleted(int bytes_read, | 380 void SocketReadFunction::OnCompleted(int bytes_read, |
377 scoped_refptr<net::IOBuffer> io_buffer) { | 381 scoped_refptr<net::IOBuffer> io_buffer) { |
378 DictionaryValue* result = new DictionaryValue(); | 382 DictionaryValue* result = new DictionaryValue(); |
379 result->SetInteger(kResultCodeKey, bytes_read); | 383 result->SetInteger(kResultCodeKey, bytes_read); |
380 if (bytes_read > 0) { | 384 if (bytes_read > 0) { |
381 result->Set(kDataKey, | 385 result->Set(kDataKey, |
382 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 386 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
383 bytes_read)); | 387 bytes_read)); |
| 388 |
384 } else { | 389 } else { |
385 result->Set(kDataKey, new base::BinaryValue()); | 390 result->Set(kDataKey, new base::BinaryValue()); |
386 } | 391 } |
387 SetResult(result); | 392 SetResult(result); |
388 | 393 |
389 AsyncWorkCompleted(); | 394 AsyncWorkCompleted(); |
390 } | 395 } |
391 | 396 |
392 SocketWriteFunction::SocketWriteFunction() | 397 SocketWriteFunction::SocketWriteFunction() |
393 : socket_id_(0), | 398 : socket_id_(0), |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 make_linked_ptr(new api::socket::NetworkInterface); | 678 make_linked_ptr(new api::socket::NetworkInterface); |
674 info->name = i->name; | 679 info->name = i->name; |
675 info->address = net::IPAddressToString(i->address); | 680 info->address = net::IPAddressToString(i->address); |
676 create_arg.push_back(info); | 681 create_arg.push_back(info); |
677 } | 682 } |
678 | 683 |
679 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 684 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
680 SendResponse(true); | 685 SendResponse(true); |
681 } | 686 } |
682 | 687 |
| 688 SocketJoinGroupFunction::SocketJoinGroupFunction() |
| 689 : params_(NULL) {} |
| 690 |
| 691 SocketJoinGroupFunction::~SocketJoinGroupFunction() {} |
| 692 |
| 693 bool SocketJoinGroupFunction::Prepare() { |
| 694 params_ = api::socket::JoinGroup::Params::Create(*args_); |
| 695 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 696 return true; |
| 697 } |
| 698 |
| 699 void SocketJoinGroupFunction::Work() { |
| 700 int result = -1; |
| 701 Socket* socket = GetSocket(params_->socket_id); |
| 702 if (!socket) { |
| 703 error_ = kSocketNotFoundError; |
| 704 SetResult(Value::CreateIntegerValue(result)); |
| 705 return; |
| 706 } |
| 707 |
| 708 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 709 SocketPermission::CheckParam param( |
| 710 SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP, |
| 711 kWildcardArress, |
| 712 kWildcardPortNumber); |
| 713 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 714 ¶m)) { |
| 715 error_ = kPermissionError; |
| 716 SetResult(Value::CreateIntegerValue(result)); |
| 717 return; |
| 718 } |
| 719 } else { |
| 720 error_ = kMulticastSocketTypeError; |
| 721 SetResult(Value::CreateIntegerValue(result)); |
| 722 return; |
| 723 } |
| 724 |
| 725 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address); |
| 726 if (result != 0) { |
| 727 error_ = net::ErrorToString(result); |
| 728 } |
| 729 SetResult(Value::CreateIntegerValue(result)); |
| 730 } |
| 731 |
| 732 |
| 733 SocketLeaveGroupFunction::SocketLeaveGroupFunction() |
| 734 : params_(NULL) {} |
| 735 |
| 736 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} |
| 737 |
| 738 bool SocketLeaveGroupFunction::Prepare() { |
| 739 params_ = api::socket::LeaveGroup::Params::Create(*args_); |
| 740 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 741 return true; |
| 742 } |
| 743 |
| 744 void SocketLeaveGroupFunction::Work() { |
| 745 int result = -1; |
| 746 Socket* socket = GetSocket(params_->socket_id); |
| 747 |
| 748 if (!socket) { |
| 749 error_ = kSocketNotFoundError; |
| 750 SetResult(Value::CreateIntegerValue(result)); |
| 751 return; |
| 752 } |
| 753 |
| 754 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 755 SocketPermission::CheckParam param( |
| 756 SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP, |
| 757 kWildcardArress, |
| 758 kWildcardPortNumber); |
| 759 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 760 ¶m)) { |
| 761 error_ = kPermissionError; |
| 762 SetResult(Value::CreateIntegerValue(result)); |
| 763 return; |
| 764 } |
| 765 } else { |
| 766 error_ = kMulticastSocketTypeError; |
| 767 SetResult(Value::CreateIntegerValue(result)); |
| 768 return; |
| 769 } |
| 770 |
| 771 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address); |
| 772 if (result != 0) { |
| 773 error_ = net::ErrorToString(result); |
| 774 } |
| 775 SetResult(Value::CreateIntegerValue(result)); |
| 776 } |
| 777 |
| 778 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() |
| 779 : params_(NULL) {} |
| 780 |
| 781 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {} |
| 782 |
| 783 bool SocketSetMulticastTimeToLiveFunction::Prepare() { |
| 784 params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_); |
| 785 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 786 return true; |
| 787 } |
| 788 void SocketSetMulticastTimeToLiveFunction::Work() { |
| 789 int result = -1; |
| 790 Socket* socket = GetSocket(params_->socket_id); |
| 791 if (!socket) { |
| 792 error_ = kSocketNotFoundError; |
| 793 SetResult(Value::CreateIntegerValue(result)); |
| 794 return; |
| 795 } |
| 796 |
| 797 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 798 SocketPermission::CheckParam param( |
| 799 SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP, |
| 800 kWildcardArress, |
| 801 kWildcardPortNumber); |
| 802 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 803 ¶m)) { |
| 804 error_ = kPermissionError; |
| 805 SetResult(Value::CreateIntegerValue(result)); |
| 806 return; |
| 807 } |
| 808 } else { |
| 809 error_ = kMulticastSocketTypeError; |
| 810 SetResult(Value::CreateIntegerValue(result)); |
| 811 return; |
| 812 } |
| 813 |
| 814 result = static_cast<UDPSocket*>(socket)-> |
| 815 SetMulticastTimeToLive(params_->ttl); |
| 816 if (result != 0) { |
| 817 error_ = net::ErrorToString(result); |
| 818 } |
| 819 SetResult(Value::CreateIntegerValue(result)); |
| 820 } |
| 821 |
| 822 SocketSetMulticastLoopbackModeFunction::SocketSetMulticastLoopbackModeFunction() |
| 823 : params_(NULL) {} |
| 824 |
| 825 SocketSetMulticastLoopbackModeFunction:: |
| 826 ~SocketSetMulticastLoopbackModeFunction() {} |
| 827 |
| 828 bool SocketSetMulticastLoopbackModeFunction::Prepare() { |
| 829 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_); |
| 830 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 831 return true; |
| 832 } |
| 833 |
| 834 void SocketSetMulticastLoopbackModeFunction::Work() { |
| 835 int result = -1; |
| 836 Socket* socket = GetSocket(params_->socket_id); |
| 837 if (!socket) { |
| 838 error_ = kSocketNotFoundError; |
| 839 SetResult(Value::CreateIntegerValue(result)); |
| 840 return; |
| 841 } |
| 842 |
| 843 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 844 SocketPermission::CheckParam param( |
| 845 SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP, |
| 846 kWildcardArress, |
| 847 kWildcardPortNumber); |
| 848 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 849 ¶m)) { |
| 850 error_ = kPermissionError; |
| 851 SetResult(Value::CreateIntegerValue(result)); |
| 852 return; |
| 853 } |
| 854 } else { |
| 855 error_ = kMulticastSocketTypeError; |
| 856 SetResult(Value::CreateIntegerValue(result)); |
| 857 return; |
| 858 } |
| 859 |
| 860 result = static_cast<UDPSocket*>(socket)-> |
| 861 SetMulticastLoopbackMode(params_->enabled); |
| 862 if (result != 0) { |
| 863 error_ = net::ErrorToString(result); |
| 864 } |
| 865 SetResult(Value::CreateIntegerValue(result)); |
| 866 } |
| 867 |
| 868 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() |
| 869 : params_(NULL) {} |
| 870 |
| 871 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {} |
| 872 |
| 873 bool SocketGetJoinedGroupsFunction::Prepare() { |
| 874 params_ = api::socket::GetJoinedGroups::Params::Create(*args_); |
| 875 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 876 return true; |
| 877 } |
| 878 |
| 879 void SocketGetJoinedGroupsFunction::Work() { |
| 880 int result = -1; |
| 881 Socket* socket = GetSocket(params_->socket_id); |
| 882 if (!socket) { |
| 883 error_ = kSocketNotFoundError; |
| 884 SetResult(Value::CreateIntegerValue(result)); |
| 885 return; |
| 886 } |
| 887 |
| 888 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 889 SocketPermission::CheckParam param( |
| 890 SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP, |
| 891 kWildcardArress, |
| 892 kWildcardPortNumber); |
| 893 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 894 ¶m)) { |
| 895 error_ = kPermissionError; |
| 896 SetResult(Value::CreateIntegerValue(result)); |
| 897 return; |
| 898 } |
| 899 } else { |
| 900 error_ = kMulticastSocketTypeError; |
| 901 SetResult(Value::CreateIntegerValue(result)); |
| 902 return; |
| 903 } |
| 904 |
| 905 base::hash_set<std::string> groups; |
| 906 result = static_cast<UDPSocket*>(socket)-> |
| 907 GetJoinedGroups(groups); |
| 908 if (result != 0) { |
| 909 error_ = net::ErrorToString(result); |
| 910 } |
| 911 base::ListValue* list = new base::ListValue(); |
| 912 for (base::hash_set<std::string>::iterator it = groups.begin(); |
| 913 it != groups.end(); ++it) { |
| 914 list->AppendString(*it); |
| 915 } |
| 916 SetResult(list); |
| 917 } |
683 } // namespace extensions | 918 } // namespace extensions |
OLD | NEW |