/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "domstubs.idl"

interface nsIDOMTCPSocket;
interface nsIDOMTCPServerSocket;
interface nsITCPServerSocketParent;
interface nsITCPSocketIntermediary;

// Interface required to allow the TCP socket object (TCPSocket.js) in the
// parent process to talk to the parent IPC actor, TCPSocketParent, which
// is written in C++.
[scriptable, uuid(868662a4-681c-4b89-9f02-6fe5b7ace265)]
interface nsITCPSocketParent : nsISupports
{
  [implicit_jscontext] void initJS(in jsval intermediary);

  // Trigger a callback in the content process for |type|, providing a serialized
  // argument of |data|, and update the child's readyState value with the given
  // values.
  //
  // @param type
  //        Event type: 'onopen', 'ondata', 'onerror' or 'onclose'. 'odrain' is
  //        controlled by child.
  // @param data
  //        Serialized data that is passed to event handler.
  // @param readyState
  //        Current ready state.
  [implicit_jscontext] void sendEvent(in DOMString type,
                                      in jsval data,
                                      in DOMString readyState);

  // Initialize a parent socket object. It is called from the parent side socket,
  // which is generated in accepting any open request on the parent side.
  // The socket after being initialized will be established.
  //
  // @param socket
  //        The socket on the parent side.
  // @param intermediary
  //        Intermediate class object. See nsITCPSocketIntermediary.
  [implicit_jscontext] void setSocketAndIntermediary(in nsIDOMTCPSocket socket,
                                                     in nsITCPSocketIntermediary intermediary);

  // When parent's buffered amount is updated and it wants to inform child to
  // update the bufferedAmount as well.
  //
  // @param bufferedAmount
  //        The new value of bufferedAmount that is going to be set to child's
  //        bufferedAmount.
  // @param trackingNumber
  //        Parent's current tracking number, reflecting the number of calls to
  //        send() on the child process. This number is sent back to the child
  //        to make sure the bufferedAmount updated on the child will correspond
  //        to the latest call of send().
  void sendUpdateBufferedAmount(in uint32_t bufferedAmount, in uint32_t trackingNumber);
};

// Intermediate class to handle sending multiple possible data types
// and kicking off the chrome process socket object's connection.
// This interface is the bridge of TCPSocketParent, which is written in C++,
// and TCPSocket, which is written in Javascript. TCPSocketParentIntermediary
// implements nsITCPSocketIntermediary in Javascript.
[scriptable, uuid(c434224a-dbb7-4869-8b2b-e49cee990e85)]
interface nsITCPSocketIntermediary : nsISupports {
  // Open the connection to the server with the given parameters
  nsIDOMTCPSocket open(in nsITCPSocketParent parent,
                       in DOMString host, in unsigned short port,
                       in boolean useSSL, in DOMString binaryType,
                       in unsigned long appId);

  // Listen on a port
  nsIDOMTCPServerSocket listen(in nsITCPServerSocketParent parent,
                               in unsigned short port, in unsigned short backlog,
                               in DOMString binaryType);

  // Called when received a child request to send a string.
  void onRecvSendString(in DOMString data, in uint32_t trackingNumber);

  // Called when received a child request to send an array buffer.
  void onRecvSendArrayBuffer(in jsval data, in uint32_t trackingNumber);
};
