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