BlobParent.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #ifndef mozilla_dom_ipc_BlobParent_h
  6. #define mozilla_dom_ipc_BlobParent_h
  7. #include "mozilla/Attributes.h"
  8. #include "mozilla/StaticPtr.h"
  9. #include "mozilla/dom/PBlobParent.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsTArray.h"
  12. template <class, class> class nsDataHashtable;
  13. class nsIDHashKey;
  14. class nsIEventTarget;
  15. class nsIRemoteBlob;
  16. template <class> class nsRevocableEventPtr;
  17. class nsString;
  18. namespace mozilla {
  19. class Mutex;
  20. namespace ipc {
  21. class PBackgroundParent;
  22. } // namespace ipc
  23. namespace dom {
  24. class ContentParent;
  25. class BlobImpl;
  26. class nsIContentParent;
  27. class PBlobStreamParent;
  28. class BlobParent final
  29. : public PBlobParent
  30. {
  31. typedef mozilla::ipc::PBackgroundParent PBackgroundParent;
  32. class IDTableEntry;
  33. typedef nsDataHashtable<nsIDHashKey, IDTableEntry*> IDTable;
  34. class OpenStreamRunnable;
  35. friend class OpenStreamRunnable;
  36. class RemoteBlobImpl;
  37. struct CreateBlobImplMetadata;
  38. static StaticAutoPtr<IDTable> sIDTable;
  39. static StaticAutoPtr<Mutex> sIDTableMutex;
  40. BlobImpl* mBlobImpl;
  41. RemoteBlobImpl* mRemoteBlobImpl;
  42. // One of these will be null and the other non-null.
  43. PBackgroundParent* mBackgroundManager;
  44. nsCOMPtr<nsIContentParent> mContentManager;
  45. nsCOMPtr<nsIEventTarget> mEventTarget;
  46. // nsIInputStreams backed by files must ensure that the files are actually
  47. // opened and closed on a background thread before we can send their file
  48. // handles across to the child. The child process could crash during this
  49. // process so we need to make sure we cancel the intended response in such a
  50. // case. We do that by holding an array of nsRevocableEventPtr. If the child
  51. // crashes then this actor will be destroyed and the nsRevocableEventPtr
  52. // destructor will cancel any stream events that are currently in flight.
  53. nsTArray<nsRevocableEventPtr<OpenStreamRunnable>> mOpenStreamRunnables;
  54. RefPtr<IDTableEntry> mIDTableEntry;
  55. bool mOwnsBlobImpl;
  56. public:
  57. class FriendKey;
  58. static void
  59. Startup(const FriendKey& aKey);
  60. // These create functions are called on the sending side.
  61. static BlobParent*
  62. GetOrCreate(nsIContentParent* aManager, BlobImpl* aBlobImpl);
  63. static BlobParent*
  64. GetOrCreate(PBackgroundParent* aManager, BlobImpl* aBlobImpl);
  65. // These create functions are called on the receiving side.
  66. static BlobParent*
  67. Create(nsIContentParent* aManager,
  68. const ParentBlobConstructorParams& aParams);
  69. static BlobParent*
  70. Create(PBackgroundParent* aManager,
  71. const ParentBlobConstructorParams& aParams);
  72. static void
  73. Destroy(PBlobParent* aActor)
  74. {
  75. delete static_cast<BlobParent*>(aActor);
  76. }
  77. static already_AddRefed<BlobImpl>
  78. GetBlobImplForID(const nsID& aID);
  79. bool
  80. HasManager() const
  81. {
  82. return mBackgroundManager || mContentManager;
  83. }
  84. PBackgroundParent*
  85. GetBackgroundManager() const
  86. {
  87. return mBackgroundManager;
  88. }
  89. nsIContentParent*
  90. GetContentManager() const
  91. {
  92. return mContentManager;
  93. }
  94. // Get the BlobImpl associated with this actor.
  95. already_AddRefed<BlobImpl>
  96. GetBlobImpl();
  97. void
  98. AssertIsOnOwningThread() const
  99. #ifdef DEBUG
  100. ;
  101. #else
  102. { }
  103. #endif
  104. private:
  105. // These constructors are called on the sending side.
  106. BlobParent(nsIContentParent* aManager, IDTableEntry* aIDTableEntry);
  107. BlobParent(PBackgroundParent* aManager, IDTableEntry* aIDTableEntry);
  108. // These constructors are called on the receiving side.
  109. BlobParent(nsIContentParent* aManager,
  110. BlobImpl* aBlobImpl,
  111. IDTableEntry* aIDTableEntry);
  112. BlobParent(PBackgroundParent* aManager,
  113. BlobImpl* aBlobImpl,
  114. IDTableEntry* aIDTableEntry);
  115. // Only destroyed by BackgroundParentImpl and ContentParent.
  116. ~BlobParent();
  117. void
  118. CommonInit(IDTableEntry* aIDTableEntry);
  119. void
  120. CommonInit(BlobImpl* aBlobImpl, IDTableEntry* aIDTableEntry);
  121. template <class ParentManagerType>
  122. static BlobParent*
  123. GetOrCreateFromImpl(ParentManagerType* aManager,
  124. BlobImpl* aBlobImpl);
  125. template <class ParentManagerType>
  126. static BlobParent*
  127. CreateFromParams(ParentManagerType* aManager,
  128. const ParentBlobConstructorParams& aParams);
  129. template <class ParentManagerType>
  130. static BlobParent*
  131. SendSliceConstructor(ParentManagerType* aManager,
  132. const ParentBlobConstructorParams& aParams,
  133. const ChildBlobConstructorParams& aOtherSideParams);
  134. static BlobParent*
  135. MaybeGetActorFromRemoteBlob(nsIRemoteBlob* aRemoteBlob,
  136. nsIContentParent* aManager);
  137. static BlobParent*
  138. MaybeGetActorFromRemoteBlob(nsIRemoteBlob* aRemoteBlob,
  139. PBackgroundParent* aManager);
  140. void
  141. NoteDyingRemoteBlobImpl();
  142. void
  143. NoteRunnableCompleted(OpenStreamRunnable* aRunnable);
  144. nsIEventTarget*
  145. EventTarget() const
  146. {
  147. return mEventTarget;
  148. }
  149. bool
  150. IsOnOwningThread() const;
  151. // These methods are only called by the IPDL message machinery.
  152. virtual void
  153. ActorDestroy(ActorDestroyReason aWhy) override;
  154. virtual PBlobStreamParent*
  155. AllocPBlobStreamParent(const uint64_t& aStart,
  156. const uint64_t& aLength) override;
  157. virtual bool
  158. RecvPBlobStreamConstructor(PBlobStreamParent* aActor,
  159. const uint64_t& aStart,
  160. const uint64_t& aLength) override;
  161. virtual bool
  162. DeallocPBlobStreamParent(PBlobStreamParent* aActor) override;
  163. virtual bool
  164. RecvResolveMystery(const ResolveMysteryParams& aParams) override;
  165. virtual bool
  166. RecvBlobStreamSync(const uint64_t& aStart,
  167. const uint64_t& aLength,
  168. InputStreamParams* aParams,
  169. OptionalFileDescriptorSet* aFDs) override;
  170. virtual bool
  171. RecvWaitForSliceCreation() override;
  172. virtual bool
  173. RecvGetFileId(int64_t* aFileId) override;
  174. virtual bool
  175. RecvGetFilePath(nsString* aFilePath) override;
  176. };
  177. // Only let ContentParent call BlobParent::Startup() and ensure that
  178. // ContentParent can't access any other BlobParent internals.
  179. class BlobParent::FriendKey final
  180. {
  181. friend class ContentParent;
  182. private:
  183. FriendKey()
  184. { }
  185. FriendKey(const FriendKey& /* aOther */)
  186. { }
  187. public:
  188. ~FriendKey()
  189. { }
  190. };
  191. } // namespace dom
  192. } // namespace mozilla
  193. #endif // mozilla_dom_ipc_BlobParent_h