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" |
| 11 #include "build/build_config.h" |
11 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
12 #include "content/public/browser/resource_context.h" | 13 #include "content/public/browser/resource_context.h" |
13 #include "extensions/browser/api/dns/host_resolver_wrapper.h" | 14 #include "extensions/browser/api/dns/host_resolver_wrapper.h" |
14 #include "extensions/browser/api/socket/socket.h" | 15 #include "extensions/browser/api/socket/socket.h" |
15 #include "extensions/browser/api/socket/tcp_socket.h" | 16 #include "extensions/browser/api/socket/tcp_socket.h" |
16 #include "extensions/browser/api/socket/tls_socket.h" | 17 #include "extensions/browser/api/socket/tls_socket.h" |
17 #include "extensions/browser/api/socket/udp_socket.h" | 18 #include "extensions/browser/api/socket/udp_socket.h" |
18 #include "extensions/browser/extension_system.h" | 19 #include "extensions/browser/extension_system.h" |
19 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
20 #include "extensions/common/permissions/permissions_data.h" | 21 #include "extensions/common/permissions/permissions_data.h" |
(...skipping 27 matching lines...) Expand all Loading... |
48 const char kSocketNotFoundError[] = "Socket not found"; | 49 const char kSocketNotFoundError[] = "Socket not found"; |
49 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 50 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
50 const char kPermissionError[] = "App does not have permission"; | 51 const char kPermissionError[] = "App does not have permission"; |
51 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 52 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
52 const char kTCPSocketBindError[] = | 53 const char kTCPSocketBindError[] = |
53 "TCP socket does not support bind. For TCP server please use listen."; | 54 "TCP socket does not support bind. For TCP server please use listen."; |
54 const char kMulticastSocketTypeError[] = "Only UDP socket supports multicast."; | 55 const char kMulticastSocketTypeError[] = "Only UDP socket supports multicast."; |
55 const char kSecureSocketTypeError[] = "Only TCP sockets are supported for TLS."; | 56 const char kSecureSocketTypeError[] = "Only TCP sockets are supported for TLS."; |
56 const char kSocketNotConnectedError[] = "Socket not connected"; | 57 const char kSocketNotConnectedError[] = "Socket not connected"; |
57 const char kWildcardAddress[] = "*"; | 58 const char kWildcardAddress[] = "*"; |
58 const uint16 kWildcardPort = 0; | 59 const uint16_t kWildcardPort = 0; |
59 | 60 |
60 #if defined(OS_CHROMEOS) | 61 #if defined(OS_CHROMEOS) |
61 const char kFirewallFailure[] = "Failed to open firewall port"; | 62 const char kFirewallFailure[] = "Failed to open firewall port"; |
62 #endif // OS_CHROMEOS | 63 #endif // OS_CHROMEOS |
63 | 64 |
64 SocketAsyncApiFunction::SocketAsyncApiFunction() {} | 65 SocketAsyncApiFunction::SocketAsyncApiFunction() {} |
65 | 66 |
66 SocketAsyncApiFunction::~SocketAsyncApiFunction() {} | 67 SocketAsyncApiFunction::~SocketAsyncApiFunction() {} |
67 | 68 |
68 bool SocketAsyncApiFunction::PrePrepare() { | 69 bool SocketAsyncApiFunction::PrePrepare() { |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } | 262 } |
262 | 263 |
263 SocketConnectFunction::~SocketConnectFunction() {} | 264 SocketConnectFunction::~SocketConnectFunction() {} |
264 | 265 |
265 bool SocketConnectFunction::Prepare() { | 266 bool SocketConnectFunction::Prepare() { |
266 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 267 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
267 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); | 268 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); |
268 int port; | 269 int port; |
269 EXTENSION_FUNCTION_VALIDATE( | 270 EXTENSION_FUNCTION_VALIDATE( |
270 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); | 271 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); |
271 port_ = static_cast<uint16>(port); | 272 port_ = static_cast<uint16_t>(port); |
272 return true; | 273 return true; |
273 } | 274 } |
274 | 275 |
275 void SocketConnectFunction::AsyncWorkStart() { | 276 void SocketConnectFunction::AsyncWorkStart() { |
276 Socket* socket = GetSocket(socket_id_); | 277 Socket* socket = GetSocket(socket_id_); |
277 if (!socket) { | 278 if (!socket) { |
278 error_ = kSocketNotFoundError; | 279 error_ = kSocketNotFoundError; |
279 SetResult(new base::FundamentalValue(-1)); | 280 SetResult(new base::FundamentalValue(-1)); |
280 AsyncWorkCompleted(); | 281 AsyncWorkCompleted(); |
281 return; | 282 return; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 error_ = kSocketNotFoundError; | 350 error_ = kSocketNotFoundError; |
350 SetResult(base::Value::CreateNullValue()); | 351 SetResult(base::Value::CreateNullValue()); |
351 } | 352 } |
352 | 353 |
353 bool SocketBindFunction::Prepare() { | 354 bool SocketBindFunction::Prepare() { |
354 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 355 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
355 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); | 356 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); |
356 int port; | 357 int port; |
357 EXTENSION_FUNCTION_VALIDATE( | 358 EXTENSION_FUNCTION_VALIDATE( |
358 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); | 359 args_->GetInteger(2, &port) && port >= 0 && port <= 65535); |
359 port_ = static_cast<uint16>(port); | 360 port_ = static_cast<uint16_t>(port); |
360 return true; | 361 return true; |
361 } | 362 } |
362 | 363 |
363 void SocketBindFunction::AsyncWorkStart() { | 364 void SocketBindFunction::AsyncWorkStart() { |
364 Socket* socket = GetSocket(socket_id_); | 365 Socket* socket = GetSocket(socket_id_); |
365 if (!socket) { | 366 if (!socket) { |
366 error_ = kSocketNotFoundError; | 367 error_ = kSocketNotFoundError; |
367 SetResult(new base::FundamentalValue(-1)); | 368 SetResult(new base::FundamentalValue(-1)); |
368 AsyncWorkCompleted(); | 369 AsyncWorkCompleted(); |
369 return; | 370 return; |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 return; | 565 return; |
565 } | 566 } |
566 | 567 |
567 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, | 568 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, |
568 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); | 569 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); |
569 } | 570 } |
570 | 571 |
571 void SocketRecvFromFunction::OnCompleted(int bytes_read, | 572 void SocketRecvFromFunction::OnCompleted(int bytes_read, |
572 scoped_refptr<net::IOBuffer> io_buffer, | 573 scoped_refptr<net::IOBuffer> io_buffer, |
573 const std::string& address, | 574 const std::string& address, |
574 uint16 port) { | 575 uint16_t port) { |
575 base::DictionaryValue* result = new base::DictionaryValue(); | 576 base::DictionaryValue* result = new base::DictionaryValue(); |
576 result->SetInteger(kResultCodeKey, bytes_read); | 577 result->SetInteger(kResultCodeKey, bytes_read); |
577 if (bytes_read > 0) { | 578 if (bytes_read > 0) { |
578 result->Set(kDataKey, | 579 result->Set(kDataKey, |
579 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), | 580 base::BinaryValue::CreateWithCopiedBuffer(io_buffer->data(), |
580 bytes_read)); | 581 bytes_read)); |
581 } else { | 582 } else { |
582 result->Set(kDataKey, new base::BinaryValue()); | 583 result->Set(kDataKey, new base::BinaryValue()); |
583 } | 584 } |
584 result->SetString(kAddressKey, address); | 585 result->SetString(kAddressKey, address); |
(...skipping 10 matching lines...) Expand all Loading... |
595 SocketSendToFunction::~SocketSendToFunction() {} | 596 SocketSendToFunction::~SocketSendToFunction() {} |
596 | 597 |
597 bool SocketSendToFunction::Prepare() { | 598 bool SocketSendToFunction::Prepare() { |
598 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 599 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
599 base::BinaryValue* data = NULL; | 600 base::BinaryValue* data = NULL; |
600 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 601 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
601 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); | 602 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); |
602 int port; | 603 int port; |
603 EXTENSION_FUNCTION_VALIDATE( | 604 EXTENSION_FUNCTION_VALIDATE( |
604 args_->GetInteger(3, &port) && port >= 0 && port <= 65535); | 605 args_->GetInteger(3, &port) && port >= 0 && port <= 65535); |
605 port_ = static_cast<uint16>(port); | 606 port_ = static_cast<uint16_t>(port); |
606 | 607 |
607 io_buffer_size_ = data->GetSize(); | 608 io_buffer_size_ = data->GetSize(); |
608 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 609 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
609 return true; | 610 return true; |
610 } | 611 } |
611 | 612 |
612 void SocketSendToFunction::AsyncWorkStart() { | 613 void SocketSendToFunction::AsyncWorkStart() { |
613 Socket* socket = GetSocket(socket_id_); | 614 Socket* socket = GetSocket(socket_id_); |
614 if (!socket) { | 615 if (!socket) { |
615 error_ = kSocketNotFoundError; | 616 error_ = kSocketNotFoundError; |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1066 } else { | 1067 } else { |
1067 RemoveSocket(params_->socket_id); | 1068 RemoveSocket(params_->socket_id); |
1068 error_ = net::ErrorToString(result); | 1069 error_ = net::ErrorToString(result); |
1069 } | 1070 } |
1070 | 1071 |
1071 results_ = api::socket::Secure::Results::Create(result); | 1072 results_ = api::socket::Secure::Results::Create(result); |
1072 AsyncWorkCompleted(); | 1073 AsyncWorkCompleted(); |
1073 } | 1074 } |
1074 | 1075 |
1075 } // namespace extensions | 1076 } // namespace extensions |
OLD | NEW |