/* 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"
#include "nsIObserver.idl"

interface imgIRequest;
interface imgIContainer;
interface nsICancelable;
interface nsIPrincipal;
interface nsIURI;

%{C++
#define ALERT_NOTIFICATION_CONTRACTID "@mozilla.org/alert-notification;1"

#include "nsString.h"
static constexpr nsLiteralString kAlertActionDisable = u"snooze"_ns;
static constexpr nsLiteralString kAlertActionSettings = u"settings"_ns;
%}


[scriptable, uuid(a054c2c9-2787-4686-859c-45609d790056)]
interface nsIAlertAction : nsISupports
{
  /**
   * Returns a string identifying a user action to be displayed on the alert.
   *
   * This string is an opaque identifier that identifies an action in potential
   * callbacks; it is not displayed to the user.
   */
  readonly attribute AString action;

  /**
   * Returns a string containing action text to be shown to the user.
   */
  readonly attribute AString title;

  /**
   * Returns a string containing the URL of an icon to display with the action.
   */
  readonly attribute AString iconURL;

  /**
   * On Windows, chrome-privileged notifications -- i.e., those with a
   * non-actionable principal -- can have actions that are activated by Windows
   * and not processed by Firefox.  When `windowsSystemActivationType` is true,
   * we request Windows to process `action`.  At the time of writing, Windows
   * recognizes the following actions:
   *
   * - `action="dismiss"` dismisses the alert entirely.
   * - `action="snooze"` snoozes the alert, generally making it disappear before
   *   reappearing a Windows-determined amount of time later.
   *
   * On non-Windows, this field is ignored.
   */
  readonly attribute boolean windowsSystemActivationType;

  /**
   * On Windows, chrome-privileged notifications -- i.e., those with a
   * non-actionable principal -- can have action-specific `opaqueRelaunchData`.
   * This data will be provided to the application at relaunch and can trigger
   * specific actions.
   *
   * On non-Windows, this field is ignored.
   */
  readonly attribute AString opaqueRelaunchData;
};

[scriptable, uuid(cf2e4cb6-4b8f-4eca-aea9-d51a8f9f7a50)]
interface nsIAlertNotification : nsISupports
{
  /** Initializes an alert notification. */
  void init([optional] in AString aName,
            [optional] in AString aImageURL,
            [optional] in AString aTitle,
            [optional] in AString aText,
            [optional] in boolean aTextClickable,
            [optional] in AString aCookie,
            [optional] in AString aDir,
            [optional] in AString aLang,
            [optional] in AString aData,
            [optional] in nsIPrincipal aPrincipal,
            [optional] in boolean aInPrivateBrowsing,
            [optional] in boolean aRequireInteraction,
            [optional] in boolean aSilent,
            [optional] in Array<uint32_t> aVibrate);

  /**
   * Initializes an alert notification with another instance.
   * Can be useful in JS as it can provide a dictionary object.
   */
  void initWithObject(in nsIAlertNotification aAlertNotification);

  /**
   * The unique ID of the notification, based on the profile path and the caller
   * given name.
   *
   * Throws if init() is not called yet.
   */
  readonly attribute AString id;

  /**
   * The name of the notification given by the caller.
   * Notifications will replace previous notifications with the same name.
   */
  readonly attribute AString name;

  /**
   * A URL identifying the image to put in the alert. The OS X backend limits
   * the amount of time it will wait for the image to load to six seconds. After
   * that time, the alert will show without an image.
   */
  readonly attribute AString imageURL;

  /**
   * The actual image container to use for the alert. This should be what imageURL returns.
   */
  attribute imgIContainer image;

  /** The title for the alert. */
  readonly attribute AString title;

  /** The contents of the alert. */
  readonly attribute AString text;

  /**
   * Controls the click behavior. If true, the alert listener will be notified
   * when the user clicks on the alert.
   */
  readonly attribute boolean textClickable;

