Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / socket / udp_socket.h
blobd43bc27b76e04296a7099b1d9d8ddd58e2c49944
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 #ifndef EXTENSIONS_BROWSER_API_SOCKET_UDP_SOCKET_H_
6 #define EXTENSIONS_BROWSER_API_SOCKET_UDP_SOCKET_H_
8 #include <string>
9 #include <vector>
11 #include "extensions/browser/api/socket/socket.h"
12 #include "net/udp/udp_socket.h"
14 namespace extensions {
16 class UDPSocket : public Socket {
17 public:
18 explicit UDPSocket(const std::string& owner_extension_id);
19 ~UDPSocket() override;
21 void Connect(const net::AddressList& address,
22 const CompletionCallback& callback) override;
23 void Disconnect() override;
24 int Bind(const std::string& address, uint16 port) override;
25 void Read(int count, const ReadCompletionCallback& callback) override;
26 void RecvFrom(int count, const RecvFromCompletionCallback& callback) override;
27 void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
28 int byte_count,
29 const net::IPEndPoint& address,
30 const CompletionCallback& callback) override;
32 bool IsConnected() override;
34 bool GetPeerAddress(net::IPEndPoint* address) override;
35 bool GetLocalAddress(net::IPEndPoint* address) override;
36 Socket::SocketType GetSocketType() const override;
38 bool IsBound();
40 int JoinGroup(const std::string& address);
41 int LeaveGroup(const std::string& address);
43 int SetMulticastTimeToLive(int ttl);
44 int SetMulticastLoopbackMode(bool loopback);
46 int SetBroadcast(bool enabled);
48 const std::vector<std::string>& GetJoinedGroups() const;
50 protected:
51 int WriteImpl(net::IOBuffer* io_buffer,
52 int io_buffer_size,
53 const net::CompletionCallback& callback) override;
55 private:
56 // Make net::IPEndPoint can be refcounted
57 typedef base::RefCountedData<net::IPEndPoint> IPEndPoint;
59 void OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, int result);
60 void OnRecvFromComplete(scoped_refptr<net::IOBuffer> io_buffer,
61 scoped_refptr<IPEndPoint> address,
62 int result);
63 void OnSendToComplete(int result);
65 net::UDPSocket socket_;
67 ReadCompletionCallback read_callback_;
69 RecvFromCompletionCallback recv_from_callback_;
71 CompletionCallback send_to_callback_;
73 std::vector<std::string> multicast_groups_;
76 // UDP Socket instances from the "sockets.udp" namespace. These are regular
77 // socket objects with additional properties related to the behavior defined in
78 // the "sockets.udp" namespace.
79 class ResumableUDPSocket : public UDPSocket {
80 public:
81 explicit ResumableUDPSocket(const std::string& owner_extension_id);
83 // Overriden from ApiResource
84 bool IsPersistent() const override;
86 const std::string& name() const { return name_; }
87 void set_name(const std::string& name) { name_ = name; }
89 bool persistent() const { return persistent_; }
90 void set_persistent(bool persistent) { persistent_ = persistent; }
92 int buffer_size() const { return buffer_size_; }
93 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; }
95 bool paused() const { return paused_; }
96 void set_paused(bool paused) { paused_ = paused; }
98 private:
99 friend class ApiResourceManager<ResumableUDPSocket>;
100 static const char* service_name() { return "ResumableUDPSocketManager"; }
102 // Application-defined string - see sockets_udp.idl.
103 std::string name_;
104 // Flag indicating whether the socket is left open when the application is
105 // suspended - see sockets_udp.idl.
106 bool persistent_;
107 // The size of the buffer used to receive data - see sockets_udp.idl.
108 int buffer_size_;
109 // Flag indicating whether a connected socket blocks its peer from sending
110 // more data - see sockets_udp.idl.
111 bool paused_;
114 } // namespace extensions
116 #endif // EXTENSIONS_BROWSER_API_SOCKET_UDP_SOCKET_H_