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/permissions/socket_permission.h" | 8 #include "chrome/common/extensions/permissions/socket_permission.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
22 | 22 |
23 namespace extensions { | 23 namespace extensions { |
24 | 24 |
25 const char kAddressKey[] = "address"; | 25 const char kAddressKey[] = "address"; |
26 const char kPortKey[] = "port"; | 26 const char kPortKey[] = "port"; |
27 const char kBytesWrittenKey[] = "bytesWritten"; | 27 const char kBytesWrittenKey[] = "bytesWritten"; |
28 const char kDataKey[] = "data"; | 28 const char kDataKey[] = "data"; |
29 const char kResultCodeKey[] = "resultCode"; | 29 const char kResultCodeKey[] = "resultCode"; |
30 const char kSocketIdKey[] = "socketId"; | 30 const char kSocketIdKey[] = "socketId"; |
31 const char kTCPOption[] = "tcp"; | |
32 const char kUDPOption[] = "udp"; | |
33 const char kUnknown[] = "unknown"; | |
34 | 31 |
35 const char kSocketNotFoundError[] = "Socket not found"; | 32 const char kSocketNotFoundError[] = "Socket not found"; |
36 const char kSocketTypeInvalidError[] = "Socket type is not supported"; | 33 const char kSocketTypeInvalidError[] = "Socket type is not supported"; |
37 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 34 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
38 const char kPermissionError[] = "Caller does not have permission"; | 35 const char kPermissionError[] = "Caller does not have permission"; |
39 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 36 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
40 | 37 |
41 SocketAsyncApiFunction::SocketAsyncApiFunction() | 38 SocketAsyncApiFunction::SocketAsyncApiFunction() |
42 : manager_(NULL) { | 39 : manager_(NULL) { |
43 } | 40 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 src_id_(-1), | 110 src_id_(-1), |
114 event_notifier_(NULL) { | 111 event_notifier_(NULL) { |
115 } | 112 } |
116 | 113 |
117 SocketCreateFunction::~SocketCreateFunction() {} | 114 SocketCreateFunction::~SocketCreateFunction() {} |
118 | 115 |
119 bool SocketCreateFunction::Prepare() { | 116 bool SocketCreateFunction::Prepare() { |
120 params_ = api::socket::Create::Params::Create(*args_); | 117 params_ = api::socket::Create::Params::Create(*args_); |
121 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 118 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
122 | 119 |
123 if (params_->type == kTCPOption) { | 120 switch (params_->type) { |
124 socket_type_ = kSocketTypeTCP; | 121 case extensions::api::socket::SOCKET_SOCKET_TYPE_TCP: |
125 } else if (params_->type == kUDPOption) { | 122 socket_type_ = kSocketTypeTCP; |
126 socket_type_ = kSocketTypeUDP; | 123 break; |
127 } else { | 124 case extensions::api::socket::SOCKET_SOCKET_TYPE_UDP: |
128 error_ = kSocketTypeInvalidError; | 125 socket_type_ = kSocketTypeUDP; |
129 return false; | 126 break; |
| 127 case extensions::api::socket::SOCKET_SOCKET_TYPE_NONE: |
| 128 NOTREACHED(); |
| 129 break; |
130 } | 130 } |
| 131 |
131 if (params_->options.get()) { | 132 if (params_->options.get()) { |
132 scoped_ptr<DictionaryValue> options = params_->options->ToValue(); | 133 scoped_ptr<DictionaryValue> options = params_->options->ToValue(); |
133 src_id_ = ExtractSrcId(options.get()); | 134 src_id_ = ExtractSrcId(options.get()); |
134 event_notifier_ = CreateEventNotifier(src_id_); | 135 event_notifier_ = CreateEventNotifier(src_id_); |
135 } | 136 } |
136 | 137 |
137 return true; | 138 return true; |
138 } | 139 } |
139 | 140 |
140 void SocketCreateFunction::Work() { | 141 void SocketCreateFunction::Work() { |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 528 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
528 return true; | 529 return true; |
529 } | 530 } |
530 | 531 |
531 void SocketGetInfoFunction::Work() { | 532 void SocketGetInfoFunction::Work() { |
532 api::socket::SocketInfo info; | 533 api::socket::SocketInfo info; |
533 Socket* socket = GetSocket(params_->socket_id); | 534 Socket* socket = GetSocket(params_->socket_id); |
534 if (socket) { | 535 if (socket) { |
535 // This represents what we know about the socket, and does not call through | 536 // This represents what we know about the socket, and does not call through |
536 // to the system. | 537 // to the system. |
537 switch (socket->GetSocketType()) { | 538 if (socket->GetSocketType() == Socket::TYPE_TCP) |
538 case Socket::TYPE_TCP: | 539 info.socket_type = extensions::api::socket::SOCKET_SOCKET_TYPE_TCP; |
539 info.socket_type = kTCPOption; | 540 else |
540 break; | 541 info.socket_type = extensions::api::socket::SOCKET_SOCKET_TYPE_UDP; |
541 case Socket::TYPE_UDP: | |
542 info.socket_type = kUDPOption; | |
543 break; | |
544 default: | |
545 NOTREACHED() << "Unknown socket type."; | |
546 info.socket_type = kUnknown; | |
547 break; | |
548 } | |
549 info.connected = socket->IsConnected(); | 542 info.connected = socket->IsConnected(); |
550 | 543 |
551 // Grab the peer address as known by the OS. This and the call below will | 544 // Grab the peer address as known by the OS. This and the call below will |
552 // always succeed while the socket is connected, even if the socket has | 545 // always succeed while the socket is connected, even if the socket has |
553 // been remotely closed by the peer; only reading the socket will reveal | 546 // been remotely closed by the peer; only reading the socket will reveal |
554 // that it should be closed locally. | 547 // that it should be closed locally. |
555 net::IPEndPoint peerAddress; | 548 net::IPEndPoint peerAddress; |
556 if (socket->GetPeerAddress(&peerAddress)) { | 549 if (socket->GetPeerAddress(&peerAddress)) { |
557 info.peer_address.reset( | 550 info.peer_address.reset( |
558 new std::string(peerAddress.ToStringWithoutPort())); | 551 new std::string(peerAddress.ToStringWithoutPort())); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 info->name = i->name; | 605 info->name = i->name; |
613 info->address = net::IPAddressToString(i->address); | 606 info->address = net::IPAddressToString(i->address); |
614 create_arg.push_back(info); | 607 create_arg.push_back(info); |
615 } | 608 } |
616 | 609 |
617 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 610 results_ = api::socket::GetNetworkList::Results::Create(create_arg); |
618 SendResponse(true); | 611 SendResponse(true); |
619 } | 612 } |
620 | 613 |
621 } // namespace extensions | 614 } // namespace extensions |
OLD | NEW |