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

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: Fix tests 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 "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
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
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
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) {}
mmenke 2013/04/12 21:07:41 4 space indent. Comment applies to whole file.
Bei Zhang 2013/04/15 22:30:26 Done.
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 &param)) {
mmenke 2013/04/12 21:07:41 Should either be aligned with APIPermission::kSock
Bei Zhang 2013/04/15 22:30:26 Done.
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 &param)) {
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 result = static_cast<UDPSocket*>(socket)->
799 SetMulticastTimeToLive(params_->ttl);
800 if (result != 0) {
801 error_ = net::ErrorToString(result);
802 }
803 SetResult(Value::CreateIntegerValue(result));
804 } else {
805 error_ = kMulticastSocketTypeError;
806 }
807 SetResult(Value::CreateIntegerValue(result));
808 }
809
810 SocketSetMulticastLoopbackModeFunction::SocketSetMulticastLoopbackModeFunction()
811 : params_(NULL) {}
812
813 SocketSetMulticastLoopbackModeFunction::
814 ~SocketSetMulticastLoopbackModeFunction() {}
815
816 bool SocketSetMulticastLoopbackModeFunction::Prepare() {
817 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_);
818 EXTENSION_FUNCTION_VALIDATE(params_.get());
819 return true;
820 }
821
822 void SocketSetMulticastLoopbackModeFunction::Work() {
823 int result = -1;
824 Socket* socket = GetSocket(params_->socket_id);
825 if (!socket) {
826 error_ = kSocketNotFoundError;
827 SetResult(Value::CreateIntegerValue(result));
828 return;
829 }
830
831 if (socket->GetSocketType() == Socket::TYPE_UDP) {
832 result = static_cast<UDPSocket*>(socket)->
833 SetMulticastLoopbackMode(params_->enabled);
834 if (result != 0) {
835 error_ = net::ErrorToString(result);
836 }
837 } else {
838 error_ = kMulticastSocketTypeError;
839 }
840 SetResult(Value::CreateIntegerValue(result));
841 }
842
843 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction()
844 : params_(NULL) {}
845
846 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {}
847
848 bool SocketGetJoinedGroupsFunction::Prepare() {
849 params_ = api::socket::GetJoinedGroups::Params::Create(*args_);
850 EXTENSION_FUNCTION_VALIDATE(params_.get());
851 return true;
852 }
853
854 void SocketGetJoinedGroupsFunction::Work() {
855 int result = -1;
856 Socket* socket = GetSocket(params_->socket_id);
857 if (!socket) {
858 error_ = kSocketNotFoundError;
859 SetResult(Value::CreateIntegerValue(result));
860 return;
861 }
862
863 if (socket->GetSocketType() == Socket::TYPE_UDP) {
864 SocketPermission::CheckParam param(
865 SocketPermissionRequest::UDP_SEND_TO,
866 kWildcardArress,
867 kWildcardPortNumber);
868 if (!GetExtension()->CheckAPIPermissionWithParam(APIPermission::kSocket,
869 &param)) {
870 error_ = kPermissionError;
871 SetResult(Value::CreateIntegerValue(result));
872 return;
873 }
874 } else {
875 error_ = kMulticastSocketTypeError;
876 SetResult(Value::CreateIntegerValue(result));
877 return;
878 }
879
880 base::hash_set<std::string> groups;
881 result = static_cast<UDPSocket*>(socket)->
882 GetJoinedGroups(groups);
883 if (result != 0) {
884 error_ = net::ErrorToString(result);
885 }
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);
890 }
891 SetResult(list);
892 }
683 } // namespace extensions 893 } // namespace extensions
mmenke 2013/04/12 21:07:41 Line break before end of namespace.
Bei Zhang 2013/04/15 22:30:26 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698