Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / socket / udp_socket.h
blob7a51131ddb8cf3fe6ac00ada847f5dfd1d5fff0f
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 std::string& address,
22 int port,
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,
29 int byte_count,
30 const std::string& address,
31 int port,
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;
40 bool IsBound();
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;
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_