/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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"

interface nsIFrameLoader;
interface nsIPartialSHistory;

/**
 * nsIGroupedSHistory represent a combined session history across multiple
 * root docshells (usually browser tabs). The participating nsISHistory can
 * either be in chrome process or in content process, but nsIGroupedSHistory
 * itself lives in chrome process. The communication is proxyed through
 * nsIPartialSHistory.
 */
[scriptable, builtinclass, uuid(813e498d-73a8-449a-be09-6187e62c5352)]
interface nsIGroupedSHistory : nsISupports
{
  // The total number of entries of all its partial session histories.
  [infallible] readonly attribute unsigned long count;

  /**
   * The currently active frameloader controlled by this nsIGroupedSHistory.
   */
  readonly attribute nsIFrameLoader activeFrameLoader;

  /**
   * Remove all partial histories after currently active one (if any) and then
   * append the given partial session history to the end of the list.
   */
  void appendPartialSHistory(in nsIPartialSHistory aPartialHistory);

  /**
   * Notify the grouped session history that the active partial session history
   * has been modified.
   *
   * @param aPartialHistory The partial history which was updated
   * @param aTruncate If this parameter is true, all partial session histories
   *                  after this one will be removed.
   */
  void handleSHistoryUpdate(in nsIPartialSHistory aPartialHistory, in boolean aTruncate);

  /**
   * Find the proper partial session history and navigate to the entry
   * corresponding to the given global index. Note it doesn't swap frameloaders,
   * but rather return the target loader for the caller to swap.
   *
   * This function may throw NS_ERROR_NOT_AVAILABLE if the frameloader to swap
   * to is dead.
   *
   * @param  aGlobalIndex
   *         The global index to navigate to.
   * @return The frameloader which needs to be swapped in, or null if no
   *         frameloader needs to be swapped.
   */
  nsIFrameLoader gotoIndex(in unsigned long aGlobalIndex);

  /**
   * Close the FrameLoaderOwners of the inactive PartialSHistories in this GlobalSHistory.
   * This does not remove the PartialSHistories from the GroupedSHistory.
   */
  void closeInactiveFrameLoaderOwners();

  /**
   * Add a partialSHistory as a "prerendering" partialSHistory. This
   * partialSHistory's tab will have its lifetime managed by the
   * GroupedSHistory, and will be closed when closeInactiveFrameLoaderOwners is
   * called, or whenever a SHistory update is received.
   */
  void addPrerenderingPartialSHistory(in nsIPartialSHistory aPrerendering, in long aId);

  /**
   * Switch to the prerendering partialSHistory identified by aId, appending it after the current partialSHistory.
   */
  [implicit_jscontext] nsISupports activatePrerendering(in long aId);

  /**
   * Cancel the prerendering with the given ID.
   */
  void cancelPrerendering(in long aId);
};
