FileHandleBase.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_FileHandle_h
  6. #define mozilla_dom_FileHandle_h
  7. #include "FileHandleCommon.h"
  8. #include "mozilla/dom/BindingDeclarations.h"
  9. #include "mozilla/dom/FileModeBinding.h"
  10. #include "mozilla/dom/Nullable.h"
  11. #include "mozilla/dom/TypedArray.h"
  12. template <class> struct already_AddRefed;
  13. class nsAString;
  14. struct PRThread;
  15. namespace mozilla {
  16. class ErrorResult;
  17. namespace dom {
  18. class BackgroundFileHandleChild;
  19. class Blob;
  20. class FileRequestBase;
  21. class FileRequestData;
  22. class FileRequestParams;
  23. class MutableFileBase;
  24. class StringOrArrayBufferOrArrayBufferViewOrBlob;
  25. /**
  26. * This class provides a base for FileHandle implementations.
  27. */
  28. class FileHandleBase
  29. : public RefCountedThreadObject
  30. {
  31. public:
  32. enum ReadyState
  33. {
  34. INITIAL = 0,
  35. LOADING,
  36. FINISHING,
  37. DONE
  38. };
  39. private:
  40. BackgroundFileHandleChild* mBackgroundActor;
  41. uint64_t mLocation;
  42. uint32_t mPendingRequestCount;
  43. ReadyState mReadyState;
  44. FileMode mMode;
  45. bool mAborted;
  46. bool mCreating;
  47. DEBUGONLY(bool mSentFinishOrAbort;)
  48. DEBUGONLY(bool mFiredCompleteOrAbort;)
  49. public:
  50. static FileHandleBase*
  51. GetCurrent();
  52. void
  53. SetBackgroundActor(BackgroundFileHandleChild* aActor);
  54. void
  55. ClearBackgroundActor()
  56. {
  57. AssertIsOnOwningThread();
  58. mBackgroundActor = nullptr;
  59. }
  60. void
  61. StartRequest(FileRequestBase* aFileRequest, const FileRequestParams& aParams);
  62. void
  63. OnNewRequest();
  64. void
  65. OnRequestFinished(bool aActorDestroyedNormally);
  66. bool
  67. IsOpen() const;
  68. bool
  69. IsFinishingOrDone() const
  70. {
  71. AssertIsOnOwningThread();
  72. return mReadyState == FINISHING || mReadyState == DONE;
  73. }
  74. bool
  75. IsDone() const
  76. {
  77. AssertIsOnOwningThread();
  78. return mReadyState == DONE;
  79. }
  80. bool
  81. IsAborted() const
  82. {
  83. AssertIsOnOwningThread();
  84. return mAborted;
  85. }
  86. void
  87. SetCreating()
  88. {
  89. mCreating = true;
  90. }
  91. void
  92. Abort();
  93. // Shared WebIDL (IndexedDB FileHandle and FileSystem FileHandle)
  94. FileMode
  95. Mode() const
  96. {
  97. AssertIsOnOwningThread();
  98. return mMode;
  99. }
  100. bool
  101. Active() const
  102. {
  103. AssertIsOnOwningThread();
  104. return IsOpen();
  105. }
  106. Nullable<uint64_t>
  107. GetLocation() const
  108. {
  109. AssertIsOnOwningThread();
  110. if (mLocation == UINT64_MAX) {
  111. return Nullable<uint64_t>();
  112. }
  113. return Nullable<uint64_t>(mLocation);
  114. }
  115. void
  116. SetLocation(const Nullable<uint64_t>& aLocation)
  117. {
  118. AssertIsOnOwningThread();
  119. // Null means the end-of-file.
  120. if (aLocation.IsNull()) {
  121. mLocation = UINT64_MAX;
  122. } else {
  123. mLocation = aLocation.Value();
  124. }
  125. }
  126. already_AddRefed<FileRequestBase>
  127. Read(uint64_t aSize, bool aHasEncoding, const nsAString& aEncoding,
  128. ErrorResult& aRv);
  129. already_AddRefed<FileRequestBase>
  130. Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv);
  131. already_AddRefed<FileRequestBase>
  132. Flush(ErrorResult& aRv);
  133. void
  134. Abort(ErrorResult& aRv);
  135. // Must be overridden in subclasses.
  136. virtual MutableFileBase*
  137. MutableFile() const = 0;
  138. // May be overridden in subclasses.
  139. virtual void
  140. HandleCompleteOrAbort(bool aAborted);
  141. protected:
  142. FileHandleBase(DEBUGONLY(PRThread* aOwningThread,)
  143. FileMode aMode);
  144. ~FileHandleBase();
  145. void
  146. OnReturnToEventLoop();
  147. bool
  148. CheckState(ErrorResult& aRv);
  149. bool
  150. CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv);
  151. bool
  152. CheckStateForWrite(ErrorResult& aRv);
  153. bool
  154. CheckStateForWriteOrAppend(bool aAppend, ErrorResult& aRv);
  155. already_AddRefed<FileRequestBase>
  156. WriteOrAppend(const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
  157. bool aAppend,
  158. ErrorResult& aRv);
  159. // Must be overridden in subclasses.
  160. virtual bool
  161. CheckWindow() = 0;
  162. // Must be overridden in subclasses.
  163. virtual already_AddRefed<FileRequestBase>
  164. GenerateFileRequest() = 0;
  165. private:
  166. already_AddRefed<FileRequestBase>
  167. WriteOrAppend(const nsAString& aValue, bool aAppend, ErrorResult& aRv);
  168. already_AddRefed<FileRequestBase>
  169. WriteOrAppend(const ArrayBuffer& aValue, bool aAppend, ErrorResult& aRv);
  170. already_AddRefed<FileRequestBase>
  171. WriteOrAppend(const ArrayBufferView& aValue, bool aAppend, ErrorResult& aRv);
  172. already_AddRefed<FileRequestBase>
  173. WriteOrAppend(Blob& aValue, bool aAppend, ErrorResult& aRv);
  174. already_AddRefed<FileRequestBase>
  175. WriteInternal(const FileRequestData& aData, uint64_t aDataLength,
  176. bool aAppend, ErrorResult& aRv);
  177. void
  178. SendFinish();
  179. void
  180. SendAbort();
  181. };
  182. } // namespace dom
  183. } // namespace mozilla
  184. #endif // mozilla_dom_FileHandle_h