Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Side by Side Diff: chrome/browser/extensions/api/socket/socket_api.cc

Issue 12684008: Multicast socket API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor fixes Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 kWildcardArress[] = "*";
miket_OOO 2013/04/25 21:52:12 I think this word is supposed to be "Address"
Bei Zhang 2013/04/25 23:56:21 Done.
47 const int kWildcardPortNumber = 0;
miket_OOO 2013/04/25 21:52:12 "Port Number" is overly descriptive. Just "Port" w
Bei Zhang 2013/04/25 23:56:21 Done.
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
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
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
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 SocketPermission::CheckParam param(
715 SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP,
miket_OOO 2013/04/25 21:52:12 Spelling: MULTICAST
Bei Zhang 2013/04/25 23:56:21 Done.
716 kWildcardArress,
717 kWildcardPortNumber);
718 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket,
719 &param)) {
720 error_ = kPermissionError;
721 SetResult(Value::CreateIntegerValue(result));
722 return;
723 }
724 } else {
miket_OOO 2013/04/25 21:52:12 I wouldn't put this in an else, because it's going
Bei Zhang 2013/04/25 23:56:21 Done.
725 error_ = kMulticastSocketTypeError;
726 SetResult(Value::CreateIntegerValue(result));
727 return;
728 }
729
730 result = static_cast<UDPSocket*>(socket)->JoinGroup(params_->address);
731 if (result != 0) {
732 error_ = net::ErrorToString(result);
733 }
734 SetResult(Value::CreateIntegerValue(result));
735 }
736
737
738 SocketLeaveGroupFunction::SocketLeaveGroupFunction()
739 : params_(NULL) {}
740
741 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {}
742
743 bool SocketLeaveGroupFunction::Prepare() {
744 params_ = api::socket::LeaveGroup::Params::Create(*args_);
745 EXTENSION_FUNCTION_VALIDATE(params_.get());
746 return true;
747 }
748
749 void SocketLeaveGroupFunction::Work() {
750 int result = -1;
751 Socket* socket = GetSocket(params_->socket_id);
752
753 if (!socket) {
754 error_ = kSocketNotFoundError;
755 SetResult(Value::CreateIntegerValue(result));
756 return;
757 }
758
759 if (socket->GetSocketType() == Socket::TYPE_UDP) {
miket_OOO 2013/04/25 21:52:12 Same here -- rearrange so obvious validation failu
Bei Zhang 2013/04/25 23:56:21 Done.
760 SocketPermission::CheckParam param(
761 SocketPermissionRequest::UDP_MUTICAST_MEMBERSHIP,
762 kWildcardArress,
763 kWildcardPortNumber);
764 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket,
765 &param)) {
766 error_ = kPermissionError;
767 SetResult(Value::CreateIntegerValue(result));
768 return;
769 }
770 } else {
771 error_ = kMulticastSocketTypeError;
772 SetResult(Value::CreateIntegerValue(result));
773 return;
774 }
775
776 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address);
777 if (result != 0)
778 error_ = net::ErrorToString(result);
779 SetResult(Value::CreateIntegerValue(result));
780 }
781
782 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction()
783 : params_(NULL) {}
784
785 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {}
786
787 bool SocketSetMulticastTimeToLiveFunction::Prepare() {
788 params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_);
789 EXTENSION_FUNCTION_VALIDATE(params_.get());
790 return true;
791 }
792 void SocketSetMulticastTimeToLiveFunction::Work() {
793 int result = -1;
794 Socket* socket = GetSocket(params_->socket_id);
795 if (!socket) {
796 error_ = kSocketNotFoundError;
797 SetResult(Value::CreateIntegerValue(result));
798 return;
799 }
800
801 if (socket->GetSocketType() == Socket::TYPE_UDP) {
miket_OOO 2013/04/25 21:52:12 There's a bunch of boilerplate code here. What abo
Bei Zhang 2013/04/25 23:56:21 That'll be ideal. But I think maybe we should do i
802 result = static_cast<UDPSocket*>(socket)->
803 SetMulticastTimeToLive(params_->ttl);
804 if (result != 0)
805 error_ = net::ErrorToString(result);
806 SetResult(Value::CreateIntegerValue(result));
807 } else {
808 error_ = kMulticastSocketTypeError;
809 }
810 SetResult(Value::CreateIntegerValue(result));
811 }
812
813 SocketSetMulticastLoopbackModeFunction::SocketSetMulticastLoopbackModeFunction()
814 : params_(NULL) {}
815
816 SocketSetMulticastLoopbackModeFunction::
817 ~SocketSetMulticastLoopbackModeFunction() {}
818
819 bool SocketSetMulticastLoopbackModeFunction::Prepare() {
820 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_);
821 EXTENSION_FUNCTION_VALIDATE(params_.get());
822 return true;
823 }
824
825 void SocketSetMulticastLoopbackModeFunction::Work() {
826 int result = -1;
827 Socket* socket = GetSocket(params_->socket_id);
828 if (!socket) {
829 error_ = kSocketNotFoundError;
830 SetResult(Value::CreateIntegerValue(result));
831 return;
832 }
833
834 if (socket->GetSocketType() == Socket::TYPE_UDP) {
835 result = static_cast<UDPSocket*>(socket)->
836 SetMulticastLoopbackMode(params_->enabled);
837 if (result != 0)
838 error_ = net::ErrorToString(result);
839 } else {
840 error_ = kMulticastSocketTypeError;
841 }
842 SetResult(Value::CreateIntegerValue(result));
843 }
844
845 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction()
846 : params_(NULL) {}
847
848 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {}
849
850 bool SocketGetJoinedGroupsFunction::Prepare() {
851 params_ = api::socket::GetJoinedGroups::Params::Create(*args_);
852 EXTENSION_FUNCTION_VALIDATE(params_.get());
853 return true;
854 }
855
856 void SocketGetJoinedGroupsFunction::Work() {
857 int result = -1;
858 Socket* socket = GetSocket(params_->socket_id);
859 if (!socket) {
860 error_ = kSocketNotFoundError;
861 SetResult(Value::CreateIntegerValue(result));
862 return;
863 }
864
865 if (socket->GetSocketType() == Socket::TYPE_UDP) {
866 SocketPermission::CheckParam param(
867 SocketPermissionRequest::UDP_SEND_TO,
868 kWildcardArress,
869 kWildcardPortNumber);
870 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket,
871 &param)) {
872 error_ = kPermissionError;
873 SetResult(Value::CreateIntegerValue(result));
874 return;
875 }
876 } else {
877 error_ = kMulticastSocketTypeError;
878 SetResult(Value::CreateIntegerValue(result));
879 return;
880 }
881
882 base::hash_set<std::string> groups;
883 result = static_cast<UDPSocket*>(socket)->GetJoinedGroups(&groups);
884 if (result != 0)
885 error_ = net::ErrorToString(result);
886 base::ListValue* list = new base::ListValue();
887 for (base::hash_set<std::string>::iterator it = groups.begin();
888 it != groups.end(); ++it) {
889 list->AppendString(*it);
miket_OOO 2013/04/25 21:52:12 Please confirm that AppendString does a copy of th
Bei Zhang 2013/04/25 23:56:21 The copy cannot be avoided by changing the ownersh
890 }
891 SetResult(list);
892 }
893
683 } // namespace extensions 894 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698