nsIBackgroundFileSaver.idl 7.6 KB

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