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_
11 #include "extensions/browser/api/socket/socket.h"
12 #include "net/udp/udp_socket.h"
14 namespace extensions
{
16 class UDPSocket
: public Socket
{
18 explicit UDPSocket(const std::string
& owner_extension_id
);
19 ~UDPSocket() override
;
21 void Connect(const std::string
& address
,
23 const CompletionCallback
& callback
) override
;
24 void Disconnect() override
;
25 int Bind(const std::string
& address
, int port
) override
;
26 void Read(int count
, const ReadCompletionCallback
& callback
) override
;
27 void RecvFrom(int count
, const RecvFromCompletionCallback
& callback
) override
;
28 void SendTo(scoped_refptr
<net::IOBuffer
> io_buffer
,
30 const std::string
& address
,
32 const CompletionCallback
& callback
) override
;
34 bool IsConnected() override
;
36 bool GetPeerAddress(net::IPEndPoint
* address
) override
;
37 bool GetLocalAddress(net::IPEndPoint
* address
) override
;
38 Socket::SocketType
GetSocketType() const override
;
42 int JoinGroup(const std::string
& address
);
43 int LeaveGroup(const std::string
& address
);
45 int SetMulticastTimeToLive(int ttl
);
46 int SetMulticastLoopbackMode(bool loopback
);
48 const std::vector
<std::string
>& GetJoinedGroups() const;
51 int WriteImpl(net::IOBuffer
* io_buffer
,
53 const net::CompletionCallback
& callback
) override
;
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
,
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
{
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
; }
99 friend class ApiResourceManager
<ResumableUDPSocket
>;
100 static const char* service_name() { return "ResumableUDPSocketManager"; }
102 // Application-defined string - see sockets_udp.idl.
104 // Flag indicating whether the socket is left open when the application is
105 // suspended - see sockets_udp.idl.
107 // The size of the buffer used to receive data - see sockets_udp.idl.
109 // Flag indicating whether a connected socket blocks its peer from sending
110 // more data - see sockets_udp.idl.
114 } // namespace extensions
116 #endif // EXTENSIONS_BROWSER_API_SOCKET_UDP_SOCKET_H_