Interface nsIBackgroundFileSaverType

Allows saving data to a file, while handling all the input/output on a background thread, including the initial file name assignment and any subsequent renaming of the target file.

This interface is designed for file downloads. Generally, they start in the temporary directory, while the user is selecting the final name. Then, they are moved to the chosen target directory with a ".part" extension appended to the file name. Finally, they are renamed when the download is completed.

Components implementing both nsIBackgroundFileSaver and nsIStreamListener allow data to be fed using an implementation of OnDataAvailable that never blocks the calling thread. They suspend the request that drives the stream listener in case too much data is being fed, and resume it when the data has been written. Calling OnStopRequest does not necessarily close the target file, and the Finish method must be called to complete the operation.

Components implementing both nsIBackgroundFileSaver and nsIAsyncOutputStream allow data to be fed directly to the non-blocking output stream, that however may return NS_BASE_STREAM_WOULD_BLOCK in case too much data is being fed. Closing the output stream does not necessarily close the target file, and the Finish method must be called to complete the operation.

Remarks

Implementations may require the consumer to always call Finish. If the object reference is released without calling Finish, a memory leak may occur, and the target file might be kept locked. All public methods of the interface may only be called from the main thread.

Hierarchy

Properties

observer: nsIBackgroundFileSaverObserver

This observer receives notifications when the target file name changes and when the operation completes, successfully or not.

Remarks

A strong reference to the observer is held. Notification events are dispatched to the thread that created the object that implements nsIBackgroundFileSaver.

sha256Hash: string

The SHA-256 hash, in raw bytes, associated with the data that was saved.

In case the enableAppend method has been called, the hash computation includes the contents of the existing file, if any.

Throws

NS_ERROR_NOT_AVAILABLE In case the enableSha256 method has not been called, or before the onSaveComplete method has been called to notify success.

Methods

  • Increases the reference count for this interface. The associated instance will not be deleted unless the reference count is returned to zero.

    Returns

    The resulting reference count.

    Returns number

  • Parameters

    • aIID: object
    • Optional aInstancePtr: object

    Returns any

  • A run time mechanism for interface discovery.

    Returns

    NS_OK if the interface is supported by the associated instance, NS_NOINTERFACE if it is not.

    aInstancePtr must not be null.

    Parameters

    • aIID: object

      [in] A requested interface IID

    • aInstancePtr: object

      [out] A pointer to an interface pointer to receive the result.

    Returns void

  • Decreases the reference count for this interface. Generally, if the reference count returns to zero, the associated instance is deleted.

    Returns

    The resulting reference count.

    Returns number

  • Instructs the component to append data to the initial target file, that will be specified by the first call to the setTarget method, instead of overwriting the file.

    If the initial target file does not exist, this method has no effect.

    Remarks

    This must be set on the main thread before the first call to setTarget.

    Returns void

  • Instructs the component to compute the SHA-256 hash of the target file, and make it available in the sha256Hash property.

    Remarks

    This must be set on the main thread before the first call to setTarget.

    Returns void

  • Instructs the component to compute the signatureInfo of the target file, and make it available in the signatureInfo property.

    Remarks

    This must be set on the main thread before the first call to setTarget.

    Returns void

  • Terminates access to the output file, then notifies the observer with the specified status code. A failure code will force the operation to be canceled, in which case the output file will be deleted if requested.

    This forces the involved streams to be closed, thus no more data should be fed to the component after this method has been called.

    This is the last method that should be called on this object, and the target file name cannot be changed anymore after this method has been called. Conversely, before calling this method, the file can still be renamed even if all the data has been fed.

    Parameters

    • aStatus: number

      Result code that determines whether the operation should succeed or be canceled, and is notified to the observer. If the operation fails meanwhile for other reasons, or the observer has been already notified of completion, this status code is ignored.

    Returns void

  • Sets the name of the output file to be written. The target can be changed after data has already been fed, in which case the existing file will be moved to the new destination.

    In case the specified file already exists, and this method is called for the first time, the file may be either overwritten or appended to, based on whether the enableAppend method was called. Subsequent calls always overwrite the specified target file with the previously saved data.

    No file will be written until this function is called at least once. It's recommended not to feed any data until the output file is set.

    If an input/output error occurs with the specified file, the save operation fails. Failure is notified asynchronously through the observer.

    Parameters

    • aTarget: nsIFile

      New output file to be written.

    • aKeepPartial: bool

      Indicates whether aFile should be kept as partially completed, rather than deleted, if the operation fails or is canceled. This is generally set for downloads that use temporary ".part" files.

    Returns void

Generated using TypeDoc