Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / socket / socket.cc
blobed41ce047925161c22c68b462e6d022f9ab2b8ea
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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/socket/socket.h"
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "extensions/browser/api/api_resource_manager.h"
10 #include "net/base/address_list.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/net_errors.h"
14 #include "net/socket/socket.h"
16 namespace extensions {
18 const char kSocketTypeNotSupported[] = "Socket type does not support this API";
20 static base::LazyInstance<
21 BrowserContextKeyedAPIFactory<ApiResourceManager<Socket> > > g_factory =
22 LAZY_INSTANCE_INITIALIZER;
24 // static
25 template <>
26 BrowserContextKeyedAPIFactory<ApiResourceManager<Socket> >*
27 ApiResourceManager<Socket>::GetFactoryInstance() {
28 return g_factory.Pointer();
31 Socket::Socket(const std::string& owner_extension_id)
32 : ApiResource(owner_extension_id), is_connected_(false) {}
34 Socket::~Socket() {
35 // Derived destructors should make sure the socket has been closed.
36 DCHECK(!is_connected_);
39 void Socket::Write(scoped_refptr<net::IOBuffer> io_buffer,
40 int byte_count,
41 const CompletionCallback& callback) {
42 DCHECK(!callback.is_null());
43 write_queue_.push(WriteRequest(io_buffer, byte_count, callback));
44 WriteData();
47 void Socket::WriteData() {
48 // IO is pending.
49 if (io_buffer_write_.get())
50 return;
52 WriteRequest& request = write_queue_.front();
54 DCHECK(request.byte_count >= request.bytes_written);
55 io_buffer_write_ = new net::WrappedIOBuffer(request.io_buffer->data() +
56 request.bytes_written);
57 int result =
58 WriteImpl(io_buffer_write_.get(),
59 request.byte_count - request.bytes_written,
60 base::Bind(&Socket::OnWriteComplete, base::Unretained(this)));
62 if (result != net::ERR_IO_PENDING)
63 OnWriteComplete(result);
66 void Socket::OnWriteComplete(int result) {
67 io_buffer_write_ = NULL;
69 WriteRequest& request = write_queue_.front();
71 if (result >= 0) {
72 request.bytes_written += result;
73 if (request.bytes_written < request.byte_count) {
74 WriteData();
75 return;
77 DCHECK(request.bytes_written == request.byte_count);
78 result = request.bytes_written;
81 request.callback.Run(result);
82 write_queue_.pop();
84 if (!write_queue_.empty())
85 WriteData();
88 bool Socket::SetKeepAlive(bool enable, int delay) { return false; }
90 bool Socket::SetNoDelay(bool no_delay) { return false; }
92 int Socket::Listen(const std::string& address,
93 uint16 port,
94 int backlog,
95 std::string* error_msg) {
96 *error_msg = kSocketTypeNotSupported;
97 return net::ERR_FAILED;
100 void Socket::Accept(const AcceptCompletionCallback& callback) {
101 callback.Run(net::ERR_FAILED, NULL);
104 // static
105 bool Socket::StringAndPortToIPEndPoint(const std::string& ip_address_str,
106 uint16 port,
107 net::IPEndPoint* ip_end_point) {
108 DCHECK(ip_end_point);
109 net::IPAddressNumber ip_number;
110 if (!net::ParseIPLiteralToNumber(ip_address_str, &ip_number))
111 return false;
113 *ip_end_point = net::IPEndPoint(ip_number, port);
114 return true;
117 void Socket::IPEndPointToStringAndPort(const net::IPEndPoint& address,
118 std::string* ip_address_str,
119 uint16* port) {
120 DCHECK(ip_address_str);
121 DCHECK(port);
122 *ip_address_str = address.ToStringWithoutPort();
123 if (ip_address_str->empty()) {
124 *port = 0;
125 } else {
126 *port = address.port();
130 Socket::WriteRequest::WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
131 int byte_count,
132 const CompletionCallback& callback)
133 : io_buffer(io_buffer),
134 byte_count(byte_count),
135 callback(callback),
136 bytes_written(0) {}
138 Socket::WriteRequest::~WriteRequest() {}
140 } // namespace extensions