  /**
   * An opaque cookie that will be passed to the alert listener for each
   * callback.
   * TODO bug 1932368: Remove cookie field from alerts
   */
  readonly attribute AString cookie;

  /**
   * Bidi override for the title and contents. Valid values are "auto", "ltr",
   * or "rtl". Ignored if the backend doesn't support localization.
   */
  readonly attribute AString dir;

  /**
   * Language of the title and text. Ignored if the backend doesn't support
   * localization.
   */
  readonly attribute AString lang;

  /**
   * A Base64-encoded structured clone buffer containing data associated with
   * this alert. Only used for web notifications. Chrome callers should use a
   * cookie instead.
   */
  readonly attribute AString data;

  /**
   * The principal of the page that created the alert. Used for IPC security
   * checks, and to determine whether the alert is actionable.
   */
  readonly attribute nsIPrincipal principal;

  /**
   * The URI of the page that created the alert. |null| if the alert is not
   * actionable.
   */
  readonly attribute nsIURI URI;

  /**
   * Controls the image loading behavior. If true, the image request will be
   * loaded anonymously (without cookies or authorization tokens).
   */
  readonly attribute boolean inPrivateBrowsing;

  /**
   * Indicates that the notification should remain readily available until
   * the user activates or dismisses the notification.
   */
  readonly attribute boolean requireInteraction;

  /**
   * When set, indicates that no sounds or vibrations should be made.
   */
  readonly attribute boolean silent;

  /**
   * A vibration pattern to run with the display of the notification. A
   * vibration pattern can be an array with as few as one member. The values
   * are times in milliseconds where the even indices (0, 2, 4, etc.) indicate
   * how long to vibrate and the odd indices indicate how long to pause. For
   * example, [300, 100, 400] would vibrate 300ms, pause 100ms, then vibrate
   * 400ms.
   */
  readonly attribute Array<uint32_t> vibrate;

  /**
   * Actions available for users to choose from for interacting with
   * the notification.
   *
   * Implemented only for the system backend on Windows.
   */
  attribute Array<nsIAlertAction> actions;

  /**
   * Indicates whether this alert should show the source string and action
   * buttons. False for system alerts (which can omit the principal), or
   * expanded, system, and null principals.
   */
  readonly attribute boolean actionable;

  /**
   * The host and port of the originating page, or an empty string if the alert
   * is not actionable.
   */
  readonly attribute AString source;

  /**
   * The origin of the originating page, or an empty string if the alert is not
   * actionable. This corresponds to `nsIPrincipal.origin`.
   */
  readonly attribute ACString origin;

  /**
   * On Windows, chrome-privileged notifications -- i.e., those with a
   * non-actionable principal -- can have `opaqueRelaunchData`.  This data will
   * be provided to the application at relaunch and can trigger specific
   * actions.
   *
   * On non-Windows, this field is ignored.
   */
  attribute AString opaqueRelaunchData;

  /**
   * Retrieves the action object by the action name or null if not.
   *
   * @param aName The action name corresponding to nsIAlertAction.action.
   */
  nsIAlertAction getAction(in AString aName);
};

[scriptable, uuid(4b7c7108-0052-4117-881a-eab196d6bf8f)]
interface nsIAlertCallbacks : nsISupports
{
  // Called when the alert is shown.
  void onAlertShow();

  // Called when the alert is clicked.
  //
  // @param aAction The action that was clicked, if any.
  void onAlertClick([optional] in nsIAlertAction aAction);

  // Called when the alert is dismissed from the foreground and entered the
  // notification center.
  // Currently only supported on Windows.
  void onAlertDismissedFromForeground();

  // Called when the alert is closed by alerts service.
  //
  // This hints that the alert is intentionally closed, to disambiguate from
  // an early alertfinished which is recognized as an error.
  void onAlertClosed();

