Bug 1070340 (Part 1) - Add ShutdownTracker to imagelib. r=tn

This commit is contained in:
Seth Fowler 2014-09-23 15:32:16 -07:00
parent cded590dd2
commit d0a096f255
4 changed files with 125 additions and 0 deletions

View File

@ -11,6 +11,7 @@
#include "ImageFactory.h"
#include "RasterImage.h"
#include "ShutdownTracker.h"
#include "SurfaceCache.h"
#include "imgLoader.h"
@ -85,6 +86,7 @@ static bool sInitialized = false;
nsresult
mozilla::image::InitModule()
{
mozilla::image::ShutdownTracker::Initialize();
mozilla::image::DiscardTracker::Initialize();
mozilla::image::ImageFactory::Initialize();
mozilla::image::RasterImage::Initialize();

View File

@ -0,0 +1,76 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "ShutdownTracker.h"
#include "mozilla/Services.h"
#include "nsAutoPtr.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
namespace mozilla {
namespace image {
class ShutdownTrackerImpl;
///////////////////////////////////////////////////////////////////////////////
// Static Data
///////////////////////////////////////////////////////////////////////////////
// Whether we've observed shutdown starting yet.
static bool sShutdownHasStarted = false;
///////////////////////////////////////////////////////////////////////////////
// Implementation
///////////////////////////////////////////////////////////////////////////////
struct ShutdownObserver : public nsIObserver
{
NS_DECL_ISUPPORTS
NS_IMETHOD Observe(nsISupports*, const char* aTopic, const char16_t*)
{
if (strcmp(aTopic, "xpcom-shutdown") != 0) {
return NS_OK;
}
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
if (os) {
os->RemoveObserver(this, "xpcom-shutdown");
}
sShutdownHasStarted = true;
return NS_OK;
}
private:
virtual ~ShutdownObserver() { }
};
NS_IMPL_ISUPPORTS(ShutdownObserver, nsIObserver)
///////////////////////////////////////////////////////////////////////////////
// Public API
///////////////////////////////////////////////////////////////////////////////
/* static */ void
ShutdownTracker::Initialize()
{
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
if (os) {
os->AddObserver(new ShutdownObserver, "xpcom-shutdown", false);
}
}
/* static */ bool
ShutdownTracker::ShutdownHasStarted()
{
return sShutdownHasStarted;
}
} // namespace image
} // namespace mozilla

View File

@ -0,0 +1,46 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
/**
* ShutdownTracker is an imagelib-global service that allows callers to check
* whether shutdown has started.
*/
#ifndef MOZILLA_IMAGELIB_SHUTDOWNTRACKER_H_
#define MOZILLA_IMAGELIB_SHUTDOWNTRACKER_H_
namespace mozilla {
namespace image {
/**
* ShutdownTracker is an imagelib-global service that allows callers to check
* whether shutdown has started. It exists to avoid the need for registering many
* 'xpcom-shutdown' notification observers on short-lived objects, which would
* have an unnecessary performance cost.
*/
struct ShutdownTracker
{
/**
* Initialize static data. Called during imagelib module initialization.
*/
static void Initialize();
/**
* Check whether shutdown has started. Callers can use this to check whether
* it's safe to access XPCOM services; if shutdown has started, such calls
* must be avoided.
*
* @return true if shutdown has already started.
*/
static bool ShutdownHasStarted();
private:
virtual ~ShutdownTracker() = 0; // Forbid instantiation.
};
} // namespace image
} // namespace mozilla
#endif // MOZILLA_IMAGELIB_SHUTDOWNTRACKER_H_

View File

@ -32,6 +32,7 @@ UNIFIED_SOURCES += [
'imgTools.cpp',
'OrientedImage.cpp',
'ScriptedNotificationObserver.cpp',
'ShutdownTracker.cpp',
'SurfaceCache.cpp',
'SVGDocumentWrapper.cpp',
'VectorImage.cpp',