| 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/hash_tables.h" | 10 #include "base/hash_tables.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 const char kSocketIdKey[] = "socketId"; | 37 const char kSocketIdKey[] = "socketId"; |
| 38 | 38 |
| 39 const char kSocketNotFoundError[] = "Socket not found"; | 39 const char kSocketNotFoundError[] = "Socket not found"; |
| 40 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 40 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
| 41 const char kPermissionError[] = "App does not have permission"; | 41 const char kPermissionError[] = "App does not have permission"; |
| 42 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 42 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
| 43 const char kTCPSocketBindError[] = | 43 const char kTCPSocketBindError[] = |
| 44 "TCP socket does not support bind. For TCP server please use listen."; | 44 "TCP socket does not support bind. For TCP server please use listen."; |
| 45 const char kMulticastSocketTypeError[] = | 45 const char kMulticastSocketTypeError[] = |
| 46 "Only UDP socket supports multicast."; | 46 "Only UDP socket supports multicast."; |
| 47 const char kAddressReuseSocketTypeError[] = |
| 48 "There is no need to call AllowReuseAddress on TCP sockets, as TCP sockets" |
| 49 " support address reuse by default."; |
| 47 const char kWildcardAddress[] = "*"; | 50 const char kWildcardAddress[] = "*"; |
| 48 const int kWildcardPort = 0; | 51 const int kWildcardPort = 0; |
| 49 | 52 |
| 50 SocketAsyncApiFunction::SocketAsyncApiFunction() | 53 SocketAsyncApiFunction::SocketAsyncApiFunction() |
| 51 : manager_(NULL) { | 54 : manager_(NULL) { |
| 52 } | 55 } |
| 53 | 56 |
| 54 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 57 SocketAsyncApiFunction::~SocketAsyncApiFunction() { |
| 55 } | 58 } |
| 56 | 59 |
| (...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 return; | 738 return; |
| 736 } | 739 } |
| 737 | 740 |
| 738 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address); | 741 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address); |
| 739 if (result != 0) { | 742 if (result != 0) { |
| 740 error_ = net::ErrorToString(result); | 743 error_ = net::ErrorToString(result); |
| 741 } | 744 } |
| 742 SetResult(Value::CreateIntegerValue(result)); | 745 SetResult(Value::CreateIntegerValue(result)); |
| 743 } | 746 } |
| 744 | 747 |
| 745 | |
| 746 SocketLeaveGroupFunction::SocketLeaveGroupFunction() | 748 SocketLeaveGroupFunction::SocketLeaveGroupFunction() |
| 747 : params_(NULL) {} | 749 : params_(NULL) {} |
| 748 | 750 |
| 749 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} | 751 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} |
| 750 | 752 |
| 751 bool SocketLeaveGroupFunction::Prepare() { | 753 bool SocketLeaveGroupFunction::Prepare() { |
| 752 params_ = api::socket::LeaveGroup::Params::Create(*args_); | 754 params_ = api::socket::LeaveGroup::Params::Create(*args_); |
| 753 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 755 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 754 return true; | 756 return true; |
| 755 } | 757 } |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 SetResult(Value::CreateIntegerValue(result)); | 894 SetResult(Value::CreateIntegerValue(result)); |
| 893 return; | 895 return; |
| 894 } | 896 } |
| 895 | 897 |
| 896 base::ListValue* values = new base::ListValue(); | 898 base::ListValue* values = new base::ListValue(); |
| 897 values->AppendStrings((std::vector<std::string>&) | 899 values->AppendStrings((std::vector<std::string>&) |
| 898 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); | 900 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); |
| 899 SetResult(values); | 901 SetResult(values); |
| 900 } | 902 } |
| 901 | 903 |
| 904 SocketAllowAddressReuseFunction::SocketAllowAddressReuseFunction() |
| 905 : params_(NULL) {} |
| 906 |
| 907 SocketAllowAddressReuseFunction::~SocketAllowAddressReuseFunction() {} |
| 908 |
| 909 bool SocketAllowAddressReuseFunction::Prepare() { |
| 910 params_ = api::socket::AllowAddressReuse::Params::Create(*args_); |
| 911 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 912 return true; |
| 913 } |
| 914 |
| 915 void SocketAllowAddressReuseFunction::Work() { |
| 916 int result = -1; |
| 917 Socket* socket = GetSocket(params_->socket_id); |
| 918 if (!socket) { |
| 919 error_ = kSocketNotFoundError; |
| 920 SetResult(Value::CreateIntegerValue(result)); |
| 921 return; |
| 922 } |
| 923 |
| 924 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
| 925 error_ = kAddressReuseSocketTypeError; |
| 926 SetResult(Value::CreateIntegerValue(result)); |
| 927 return; |
| 928 } |
| 929 |
| 930 result = static_cast<UDPSocket*>(socket)->AllowAddressReuse(); |
| 931 if (result != 0) |
| 932 error_ = net::ErrorToString(result); |
| 933 SetResult(Value::CreateIntegerValue(result)); |
| 934 } |
| 935 |
| 902 } // namespace extensions | 936 } // namespace extensions |
| OLD | NEW |