ReadStream.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 "mozilla/dom/cache/ReadStream.h"
  6. #include "mozilla/Unused.h"
  7. #include "mozilla/dom/cache/CacheStreamControlChild.h"
  8. #include "mozilla/dom/cache/CacheStreamControlParent.h"
  9. #include "mozilla/dom/cache/CacheTypes.h"
  10. #include "mozilla/ipc/IPCStreamUtils.h"
  11. #include "mozilla/SnappyUncompressInputStream.h"
  12. #include "nsIAsyncInputStream.h"
  13. #include "nsTArray.h"
  14. namespace mozilla {
  15. namespace dom {
  16. namespace cache {
  17. using mozilla::Unused;
  18. using mozilla::ipc::AutoIPCStream;
  19. using mozilla::ipc::IPCStream;
  20. // ----------------------------------------------------------------------------
  21. // The inner stream class. This is where all of the real work is done. As
  22. // an invariant Inner::Close() must be called before ~Inner(). This is
  23. // guaranteed by our outer ReadStream class.
  24. class ReadStream::Inner final : public ReadStream::Controllable
  25. {
  26. public:
  27. Inner(StreamControl* aControl, const nsID& aId,
  28. nsIInputStream* aStream);
  29. void
  30. Serialize(CacheReadStreamOrVoid* aReadStreamOut,
  31. nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
  32. ErrorResult& aRv);
  33. void
  34. Serialize(CacheReadStream* aReadStreamOut,
  35. nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
  36. ErrorResult& aRv);
  37. // ReadStream::Controllable methods
  38. virtual void
  39. CloseStream() override;
  40. virtual void
  41. CloseStreamWithoutReporting() override;
  42. virtual bool
  43. MatchId(const nsID& aId) const override;
  44. virtual bool
  45. HasEverBeenRead() const override;
  46. // Simulate nsIInputStream methods, but we don't actually inherit from it
  47. nsresult
  48. Close();
  49. nsresult
  50. Available(uint64_t *aNumAvailableOut);
  51. nsresult
  52. Read(char *aBuf, uint32_t aCount, uint32_t *aNumReadOut);
  53. nsresult
  54. ReadSegments(nsWriteSegmentFun aWriter, void *aClosure, uint32_t aCount,
  55. uint32_t *aNumReadOut);
  56. nsresult
  57. IsNonBlocking(bool *aNonBlockingOut);
  58. private:
  59. class NoteClosedRunnable;
  60. class ForgetRunnable;
  61. ~Inner();
  62. void
  63. NoteClosed();
  64. void
  65. Forget();
  66. void
  67. NoteClosedOnOwningThread();
  68. void
  69. ForgetOnOwningThread();
  70. // Weak ref to the stream control actor. The actor will always call either
  71. // CloseStream() or CloseStreamWithoutReporting() before it's destroyed. The
  72. // weak ref is cleared in the resulting NoteClosedOnOwningThread() or
  73. // ForgetOnOwningThread() method call.
  74. StreamControl* mControl;
  75. const nsID mId;
  76. nsCOMPtr<nsIThread> mOwningThread;
  77. enum State
  78. {
  79. Open,
  80. Closed,
  81. NumStates
  82. };
  83. Atomic<State> mState;
  84. Atomic<bool> mHasEverBeenRead;
  85. // The wrapped stream objects may not be threadsafe. We need to be able
  86. // to close a stream on our owning thread while an IO thread is simultaneously
  87. // reading the same stream. Therefore, protect all access to these stream
  88. // objects with a mutex.
  89. Mutex mMutex;
  90. nsCOMPtr<nsIInputStream> mStream;
  91. nsCOMPtr<nsIInputStream> mSnappyStream;
  92. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(cache::ReadStream::Inner, override)
  93. };
  94. // ----------------------------------------------------------------------------
  95. // Runnable to notify actors that the ReadStream has closed. This must
  96. // be done on the thread associated with the PBackground actor. Must be
  97. // cancelable to execute on Worker threads (which can occur when the
  98. // ReadStream is constructed on a child process Worker thread).
  99. class ReadStream::Inner::NoteClosedRunnable final : public CancelableRunnable
  100. {
  101. public:
  102. explicit NoteClosedRunnable(ReadStream::Inner* aStream)
  103. : mStream(aStream)
  104. { }
  105. NS_IMETHOD Run() override
  106. {
  107. mStream->NoteClosedOnOwningThread();
  108. mStream = nullptr;
  109. return NS_OK;
  110. }
  111. // Note, we must proceed with the Run() method since our actor will not
  112. // clean itself up until we note that the stream is closed.
  113. nsresult Cancel() override
  114. {
  115. Run();
  116. return NS_OK;
  117. }
  118. private:
  119. ~NoteClosedRunnable() { }
  120. RefPtr<ReadStream::Inner> mStream;
  121. };
  122. // ----------------------------------------------------------------------------
  123. // Runnable to clear actors without reporting that the ReadStream has
  124. // closed. Since this can trigger actor destruction, we need to do
  125. // it on the thread associated with the PBackground actor. Must be
  126. // cancelable to execute on Worker threads (which can occur when the
  127. // ReadStream is constructed on a child process Worker thread).
  128. class ReadStream::Inner::ForgetRunnable final : public CancelableRunnable
  129. {
  130. public:
  131. explicit ForgetRunnable(ReadStream::Inner* aStream)
  132. : mStream(aStream)
  133. { }
  134. NS_IMETHOD Run() override
  135. {
  136. mStream->ForgetOnOwningThread();
  137. mStream = nullptr;
  138. return NS_OK;
  139. }
  140. // Note, we must proceed with the Run() method so that we properly
  141. // call RemoveListener on the actor.
  142. nsresult Cancel() override
  143. {
  144. Run();
  145. return NS_OK;
  146. }
  147. private:
  148. ~ForgetRunnable() { }
  149. RefPtr<ReadStream::Inner> mStream;
  150. };
  151. // ----------------------------------------------------------------------------
  152. ReadStream::Inner::Inner(StreamControl* aControl, const nsID& aId,
  153. nsIInputStream* aStream)
  154. : mControl(aControl)
  155. , mId(aId)
  156. , mOwningThread(NS_GetCurrentThread())
  157. , mState(Open)
  158. , mHasEverBeenRead(false)
  159. , mMutex("dom::cache::ReadStream")
  160. , mStream(aStream)
  161. , mSnappyStream(new SnappyUncompressInputStream(aStream))
  162. {
  163. MOZ_DIAGNOSTIC_ASSERT(mStream);
  164. MOZ_DIAGNOSTIC_ASSERT(mControl);
  165. mControl->AddReadStream(this);
  166. }
  167. void
  168. ReadStream::Inner::Serialize(CacheReadStreamOrVoid* aReadStreamOut,
  169. nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
  170. ErrorResult& aRv)
  171. {
  172. MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
  173. MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
  174. *aReadStreamOut = CacheReadStream();
  175. Serialize(&aReadStreamOut->get_CacheReadStream(), aStreamCleanupList, aRv);
  176. }
  177. void
  178. ReadStream::Inner::Serialize(CacheReadStream* aReadStreamOut,
  179. nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
  180. ErrorResult& aRv)
  181. {
  182. MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
  183. MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
  184. if (mState != Open) {
  185. aRv.ThrowTypeError<MSG_CACHE_STREAM_CLOSED>();
  186. return;
  187. }
  188. MOZ_DIAGNOSTIC_ASSERT(mControl);
  189. aReadStreamOut->id() = mId;
  190. mControl->SerializeControl(aReadStreamOut);
  191. {
  192. MutexAutoLock lock(mMutex);
  193. mControl->SerializeStream(aReadStreamOut, mStream, aStreamCleanupList);
  194. }
  195. MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut->stream().type() ==
  196. IPCStream::TInputStreamParamsWithFds);
  197. // We're passing ownership across the IPC barrier with the control, so
  198. // do not signal that the stream is closed here.
  199. Forget();
  200. }
  201. void
  202. ReadStream::Inner::CloseStream()
  203. {
  204. MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
  205. Close();
  206. }
  207. void
  208. ReadStream::Inner::CloseStreamWithoutReporting()
  209. {
  210. MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
  211. Forget();
  212. }
  213. bool
  214. ReadStream::Inner::MatchId(const nsID& aId) const
  215. {
  216. MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
  217. return mId.Equals(aId);
  218. }
  219. bool
  220. ReadStream::Inner::HasEverBeenRead() const
  221. {
  222. MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
  223. return mHasEverBeenRead;
  224. }
  225. nsresult
  226. ReadStream::Inner::Close()
  227. {
  228. // stream ops can happen on any thread
  229. nsresult rv = NS_OK;
  230. {
  231. MutexAutoLock lock(mMutex);
  232. rv = mSnappyStream->Close();
  233. }
  234. NoteClosed();
  235. return rv;
  236. }
  237. nsresult
  238. ReadStream::Inner::Available(uint64_t* aNumAvailableOut)
  239. {
  240. // stream ops can happen on any thread
  241. nsresult rv = NS_OK;
  242. {
  243. MutexAutoLock lock(mMutex);
  244. rv = mSnappyStream->Available(aNumAvailableOut);
  245. }
  246. if (NS_FAILED(rv)) {
  247. Close();
  248. }
  249. return rv;
  250. }
  251. nsresult
  252. ReadStream::Inner::Read(char* aBuf, uint32_t aCount, uint32_t* aNumReadOut)
  253. {
  254. // stream ops can happen on any thread
  255. MOZ_DIAGNOSTIC_ASSERT(aNumReadOut);
  256. nsresult rv = NS_OK;
  257. {
  258. MutexAutoLock lock(mMutex);
  259. rv = mSnappyStream->Read(aBuf, aCount, aNumReadOut);
  260. }
  261. if ((NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) ||
  262. *aNumReadOut == 0) {
  263. Close();
  264. }
  265. mHasEverBeenRead = true;
  266. return rv;
  267. }
  268. nsresult
  269. ReadStream::Inner::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
  270. uint32_t aCount, uint32_t* aNumReadOut)
  271. {
  272. // stream ops can happen on any thread
  273. MOZ_DIAGNOSTIC_ASSERT(aNumReadOut);
  274. if (aCount) {
  275. mHasEverBeenRead = true;
  276. }
  277. nsresult rv = NS_OK;
  278. {
  279. MutexAutoLock lock(mMutex);
  280. rv = mSnappyStream->ReadSegments(aWriter, aClosure, aCount, aNumReadOut);
  281. }
  282. if ((NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK &&
  283. rv != NS_ERROR_NOT_IMPLEMENTED) || *aNumReadOut == 0) {
  284. Close();
  285. }
  286. // Verify bytes were actually read before marking as being ever read. For
  287. // example, code can test if the stream supports ReadSegments() by calling
  288. // this method with a dummy callback which doesn't read anything. We don't
  289. // want to trigger on that.
  290. if (*aNumReadOut) {
  291. mHasEverBeenRead = true;
  292. }
  293. return rv;
  294. }
  295. nsresult
  296. ReadStream::Inner::IsNonBlocking(bool* aNonBlockingOut)
  297. {
  298. // stream ops can happen on any thread
  299. MutexAutoLock lock(mMutex);
  300. return mSnappyStream->IsNonBlocking(aNonBlockingOut);
  301. }
  302. ReadStream::Inner::~Inner()
  303. {
  304. // Any thread
  305. MOZ_DIAGNOSTIC_ASSERT(mState == Closed);
  306. MOZ_DIAGNOSTIC_ASSERT(!mControl);
  307. }
  308. void
  309. ReadStream::Inner::NoteClosed()
  310. {
  311. // Any thread
  312. if (mState == Closed) {
  313. return;
  314. }
  315. if (NS_GetCurrentThread() == mOwningThread) {
  316. NoteClosedOnOwningThread();
  317. return;
  318. }
  319. nsCOMPtr<nsIRunnable> runnable = new NoteClosedRunnable(this);
  320. MOZ_ALWAYS_SUCCEEDS(
  321. mOwningThread->Dispatch(runnable, nsIThread::DISPATCH_NORMAL));
  322. }
  323. void
  324. ReadStream::Inner::Forget()
  325. {
  326. // Any thread
  327. if (mState == Closed) {
  328. return;
  329. }
  330. if (NS_GetCurrentThread() == mOwningThread) {
  331. ForgetOnOwningThread();
  332. return;
  333. }
  334. nsCOMPtr<nsIRunnable> runnable = new ForgetRunnable(this);
  335. MOZ_ALWAYS_SUCCEEDS(
  336. mOwningThread->Dispatch(runnable, nsIThread::DISPATCH_NORMAL));
  337. }
  338. void
  339. ReadStream::Inner::NoteClosedOnOwningThread()
  340. {
  341. MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
  342. // Mark closed and do nothing if we were already closed
  343. if (!mState.compareExchange(Open, Closed)) {
  344. return;
  345. }
  346. MOZ_DIAGNOSTIC_ASSERT(mControl);
  347. mControl->NoteClosed(this, mId);
  348. mControl = nullptr;
  349. }
  350. void
  351. ReadStream::Inner::ForgetOnOwningThread()
  352. {
  353. MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
  354. // Mark closed and do nothing if we were already closed
  355. if (!mState.compareExchange(Open, Closed)) {
  356. return;
  357. }
  358. MOZ_DIAGNOSTIC_ASSERT(mControl);
  359. mControl->ForgetReadStream(this);
  360. mControl = nullptr;
  361. }
  362. // ----------------------------------------------------------------------------
  363. NS_IMPL_ISUPPORTS(cache::ReadStream, nsIInputStream, ReadStream);
  364. // static
  365. already_AddRefed<ReadStream>
  366. ReadStream::Create(const CacheReadStreamOrVoid& aReadStreamOrVoid)
  367. {
  368. if (aReadStreamOrVoid.type() == CacheReadStreamOrVoid::Tvoid_t) {
  369. return nullptr;
  370. }
  371. return Create(aReadStreamOrVoid.get_CacheReadStream());
  372. }
  373. // static
  374. already_AddRefed<ReadStream>
  375. ReadStream::Create(const CacheReadStream& aReadStream)
  376. {
  377. // The parameter may or may not be for a Cache created stream. The way we
  378. // tell is by looking at the stream control actor. If the actor exists,
  379. // then we know the Cache created it.
  380. if (!aReadStream.controlChild() && !aReadStream.controlParent()) {
  381. return nullptr;
  382. }
  383. MOZ_DIAGNOSTIC_ASSERT(aReadStream.stream().type() ==
  384. IPCStream::TInputStreamParamsWithFds);
  385. // Control is guaranteed to survive this method as ActorDestroy() cannot
  386. // run on this thread until we complete.
  387. StreamControl* control;
  388. if (aReadStream.controlChild()) {
  389. auto actor = static_cast<CacheStreamControlChild*>(aReadStream.controlChild());
  390. control = actor;
  391. } else {
  392. auto actor = static_cast<CacheStreamControlParent*>(aReadStream.controlParent());
  393. control = actor;
  394. }
  395. MOZ_DIAGNOSTIC_ASSERT(control);
  396. nsCOMPtr<nsIInputStream> stream = DeserializeIPCStream(aReadStream.stream());
  397. MOZ_DIAGNOSTIC_ASSERT(stream);
  398. // Currently we expect all cache read streams to be blocking file streams.
  399. #if !defined(RELEASE_OR_BETA)
  400. nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(stream);
  401. MOZ_DIAGNOSTIC_ASSERT(!asyncStream);
  402. #endif
  403. RefPtr<Inner> inner = new Inner(control, aReadStream.id(), stream);
  404. RefPtr<ReadStream> ref = new ReadStream(inner);
  405. return ref.forget();
  406. }
  407. // static
  408. already_AddRefed<ReadStream>
  409. ReadStream::Create(PCacheStreamControlParent* aControl, const nsID& aId,
  410. nsIInputStream* aStream)
  411. {
  412. MOZ_DIAGNOSTIC_ASSERT(aControl);
  413. auto actor = static_cast<CacheStreamControlParent*>(aControl);
  414. RefPtr<Inner> inner = new Inner(actor, aId, aStream);
  415. RefPtr<ReadStream> ref = new ReadStream(inner);
  416. return ref.forget();
  417. }
  418. void
  419. ReadStream::Serialize(CacheReadStreamOrVoid* aReadStreamOut,
  420. nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
  421. ErrorResult& aRv)
  422. {
  423. mInner->Serialize(aReadStreamOut, aStreamCleanupList, aRv);
  424. }
  425. void
  426. ReadStream::Serialize(CacheReadStream* aReadStreamOut,
  427. nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
  428. ErrorResult& aRv)
  429. {
  430. mInner->Serialize(aReadStreamOut, aStreamCleanupList, aRv);
  431. }
  432. ReadStream::ReadStream(ReadStream::Inner* aInner)
  433. : mInner(aInner)
  434. {
  435. MOZ_DIAGNOSTIC_ASSERT(mInner);
  436. }
  437. ReadStream::~ReadStream()
  438. {
  439. // Explicitly close the inner stream so that it does not have to
  440. // deal with implicitly closing at destruction time.
  441. mInner->Close();
  442. }
  443. NS_IMETHODIMP
  444. ReadStream::Close()
  445. {
  446. return mInner->Close();
  447. }
  448. NS_IMETHODIMP
  449. ReadStream::Available(uint64_t* aNumAvailableOut)
  450. {
  451. return mInner->Available(aNumAvailableOut);
  452. }
  453. NS_IMETHODIMP
  454. ReadStream::Read(char* aBuf, uint32_t aCount, uint32_t* aNumReadOut)
  455. {
  456. return mInner->Read(aBuf, aCount, aNumReadOut);
  457. }
  458. NS_IMETHODIMP
  459. ReadStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
  460. uint32_t aCount, uint32_t* aNumReadOut)
  461. {
  462. return mInner->ReadSegments(aWriter, aClosure, aCount, aNumReadOut);
  463. }
  464. NS_IMETHODIMP
  465. ReadStream::IsNonBlocking(bool* aNonBlockingOut)
  466. {
  467. return mInner->IsNonBlocking(aNonBlockingOut);
  468. }
  469. } // namespace cache
  470. } // namespace dom
  471. } // namespace mozilla