summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/socket/socket_api_controller.cc
blob: 30e3a3ae71f5667cd5c2a812734a5d3476738ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// 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/api/socket/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 extensions {

// A Socket wraps a low-level socket and includes housekeeping information that
// we need to manage it in the context of an extension.
class Socket {
 public:
  Socket(const Profile* profile, const std::string& src_extension_id,
         const GURL& src_url);
  ~Socket();

  bool Connect(const net::IPEndPoint& ip_end_point);
  void Close();
  int Write(const std::string message);

 private:
  // TODO(miket): this metadata will enable us to pass events back to the
  // extension that created this Socket.
  const Profile* profile_;
  int id_;
  std::string src_extension_id_;
  GURL src_url_;

  scoped_ptr<net::UDPClientSocket> udp_client_socket_;
  bool is_connected_;

  // A callback required by UDPClientSocket::Write().
  void OnIOComplete(int result);
};

Socket::Socket(const Profile* profile, const std::string& src_extension_id,
               const GURL& src_url)
    : profile_(profile),
      src_extension_id_(src_extension_id),
      src_url_(src_url),
      udp_client_socket_(new UDPClientSocket(
          DatagramSocket::DEFAULT_BIND,
          RandIntCallback(),
          NULL,
          NetLog::Source())),
      is_connected_(false) {}

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) == net::OK;
  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(),
        base::Bind(&Socket::OnIOComplete, base::Unretained(this)));
    if (rv <= 0) {
      // We pass all errors, including ERROR_IO_PENDING, back to the caller.
      return bytes_sent > 0 ? bytes_sent : rv;
    }
    bytes_sent += rv;
    buffer->DidConsume(rv);
  }
  return bytes_sent;
}

SocketController::SocketController() : next_socket_id_(1) {
}

SocketController::~SocketController() {}

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.get();
  return NULL;
}

int SocketController::CreateUdp(const Profile* profile,
                                const std::string& extension_id,
                                const GURL& src_url) {
  linked_ptr<Socket> socket(new Socket(profile, extension_id, src_url));
  CHECK(socket.get());
  socket_map_[next_socket_id_] = socket;
  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.
//
// static
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);
}

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