/* -*- Mode: IDL; tab-width: 4; 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 nsIArray;
interface nsILocalFile;

/**
 * Application caches can store a set of namespace entries that affect
 * loads from the application cache.  If a load from the cache fails
 * to match an exact cache entry, namespaces entries will be searched
 * for a substring match, and should be applied appropriately.
 */
[scriptable, uuid(96e4c264-2065-4ce9-93bb-43734c62c4eb)]
interface nsIApplicationCacheNamespace : nsISupports
{
    /**
     * Items matching this namespace can be fetched from the network
     * when loading from this cache.  The "data" attribute is unused.
     */
    const unsigned long NAMESPACE_BYPASS = 1 << 0;

    /**
     * Items matching this namespace can be fetched from the network
     * when loading from this cache.  If the load fails, the cache entry
     * specified by the "data" attribute should be loaded instead.
     */
    const unsigned long NAMESPACE_FALLBACK = 1 << 1;

    /**
     * Items matching this namespace should be cached
     * opportunistically.  Successful toplevel loads of documents
     * in this namespace should be placed in the application cache.
     * Namespaces specifying NAMESPACE_OPPORTUNISTIC may also specify
     * NAMESPACE_FALLBACK to supply a fallback entry.
     */
    const unsigned long NAMESPACE_OPPORTUNISTIC = 1 << 2;

    /**
     * Initialize the namespace.
     */
    void init(in unsigned long itemType,
              in ACString namespaceSpec,
              in ACString data);

    /**
     * The namespace type.
     */
    readonly attribute unsigned long itemType;

    /**
     * The prefix of this namespace.  This should be the asciiSpec of the
     * URI prefix.
     */
    readonly attribute ACString namespaceSpec;

    /**
     * Data associated with this namespace, such as a fallback.  URI data should
     * use the asciiSpec of the URI.
     */
    readonly attribute ACString data;
};

/**
 * Application caches store resources for offline use.  Each
 * application cache has a unique client ID for use with
 * nsICacheService::openSession() to access the cache's entries.
 *
 * Each entry in the application cache can be marked with a set of
 * types, as discussed in the WHAT-WG offline applications
 * specification.
 *
 * All application caches with the same group ID belong to a cache
 * group.  Each group has one "active" cache that will service future
 * loads.  Inactive caches will be removed from the cache when they are
 * no longer referenced.
 */
[scriptable, uuid(231e1e53-05c1-41b6-b9de-dbbcce9385c9)]
interface nsIApplicationCache : nsISupports
{
    /**
     * Init this application cache instance to just hold the group ID and
     * the client ID to work just as a handle to the real cache. Used on
     * content process to simplify the application cache code.
     */
    void initAsHandle(in ACString groupId, in ACString clientId);

    /**
     * Entries in an application cache can be marked as one or more of
     * the following types.
     */

    /* This item is the application manifest. */
    const unsigned long ITEM_MANIFEST =      1 << 0;

    /* This item was explicitly listed in the application manifest. */
    const unsigned long ITEM_EXPLICIT =      1 << 1;

    /* This item was navigated in a toplevel browsing context, and
     * named this cache's group as its manifest. */
    const unsigned long ITEM_IMPLICIT =      1 << 2;

    /* This item was added by the dynamic scripting API */
    const unsigned long ITEM_DYNAMIC =       1 << 3;

    /* This item was listed in the application manifest, but named a
     * different cache group as its manifest. */
    const unsigned long ITEM_FOREIGN = 1 << 4;

    /* This item was listed as a fallback entry. */
    const unsigned long ITEM_FALLBACK = 1 << 5;

    /* This item matched an opportunistic cache namespace and was
     * cached accordingly. */
    const unsigned long ITEM_OPPORTUNISTIC = 1 << 6;

    /**
     * The group ID for this cache group.  This is the URI of the
     * manifest file.
     **/
    readonly attribute ACString groupID;

    /**
     * The client ID for this application cache.  Clients can open a
     * session with nsICacheService::createSession() using this client
     * ID and a storage policy of STORE_OFFLINE to access this cache.
     */
    readonly attribute ACString clientID;

    /**
     * TRUE if the cache is the active cache for this group.
     */
    readonly attribute boolean active;

    /**
     * The disk usage of the application cache, in bytes.
     */
    readonly attribute unsigned long usage;

    /**
     * Makes this cache the active application cache for this group.
     * Future loads associated with this group will come from this
     * cache.  Other caches from this cache group will be deactivated.
     */
    void activate();

    /**
     * Discard this application cache.  Removes all cached resources
     * for this cache.  If this is the active application cache for the
     * group, the group will be removed.
     */
    void discard();

    /**
     * Adds item types to a given entry.
     */
    void markEntry(in ACString key, in unsigned long typeBits);

    /**
     * Removes types from a given entry.  If the resulting entry has
     * no types left, the entry is removed.
     */
    void unmarkEntry(in ACString key, in unsigned long typeBits);

    /**
     * Gets the types for a given entry.
     */
    unsigned long getTypes(in ACString key);

    /**
     * Returns any entries in the application cache whose type matches
     * one or more of the bits in typeBits.
     */
    void gatherEntries(in PRUint32 typeBits,
                       out unsigned long count,
                       [array, size_is(count)] out string keys);

    /**
     * Add a set of namespace entries to the application cache.
     * @param namespaces
     *        An nsIArray of nsIApplicationCacheNamespace entries.
     */
    void addNamespaces(in nsIArray namespaces);

    /**
     * Get the most specific namespace matching a given key.
     */
    nsIApplicationCacheNamespace getMatchingNamespace(in ACString key);

    /**
     * If set, this offline cache is placed in a different directory
     * than the current application profile.
     */
    readonly attribute nsILocalFile cacheDirectory;
};
