CacheStorageChild.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/CacheStorageChild.h"
  6. #include "mozilla/Unused.h"
  7. #include "mozilla/dom/cache/CacheChild.h"
  8. #include "mozilla/dom/cache/CacheOpChild.h"
  9. #include "mozilla/dom/cache/CacheStorage.h"
  10. namespace mozilla {
  11. namespace dom {
  12. namespace cache {
  13. // declared in ActorUtils.h
  14. void
  15. DeallocPCacheStorageChild(PCacheStorageChild* aActor)
  16. {
  17. delete aActor;
  18. }
  19. CacheStorageChild::CacheStorageChild(CacheStorage* aListener,
  20. CacheWorkerHolder* aWorkerHolder)
  21. : mListener(aListener)
  22. , mNumChildActors(0)
  23. , mDelayedDestroy(false)
  24. {
  25. MOZ_COUNT_CTOR(cache::CacheStorageChild);
  26. MOZ_DIAGNOSTIC_ASSERT(mListener);
  27. SetWorkerHolder(aWorkerHolder);
  28. }
  29. CacheStorageChild::~CacheStorageChild()
  30. {
  31. MOZ_COUNT_DTOR(cache::CacheStorageChild);
  32. NS_ASSERT_OWNINGTHREAD(CacheStorageChild);
  33. MOZ_DIAGNOSTIC_ASSERT(!mListener);
  34. }
  35. void
  36. CacheStorageChild::ClearListener()
  37. {
  38. NS_ASSERT_OWNINGTHREAD(CacheStorageChild);
  39. MOZ_DIAGNOSTIC_ASSERT(mListener);
  40. mListener = nullptr;
  41. }
  42. void
  43. CacheStorageChild::ExecuteOp(nsIGlobalObject* aGlobal, Promise* aPromise,
  44. nsISupports* aParent, const CacheOpArgs& aArgs)
  45. {
  46. mNumChildActors += 1;
  47. Unused << SendPCacheOpConstructor(
  48. new CacheOpChild(GetWorkerHolder(), aGlobal, aParent, aPromise), aArgs);
  49. }
  50. void
  51. CacheStorageChild::StartDestroyFromListener()
  52. {
  53. NS_ASSERT_OWNINGTHREAD(CacheStorageChild);
  54. // The listener should be held alive by any async operations, so if it
  55. // is going away then there must not be any child actors. This in turn
  56. // ensures that StartDestroy() will not trigger the delayed path.
  57. MOZ_DIAGNOSTIC_ASSERT(!mNumChildActors);
  58. StartDestroy();
  59. }
  60. void
  61. CacheStorageChild::StartDestroy()
  62. {
  63. NS_ASSERT_OWNINGTHREAD(CacheStorageChild);
  64. // If we have outstanding child actors, then don't destroy ourself yet.
  65. // The child actors should be short lived and we should allow them to complete
  66. // if possible. NoteDeletedActor() will call back into this Shutdown()
  67. // method when the last child actor is gone.
  68. if (mNumChildActors) {
  69. mDelayedDestroy = true;
  70. return;
  71. }
  72. RefPtr<CacheStorage> listener = mListener;
  73. // StartDestroy() can get called from either CacheStorage or the
  74. // CacheWorkerHolder.
  75. // Theoretically we can get double called if the right race happens. Handle
  76. // that by just ignoring the second StartDestroy() call.
  77. if (!listener) {
  78. return;
  79. }
  80. listener->DestroyInternal(this);
  81. // CacheStorage listener should call ClearListener() in DestroyInternal()
  82. MOZ_DIAGNOSTIC_ASSERT(!mListener);
  83. // Start actor destruction from parent process
  84. Unused << SendTeardown();
  85. }
  86. void
  87. CacheStorageChild::ActorDestroy(ActorDestroyReason aReason)
  88. {
  89. NS_ASSERT_OWNINGTHREAD(CacheStorageChild);
  90. RefPtr<CacheStorage> listener = mListener;
  91. if (listener) {
  92. listener->DestroyInternal(this);
  93. // CacheStorage listener should call ClearListener() in DestroyInternal()
  94. MOZ_DIAGNOSTIC_ASSERT(!mListener);
  95. }
  96. RemoveWorkerHolder();
  97. }
  98. PCacheOpChild*
  99. CacheStorageChild::AllocPCacheOpChild(const CacheOpArgs& aOpArgs)
  100. {
  101. MOZ_CRASH("CacheOpChild should be manually constructed.");
  102. return nullptr;
  103. }
  104. bool
  105. CacheStorageChild::DeallocPCacheOpChild(PCacheOpChild* aActor)
  106. {
  107. delete aActor;
  108. NoteDeletedActor();
  109. return true;
  110. }
  111. void
  112. CacheStorageChild::NoteDeletedActor()
  113. {
  114. MOZ_DIAGNOSTIC_ASSERT(mNumChildActors);
  115. mNumChildActors -= 1;
  116. if (!mNumChildActors && mDelayedDestroy) {
  117. StartDestroy();
  118. }
  119. }
  120. } // namespace cache
  121. } // namespace dom
  122. } // namespace mozilla