MediaShutdownManager.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et cindent: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include "mozilla/Logging.h"
  7. #include "mozilla/StaticPtr.h"
  8. #include "nsContentUtils.h"
  9. #include "MediaDecoder.h"
  10. #include "MediaShutdownManager.h"
  11. namespace mozilla {
  12. extern LazyLogModule gMediaDecoderLog;
  13. #define DECODER_LOG(type, msg) MOZ_LOG(gMediaDecoderLog, type, msg)
  14. NS_IMPL_ISUPPORTS(MediaShutdownManager, nsIAsyncShutdownBlocker)
  15. MediaShutdownManager::MediaShutdownManager()
  16. {
  17. MOZ_ASSERT(NS_IsMainThread());
  18. MOZ_COUNT_CTOR(MediaShutdownManager);
  19. }
  20. MediaShutdownManager::~MediaShutdownManager()
  21. {
  22. MOZ_ASSERT(NS_IsMainThread());
  23. MOZ_COUNT_DTOR(MediaShutdownManager);
  24. }
  25. // Note that we don't use ClearOnShutdown() on this StaticRefPtr, as that
  26. // may interfere with our shutdown listener.
  27. StaticRefPtr<MediaShutdownManager> MediaShutdownManager::sInstance;
  28. MediaShutdownManager&
  29. MediaShutdownManager::Instance()
  30. {
  31. MOZ_ASSERT(NS_IsMainThread());
  32. MOZ_DIAGNOSTIC_ASSERT(sInstance);
  33. return *sInstance;
  34. }
  35. static nsCOMPtr<nsIAsyncShutdownClient>
  36. GetShutdownBarrier()
  37. {
  38. nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdown();
  39. MOZ_RELEASE_ASSERT(svc);
  40. nsCOMPtr<nsIAsyncShutdownClient> barrier;
  41. nsresult rv = svc->GetProfileBeforeChange(getter_AddRefs(barrier));
  42. if (!barrier) {
  43. // We are probably in a content process. We need to do cleanup at
  44. // XPCOM shutdown in leakchecking builds.
  45. rv = svc->GetXpcomWillShutdown(getter_AddRefs(barrier));
  46. }
  47. MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
  48. MOZ_RELEASE_ASSERT(barrier);
  49. return barrier.forget();
  50. }
  51. void
  52. MediaShutdownManager::InitStatics()
  53. {
  54. MOZ_ASSERT(NS_IsMainThread());
  55. static bool sInitDone = false;
  56. if (sInitDone) {
  57. return;
  58. }
  59. sInitDone = true;
  60. sInstance = new MediaShutdownManager();
  61. nsresult rv = GetShutdownBarrier()->AddBlocker(
  62. sInstance, NS_LITERAL_STRING(__FILE__), __LINE__,
  63. NS_LITERAL_STRING("MediaShutdownManager shutdown"));
  64. if (NS_FAILED(rv)) {
  65. MOZ_CRASH_UNSAFE_PRINTF("Failed to add shutdown blocker! rv=%x", uint32_t(rv));
  66. }
  67. }
  68. void
  69. MediaShutdownManager::RemoveBlocker()
  70. {
  71. MOZ_ASSERT(NS_IsMainThread());
  72. MOZ_ASSERT(mIsDoingXPCOMShutDown);
  73. MOZ_ASSERT(mDecoders.Count() == 0);
  74. GetShutdownBarrier()->RemoveBlocker(this);
  75. // Clear our singleton reference. This will probably delete
  76. // this instance, so don't deref |this| clearing sInstance.
  77. sInstance = nullptr;
  78. DECODER_LOG(LogLevel::Debug, ("MediaShutdownManager::BlockShutdown() end."));
  79. }
  80. nsresult
  81. MediaShutdownManager::Register(MediaDecoder* aDecoder)
  82. {
  83. MOZ_ASSERT(NS_IsMainThread());
  84. if (mIsDoingXPCOMShutDown) {
  85. return NS_ERROR_ABORT;
  86. }
  87. // Don't call Register() after you've Unregistered() all the decoders,
  88. // that's not going to work.
  89. MOZ_ASSERT(!mDecoders.Contains(aDecoder));
  90. mDecoders.PutEntry(aDecoder);
  91. MOZ_ASSERT(mDecoders.Contains(aDecoder));
  92. MOZ_ASSERT(mDecoders.Count() > 0);
  93. return NS_OK;
  94. }
  95. void
  96. MediaShutdownManager::Unregister(MediaDecoder* aDecoder)
  97. {
  98. MOZ_ASSERT(NS_IsMainThread());
  99. if (!mDecoders.Contains(aDecoder)) {
  100. return;
  101. }
  102. mDecoders.RemoveEntry(aDecoder);
  103. if (mIsDoingXPCOMShutDown && mDecoders.Count() == 0) {
  104. RemoveBlocker();
  105. }
  106. }
  107. NS_IMETHODIMP
  108. MediaShutdownManager::GetName(nsAString& aName)
  109. {
  110. aName = NS_LITERAL_STRING("MediaShutdownManager: shutdown");
  111. return NS_OK;
  112. }
  113. NS_IMETHODIMP
  114. MediaShutdownManager::GetState(nsIPropertyBag**)
  115. {
  116. return NS_OK;
  117. }
  118. NS_IMETHODIMP
  119. MediaShutdownManager::BlockShutdown(nsIAsyncShutdownClient*)
  120. {
  121. MOZ_ASSERT(NS_IsMainThread());
  122. MOZ_ASSERT(sInstance);
  123. DECODER_LOG(LogLevel::Debug, ("MediaShutdownManager::BlockShutdown() start..."));
  124. // Set this flag to ensure no Register() is allowed when Shutdown() begins.
  125. mIsDoingXPCOMShutDown = true;
  126. auto oldCount = mDecoders.Count();
  127. if (oldCount == 0) {
  128. RemoveBlocker();
  129. return NS_OK;
  130. }
  131. // Iterate over the decoders and shut them down.
  132. for (auto iter = mDecoders.Iter(); !iter.Done(); iter.Next()) {
  133. iter.Get()->GetKey()->NotifyXPCOMShutdown();
  134. // Check MediaDecoder::Shutdown doesn't call Unregister() synchronously in
  135. // order not to corrupt our hashtable traversal.
  136. MOZ_ASSERT(mDecoders.Count() == oldCount);
  137. }
  138. return NS_OK;
  139. }
  140. } // namespace mozilla