Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: chrome/browser/extensions/socket_api_controller.cc

Issue 8743017: Real (but naive) UDP socket sending. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Initial. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/json/json_writer.h"
6 #include "base/stl_util.h"
7 #include "base/values.h"
8 #include "chrome/browser/extensions/socket_api_constants.h"
9 #include "chrome/browser/extensions/socket_api_controller.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/rand_callback.h"
14 #include "net/udp/datagram_socket.h"
15 #include "net/udp/udp_client_socket.h"
16 #include "net/udp/udp_socket.h"
17
18 using namespace net;
19
20 namespace constants = extensions::socket_api_constants;
21
22 namespace extensions {
23
24 Socket::Socket(const Profile* profile)
25 : profile_(profile),
26 src_id_(-1),
27 udp_client_socket_(new UDPClientSocket(
28 DatagramSocket::DEFAULT_BIND,
29 RandIntCallback(),
30 NULL,
31 NetLog::Source())),
32 is_connected_(false),
33 ALLOW_THIS_IN_INITIALIZER_LIST(
34 io_callback_(this, &Socket::OnIOComplete)) {}
35
36 Socket::~Socket() {
37 if (is_connected_) {
38 Close();
39 }
40 }
41
42 void Socket::OnIOComplete(int result) {
43 // We don't need to do anything.
44 }
45
46 bool Socket::Connect(const net::IPEndPoint& ip_end_point) {
47 is_connected_ = udp_client_socket_->Connect(ip_end_point);
48 return is_connected_;
49 }
50
51 void Socket::Close() {
52 is_connected_ = false;
53 udp_client_socket_->Close();
54 }
55
56 int Socket::Write(const std::string message) {
57 int length = message.length();
58 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(message));
59 scoped_refptr<DrainableIOBuffer> buffer(
60 new DrainableIOBuffer(io_buffer, length));
61
62 int bytes_sent = 0;
63 while (buffer->BytesRemaining()) {
64 int rv = udp_client_socket_->Write(buffer,
65 buffer->BytesRemaining(),
66 &io_callback_);
67 if (rv <= 0) {
68 // 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
69 return bytes_sent > 0 ? bytes_sent : rv;
70 }
71 bytes_sent += rv;
72 buffer->DidConsume(rv);
73 }
74 return bytes_sent;
75 }
76
77 SocketController* SocketController::GetInstance() {
78 return Singleton<SocketController>::get();
79 }
80
81 SocketController::SocketController() : next_socket_id_(1) {}
82
83 SocketController::~SocketController() {
84 STLDeleteValues(&socket_map_);
85 }
86
87 Socket* SocketController::GetSocket(int socket_id) {
88 // TODO(miket): we should verify that the extension asking for the
89 // socket is the same one that created it.
90 SocketMap::iterator i = socket_map_.find(socket_id);
91 if (i != socket_map_.end())
92 return i->second;
93 return NULL;
94 }
95
96 int SocketController::CreateUdp(const Profile* profile,
97 const std::string& extension_id,
98 const GURL& src_url) {
99 Socket* socket = new Socket(profile);
100 CHECK(socket);
101 socket->set_src_extension_id(extension_id);
102 socket->set_src_url(src_url);
103 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
104 return next_socket_id_++;
105 }
106
107 bool SocketController::DestroyUdp(int socket_id) {
108 Socket* socket = GetSocket(socket_id);
109 if (!socket)
110 return false;
111 delete socket;
112 socket_map_.erase(socket_id);
113 return true;
114 }
115
116 // TODO(miket): it *might* be nice to be able to resolve DNS. I am not putting
117 // in interesting error reporting for this method because we clearly can't
118 // 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.
119 bool SocketController::CreateIPEndPoint(const std::string address, int port,
120 net::IPEndPoint* ip_end_point) {
121 net::IPAddressNumber ip_number;
122 bool rv = net::ParseIPLiteralToNumber(address, &ip_number);
123 if (!rv)
124 return false;
125 *ip_end_point = net::IPEndPoint(ip_number, port);
126 return true;
127 }
128
129 bool SocketController::ConnectUdp(int socket_id, const std::string address,
130 int port) {
131 Socket* socket = GetSocket(socket_id);
132 if (!socket)
133 return false;
134 net::IPEndPoint ip_end_point;
135 if (!CreateIPEndPoint(address, port, &ip_end_point))
136 return false;
137 return socket->Connect(ip_end_point) == net::OK;
138 }
139
140 void SocketController::CloseUdp(int socket_id) {
141 Socket* socket = GetSocket(socket_id);
142 if (socket)
143 socket->Close();
144 }
145
146 int SocketController::WriteUdp(int socket_id, const std::string message) {
147 Socket* socket = GetSocket(socket_id);
148 if (!socket) {
149 return -1;
150 }
151 return socket->Write(message);
152 }
153
154 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698