2013-12-18 07:59:11 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-05-20 12:17:40 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-12-18 07:59:11 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2017-11-07 02:57:34 +03:00
|
|
|
#include "mozilla/Services.h"
|
2016-05-20 12:17:40 +03:00
|
|
|
|
2013-12-18 07:59:11 +04:00
|
|
|
#include "MediaDecoder.h"
|
2016-05-20 12:17:40 +03:00
|
|
|
#include "MediaShutdownManager.h"
|
2013-12-18 07:59:11 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-05-08 11:09:03 +03:00
|
|
|
#undef LOGW
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
extern LazyLogModule gMediaDecoderLog;
|
2015-05-21 23:22:04 +03:00
|
|
|
#define DECODER_LOG(type, msg) MOZ_LOG(gMediaDecoderLog, type, msg)
|
2017-05-08 11:09:03 +03:00
|
|
|
#define LOGW(...) NS_WARNING(nsPrintfCString(__VA_ARGS__).get())
|
2014-02-18 02:53:53 +04:00
|
|
|
|
2016-05-20 12:17:40 +03:00
|
|
|
NS_IMPL_ISUPPORTS(MediaShutdownManager, nsIAsyncShutdownBlocker)
|
2013-12-18 07:59:11 +04:00
|
|
|
|
|
|
|
MediaShutdownManager::MediaShutdownManager()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-19 12:09:56 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sInitPhase == NotInited);
|
2013-12-18 07:59:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaShutdownManager::~MediaShutdownManager()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we don't use ClearOnShutdown() on this StaticRefPtr, as that
|
|
|
|
// may interfere with our shutdown listener.
|
|
|
|
StaticRefPtr<MediaShutdownManager> MediaShutdownManager::sInstance;
|
|
|
|
|
2017-06-19 12:09:56 +03:00
|
|
|
MediaShutdownManager::InitPhase MediaShutdownManager::sInitPhase = MediaShutdownManager::NotInited;
|
|
|
|
|
2013-12-18 07:59:11 +04:00
|
|
|
MediaShutdownManager&
|
|
|
|
MediaShutdownManager::Instance()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-19 12:14:06 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
if (!sInstance) {
|
|
|
|
MOZ_CRASH_UNSAFE_PRINTF("sInstance is null. sInitPhase=%d", int(sInitPhase));
|
|
|
|
}
|
|
|
|
#endif
|
2013-12-18 07:59:11 +04:00
|
|
|
return *sInstance;
|
|
|
|
}
|
|
|
|
|
2016-05-20 12:17:40 +03:00
|
|
|
static nsCOMPtr<nsIAsyncShutdownClient>
|
|
|
|
GetShutdownBarrier()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdown();
|
|
|
|
MOZ_RELEASE_ASSERT(svc);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncShutdownClient> barrier;
|
|
|
|
nsresult rv = svc->GetProfileBeforeChange(getter_AddRefs(barrier));
|
|
|
|
if (!barrier) {
|
2016-06-02 01:05:53 +03:00
|
|
|
// We are probably in a content process. We need to do cleanup at
|
|
|
|
// XPCOM shutdown in leakchecking builds.
|
|
|
|
rv = svc->GetXpcomWillShutdown(getter_AddRefs(barrier));
|
2016-05-20 12:17:40 +03:00
|
|
|
}
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
MOZ_RELEASE_ASSERT(barrier);
|
|
|
|
return barrier.forget();
|
|
|
|
}
|
|
|
|
|
2017-02-03 10:12:22 +03:00
|
|
|
void
|
|
|
|
MediaShutdownManager::InitStatics()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-19 12:09:56 +03:00
|
|
|
if (sInitPhase != NotInited) {
|
2017-02-03 10:12:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sInstance = new MediaShutdownManager();
|
2017-06-19 12:09:56 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sInstance);
|
2017-02-03 10:12:22 +03:00
|
|
|
|
|
|
|
nsresult rv = GetShutdownBarrier()->AddBlocker(
|
|
|
|
sInstance, NS_LITERAL_STRING(__FILE__), __LINE__,
|
|
|
|
NS_LITERAL_STRING("MediaShutdownManager shutdown"));
|
|
|
|
if (NS_FAILED(rv)) {
|
2017-05-08 11:09:03 +03:00
|
|
|
LOGW("Failed to add shutdown blocker! rv=%x", uint32_t(rv));
|
2017-06-19 12:09:56 +03:00
|
|
|
sInitPhase = InitFailed;
|
|
|
|
return;
|
2017-02-03 10:12:22 +03:00
|
|
|
}
|
2017-06-19 12:09:56 +03:00
|
|
|
sInitPhase = InitSucceeded;
|
2017-02-03 10:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaShutdownManager::RemoveBlocker()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-19 12:09:56 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sInitPhase == XPCOMShutdownStarted);
|
2017-02-03 10:12:22 +03:00
|
|
|
MOZ_ASSERT(mDecoders.Count() == 0);
|
|
|
|
GetShutdownBarrier()->RemoveBlocker(this);
|
|
|
|
// Clear our singleton reference. This will probably delete
|
|
|
|
// this instance, so don't deref |this| clearing sInstance.
|
2017-06-19 12:09:56 +03:00
|
|
|
sInitPhase = XPCOMShutdownEnded;
|
2017-02-03 10:12:22 +03:00
|
|
|
sInstance = nullptr;
|
|
|
|
DECODER_LOG(LogLevel::Debug, ("MediaShutdownManager::BlockShutdown() end."));
|
|
|
|
}
|
|
|
|
|
2017-03-02 11:08:28 +03:00
|
|
|
nsresult
|
2013-12-18 07:59:11 +04:00
|
|
|
MediaShutdownManager::Register(MediaDecoder* aDecoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-19 12:09:56 +03:00
|
|
|
if (sInitPhase == InitFailed) {
|
2017-05-08 11:09:03 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2017-06-19 12:09:56 +03:00
|
|
|
if (sInitPhase == XPCOMShutdownStarted) {
|
2017-03-02 11:08:28 +03:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2013-12-18 07:59:11 +04:00
|
|
|
// Don't call Register() after you've Unregistered() all the decoders,
|
|
|
|
// that's not going to work.
|
|
|
|
MOZ_ASSERT(!mDecoders.Contains(aDecoder));
|
|
|
|
mDecoders.PutEntry(aDecoder);
|
|
|
|
MOZ_ASSERT(mDecoders.Contains(aDecoder));
|
|
|
|
MOZ_ASSERT(mDecoders.Count() > 0);
|
2017-03-02 11:08:28 +03:00
|
|
|
return NS_OK;
|
2013-12-18 07:59:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaShutdownManager::Unregister(MediaDecoder* aDecoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-03-02 11:08:28 +03:00
|
|
|
if (!mDecoders.Contains(aDecoder)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-04 11:13:25 +03:00
|
|
|
mDecoders.RemoveEntry(aDecoder);
|
2017-06-19 12:09:56 +03:00
|
|
|
if (sInitPhase == XPCOMShutdownStarted && mDecoders.Count() == 0) {
|
2017-02-03 10:12:22 +03:00
|
|
|
RemoveBlocker();
|
|
|
|
}
|
2013-12-18 07:59:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-05-20 12:17:40 +03:00
|
|
|
MediaShutdownManager::GetName(nsAString& aName)
|
2013-12-18 07:59:11 +04:00
|
|
|
{
|
2016-05-20 12:17:40 +03:00
|
|
|
aName = NS_LITERAL_STRING("MediaShutdownManager: shutdown");
|
2013-12-18 07:59:11 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-20 12:17:40 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaShutdownManager::GetState(nsIPropertyBag**)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaShutdownManager::BlockShutdown(nsIAsyncShutdownClient*)
|
2013-12-18 07:59:11 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-19 12:09:56 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sInitPhase == InitSucceeded);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sInstance);
|
2013-12-18 07:59:11 +04:00
|
|
|
|
2016-05-24 06:47:23 +03:00
|
|
|
DECODER_LOG(LogLevel::Debug, ("MediaShutdownManager::BlockShutdown() start..."));
|
2014-02-18 02:53:53 +04:00
|
|
|
|
2016-05-04 11:13:25 +03:00
|
|
|
// Set this flag to ensure no Register() is allowed when Shutdown() begins.
|
2017-06-19 12:09:56 +03:00
|
|
|
sInitPhase = XPCOMShutdownStarted;
|
2013-12-18 07:59:11 +04:00
|
|
|
|
2017-02-03 10:12:22 +03:00
|
|
|
auto oldCount = mDecoders.Count();
|
|
|
|
if (oldCount == 0) {
|
|
|
|
RemoveBlocker();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-05-04 11:13:25 +03:00
|
|
|
|
|
|
|
// Iterate over the decoders and shut them down.
|
2015-07-16 20:59:54 +03:00
|
|
|
for (auto iter = mDecoders.Iter(); !iter.Done(); iter.Next()) {
|
2017-02-03 12:12:38 +03:00
|
|
|
iter.Get()->GetKey()->NotifyXPCOMShutdown();
|
2016-05-04 11:13:25 +03:00
|
|
|
// Check MediaDecoder::Shutdown doesn't call Unregister() synchronously in
|
|
|
|
// order not to corrupt our hashtable traversal.
|
|
|
|
MOZ_ASSERT(mDecoders.Count() == oldCount);
|
2015-07-16 20:59:54 +03:00
|
|
|
}
|
2014-02-18 02:53:53 +04:00
|
|
|
|
2016-05-20 12:17:40 +03:00
|
|
|
return NS_OK;
|
2013-12-18 07:59:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
2017-05-08 11:09:03 +03:00
|
|
|
|
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef LOGW
|