OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/socket/socket_api.h" | 5 #include "extensions/browser/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/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 const char kSocketNotFoundError[] = "Socket not found"; | 42 const char kSocketNotFoundError[] = "Socket not found"; |
43 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 43 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
44 const char kPermissionError[] = "App does not have permission"; | 44 const char kPermissionError[] = "App does not have permission"; |
45 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 45 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
46 const char kTCPSocketBindError[] = | 46 const char kTCPSocketBindError[] = |
47 "TCP socket does not support bind. For TCP server please use listen."; | 47 "TCP socket does not support bind. For TCP server please use listen."; |
48 const char kMulticastSocketTypeError[] = "Only UDP socket supports multicast."; | 48 const char kMulticastSocketTypeError[] = "Only UDP socket supports multicast."; |
49 const char kSecureSocketTypeError[] = "Only TCP sockets are supported for TLS."; | 49 const char kSecureSocketTypeError[] = "Only TCP sockets are supported for TLS."; |
50 const char kSocketNotConnectedError[] = "Socket not connected"; | 50 const char kSocketNotConnectedError[] = "Socket not connected"; |
51 const char kWildcardAddress[] = "*"; | 51 const char kWildcardAddress[] = "*"; |
52 const int kWildcardPort = 0; | 52 const uint16 kWildcardPort = 0; |
53 | 53 |
54 SocketAsyncApiFunction::SocketAsyncApiFunction() {} | 54 SocketAsyncApiFunction::SocketAsyncApiFunction() {} |
55 | 55 |
56 SocketAsyncApiFunction::~SocketAsyncApiFunction() {} | 56 SocketAsyncApiFunction::~SocketAsyncApiFunction() {} |
57 | 57 |
58 bool SocketAsyncApiFunction::PrePrepare() { | 58 bool SocketAsyncApiFunction::PrePrepare() { |
59 manager_ = CreateSocketResourceManager(); | 59 manager_ = CreateSocketResourceManager(); |
60 return manager_->SetBrowserContext(browser_context()); | 60 return manager_->SetBrowserContext(browser_context()); |
61 } | 61 } |
62 | 62 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 | 185 |
186 SocketConnectFunction::SocketConnectFunction() | 186 SocketConnectFunction::SocketConnectFunction() |
187 : socket_id_(0), hostname_(), port_(0) { | 187 : socket_id_(0), hostname_(), port_(0) { |
188 } | 188 } |
189 | 189 |
190 SocketConnectFunction::~SocketConnectFunction() {} | 190 SocketConnectFunction::~SocketConnectFunction() {} |
191 | 191 |
192 bool SocketConnectFunction::Prepare() { | 192 bool SocketConnectFunction::Prepare() { |
193 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 193 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
194 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); | 194 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); |
195 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); | 195 int port; |
196 EXTENSION_FUNCTION_VALIDATE( | |
197 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); | |
Peter Kasting
2014/11/12 23:54:26
Maybe we should have had these checks already?
Jeffrey Yasskin
2014/11/17 07:00:33
Yes.
| |
198 port_ = static_cast<uint16>(port); | |
196 return true; | 199 return true; |
197 } | 200 } |
198 | 201 |
199 void SocketConnectFunction::AsyncWorkStart() { | 202 void SocketConnectFunction::AsyncWorkStart() { |
200 Socket* socket = GetSocket(socket_id_); | 203 Socket* socket = GetSocket(socket_id_); |
201 if (!socket) { | 204 if (!socket) { |
202 error_ = kSocketNotFoundError; | 205 error_ = kSocketNotFoundError; |
203 SetResult(new base::FundamentalValue(-1)); | 206 SetResult(new base::FundamentalValue(-1)); |
204 AsyncWorkCompleted(); | 207 AsyncWorkCompleted(); |
205 return; | 208 return; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 if (socket) | 274 if (socket) |
272 socket->Disconnect(); | 275 socket->Disconnect(); |
273 else | 276 else |
274 error_ = kSocketNotFoundError; | 277 error_ = kSocketNotFoundError; |
275 SetResult(base::Value::CreateNullValue()); | 278 SetResult(base::Value::CreateNullValue()); |
276 } | 279 } |
277 | 280 |
278 bool SocketBindFunction::Prepare() { | 281 bool SocketBindFunction::Prepare() { |
279 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 282 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
280 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); | 283 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); |
281 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); | 284 int port; |
285 EXTENSION_FUNCTION_VALIDATE( | |
286 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); | |
287 port_ = static_cast<uint16>(port); | |
282 return true; | 288 return true; |
283 } | 289 } |
284 | 290 |
285 void SocketBindFunction::Work() { | 291 void SocketBindFunction::Work() { |
286 int result = -1; | 292 int result = -1; |
287 Socket* socket = GetSocket(socket_id_); | 293 Socket* socket = GetSocket(socket_id_); |
288 | 294 |
289 if (!socket) { | 295 if (!socket) { |
290 error_ = kSocketNotFoundError; | 296 error_ = kSocketNotFoundError; |
291 SetResult(new base::FundamentalValue(result)); | 297 SetResult(new base::FundamentalValue(result)); |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
473 return; | 479 return; |
474 } | 480 } |
475 | 481 |
476 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, | 482 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, |
477 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); | 483 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); |
478 } | 484 } |
479 | 485 |
480 void SocketRecvFromFunction::OnCompleted(int bytes_read, | 486 void SocketRecvFromFunction::OnCompleted(int bytes_read, |
481 scoped_refptr<net::IOBuffer> io_buffer, | 487 scoped_refptr<net::IOBuffer> io_buffer, |
482 const std::string& address, | 488 const std::string& address, |
483 int port) { | 489 uint16 port) { |
484 base::DictionaryValue* result = new base::DictionaryValue(); | 490 base::DictionaryValue* result = new base::DictionaryValue(); |
485 result->SetInteger(kResultCodeKey, bytes_read); | 491 result->SetInteger(kResultCodeKey, bytes_read); |
486 if (bytes_read > 0) { | 492 if (bytes_read > 0) { |
487 result->Set(kDataKey, | 493 result->Set(kDataKey, |
488 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 494 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
489 bytes_read)); | 495 bytes_read)); |
490 } else { | 496 } else { |
491 result->Set(kDataKey, new base::BinaryValue()); | 497 result->Set(kDataKey, new base::BinaryValue()); |
492 } | 498 } |
493 result->SetString(kAddressKey, address); | 499 result->SetString(kAddressKey, address); |
494 result->SetInteger(kPortKey, port); | 500 result->SetInteger(kPortKey, port); |
495 SetResult(result); | 501 SetResult(result); |
496 | 502 |
497 AsyncWorkCompleted(); | 503 AsyncWorkCompleted(); |
498 } | 504 } |
499 | 505 |
500 SocketSendToFunction::SocketSendToFunction() | 506 SocketSendToFunction::SocketSendToFunction() |
501 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0), port_(0) { | 507 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0), port_(0) { |
502 } | 508 } |
503 | 509 |
504 SocketSendToFunction::~SocketSendToFunction() {} | 510 SocketSendToFunction::~SocketSendToFunction() {} |
505 | 511 |
506 bool SocketSendToFunction::Prepare() { | 512 bool SocketSendToFunction::Prepare() { |
507 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 513 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
508 base::BinaryValue* data = NULL; | 514 base::BinaryValue* data = NULL; |
509 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 515 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
510 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); | 516 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); |
511 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); | 517 int port; |
518 EXTENSION_FUNCTION_VALIDATE( | |
519 args_->GetInteger(3, &port) && port >= 0 && port <= 65535); | |
520 port_ = static_cast<uint16>(port); | |
512 | 521 |
513 io_buffer_size_ = data->GetSize(); | 522 io_buffer_size_ = data->GetSize(); |
514 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 523 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
515 return true; | 524 return true; |
516 } | 525 } |
517 | 526 |
518 void SocketSendToFunction::AsyncWorkStart() { | 527 void SocketSendToFunction::AsyncWorkStart() { |
519 Socket* socket = GetSocket(socket_id_); | 528 Socket* socket = GetSocket(socket_id_); |
520 if (!socket) { | 529 if (!socket) { |
521 error_ = kSocketNotFoundError; | 530 error_ = kSocketNotFoundError; |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
979 } else { | 988 } else { |
980 RemoveSocket(params_->socket_id); | 989 RemoveSocket(params_->socket_id); |
981 error_ = net::ErrorToString(result); | 990 error_ = net::ErrorToString(result); |
982 } | 991 } |
983 | 992 |
984 results_ = core_api::socket::Secure::Results::Create(result); | 993 results_ = core_api::socket::Secure::Results::Create(result); |
985 AsyncWorkCompleted(); | 994 AsyncWorkCompleted(); |
986 } | 995 } |
987 | 996 |
988 } // namespace extensions | 997 } // namespace extensions |
OLD | NEW |