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