/* 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 "nsISupports.idl"
/**
 * Some utility methods used by the url classifier.
 */

interface nsIURI;
interface nsIChannel;

/**
 * Interface for parseFindFullHashResponseV4 callback
 */
[scriptable, uuid(fbb9684a-a0aa-11e6-88b0-08606e456b8a)]
interface nsIUrlClassifierParseFindFullHashCallback : nsISupports {
  /**
   * Callback when a match is found in full hash response. This callback may be
   * called multiple times when there are more than one matches in response.
   *
   * @param aCompleteHash A 32-byte complete hash string.
   * @param aTableNames The table names that this complete hash is associated with.
   *                    Since the server responded with a threat type, multiple
   *                    list names can be returned. The caller is reponsible
   *                    for filtering out the unrequested table names.
   *                    See |convertThreatTypeToListNames| for the format.
   * @param aPerHashCacheDuration See "FindFullHashesResponse" in safebrowsing.proto.
   *
   */
  void onCompleteHashFound(in ACString aCompleteHash,
                           in ACString aTableNames,
                           in unsigned long aPerHashCacheDuration);

  /**
   * Callback when full hash response is received.
   *
   * @param aMinWaitDuration See "FindFullHashesResponse" in safebrowsing.proto.
   * @param aNegCacheDuration See "FindFullHashesResponse" in safebrowsing.proto.
   *
   */
  void onResponseParsed(in unsigned long aMinWaitDuration,
                        in unsigned long aNegCacheDuration);
};

[scriptable, uuid(e4f0e59c-b922-48b0-a7b6-1735c1f96fed)]
interface nsIUrlClassifierUtils : nsISupports
{
  /**
   * Get the lookup string for a given URI.  This normalizes the hostname,
   * url-decodes the string, and strips off the protocol.
   *
   * @param uri URI to get the lookup key for.
   *
   * @returns String containing the canonicalized URI.
   */
  ACString getKeyForURI(in nsIURI uri);

  /**
   * Get the provider by table name.
   *
   * @param tableName The table name that we want to lookup
   *
   * @returns the provider name that the given table belongs.
   */
  ACString getProvider(in ACString tableName);

  /**
   * Get the provider used for Telemetry.
   * Because recording Telemetry will leak user-controlled strings,
   * only built-in providers should be recorded.
   *
   * @param tableName The table name that we want to lookup
   *
   * @returns the filtered provider for telemetry.
   *
   */
  ACString getTelemetryProvider(in ACString tableName);

  /**
   * Get the protocol version for the given provider.
   *
   * @param provider String the provider name. e.g. "google"
   *
   * @returns String to indicate the protocol version. e.g. "2.2"
   */
  ACString getProtocolVersion(in ACString provider);

  /**
   * Convert threat type to list name.
   *
   * @param Integer to indicate threat type.
   *
   * @returns The list names separated by ','. For example,
   *          'goog-phish-proto,test-phish-proto'.
   */
  ACString convertThreatTypeToListNames(in uint32_t threatType);

  /**
   * Convert list name to threat type.
   *
   * @param The list name.
   *
   * @returns The threat type in integer.
   */
  uint32_t convertListNameToThreatType(in ACString listName);


  /**
   * Convert list name of the V5 server to the list name of the local database.
   *
   * @param The list name of the V5 server.
   *
   * @returns The list name of the local database.
   */
  ACString convertServerListNameToLocalListNameV5(in ACString serverListName);

  /**
   * Convert list name of the local database to the list name of the V5 server.
   *
   * @param The list name of the local database.
   *
   * @returns The list name of the V5 server.
   */
  ACString convertLocalListNameToServerListNameV5(in ACString localListName);

  /**
   * Convert V5 threat type to list name.
   *
   * @param Integer to indicate threat type.
   *
   * @returns The list names separated by ','. For example,
   *          'goog-phish-proto,test-phish-proto'.
   */
  ACString convertThreatTypeToListNamesV5(in uint32_t threatType);

  /**
   * Make update request for given lists and their states.
   *
   * @param aListNames An array of list name represented in string.
   * @param aState An array of states (encoded in base64 format) for each list.
   *
   * The two argument arrays must be the same length.
   *
   * @returns A base64url encoded string.
   */
  ACString makeUpdateRequestV4(in Array<ACString> aListNames,
                               in Array<ACString> aStatesBase64);

  /**
   * Make update request of V5 protocol for given lists and their states.
   *
   * @param aListNames An array of list name represented in string.
   * @param aState An array of states (encoded in base64 format) for each list.
   *
   * The two argument arrays must be the same length.
   *
   * @returns A base64url encoded string.
   */
  ACString makeUpdateRequestV5(in Array<ACString> aListNames,
                               in Array<ACString> aStatesBase64);

    /**
   * Make "find full hash" request by for the given prefixes.
   *
   * @param aListNames An array of list names represented in string.
   * @param aListStatesBase64 An array of list states represented in base64.
   * @param aPrefixes An array of prefixes for which we'd like to find full hashes.
   *
   * The aListNames and aListStatesBase64 arrays must be the same length.
   *
   * @returns A base64url encoded string.
   */
  ACString makeFindFullHashRequestV4(in Array<ACString> aListNames,
                                     in Array<ACString> aListStatesBase64,
                                     in Array<ACString> aPrefixes);

  /**
   * Make "hashes:search" request by for the given prefixes.
   *
   * @param aHashPrefixes An array of prefixes for which we'd like to find full
   *                      hashes.
   *
   * @returns A query parameter string for the "hashes:search" request.
   */
  ACString makeFindFullHashRequestV5(in Array<ACString> aHashPrefixes);

  /**
   * Make ThreatHit report request body.
   *
   * @param aChannel channel which encountered the threat.
   * @param aListName listname represented in string.
   * @param aHashBase64 hash-based hit represented in base64.
   *
   * @returns A base64 encoded string.
   */
  ACString makeThreatHitReport(in nsIChannel aChannel,
                               in ACString aListName,
                               in ACString aHashBase64);

  /**
   * Parse V4 FindFullHash response.
   *
   * @param aResponse Byte stream from the server.
   * @param aCallback The callback function on each complete hash parsed.
   *                  Can be called multiple times in one parsing.
   */
  void parseFindFullHashResponseV4(in ACString aResponse,
                                   in nsIUrlClassifierParseFindFullHashCallback aCallback);

  /**
   * Parse FindFullHash response for the V5 protocol. In V5, the FindFullHash
   * is implemented as the hashes::search API.
   *
   * @param aResponse Byte stream from the server.
   * @param aCallback The callback function on each complete hash parsed.
   *                  Can be called multiple times in one parsing.
   */
  void parseFindFullHashResponseV5(in ACString aResponse,
                                   in nsIUrlClassifierParseFindFullHashCallback aCallback);
};