  // Called when the alert is finished its lifecycle.
  //
  // Usually it indicates the alert is closed. But if this is called before
  // onAlertShow, it indicates that there was an error.
  void onAlertFinished();

  // Called when the user requested to open the settings page.
  void onAlertSettings();

  // Called when the user requested to disable alerts.
  void onAlertDisable();
};

[scriptable, uuid(f7a36392-d98b-4141-a7d7-4e46642684e3)]
interface nsIAlertsService : nsISupports
{
  /**
   * Initializes and shows an |nsIAlertNotification| with the given parameters.
   *
   * @param aAlert The alert notification to show.
   * @param aAlertListener Used for callbacks. May be omitted if the caller
   *                       doesn't care about callbacks.
   * @see nsIAlertNotification for descriptions of all other parameters.
   * @throws NS_ERROR_NOT_AVAILABLE If the notification cannot be displayed.
   *
   * The following arguments will be passed to the alertListener's observe()
   * method:
   *   subject - null
   *   topic   - "alertfinished" when the alert goes away
   *             "alertdisablecallback" when alerts should be disabled for the principal
   *             "alertsettingscallback" when alert settings should be opened
   *             "alertclickcallback" when the text is clicked
   *             "alertshow" when the alert is shown
   *   data    - On desktop: the value of the cookie attribute of aAlert.
   *             On Android: the action associated with the notification.
   *             See: https://bugzilla.mozilla.org/show_bug.cgi?id=2022705#c1
   *
   * @note Depending on current circumstances (if the user's in a fullscreen
   *       application, for instance), the alert might not be displayed at all.
   *       In that case, if an alert listener is passed in it will receive the
   *       "alertfinished" notification immediately.
   */
  void showAlert(in nsIAlertNotification aAlert,
                 [optional] in nsIObserver aAlertListener);

  void showAlertWithCallbacks(in nsIAlertNotification aAlert,
                              in nsIAlertCallbacks aAlertCallbacks);

  /**
   * Close alerts created by the service.
   *
   * @param aName          The name of the notification to close. If no name
   *                       is provided then only a notification created with
   *                       no name (if any) will be closed.
   * @param aContextClosed The notification was implicitly closed, e.g. by tab
   *                       or window closure. This is necessary to track as some
   *                       platforms intentionally leave the notification visible
   *                       unless explicitly closed, e.g. by notification.close().
   */
  void closeAlert([optional] in AString aName, [optional] in boolean aContextClosed);

  /**
   * Returns identifiers of notifications that exist in OS notification center.
   */
  Array<AString> getHistory();

  /**
   * Clean up all resources used to listen to alerts.
   */
  void teardown();

  /**
   * Close all alerts opened for Private Browsing Mode.
   */
  void pbmTeardown();

  /**
   * Returns whether full-screen mode is enabled.
   *
   * @retval true On Windows if any of these apply:
   *              - App is in full-screen mode (F11).
   *              - App is in presentation mode.
   *              - Videogame is in full-screen mode.
   *              - Videogame is in exclusive full-screen mode.
   *
   * @retval false On Windows if none of the above apply, or on other platforms.
   */
  boolean isFullscreen();
};

[scriptable, uuid(c5d63e3a-259d-45a8-b964-8377967cb4d2)]
interface nsIAlertsDoNotDisturb : nsISupports
{
  /**
   * Toggles a manual Do Not Disturb mode for the service to reduce the amount
   * of disruption that alerts cause the user.
   * This may mean only displaying them in a notification tray/center or not
   * displaying them at all. If a system backend already supports a similar
   * feature controlled by the user, enabling this may not have any impact on
   * code to show an alert. e.g. on OS X, the system will take care not
   * disrupting a user if we simply create a notification like usual.
   */
  attribute boolean manualDoNotDisturb;

  /**
   * Toggles a mode for the service to suppress all notifications from
   * being dispatched when sharing the screen via the getMediaDisplay
   * API.
   */
  attribute boolean suppressForScreenSharing;
};
