/* 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/. */

#ifndef mozilla_net_DashboardTypes_h_
#define mozilla_net_DashboardTypes_h_

#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "nsHttp.h"
#include "nsString.h"
#include "nsTArray.h"

namespace mozilla {
namespace net {

struct SocketInfo {
  nsCString host;
  uint64_t sent;
  uint64_t received;
  uint16_t port;
  bool active;
  nsCString type;
  nsCString originAttributesSuffix;
};

inline bool operator==(const SocketInfo& a, const SocketInfo& b) {
  return a.host == b.host && a.sent == b.sent && a.received == b.received &&
         a.port == b.port && a.active == b.active && a.type == b.type &&
         a.originAttributesSuffix == b.originAttributesSuffix;
}

struct DnsAndConnectSockets {
  bool speculative;
};

struct DNSCacheEntries {
  nsCString hostname;
  nsTArray<nsCString> hostaddr;
  uint16_t family{0};
  int64_t expiration{0};
  bool TRR{false};
  nsCString originAttributesSuffix;
  nsCString flags;
  uint16_t resolveType{0};
};

struct HttpConnInfo {
  uint32_t ttl;
  uint32_t rtt;
  nsString protocolVersion;

  void SetHTTPProtocolVersion(HttpVersion pv);
};

struct HttpRetParams {
  nsCString host;
  CopyableTArray<HttpConnInfo> active;
  CopyableTArray<HttpConnInfo> idle;
  CopyableTArray<DnsAndConnectSockets> dnsAndSocks;
  uint32_t counter;
  uint16_t port;
  nsCString httpVersion;
  bool ssl;
  nsCString originAttributesSuffix;
};

struct Http3ConnStats {
  // Total packets received, including all the bad ones.
  uint64_t packetsRx;
  // Duplicate packets received.
  uint64_t dupsRx;
  // Dropped packets or dropped garbage.
  uint64_t droppedRx;
  // The number of packet that were saved for later processing.
  uint64_t savedDatagrams;
  // Total packets sent.
  uint64_t packetsTx;
  // Total number of packets that are declared lost.
  uint64_t lost;
  // Late acknowledgments, for packets that were declared lost already.
  uint64_t lateAck;
  // Acknowledgments for packets that contained data that was marked
  // for retransmission when the PTO timer popped.
  uint64_t ptoAck;
  // Count PTOs. Single PTOs, 2 PTOs in a row, 3 PTOs in row, etc. are counted
  // separately.
  CopyableTArray<uint64_t> ptoCounts;
  // The count of WouldBlock errors encountered during receive operations.
  uint64_t wouldBlockRx;
  // The count of WouldBlock errors encountered during transmit operations.
  uint64_t wouldBlockTx;
};

struct Http3ConnectionStatsParams {
  nsCString host;
  uint16_t port;
  CopyableTArray<Http3ConnStats> stats;
};

}  // namespace net
}  // namespace mozilla

namespace IPC {

DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::net::SocketInfo, host, sent,
                                  received, port, active, type,
                                  originAttributesSuffix);

DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::net::DNSCacheEntries, hostname,
                                  hostaddr, family, expiration, TRR,
                                  originAttributesSuffix, flags, resolveType);

DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::net::DnsAndConnectSockets,
                                  speculative);

DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::net::HttpConnInfo, ttl, rtt,
                                  protocolVersion);

DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::net::HttpRetParams, host, active,
                                  idle, dnsAndSocks, counter, port, httpVersion,
                                  ssl, originAttributesSuffix);

DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::net::Http3ConnStats, packetsRx,
                                  dupsRx, droppedRx, savedDatagrams, packetsTx,
                                  lost, lateAck, ptoAck, ptoCounts,
                                  wouldBlockRx, wouldBlockTx);

DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::net::Http3ConnectionStatsParams,
                                  host, port, stats);

}  // namespace IPC

#endif  // mozilla_net_DashboardTypes_h_
