Chromium Code Reviews| Index: chrome/browser/extensions/socket_api_controller.cc |
| diff --git a/chrome/browser/extensions/socket_api_controller.cc b/chrome/browser/extensions/socket_api_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..df74b96c579ab47ece2f6bf425ab7a070850608e |
| --- /dev/null |
| +++ b/chrome/browser/extensions/socket_api_controller.cc |
| @@ -0,0 +1,154 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/json/json_writer.h" |
| +#include "base/stl_util.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/extensions/socket_api_constants.h" |
| +#include "chrome/browser/extensions/socket_api_controller.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/rand_callback.h" |
| +#include "net/udp/datagram_socket.h" |
| +#include "net/udp/udp_client_socket.h" |
| +#include "net/udp/udp_socket.h" |
| + |
| +using namespace net; |
| + |
| +namespace constants = extensions::socket_api_constants; |
| + |
| +namespace extensions { |
| + |
| +Socket::Socket(const Profile* profile) |
| + : profile_(profile), |
| + src_id_(-1), |
| + udp_client_socket_(new UDPClientSocket( |
| + DatagramSocket::DEFAULT_BIND, |
| + RandIntCallback(), |
| + NULL, |
| + NetLog::Source())), |
| + is_connected_(false), |
| + ALLOW_THIS_IN_INITIALIZER_LIST( |
| + io_callback_(this, &Socket::OnIOComplete)) {} |
| + |
| +Socket::~Socket() { |
| + if (is_connected_) { |
| + Close(); |
| + } |
| +} |
| + |
| +void Socket::OnIOComplete(int result) { |
| + // We don't need to do anything. |
| +} |
| + |
| +bool Socket::Connect(const net::IPEndPoint& ip_end_point) { |
| + is_connected_ = udp_client_socket_->Connect(ip_end_point); |
| + return is_connected_; |
| +} |
| + |
| +void Socket::Close() { |
| + is_connected_ = false; |
| + udp_client_socket_->Close(); |
| +} |
| + |
| +int Socket::Write(const std::string message) { |
| + int length = message.length(); |
| + scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(message)); |
| + scoped_refptr<DrainableIOBuffer> buffer( |
| + new DrainableIOBuffer(io_buffer, length)); |
| + |
| + int bytes_sent = 0; |
| + while (buffer->BytesRemaining()) { |
| + int rv = udp_client_socket_->Write(buffer, |
| + buffer->BytesRemaining(), |
| + &io_callback_); |
| + if (rv <= 0) { |
| + // We pass all errors, including ERROR_IO_PENDING, back to the caller. |
|
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
Based on my reading of http://www.google.com/codes
miket_OOO
2011/12/02 21:06:36
Ugh, yeah, you're right. I thought we were suppose
|
| + return bytes_sent > 0 ? bytes_sent : rv; |
| + } |
| + bytes_sent += rv; |
| + buffer->DidConsume(rv); |
| + } |
| + return bytes_sent; |
| +} |
| + |
| +SocketController* SocketController::GetInstance() { |
| + return Singleton<SocketController>::get(); |
| +} |
| + |
| +SocketController::SocketController() : next_socket_id_(1) {} |
| + |
| +SocketController::~SocketController() { |
| + STLDeleteValues(&socket_map_); |
| +} |
| + |
| +Socket* SocketController::GetSocket(int socket_id) { |
| + // TODO(miket): we should verify that the extension asking for the |
| + // socket is the same one that created it. |
| + SocketMap::iterator i = socket_map_.find(socket_id); |
| + if (i != socket_map_.end()) |
| + return i->second; |
| + return NULL; |
| +} |
| + |
| +int SocketController::CreateUdp(const Profile* profile, |
| + const std::string& extension_id, |
| + const GURL& src_url) { |
| + Socket* socket = new Socket(profile); |
| + CHECK(socket); |
| + socket->set_src_extension_id(extension_id); |
| + socket->set_src_url(src_url); |
| + socket_map_[next_socket_id_] = socket; |
|
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
Should you also be setting the socket's src_id?
miket_OOO
2011/12/02 21:06:36
No. I've removed src_id from this CL. I took it fr
|
| + return next_socket_id_++; |
| +} |
| + |
| +bool SocketController::DestroyUdp(int socket_id) { |
| + Socket* socket = GetSocket(socket_id); |
| + if (!socket) |
| + return false; |
| + delete socket; |
| + socket_map_.erase(socket_id); |
| + return true; |
| +} |
| + |
| +// TODO(miket): it *might* be nice to be able to resolve DNS. I am not putting |
| +// in interesting error reporting for this method because we clearly can't |
| +// leave experimental without DNS resolution. |
|
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
It's customary to add a "// static" line in the co
miket_OOO
2011/12/02 21:06:36
Done.
|
| +bool SocketController::CreateIPEndPoint(const std::string address, int port, |
| + net::IPEndPoint* ip_end_point) { |
| + net::IPAddressNumber ip_number; |
| + bool rv = net::ParseIPLiteralToNumber(address, &ip_number); |
| + if (!rv) |
| + return false; |
| + *ip_end_point = net::IPEndPoint(ip_number, port); |
| + return true; |
| +} |
| + |
| +bool SocketController::ConnectUdp(int socket_id, const std::string address, |
| + int port) { |
| + Socket* socket = GetSocket(socket_id); |
| + if (!socket) |
| + return false; |
| + net::IPEndPoint ip_end_point; |
| + if (!CreateIPEndPoint(address, port, &ip_end_point)) |
| + return false; |
| + return socket->Connect(ip_end_point) == net::OK; |
| +} |
| + |
| +void SocketController::CloseUdp(int socket_id) { |
| + Socket* socket = GetSocket(socket_id); |
| + if (socket) |
| + socket->Close(); |
| +} |
| + |
| +int SocketController::WriteUdp(int socket_id, const std::string message) { |
| + Socket* socket = GetSocket(socket_id); |
| + if (!socket) { |
| + return -1; |
| + } |
| + return socket->Write(message); |
| +} |
| + |
| +} // namespace extensions |