Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsISupports.idl" |
michael@0 | 8 | |
michael@0 | 9 | interface nsIArray; |
michael@0 | 10 | interface nsIBackgroundFileSaverObserver; |
michael@0 | 11 | interface nsIFile; |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * Allows saving data to a file, while handling all the input/output on a |
michael@0 | 15 | * background thread, including the initial file name assignment and any |
michael@0 | 16 | * subsequent renaming of the target file. |
michael@0 | 17 | * |
michael@0 | 18 | * This interface is designed for file downloads. Generally, they start in the |
michael@0 | 19 | * temporary directory, while the user is selecting the final name. Then, they |
michael@0 | 20 | * are moved to the chosen target directory with a ".part" extension appended to |
michael@0 | 21 | * the file name. Finally, they are renamed when the download is completed. |
michael@0 | 22 | * |
michael@0 | 23 | * Components implementing both nsIBackgroundFileSaver and nsIStreamListener |
michael@0 | 24 | * allow data to be fed using an implementation of OnDataAvailable that never |
michael@0 | 25 | * blocks the calling thread. They suspend the request that drives the stream |
michael@0 | 26 | * listener in case too much data is being fed, and resume it when the data has |
michael@0 | 27 | * been written. Calling OnStopRequest does not necessarily close the target |
michael@0 | 28 | * file, and the Finish method must be called to complete the operation. |
michael@0 | 29 | * |
michael@0 | 30 | * Components implementing both nsIBackgroundFileSaver and nsIAsyncOutputStream |
michael@0 | 31 | * allow data to be fed directly to the non-blocking output stream, that however |
michael@0 | 32 | * may return NS_BASE_STREAM_WOULD_BLOCK in case too much data is being fed. |
michael@0 | 33 | * Closing the output stream does not necessarily close the target file, and the |
michael@0 | 34 | * Finish method must be called to complete the operation. |
michael@0 | 35 | * |
michael@0 | 36 | * @remarks Implementations may require the consumer to always call Finish. If |
michael@0 | 37 | * the object reference is released without calling Finish, a memory |
michael@0 | 38 | * leak may occur, and the target file might be kept locked. All |
michael@0 | 39 | * public methods of the interface may only be called from the main |
michael@0 | 40 | * thread. |
michael@0 | 41 | */ |
michael@0 | 42 | [scriptable, uuid(c43544a4-682c-4262-b407-2453d26e660d)] |
michael@0 | 43 | interface nsIBackgroundFileSaver : nsISupports |
michael@0 | 44 | { |
michael@0 | 45 | /** |
michael@0 | 46 | * This observer receives notifications when the target file name changes and |
michael@0 | 47 | * when the operation completes, successfully or not. |
michael@0 | 48 | * |
michael@0 | 49 | * @remarks A strong reference to the observer is held. Notification events |
michael@0 | 50 | * are dispatched to the thread that created the object that |
michael@0 | 51 | * implements nsIBackgroundFileSaver. |
michael@0 | 52 | */ |
michael@0 | 53 | attribute nsIBackgroundFileSaverObserver observer; |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * An nsIArray of nsIX509CertList, representing a chain of X.509 signatures on |
michael@0 | 57 | * the downloaded file. Each list may belong to a different signer and contain |
michael@0 | 58 | * certificates all the way up to the root. |
michael@0 | 59 | * |
michael@0 | 60 | * @throws NS_ERROR_NOT_AVAILABLE |
michael@0 | 61 | * In case this is called before the onSaveComplete method has been |
michael@0 | 62 | * called to notify success, or enableSignatureInfo has not been |
michael@0 | 63 | * called. |
michael@0 | 64 | */ |
michael@0 | 65 | readonly attribute nsIArray signatureInfo; |
michael@0 | 66 | |
michael@0 | 67 | /** |
michael@0 | 68 | * The SHA-256 hash, in raw bytes, associated with the data that was saved. |
michael@0 | 69 | * |
michael@0 | 70 | * In case the enableAppend method has been called, the hash computation |
michael@0 | 71 | * includes the contents of the existing file, if any. |
michael@0 | 72 | * |
michael@0 | 73 | * @throws NS_ERROR_NOT_AVAILABLE |
michael@0 | 74 | * In case the enableSha256 method has not been called, or before the |
michael@0 | 75 | * onSaveComplete method has been called to notify success. |
michael@0 | 76 | */ |
michael@0 | 77 | readonly attribute ACString sha256Hash; |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Instructs the component to compute the signatureInfo of the target file, |
michael@0 | 81 | * and make it available in the signatureInfo property. |
michael@0 | 82 | * |
michael@0 | 83 | * @remarks This must be set on the main thread before the first call to |
michael@0 | 84 | * setTarget. |
michael@0 | 85 | */ |
michael@0 | 86 | void enableSignatureInfo(); |
michael@0 | 87 | |
michael@0 | 88 | /** |
michael@0 | 89 | * Instructs the component to compute the SHA-256 hash of the target file, and |
michael@0 | 90 | * make it available in the sha256Hash property. |
michael@0 | 91 | * |
michael@0 | 92 | * @remarks This must be set on the main thread before the first call to |
michael@0 | 93 | * setTarget. |
michael@0 | 94 | */ |
michael@0 | 95 | void enableSha256(); |
michael@0 | 96 | |
michael@0 | 97 | /** |
michael@0 | 98 | * Instructs the component to append data to the initial target file, that |
michael@0 | 99 | * will be specified by the first call to the setTarget method, instead of |
michael@0 | 100 | * overwriting the file. |
michael@0 | 101 | * |
michael@0 | 102 | * If the initial target file does not exist, this method has no effect. |
michael@0 | 103 | * |
michael@0 | 104 | * @remarks This must be set on the main thread before the first call to |
michael@0 | 105 | * setTarget. |
michael@0 | 106 | */ |
michael@0 | 107 | void enableAppend(); |
michael@0 | 108 | |
michael@0 | 109 | /** |
michael@0 | 110 | * Sets the name of the output file to be written. The target can be changed |
michael@0 | 111 | * after data has already been fed, in which case the existing file will be |
michael@0 | 112 | * moved to the new destination. |
michael@0 | 113 | * |
michael@0 | 114 | * In case the specified file already exists, and this method is called for |
michael@0 | 115 | * the first time, the file may be either overwritten or appended to, based on |
michael@0 | 116 | * whether the enableAppend method was called. Subsequent calls always |
michael@0 | 117 | * overwrite the specified target file with the previously saved data. |
michael@0 | 118 | * |
michael@0 | 119 | * No file will be written until this function is called at least once. It's |
michael@0 | 120 | * recommended not to feed any data until the output file is set. |
michael@0 | 121 | * |
michael@0 | 122 | * If an input/output error occurs with the specified file, the save operation |
michael@0 | 123 | * fails. Failure is notified asynchronously through the observer. |
michael@0 | 124 | * |
michael@0 | 125 | * @param aTarget |
michael@0 | 126 | * New output file to be written. |
michael@0 | 127 | * @param aKeepPartial |
michael@0 | 128 | * Indicates whether aFile should be kept as partially completed, |
michael@0 | 129 | * rather than deleted, if the operation fails or is canceled. This is |
michael@0 | 130 | * generally set for downloads that use temporary ".part" files. |
michael@0 | 131 | */ |
michael@0 | 132 | void setTarget(in nsIFile aTarget, in bool aKeepPartial); |
michael@0 | 133 | |
michael@0 | 134 | /** |
michael@0 | 135 | * Terminates access to the output file, then notifies the observer with the |
michael@0 | 136 | * specified status code. A failure code will force the operation to be |
michael@0 | 137 | * canceled, in which case the output file will be deleted if requested. |
michael@0 | 138 | * |
michael@0 | 139 | * This forces the involved streams to be closed, thus no more data should be |
michael@0 | 140 | * fed to the component after this method has been called. |
michael@0 | 141 | * |
michael@0 | 142 | * This is the last method that should be called on this object, and the |
michael@0 | 143 | * target file name cannot be changed anymore after this method has been |
michael@0 | 144 | * called. Conversely, before calling this method, the file can still be |
michael@0 | 145 | * renamed even if all the data has been fed. |
michael@0 | 146 | * |
michael@0 | 147 | * @param aStatus |
michael@0 | 148 | * Result code that determines whether the operation should succeed or |
michael@0 | 149 | * be canceled, and is notified to the observer. If the operation |
michael@0 | 150 | * fails meanwhile for other reasons, or the observer has been already |
michael@0 | 151 | * notified of completion, this status code is ignored. |
michael@0 | 152 | */ |
michael@0 | 153 | void finish(in nsresult aStatus); |
michael@0 | 154 | }; |
michael@0 | 155 | |
michael@0 | 156 | [scriptable, uuid(ee7058c3-6e54-4411-b76b-3ce87b76fcb6)] |
michael@0 | 157 | interface nsIBackgroundFileSaverObserver : nsISupports |
michael@0 | 158 | { |
michael@0 | 159 | /** |
michael@0 | 160 | * Called when the name of the output file has been determined. This function |
michael@0 | 161 | * may be called more than once if the target file is renamed while saving. |
michael@0 | 162 | * |
michael@0 | 163 | * @param aSaver |
michael@0 | 164 | * Reference to the object that raised the notification. |
michael@0 | 165 | * @param aTarget |
michael@0 | 166 | * Name of the file that is being written. |
michael@0 | 167 | */ |
michael@0 | 168 | void onTargetChange(in nsIBackgroundFileSaver aSaver, in nsIFile aTarget); |
michael@0 | 169 | |
michael@0 | 170 | /** |
michael@0 | 171 | * Called when the operation completed, and the target file has been closed. |
michael@0 | 172 | * If the operation succeeded, the target file is ready to be used, otherwise |
michael@0 | 173 | * it might have been already deleted. |
michael@0 | 174 | * |
michael@0 | 175 | * @param aSaver |
michael@0 | 176 | * Reference to the object that raised the notification. |
michael@0 | 177 | * @param aStatus |
michael@0 | 178 | * Result code that determines whether the operation succeeded or |
michael@0 | 179 | * failed, as well as the failure reason. |
michael@0 | 180 | */ |
michael@0 | 181 | void onSaveComplete(in nsIBackgroundFileSaver aSaver, in nsresult aStatus); |
michael@0 | 182 | }; |