CacheWorkerHolder.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/CacheWorkerHolder.h"
  6. #include "mozilla/dom/cache/ActorChild.h"
  7. #include "WorkerPrivate.h"
  8. namespace mozilla {
  9. namespace dom {
  10. namespace cache {
  11. using mozilla::dom::workers::Terminating;
  12. using mozilla::dom::workers::Status;
  13. using mozilla::dom::workers::WorkerPrivate;
  14. // static
  15. already_AddRefed<CacheWorkerHolder>
  16. CacheWorkerHolder::Create(WorkerPrivate* aWorkerPrivate)
  17. {
  18. MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
  19. RefPtr<CacheWorkerHolder> workerHolder = new CacheWorkerHolder();
  20. if (NS_WARN_IF(!workerHolder->HoldWorker(aWorkerPrivate, Terminating))) {
  21. return nullptr;
  22. }
  23. return workerHolder.forget();
  24. }
  25. void
  26. CacheWorkerHolder::AddActor(ActorChild* aActor)
  27. {
  28. NS_ASSERT_OWNINGTHREAD(CacheWorkerHolder);
  29. MOZ_DIAGNOSTIC_ASSERT(aActor);
  30. MOZ_ASSERT(!mActorList.Contains(aActor));
  31. mActorList.AppendElement(aActor);
  32. // Allow an actor to be added after we've entered the Notifying case. We
  33. // can't stop the actor creation from racing with out destruction of the
  34. // other actors and we need to wait for this extra one to close as well.
  35. // Signal it should destroy itself right away.
  36. if (mNotified) {
  37. aActor->StartDestroy();
  38. }
  39. }
  40. void
  41. CacheWorkerHolder::RemoveActor(ActorChild* aActor)
  42. {
  43. NS_ASSERT_OWNINGTHREAD(CacheWorkerHolder);
  44. MOZ_DIAGNOSTIC_ASSERT(aActor);
  45. #if defined(RELEASE_OR_BETA)
  46. mActorList.RemoveElement(aActor);
  47. #else
  48. MOZ_DIAGNOSTIC_ASSERT(mActorList.RemoveElement(aActor));
  49. #endif
  50. MOZ_ASSERT(!mActorList.Contains(aActor));
  51. }
  52. bool
  53. CacheWorkerHolder::Notified() const
  54. {
  55. return mNotified;
  56. }
  57. bool
  58. CacheWorkerHolder::Notify(Status aStatus)
  59. {
  60. NS_ASSERT_OWNINGTHREAD(CacheWorkerHolder);
  61. // When the service worker thread is stopped we will get Terminating,
  62. // but nothing higher than that. We must shut things down at Terminating.
  63. if (aStatus < Terminating || mNotified) {
  64. return true;
  65. }
  66. mNotified = true;
  67. // Start the asynchronous destruction of our actors. These will call back
  68. // into RemoveActor() once the actor is destroyed.
  69. for (uint32_t i = 0; i < mActorList.Length(); ++i) {
  70. MOZ_DIAGNOSTIC_ASSERT(mActorList[i]);
  71. mActorList[i]->StartDestroy();
  72. }
  73. return true;
  74. }
  75. CacheWorkerHolder::CacheWorkerHolder()
  76. : mNotified(false)
  77. {
  78. }
  79. CacheWorkerHolder::~CacheWorkerHolder()
  80. {
  81. NS_ASSERT_OWNINGTHREAD(CacheWorkerHolder);
  82. MOZ_DIAGNOSTIC_ASSERT(mActorList.IsEmpty());
  83. }
  84. } // namespace cache
  85. } // namespace dom
  86. } // namespace mozilla