| 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 <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #include "net/log/net_log.h" | 33 #include "net/log/net_log.h" |
| 34 #include "net/url_request/url_request_context.h" | 34 #include "net/url_request/url_request_context.h" |
| 35 #include "net/url_request/url_request_context_getter.h" | 35 #include "net/url_request/url_request_context_getter.h" |
| 36 | 36 |
| 37 #if defined(OS_CHROMEOS) | 37 #if defined(OS_CHROMEOS) |
| 38 #include "content/public/browser/browser_thread.h" | 38 #include "content/public/browser/browser_thread.h" |
| 39 #endif // OS_CHROMEOS | 39 #endif // OS_CHROMEOS |
| 40 | 40 |
| 41 namespace extensions { | 41 namespace extensions { |
| 42 | 42 |
| 43 using content::BrowserThread; | 43 namespace { |
| 44 using content::SocketPermissionRequest; | |
| 45 | 44 |
| 46 const char kAddressKey[] = "address"; | 45 const char kAddressKey[] = "address"; |
| 47 const char kPortKey[] = "port"; | 46 const char kPortKey[] = "port"; |
| 48 const char kBytesWrittenKey[] = "bytesWritten"; | 47 const char kBytesWrittenKey[] = "bytesWritten"; |
| 49 const char kDataKey[] = "data"; | 48 const char kDataKey[] = "data"; |
| 50 const char kResultCodeKey[] = "resultCode"; | 49 const char kResultCodeKey[] = "resultCode"; |
| 51 const char kSocketIdKey[] = "socketId"; | 50 const char kSocketIdKey[] = "socketId"; |
| 52 | 51 |
| 53 const char kSocketNotFoundError[] = "Socket not found"; | 52 const char kSocketNotFoundError[] = "Socket not found"; |
| 54 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 53 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
| 55 const char kPermissionError[] = "App does not have permission"; | 54 const char kPermissionError[] = "App does not have permission"; |
| 55 const char kPortInvalidError[] = "Port must be a value between 0 and 65535."; |
| 56 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 56 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
| 57 const char kTCPSocketBindError[] = | 57 const char kTCPSocketBindError[] = |
| 58 "TCP socket does not support bind. For TCP server please use listen."; | 58 "TCP socket does not support bind. For TCP server please use listen."; |
| 59 const char kMulticastSocketTypeError[] = "Only UDP socket supports multicast."; | 59 const char kMulticastSocketTypeError[] = "Only UDP socket supports multicast."; |
| 60 const char kSecureSocketTypeError[] = "Only TCP sockets are supported for TLS."; | 60 const char kSecureSocketTypeError[] = "Only TCP sockets are supported for TLS."; |
| 61 const char kSocketNotConnectedError[] = "Socket not connected"; | 61 const char kSocketNotConnectedError[] = "Socket not connected"; |
| 62 const char kWildcardAddress[] = "*"; | 62 const char kWildcardAddress[] = "*"; |
| 63 const uint16_t kWildcardPort = 0; | 63 const uint16_t kWildcardPort = 0; |
| 64 | 64 |
| 65 #if defined(OS_CHROMEOS) | 65 #if defined(OS_CHROMEOS) |
| 66 const char kFirewallFailure[] = "Failed to open firewall port"; | 66 const char kFirewallFailure[] = "Failed to open firewall port"; |
| 67 #endif // OS_CHROMEOS | 67 #endif // OS_CHROMEOS |
| 68 | 68 |
| 69 bool IsPortValid(int port) { |
| 70 return port >= 0 && port <= 65535; |
| 71 } |
| 72 |
| 73 } // namespace |
| 74 |
| 75 using content::BrowserThread; |
| 76 using content::SocketPermissionRequest; |
| 77 |
| 69 SocketAsyncApiFunction::SocketAsyncApiFunction() {} | 78 SocketAsyncApiFunction::SocketAsyncApiFunction() {} |
| 70 | 79 |
| 71 SocketAsyncApiFunction::~SocketAsyncApiFunction() {} | 80 SocketAsyncApiFunction::~SocketAsyncApiFunction() {} |
| 72 | 81 |
| 73 bool SocketAsyncApiFunction::PrePrepare() { | 82 bool SocketAsyncApiFunction::PrePrepare() { |
| 74 manager_ = CreateSocketResourceManager(); | 83 manager_ = CreateSocketResourceManager(); |
| 75 return manager_->SetBrowserContext(browser_context()); | 84 return manager_->SetBrowserContext(browser_context()); |
| 76 } | 85 } |
| 77 | 86 |
| 78 bool SocketAsyncApiFunction::Respond() { return error_.empty(); } | 87 bool SocketAsyncApiFunction::Respond() { return error_.empty(); } |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 SocketConnectFunction::SocketConnectFunction() | 273 SocketConnectFunction::SocketConnectFunction() |
| 265 : socket_id_(0), hostname_(), port_(0) { | 274 : socket_id_(0), hostname_(), port_(0) { |
| 266 } | 275 } |
| 267 | 276 |
| 268 SocketConnectFunction::~SocketConnectFunction() {} | 277 SocketConnectFunction::~SocketConnectFunction() {} |
| 269 | 278 |
| 270 bool SocketConnectFunction::Prepare() { | 279 bool SocketConnectFunction::Prepare() { |
| 271 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 280 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 272 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); | 281 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); |
| 273 int port; | 282 int port; |
| 274 EXTENSION_FUNCTION_VALIDATE( | 283 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port)); |
| 275 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); | 284 if (!IsPortValid(port)) { |
| 285 error_ = kPortInvalidError; |
| 286 return false; |
| 287 } |
| 276 port_ = static_cast<uint16_t>(port); | 288 port_ = static_cast<uint16_t>(port); |
| 277 return true; | 289 return true; |
| 278 } | 290 } |
| 279 | 291 |
| 280 void SocketConnectFunction::AsyncWorkStart() { | 292 void SocketConnectFunction::AsyncWorkStart() { |
| 281 Socket* socket = GetSocket(socket_id_); | 293 Socket* socket = GetSocket(socket_id_); |
| 282 if (!socket) { | 294 if (!socket) { |
| 283 error_ = kSocketNotFoundError; | 295 error_ = kSocketNotFoundError; |
| 284 SetResult(base::MakeUnique<base::FundamentalValue>(-1)); | 296 SetResult(base::MakeUnique<base::FundamentalValue>(-1)); |
| 285 AsyncWorkCompleted(); | 297 AsyncWorkCompleted(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 socket->Disconnect(); | 364 socket->Disconnect(); |
| 353 else | 365 else |
| 354 error_ = kSocketNotFoundError; | 366 error_ = kSocketNotFoundError; |
| 355 SetResult(base::Value::CreateNullValue()); | 367 SetResult(base::Value::CreateNullValue()); |
| 356 } | 368 } |
| 357 | 369 |
| 358 bool SocketBindFunction::Prepare() { | 370 bool SocketBindFunction::Prepare() { |
| 359 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 371 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 360 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); | 372 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); |
| 361 int port; | 373 int port; |
| 362 EXTENSION_FUNCTION_VALIDATE( | 374 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port)); |
| 363 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); | 375 if (!IsPortValid(port)) { |
| 376 error_ = kPortInvalidError; |
| 377 return false; |
| 378 } |
| 364 port_ = static_cast<uint16_t>(port); | 379 port_ = static_cast<uint16_t>(port); |
| 365 return true; | 380 return true; |
| 366 } | 381 } |
| 367 | 382 |
| 368 void SocketBindFunction::AsyncWorkStart() { | 383 void SocketBindFunction::AsyncWorkStart() { |
| 369 Socket* socket = GetSocket(socket_id_); | 384 Socket* socket = GetSocket(socket_id_); |
| 370 if (!socket) { | 385 if (!socket) { |
| 371 error_ = kSocketNotFoundError; | 386 error_ = kSocketNotFoundError; |
| 372 SetResult(base::MakeUnique<base::FundamentalValue>(-1)); | 387 SetResult(base::MakeUnique<base::FundamentalValue>(-1)); |
| 373 AsyncWorkCompleted(); | 388 AsyncWorkCompleted(); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 } | 615 } |
| 601 | 616 |
| 602 SocketSendToFunction::~SocketSendToFunction() {} | 617 SocketSendToFunction::~SocketSendToFunction() {} |
| 603 | 618 |
| 604 bool SocketSendToFunction::Prepare() { | 619 bool SocketSendToFunction::Prepare() { |
| 605 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 620 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 606 base::BinaryValue* data = NULL; | 621 base::BinaryValue* data = NULL; |
| 607 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 622 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
| 608 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); | 623 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); |
| 609 int port; | 624 int port; |
| 610 EXTENSION_FUNCTION_VALIDATE( | 625 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port)); |
| 611 args_->GetInteger(3, &port) && port >= 0 && port <= 65535); | 626 if (!IsPortValid(port)) { |
| 627 error_ = kPortInvalidError; |
| 628 return false; |
| 629 } |
| 612 port_ = static_cast<uint16_t>(port); | 630 port_ = static_cast<uint16_t>(port); |
| 613 | 631 |
| 614 io_buffer_size_ = data->GetSize(); | 632 io_buffer_size_ = data->GetSize(); |
| 615 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 633 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
| 616 return true; | 634 return true; |
| 617 } | 635 } |
| 618 | 636 |
| 619 void SocketSendToFunction::AsyncWorkStart() { | 637 void SocketSendToFunction::AsyncWorkStart() { |
| 620 Socket* socket = GetSocket(socket_id_); | 638 Socket* socket = GetSocket(socket_id_); |
| 621 if (!socket) { | 639 if (!socket) { |
| (...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1074 } else { | 1092 } else { |
| 1075 RemoveSocket(params_->socket_id); | 1093 RemoveSocket(params_->socket_id); |
| 1076 error_ = net::ErrorToString(result); | 1094 error_ = net::ErrorToString(result); |
| 1077 } | 1095 } |
| 1078 | 1096 |
| 1079 results_ = api::socket::Secure::Results::Create(result); | 1097 results_ = api::socket::Secure::Results::Create(result); |
| 1080 AsyncWorkCompleted(); | 1098 AsyncWorkCompleted(); |
| 1081 } | 1099 } |
| 1082 | 1100 |
| 1083 } // namespace extensions | 1101 } // namespace extensions |
| OLD | NEW |