ActorsParent.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef mozilla_dom_filehandle_ActorsParent_h
  5. #define mozilla_dom_filehandle_ActorsParent_h
  6. #include "mozilla/dom/FileHandleStorage.h"
  7. #include "mozilla/dom/PBackgroundMutableFileParent.h"
  8. #include "mozilla/ipc/BackgroundParent.h"
  9. #include "nsAutoPtr.h"
  10. #include "nsClassHashtable.h"
  11. #include "nsCOMPtr.h"
  12. #include "nsHashKeys.h"
  13. #include "nsString.h"
  14. #include "nsTArrayForwardDeclare.h"
  15. #include "nsTHashtable.h"
  16. template <class> struct already_AddRefed;
  17. class nsIFile;
  18. class nsIRunnable;
  19. class nsIThreadPool;
  20. namespace mozilla {
  21. namespace ipc {
  22. class PBackgroundParent;
  23. } // namespace ipc
  24. namespace dom {
  25. class BlobImpl;
  26. class FileHandle;
  27. class FileHandleOp;
  28. class FileHandleThreadPool final
  29. {
  30. class FileHandleQueue;
  31. struct DelayedEnqueueInfo;
  32. class DirectoryInfo;
  33. struct StoragesCompleteCallback;
  34. nsCOMPtr<nsIThreadPool> mThreadPool;
  35. nsCOMPtr<nsIEventTarget> mOwningThread;
  36. nsClassHashtable<nsCStringHashKey, DirectoryInfo> mDirectoryInfos;
  37. nsTArray<nsAutoPtr<StoragesCompleteCallback>> mCompleteCallbacks;
  38. bool mShutdownRequested;
  39. bool mShutdownComplete;
  40. public:
  41. static already_AddRefed<FileHandleThreadPool>
  42. Create();
  43. #ifdef DEBUG
  44. void
  45. AssertIsOnOwningThread() const;
  46. nsIEventTarget*
  47. GetThreadPoolEventTarget() const;
  48. #else
  49. void
  50. AssertIsOnOwningThread() const
  51. { }
  52. #endif
  53. void
  54. Enqueue(FileHandle* aFileHandle,
  55. FileHandleOp* aFileHandleOp,
  56. bool aFinish);
  57. NS_INLINE_DECL_REFCOUNTING(FileHandleThreadPool)
  58. void
  59. WaitForDirectoriesToComplete(nsTArray<nsCString>&& aDirectoryIds,
  60. nsIRunnable* aCallback);
  61. void
  62. Shutdown();
  63. private:
  64. FileHandleThreadPool();
  65. // Reference counted.
  66. ~FileHandleThreadPool();
  67. nsresult
  68. Init();
  69. void
  70. Cleanup();
  71. void
  72. FinishFileHandle(FileHandle* aFileHandle);
  73. bool
  74. MaybeFireCallback(StoragesCompleteCallback* aCallback);
  75. };
  76. class BackgroundMutableFileParentBase
  77. : public PBackgroundMutableFileParent
  78. {
  79. nsTHashtable<nsPtrHashKey<FileHandle>> mFileHandles;
  80. nsCString mDirectoryId;
  81. nsString mFileName;
  82. FileHandleStorage mStorage;
  83. bool mInvalidated;
  84. bool mActorWasAlive;
  85. bool mActorDestroyed;
  86. protected:
  87. nsCOMPtr<nsIFile> mFile;
  88. public:
  89. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BackgroundMutableFileParentBase)
  90. void
  91. Invalidate();
  92. FileHandleStorage
  93. Storage() const
  94. {
  95. return mStorage;
  96. }
  97. const nsCString&
  98. DirectoryId() const
  99. {
  100. return mDirectoryId;
  101. }
  102. const nsString&
  103. FileName() const
  104. {
  105. return mFileName;
  106. }
  107. bool
  108. RegisterFileHandle(FileHandle* aFileHandle);
  109. void
  110. UnregisterFileHandle(FileHandle* aFileHandle);
  111. void
  112. SetActorAlive();
  113. bool
  114. IsActorDestroyed() const
  115. {
  116. mozilla::ipc::AssertIsOnBackgroundThread();
  117. return mActorWasAlive && mActorDestroyed;
  118. }
  119. bool
  120. IsInvalidated() const
  121. {
  122. mozilla::ipc::AssertIsOnBackgroundThread();
  123. return mInvalidated;
  124. }
  125. virtual void
  126. NoteActiveState()
  127. { }
  128. virtual void
  129. NoteInactiveState()
  130. { }
  131. virtual mozilla::ipc::PBackgroundParent*
  132. GetBackgroundParent() const = 0;
  133. virtual already_AddRefed<nsISupports>
  134. CreateStream(bool aReadOnly);
  135. virtual already_AddRefed<BlobImpl>
  136. CreateBlobImpl()
  137. {
  138. return nullptr;
  139. }
  140. protected:
  141. BackgroundMutableFileParentBase(FileHandleStorage aStorage,
  142. const nsACString& aDirectoryId,
  143. const nsAString& aFileName,
  144. nsIFile* aFile);
  145. // Reference counted.
  146. ~BackgroundMutableFileParentBase();
  147. // IPDL methods are only called by IPDL.
  148. virtual void
  149. ActorDestroy(ActorDestroyReason aWhy) override;
  150. virtual PBackgroundFileHandleParent*
  151. AllocPBackgroundFileHandleParent(const FileMode& aMode) override;
  152. virtual bool
  153. RecvPBackgroundFileHandleConstructor(PBackgroundFileHandleParent* aActor,
  154. const FileMode& aMode) override;
  155. virtual bool
  156. DeallocPBackgroundFileHandleParent(PBackgroundFileHandleParent* aActor)
  157. override;
  158. virtual bool
  159. RecvDeleteMe() override;
  160. virtual bool
  161. RecvGetFileId(int64_t* aFileId) override;
  162. };
  163. } // namespace dom
  164. } // namespace mozilla
  165. #endif // mozilla_dom_filehandle_ActorsParent_h