Home | History | Annotate | Download | only in api
      1 // Copyright 2013 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 // Use the <code>chrome.sockets.udp</code> API to send and receive data over the
      6 // network using UDP connections. This API supersedes the UDP functionality
      7 // previously found in the "socket" API. Note that the socket ids created from
      8 // this namespace are not compatible with ids created in other namespaces.
      9 namespace sockets.udp {
     10   // The socket properties specified in the <code>create</code> or
     11   // <code>update</code> function. Each property is optional. If a property
     12   // value is not specified, a default value is used when calling
     13   // <code>create</code>, or the existing value if preserved when calling
     14   // <code>update</code>.
     15   dictionary SocketProperties {
     16     // Flag indicating if the socket is left open when the event page of the
     17     // application is unloaded (see
     18     // <a href="http://developer.chrome.com/apps/app_lifecycle.html">Manage App
     19     // Lifecycle</a>). The default value is "false." When the application is
     20     // loaded, any sockets previously opened with persistent=true can be fetched
     21     // with <code>getSockets</code>.
     22     boolean? persistent;
     23 
     24     // An application-defined string associated with the socket.
     25     DOMString? name;
     26 
     27     // The size of the buffer used to receive data. If the buffer is too small
     28     // to receive the UDP packet, data is lost. The default value is 4096.
     29     long? bufferSize;
     30   };
     31 
     32   // Result of <code>create</code> call.
     33   dictionary CreateInfo {
     34     // The ID of the newly created socket.
     35     long socketId;
     36   };
     37 
     38   // Callback from the <code>create</code> method.
     39   // |createInfo| : The result of the socket creation.
     40   callback CreateCallback = void (CreateInfo createInfo);
     41 
     42   // Callback from the <code>bind</code> method.
     43   // |result| : The result code returned from the underlying network call.
     44   // A negative value indicates an error.
     45   callback BindCallback = void (long result);
     46 
     47   // Result of the <code>send</code> method.
     48   dictionary SendInfo {
     49     // The result code returned from the underlying network call.
     50     // A negative value indicates an error.
     51     long resultCode;
     52 
     53     // The number of bytes sent (if result == 0)
     54     long? bytesSent;
     55   };
     56 
     57   // Callback from the <code>send</code> method.
     58   // |sendInfo| : Result of the <code>send</code> method.
     59   callback SendCallback = void (SendInfo sendInfo);
     60 
     61   // Callback from the <code>close<code> method.
     62   callback CloseCallback = void ();
     63 
     64   // Callback from the <code>update</code> method.
     65   callback UpdateCallback = void ();
     66 
     67   // Callback from the <code>setPaused<code> method.
     68   callback SetPausedCallback = void ();
     69 
     70   // Result of the <code>getInfo</code> method.
     71   dictionary SocketInfo {
     72     // The socket identifier.
     73     long socketId;
     74 
     75     // Flag indicating whether the socket is left open when the application is
     76     // suspended (see <code>SocketProperties.persistent</code>).
     77     boolean persistent;
     78 
     79     // Application-defined string associated with the socket.
     80     DOMString? name;
     81 
     82     // The size of the buffer used to receive data. If no buffer size has been
     83     // specified explictly, the value is not provided.
     84     long? bufferSize;
     85 
     86     // Flag indicating whether the socket is blocked from firing onReceive
     87     // events.
     88     boolean paused;
     89 
     90     // If the underlying socket is bound, contains its local
     91     // IPv4/6 address.
     92     DOMString? localAddress;
     93 
     94     // If the underlying socket is bound, contains its local port.
     95     long? localPort;
     96   };
     97 
     98   // Callback from the <code>getInfo</code> method.
     99   // |socketInfo| : Object containing the socket information.
    100   callback GetInfoCallback = void (SocketInfo socketInfo);
    101 
    102   // Callback from the <code>getSockets</code> method.
    103   // |socketInfos| : Array of object containing socket information.
    104   callback GetSocketsCallback = void (SocketInfo[] socketInfos);
    105 
    106   // Callback from the <code>joinGroup</code> method.
    107   // |result| : The result code returned from the underlying network call.
    108   // A negative value indicates an error.
    109   callback JoinGroupCallback = void (long result);
    110 
    111   // Callback from the <code>leaveGroup</code> method.
    112   // |result| : The result code returned from the underlying network call.
    113   // A negative value indicates an error.
    114   callback LeaveGroupCallback = void (long result);
    115 
    116   // Callback from the <code>setMulticastTimeToLive</code> method.
    117   // |result| : The result code returned from the underlying network call.
    118   // A negative value indicates an error.
    119   callback SetMulticastTimeToLiveCallback = void (long result);
    120 
    121   // Callback from the <code>setMulticastLoopbackMode</code> method.
    122   // |result| : The result code returned from the underlying network call.
    123   // A negative value indicates an error.
    124   callback SetMulticastLoopbackModeCallback = void (long result);
    125 
    126   // Callback from the <code>getJoinedGroupsCallback</code> method.
    127   // |groups| : Array of groups the socket joined.
    128   callback GetJoinedGroupsCallback = void (DOMString[] groups);
    129 
    130   // Data from an <code>onReceive</code> event.
    131   dictionary ReceiveInfo {
    132     // The socket ID.
    133     long socketId;
    134 
    135     // The UDP packet content (truncated to the current buffer size).
    136     ArrayBuffer data;
    137 
    138     // The address of the host the packet comes from.
    139     DOMString remoteAddress;
    140 
    141      // The port of the host the packet comes from.
    142     long remotePort;
    143   };
    144 
    145   // Data from an <code>onReceiveError</code> event.
    146   dictionary ReceiveErrorInfo {
    147     // The socket ID.
    148     long socketId;
    149 
    150      // The result code returned from the underlying recvfrom() call.
    151     long resultCode;
    152   };
    153 
    154   interface Functions {
    155     // Creates a UDP socket with the given properties.
    156     // |properties| : The socket properties (optional).
    157     // |callback| : Called when the socket has been created.
    158     static void create(optional SocketProperties properties,
    159                        CreateCallback callback);
    160 
    161     // Updates the socket properties.
    162     // |socketId| : The socket ID.
    163     // |properties| : The properties to update.
    164     // |callback| : Called when the properties are updated.
    165     static void update(long socketId,
    166                        SocketProperties properties,
    167                        optional UpdateCallback callback);
    168 
    169     // Pauses or unpauses a socket. A paused socket is blocked from firing
    170     // <code>onReceive</code> events.
    171     // |connectionId| : The socket ID.
    172     // |paused| : Flag to indicate whether to pause or unpause.
    173     // |callback| : Called when the socket has been successfully paused or
    174     // unpaused.
    175     static void setPaused(long socketId,
    176                           boolean paused,
    177                           optional SetPausedCallback callback);
    178 
    179     // Binds the local address and port for the socket. For a client socket, it
    180     // is recommended to use port 0 to let the platform pick a free port.
    181     //
    182     // Once the <code>bind</code> operation completes successfully,
    183     // <code>onReceive</code> events are raised when UDP packets arrive on the
    184     // address/port specified -- unless the socket is paused.
    185     //
    186     // |socketId| : The socket ID.
    187     // |address| : The address of the local machine. DNS name, IPv4 and IPv6
    188     // formats are supported. Use "0.0.0.0" to accept packets from all local
    189     // available network interfaces.
    190     // |port| : The port of the local machine. Use "0" to bind to a free port.
    191     // |callback| : Called when the <code>bind</code> operation completes.
    192     static void bind(long socketId,
    193                      DOMString address,
    194                      long port,
    195                      BindCallback callback);
    196 
    197     // Sends data on the given socket to the given address and port. The socket
    198     // must be bound to a local port before calling this method.
    199     // |socketId| : The socket ID.
    200     // |data| : The data to send.
    201     // |address| : The address of the remote machine.
    202     // |port| : The port of the remote machine.
    203     // |callback| : Called when the <code>send</code> operation completes.
    204     static void send(long socketId,
    205                      ArrayBuffer data,
    206                      DOMString address,
    207                      long port,
    208                      SendCallback callback);
    209 
    210     // Closes the socket and releases the address/port the socket is bound to.
    211     // Each socket created should be closed after use. The socket id is no
    212     // longer valid as soon at the function is called. However, the socket is
    213     // guaranteed to be closed only when the callback is invoked.
    214     // |socketId| : The socket ID.
    215     // |callback| : Called when the <code>close</code> operation completes.
    216     static void close(long socketId,
    217                       optional CloseCallback callback);
    218 
    219     // Retrieves the state of the given socket.
    220     // |socketId| : The socket ID.
    221     // |callback| : Called when the socket state is available.
    222     static void getInfo(long socketId,
    223                         GetInfoCallback callback);
    224 
    225     // Retrieves the list of currently opened sockets owned by the application.
    226     // |callback| : Called when the list of sockets is available.
    227     static void getSockets(GetSocketsCallback callback);
    228 
    229     // Joins the multicast group and starts to receive packets from that group.
    230     // The socket must be bound to a local port before calling this method.
    231     // |socketId| : The socket ID.
    232     // |address| : The group address to join. Domain names are not supported.
    233     // |callback| : Called when the <code>joinGroup</code> operation completes.
    234     static void joinGroup(long socketId,
    235                           DOMString address,
    236                           JoinGroupCallback callback);
    237 
    238     // Leaves the multicast group previously joined using
    239     // <code>joinGroup</code>. This is only necessary to call if you plan to
    240     // keep using the socketafterwards, since it will be done automatically by
    241     // the OS when the socket is closed.
    242     //
    243     // Leaving the group will prevent the router from sending multicast
    244     // datagrams to the local host, presuming no other process on the host is
    245     // still joined to the group.
    246     //
    247     // |socketId| : The socket ID.
    248     // |address| : The group address to leave. Domain names are not supported.
    249     // |callback| : Called when the <code>leaveGroup</code> operation completes.
    250     static void leaveGroup(long socketId,
    251                            DOMString address,
    252                            LeaveGroupCallback callback);
    253 
    254     // Sets the time-to-live of multicast packets sent to the multicast group.
    255     //
    256     // Calling this method does not require multicast permissions.
    257     //
    258     // |socketId| : The socket ID.
    259     // |ttl| : The time-to-live value.
    260     // |callback| : Called when the configuration operation completes.
    261     static void setMulticastTimeToLive(
    262         long socketId,
    263         long ttl,
    264         SetMulticastTimeToLiveCallback callback);
    265 
    266     // Sets whether multicast packets sent from the host to the multicast group
    267     // will be looped back to the host.
    268     //
    269     // Note: the behavior of <code>setMulticastLoopbackMode</code> is slightly
    270     // different between Windows and Unix-like systems. The inconsistency
    271     // happens only when there is more than one application on the same host
    272     // joined to the same multicast group while having different settings on
    273     // multicast loopback mode. On Windows, the applications with loopback off
    274     // will not RECEIVE the loopback packets; while on Unix-like systems, the
    275     // applications with loopback off will not SEND the loopback packets to
    276     // other applications on the same host. See MSDN: http://goo.gl/6vqbj
    277     //
    278     // Calling this method does not require multicast permissions.
    279     //
    280     // |socketId| : The socket ID.
    281     // |enabled| : Indicate whether to enable loopback mode.
    282     // |callback| : Called when the configuration operation completes.
    283     static void setMulticastLoopbackMode(
    284         long socketId,
    285         boolean enabled,
    286         SetMulticastLoopbackModeCallback callback);
    287 
    288     // Gets the multicast group addresses the socket is currently joined to.
    289     // |socketId| : The socket ID.
    290     // |callback| : Called with an array of strings of the result.
    291     static void getJoinedGroups(long socketId,
    292                                 GetJoinedGroupsCallback callback);
    293   };
    294 
    295   interface Events {
    296     // Event raised when a UDP packet has been received for the given socket.
    297     // |info| : The event data.
    298     static void onReceive(ReceiveInfo info);
    299 
    300     // Event raised when a network error occured while the runtime was waiting
    301     // for data on the socket address and port. Once this event is raised, the
    302     // socket is paused and no more <code>onReceive</code> events will be raised
    303     // for this socket until the socket is resumed.
    304     // |info| : The event data.
    305     static void onReceiveError(ReceiveErrorInfo info);
    306   };
    307 };
    308