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> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/hash_tables.h" |
8 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
9 #include "chrome/common/extensions/permissions/socket_permission.h" | 12 #include "chrome/common/extensions/permissions/socket_permission.h" |
10 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 14 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
12 #include "chrome/browser/extensions/api/socket/socket.h" | 15 #include "chrome/browser/extensions/api/socket/socket.h" |
13 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | 16 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
14 #include "chrome/browser/extensions/api/socket/udp_socket.h" | 17 #include "chrome/browser/extensions/api/socket/udp_socket.h" |
15 #include "chrome/browser/extensions/extension_system.h" | 18 #include "chrome/browser/extensions/extension_system.h" |
16 #include "chrome/browser/io_thread.h" | 19 #include "chrome/browser/io_thread.h" |
17 #include "net/base/host_port_pair.h" | 20 #include "net/base/host_port_pair.h" |
(...skipping 13 matching lines...) Expand all Loading... |
31 const char kDataKey[] = "data"; | 34 const char kDataKey[] = "data"; |
32 const char kResultCodeKey[] = "resultCode"; | 35 const char kResultCodeKey[] = "resultCode"; |
33 const char kSocketIdKey[] = "socketId"; | 36 const char kSocketIdKey[] = "socketId"; |
34 | 37 |
35 const char kSocketNotFoundError[] = "Socket not found"; | 38 const char kSocketNotFoundError[] = "Socket not found"; |
36 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 39 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
37 const char kPermissionError[] = "App does not have permission"; | 40 const char kPermissionError[] = "App does not have permission"; |
38 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 41 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
39 const char kTCPSocketBindError[] = | 42 const char kTCPSocketBindError[] = |
40 "TCP socket does not support bind. For TCP server please use listen."; | 43 "TCP socket does not support bind. For TCP server please use listen."; |
| 44 const char kMulticastSocketTypeError[] = |
| 45 "Only UDP socket supports multicast."; |
| 46 const char kWildcardAddress[] = "*"; |
| 47 const int kWildcardPort = 0; |
41 | 48 |
42 SocketAsyncApiFunction::SocketAsyncApiFunction() | 49 SocketAsyncApiFunction::SocketAsyncApiFunction() |
43 : manager_(NULL) { | 50 : manager_(NULL) { |
44 } | 51 } |
45 | 52 |
46 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 53 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
47 } | 54 } |
48 | 55 |
49 bool SocketAsyncApiFunction::PrePrepare() { | 56 bool SocketAsyncApiFunction::PrePrepare() { |
50 manager_ = ExtensionSystem::Get(profile())->socket_manager(); | 57 manager_ = ExtensionSystem::Get(profile())->socket_manager(); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 159 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
153 return true; | 160 return true; |
154 } | 161 } |
155 | 162 |
156 void SocketDestroyFunction::Work() { | 163 void SocketDestroyFunction::Work() { |
157 RemoveSocket(socket_id_); | 164 RemoveSocket(socket_id_); |
158 } | 165 } |
159 | 166 |
160 SocketConnectFunction::SocketConnectFunction() | 167 SocketConnectFunction::SocketConnectFunction() |
161 : socket_id_(0), | 168 : socket_id_(0), |
162 port_(0) { | 169 hostname_(), |
| 170 port_(0), |
| 171 socket_(NULL) { |
163 } | 172 } |
164 | 173 |
165 SocketConnectFunction::~SocketConnectFunction() { | 174 SocketConnectFunction::~SocketConnectFunction() { |
166 } | 175 } |
167 | 176 |
168 bool SocketConnectFunction::Prepare() { | 177 bool SocketConnectFunction::Prepare() { |
169 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 178 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
170 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); | 179 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); |
171 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); | 180 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); |
172 return true; | 181 return true; |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 result->SetInteger(kPortKey, port); | 478 result->SetInteger(kPortKey, port); |
470 SetResult(result); | 479 SetResult(result); |
471 | 480 |
472 AsyncWorkCompleted(); | 481 AsyncWorkCompleted(); |
473 } | 482 } |
474 | 483 |
475 SocketSendToFunction::SocketSendToFunction() | 484 SocketSendToFunction::SocketSendToFunction() |
476 : socket_id_(0), | 485 : socket_id_(0), |
477 io_buffer_(NULL), | 486 io_buffer_(NULL), |
478 io_buffer_size_(0), | 487 io_buffer_size_(0), |
479 port_(0) { | 488 port_(0), |
| 489 socket_(NULL) { |
480 } | 490 } |
481 | 491 |
482 SocketSendToFunction::~SocketSendToFunction() {} | 492 SocketSendToFunction::~SocketSendToFunction() {} |
483 | 493 |
484 bool SocketSendToFunction::Prepare() { | 494 bool SocketSendToFunction::Prepare() { |
485 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 495 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
486 base::BinaryValue *data = NULL; | 496 base::BinaryValue *data = NULL; |
487 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 497 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
488 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); | 498 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); |
489 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); | 499 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 make_linked_ptr(new api::socket::NetworkInterface); | 683 make_linked_ptr(new api::socket::NetworkInterface); |
674 info->name = i->name; | 684 info->name = i->name; |
675 info->address = net::IPAddressToString(i->address); | 685 info->address = net::IPAddressToString(i->address); |
676 create_arg.push_back(info); | 686 create_arg.push_back(info); |
677 } | 687 } |
678 | 688 |
679 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 689 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
680 SendResponse(true); | 690 SendResponse(true); |
681 } | 691 } |
682 | 692 |
| 693 SocketJoinGroupFunction::SocketJoinGroupFunction() |
| 694 : params_(NULL) {} |
| 695 |
| 696 SocketJoinGroupFunction::~SocketJoinGroupFunction() {} |
| 697 |
| 698 bool SocketJoinGroupFunction::Prepare() { |
| 699 params_ = api::socket::JoinGroup::Params::Create(*args_); |
| 700 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 701 return true; |
| 702 } |
| 703 |
| 704 void SocketJoinGroupFunction::Work() { |
| 705 int result = -1; |
| 706 Socket* socket = GetSocket(params_->socket_id); |
| 707 if (!socket) { |
| 708 error_ = kSocketNotFoundError; |
| 709 SetResult(Value::CreateIntegerValue(result)); |
| 710 return; |
| 711 } |
| 712 |
| 713 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
| 714 error_ = kMulticastSocketTypeError; |
| 715 SetResult(Value::CreateIntegerValue(result)); |
| 716 return; |
| 717 } |
| 718 |
| 719 SocketPermission::CheckParam param( |
| 720 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, |
| 721 kWildcardAddress, |
| 722 kWildcardPort); |
| 723 |
| 724 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 725 ¶m)) { |
| 726 error_ = kPermissionError; |
| 727 SetResult(Value::CreateIntegerValue(result)); |
| 728 return; |
| 729 } |
| 730 |
| 731 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address); |
| 732 if (result != 0) { |
| 733 error_ = net::ErrorToString(result); |
| 734 } |
| 735 SetResult(Value::CreateIntegerValue(result)); |
| 736 } |
| 737 |
| 738 |
| 739 SocketLeaveGroupFunction::SocketLeaveGroupFunction() |
| 740 : params_(NULL) {} |
| 741 |
| 742 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} |
| 743 |
| 744 bool SocketLeaveGroupFunction::Prepare() { |
| 745 params_ = api::socket::LeaveGroup::Params::Create(*args_); |
| 746 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 747 return true; |
| 748 } |
| 749 |
| 750 void SocketLeaveGroupFunction::Work() { |
| 751 int result = -1; |
| 752 Socket* socket = GetSocket(params_->socket_id); |
| 753 |
| 754 if (!socket) { |
| 755 error_ = kSocketNotFoundError; |
| 756 SetResult(Value::CreateIntegerValue(result)); |
| 757 return; |
| 758 } |
| 759 |
| 760 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
| 761 error_ = kMulticastSocketTypeError; |
| 762 SetResult(Value::CreateIntegerValue(result)); |
| 763 return; |
| 764 } |
| 765 |
| 766 SocketPermission::CheckParam param( |
| 767 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, |
| 768 kWildcardAddress, |
| 769 kWildcardPort); |
| 770 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 771 ¶m)) { |
| 772 error_ = kPermissionError; |
| 773 SetResult(Value::CreateIntegerValue(result)); |
| 774 return; |
| 775 } |
| 776 |
| 777 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address); |
| 778 if (result != 0) |
| 779 error_ = net::ErrorToString(result); |
| 780 SetResult(Value::CreateIntegerValue(result)); |
| 781 } |
| 782 |
| 783 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() |
| 784 : params_(NULL) {} |
| 785 |
| 786 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {} |
| 787 |
| 788 bool SocketSetMulticastTimeToLiveFunction::Prepare() { |
| 789 params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_); |
| 790 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 791 return true; |
| 792 } |
| 793 void SocketSetMulticastTimeToLiveFunction::Work() { |
| 794 int result = -1; |
| 795 Socket* socket = GetSocket(params_->socket_id); |
| 796 if (!socket) { |
| 797 error_ = kSocketNotFoundError; |
| 798 SetResult(Value::CreateIntegerValue(result)); |
| 799 return; |
| 800 } |
| 801 |
| 802 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
| 803 error_ = kMulticastSocketTypeError; |
| 804 SetResult(Value::CreateIntegerValue(result)); |
| 805 return; |
| 806 } |
| 807 |
| 808 result = static_cast<UDPSocket*>(socket)->SetMulticastTimeToLive( |
| 809 params_->ttl); |
| 810 if (result != 0) |
| 811 error_ = net::ErrorToString(result); |
| 812 SetResult(Value::CreateIntegerValue(result)); |
| 813 } |
| 814 |
| 815 SocketSetMulticastLoopbackModeFunction::SocketSetMulticastLoopbackModeFunction() |
| 816 : params_(NULL) {} |
| 817 |
| 818 SocketSetMulticastLoopbackModeFunction:: |
| 819 ~SocketSetMulticastLoopbackModeFunction() {} |
| 820 |
| 821 bool SocketSetMulticastLoopbackModeFunction::Prepare() { |
| 822 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_); |
| 823 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 824 return true; |
| 825 } |
| 826 |
| 827 void SocketSetMulticastLoopbackModeFunction::Work() { |
| 828 int result = -1; |
| 829 Socket* socket = GetSocket(params_->socket_id); |
| 830 if (!socket) { |
| 831 error_ = kSocketNotFoundError; |
| 832 SetResult(Value::CreateIntegerValue(result)); |
| 833 return; |
| 834 } |
| 835 |
| 836 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
| 837 error_ = kMulticastSocketTypeError; |
| 838 SetResult(Value::CreateIntegerValue(result)); |
| 839 return; |
| 840 } |
| 841 |
| 842 result = static_cast<UDPSocket*>(socket)-> |
| 843 SetMulticastLoopbackMode(params_->enabled); |
| 844 if (result != 0) |
| 845 error_ = net::ErrorToString(result); |
| 846 SetResult(Value::CreateIntegerValue(result)); |
| 847 } |
| 848 |
| 849 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() |
| 850 : params_(NULL) {} |
| 851 |
| 852 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {} |
| 853 |
| 854 bool SocketGetJoinedGroupsFunction::Prepare() { |
| 855 params_ = api::socket::GetJoinedGroups::Params::Create(*args_); |
| 856 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 857 return true; |
| 858 } |
| 859 |
| 860 void SocketGetJoinedGroupsFunction::Work() { |
| 861 int result = -1; |
| 862 Socket* socket = GetSocket(params_->socket_id); |
| 863 if (!socket) { |
| 864 error_ = kSocketNotFoundError; |
| 865 SetResult(Value::CreateIntegerValue(result)); |
| 866 return; |
| 867 } |
| 868 |
| 869 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
| 870 error_ = kMulticastSocketTypeError; |
| 871 SetResult(Value::CreateIntegerValue(result)); |
| 872 return; |
| 873 } |
| 874 |
| 875 SocketPermission::CheckParam param( |
| 876 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, |
| 877 kWildcardAddress, |
| 878 kWildcardPort); |
| 879 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket, |
| 880 ¶m)) { |
| 881 error_ = kPermissionError; |
| 882 SetResult(Value::CreateIntegerValue(result)); |
| 883 return; |
| 884 } |
| 885 |
| 886 base::ListValue* values = new base::ListValue(); |
| 887 values->AppendStrings((std::vector<std::string>&) |
| 888 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); |
| 889 SetResult(values); |
| 890 } |
| 891 |
683 } // namespace extensions | 892 } // namespace extensions |
OLD | NEW |