OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/sockets_tcp/sockets_tcp_api.h" | 5 #include "chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | 7 #include "chrome/browser/extensions/api/socket/tcp_socket.h" |
8 #include "chrome/browser/extensions/api/socket/tls_socket.h" | |
8 #include "chrome/browser/extensions/api/sockets_tcp/tcp_socket_event_dispatcher. h" | 9 #include "chrome/browser/extensions/api/sockets_tcp/tcp_socket_event_dispatcher. h" |
9 #include "chrome/common/extensions/api/sockets/sockets_manifest_data.h" | 10 #include "chrome/common/extensions/api/sockets/sockets_manifest_data.h" |
10 #include "content/public/common/socket_permission_request.h" | 11 #include "content/public/common/socket_permission_request.h" |
11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/url_request/url_request_context_getter.h" | |
12 | 14 |
13 using extensions::ResumableTCPSocket; | 15 using extensions::ResumableTCPSocket; |
14 using extensions::api::sockets_tcp::SocketInfo; | 16 using extensions::api::sockets_tcp::SocketInfo; |
15 using extensions::api::sockets_tcp::SocketProperties; | 17 using extensions::api::sockets_tcp::SocketProperties; |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 const char kSocketNotFoundError[] = "Socket not found"; | 21 const char kSocketNotFoundError[] = "Socket not found"; |
20 const char kPermissionError[] = "Does not have permission"; | 22 const char kPermissionError[] = "Does not have permission"; |
23 const char kInvalidSocketStateError[] = | |
24 "Socket must be a connected client TCP socket."; | |
25 const char kSocketNotConnectedError[] = "Socket not connected"; | |
21 | 26 |
22 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, | 27 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, |
23 ResumableTCPSocket* socket) { | 28 ResumableTCPSocket* socket) { |
24 linked_ptr<SocketInfo> socket_info(new SocketInfo()); | 29 linked_ptr<SocketInfo> socket_info(new SocketInfo()); |
25 // This represents what we know about the socket, and does not call through | 30 // This represents what we know about the socket, and does not call through |
26 // to the system. | 31 // to the system. |
27 socket_info->socket_id = socket_id; | 32 socket_info->socket_id = socket_id; |
28 if (!socket->name().empty()) { | 33 if (!socket->name().empty()) { |
29 socket_info->name.reset(new std::string(socket->name())); | 34 socket_info->name.reset(new std::string(socket->name())); |
30 } | 35 } |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
433 int socket_id = *it; | 438 int socket_id = *it; |
434 ResumableTCPSocket* socket = GetTcpSocket(socket_id); | 439 ResumableTCPSocket* socket = GetTcpSocket(socket_id); |
435 if (socket) { | 440 if (socket) { |
436 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); | 441 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); |
437 } | 442 } |
438 } | 443 } |
439 } | 444 } |
440 results_ = sockets_tcp::GetSockets::Results::Create(socket_infos); | 445 results_ = sockets_tcp::GetSockets::Results::Create(socket_infos); |
441 } | 446 } |
442 | 447 |
448 SocketsTcpSecureFunction::SocketsTcpSecureFunction() {} | |
449 SocketsTcpSecureFunction::~SocketsTcpSecureFunction() {} | |
450 | |
451 bool SocketsTcpSecureFunction::Prepare() { | |
452 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
453 params_ = api::socket::Secure::Params::Create(*args_); | |
454 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
455 url_request_getter_ = GetProfile()->GetRequestContext(); | |
456 return true; | |
457 } | |
458 | |
459 // Override the regular implementation, which would call AsyncWorkCompleted | |
460 // immediately after Work(). | |
461 void SocketsTcpSecureFunction::AsyncWorkStart() { | |
462 int result = net::ERR_INVALID_ARGUMENT; | |
Ryan Sleevi
2013/12/17 00:37:40
style: Previous comments about using |reuslt| here
lally
2014/01/09 18:47:16
Done.
| |
463 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
464 | |
465 ResumableTCPSocket* socket = GetTcpSocket(params_->socket_id); | |
466 Profile* profile = GetProfile(); | |
467 DCHECK(profile); | |
468 | |
469 if (!socket) { | |
470 SetResult(new base::FundamentalValue(result)); | |
471 error_ = kSocketNotFoundError; | |
472 AsyncWorkCompleted(); | |
473 return; | |
474 } | |
475 | |
476 // Make sure it's a connected TCP client socket. Error out if it's already | |
477 // secure()'d. | |
478 if (socket->GetSocketType() == Socket::TYPE_TLS | |
479 || !socket->IsConnected() | |
480 || socket->ClientStream() == NULL) { | |
Ryan Sleevi
2013/12/17 00:37:40
style: Conditionals on preceding line.
lally
2014/01/09 18:47:16
Done.
| |
481 SetResult(new base::FundamentalValue(result)); | |
482 error_ = kInvalidSocketStateError; | |
483 AsyncWorkCompleted(); | |
484 return; | |
485 } | |
486 | |
487 if (!socket->IsConnected()) { | |
488 SetResult(new base::FundamentalValue(result)); | |
489 error_ = kSocketNotConnectedError; | |
490 AsyncWorkCompleted(); | |
491 return; | |
492 } | |
493 | |
494 TLSSocket::SecureTCPSocket( | |
495 socket, profile, url_request_getter_, extension_id(), | |
496 params_->options.get(), | |
497 base::Bind(&SocketsTcpSecureFunction::TlsConnectDone, | |
498 this)); | |
499 } | |
500 | |
501 void SocketsTcpSecureFunction::TlsConnectDone(TLSSocket* sock, int result) { | |
502 if (sock) { | |
503 SetSocket(params_->socket_id, sock); | |
504 } else { | |
505 RemoveSocket(params_->socket_id); | |
506 } | |
507 | |
508 url_request_getter_->Release(); | |
509 | |
510 results_ = api::sockets_tcp::Secure::Results::Create(result); | |
511 if (result != net::OK) { | |
512 error_ = net::ErrorToString(result); | |
513 } | |
514 AsyncWorkCompleted(); | |
515 } | |
516 | |
443 } // namespace api | 517 } // namespace api |
444 } // namespace extensions | 518 } // namespace extensions |
OLD | NEW |