CacheStreamControlChild.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/CacheStreamControlChild.h"
  6. #include "mozilla/Unused.h"
  7. #include "mozilla/dom/cache/ActorUtils.h"
  8. #include "mozilla/dom/cache/CacheTypes.h"
  9. #include "mozilla/dom/cache/ReadStream.h"
  10. #include "mozilla/ipc/FileDescriptorSetChild.h"
  11. #include "mozilla/ipc/IPCStreamUtils.h"
  12. #include "mozilla/ipc/PBackgroundChild.h"
  13. #include "mozilla/ipc/PFileDescriptorSetChild.h"
  14. #include "nsISupportsImpl.h"
  15. namespace mozilla {
  16. namespace dom {
  17. namespace cache {
  18. using mozilla::dom::OptionalFileDescriptorSet;
  19. using mozilla::ipc::AutoIPCStream;
  20. using mozilla::ipc::FileDescriptor;
  21. using mozilla::ipc::FileDescriptorSetChild;
  22. using mozilla::ipc::PFileDescriptorSetChild;
  23. // declared in ActorUtils.h
  24. PCacheStreamControlChild*
  25. AllocPCacheStreamControlChild()
  26. {
  27. return new CacheStreamControlChild();
  28. }
  29. // declared in ActorUtils.h
  30. void
  31. DeallocPCacheStreamControlChild(PCacheStreamControlChild* aActor)
  32. {
  33. delete aActor;
  34. }
  35. CacheStreamControlChild::CacheStreamControlChild()
  36. : mDestroyStarted(false)
  37. , mDestroyDelayed(false)
  38. {
  39. MOZ_COUNT_CTOR(cache::CacheStreamControlChild);
  40. }
  41. CacheStreamControlChild::~CacheStreamControlChild()
  42. {
  43. NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
  44. MOZ_COUNT_DTOR(cache::CacheStreamControlChild);
  45. }
  46. void
  47. CacheStreamControlChild::StartDestroy()
  48. {
  49. NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
  50. // This can get called twice under some circumstances. For example, if the
  51. // actor is added to a CacheWorkerHolder that has already been notified and
  52. // the Cache actor has no mListener.
  53. if (mDestroyStarted) {
  54. return;
  55. }
  56. mDestroyStarted = true;
  57. // If any of the streams have started to be read, then wait for them to close
  58. // naturally.
  59. if (HasEverBeenRead()) {
  60. // Note that we are delaying so that we can re-check for active streams
  61. // in NoteClosedAfterForget().
  62. mDestroyDelayed = true;
  63. return;
  64. }
  65. // Otherwise, if the streams have not been touched then just pre-emptively
  66. // close them now. This handles the case where someone retrieves a Response
  67. // from the Cache, but never accesses the body. We should not keep the
  68. // Worker alive until that Response is GC'd just because of its ignored
  69. // body stream.
  70. // Begin shutting down all streams. This is the same as if the parent had
  71. // asked us to shutdown. So simulate the CloseAll IPC message.
  72. RecvCloseAll();
  73. }
  74. void
  75. CacheStreamControlChild::SerializeControl(CacheReadStream* aReadStreamOut)
  76. {
  77. NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
  78. MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
  79. aReadStreamOut->controlParent() = nullptr;
  80. aReadStreamOut->controlChild() = this;
  81. }
  82. void
  83. CacheStreamControlChild::SerializeStream(CacheReadStream* aReadStreamOut,
  84. nsIInputStream* aStream,
  85. nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList)
  86. {
  87. NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
  88. MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
  89. MOZ_DIAGNOSTIC_ASSERT(aStream);
  90. UniquePtr<AutoIPCStream> autoStream(new AutoIPCStream(aReadStreamOut->stream()));
  91. autoStream->Serialize(aStream, Manager());
  92. aStreamCleanupList.AppendElement(Move(autoStream));
  93. }
  94. void
  95. CacheStreamControlChild::NoteClosedAfterForget(const nsID& aId)
  96. {
  97. NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
  98. Unused << SendNoteClosed(aId);
  99. // A stream has closed. If we delayed StartDestry() due to this stream
  100. // being read, then we should check to see if any of the remaining streams
  101. // are active. If none of our other streams have been read, then we can
  102. // proceed with the shutdown now.
  103. if (mDestroyDelayed && !HasEverBeenRead()) {
  104. mDestroyDelayed = false;
  105. RecvCloseAll();
  106. }
  107. }
  108. #ifdef DEBUG
  109. void
  110. CacheStreamControlChild::AssertOwningThread()
  111. {
  112. NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
  113. }
  114. #endif
  115. void
  116. CacheStreamControlChild::ActorDestroy(ActorDestroyReason aReason)
  117. {
  118. NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
  119. CloseAllReadStreamsWithoutReporting();
  120. RemoveWorkerHolder();
  121. }
  122. bool
  123. CacheStreamControlChild::RecvClose(const nsID& aId)
  124. {
  125. NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
  126. CloseReadStreams(aId);
  127. return true;
  128. }
  129. bool
  130. CacheStreamControlChild::RecvCloseAll()
  131. {
  132. NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
  133. CloseAllReadStreams();
  134. return true;
  135. }
  136. } // namespace cache
  137. } // namespace dom
  138. } // namespace mozilla