Bug 962440 - move gfxFontInfoLoader into separate file. r=bas

This commit is contained in:
John Daggett 2014-01-29 16:39:01 +09:00
parent da9c49b67e
commit 88bc3ce902
6 changed files with 208 additions and 184 deletions

View File

@ -0,0 +1,115 @@
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 "gfxFontInfoLoader.h"
#include "nsCRT.h"
#include "nsIObserverService.h"
using namespace mozilla;
using mozilla::services::GetObserverService;
NS_IMPL_ISUPPORTS1(gfxFontInfoLoader::ShutdownObserver, nsIObserver)
NS_IMETHODIMP
gfxFontInfoLoader::ShutdownObserver::Observe(nsISupports *aSubject,
const char *aTopic,
const char16_t *someData)
{
if (!nsCRT::strcmp(aTopic, "quit-application")) {
mLoader->CancelLoader();
} else {
NS_NOTREACHED("unexpected notification topic");
}
return NS_OK;
}
void
gfxFontInfoLoader::StartLoader(uint32_t aDelay, uint32_t aInterval)
{
mInterval = aInterval;
// sanity check
if (mState != stateInitial && mState != stateTimerOff) {
CancelLoader();
}
// set up timer
if (!mTimer) {
mTimer = do_CreateInstance("@mozilla.org/timer;1");
if (!mTimer) {
NS_WARNING("Failure to create font info loader timer");
return;
}
}
// need an initial delay?
uint32_t timerInterval;
if (aDelay) {
mState = stateTimerOnDelay;
timerInterval = aDelay;
} else {
mState = stateTimerOnInterval;
timerInterval = mInterval;
}
InitLoader();
// start timer
mTimer->InitWithFuncCallback(LoaderTimerCallback, this, timerInterval,
nsITimer::TYPE_REPEATING_SLACK);
nsCOMPtr<nsIObserverService> obs = GetObserverService();
if (obs) {
mObserver = new ShutdownObserver(this);
obs->AddObserver(mObserver, "quit-application", false);
}
}
void
gfxFontInfoLoader::CancelLoader()
{
if (mState == stateInitial) {
return;
}
mState = stateTimerOff;
if (mTimer) {
mTimer->Cancel();
mTimer = nullptr;
}
RemoveShutdownObserver();
FinishLoader();
}
void
gfxFontInfoLoader::LoaderTimerFire()
{
if (mState == stateTimerOnDelay) {
mState = stateTimerOnInterval;
mTimer->SetDelay(mInterval);
}
bool done = RunLoader();
if (done) {
CancelLoader();
}
}
gfxFontInfoLoader::~gfxFontInfoLoader()
{
RemoveShutdownObserver();
}
void
gfxFontInfoLoader::RemoveShutdownObserver()
{
if (mObserver) {
nsCOMPtr<nsIObserverService> obs = GetObserverService();
if (obs) {
obs->RemoveObserver(mObserver, "quit-application");
mObserver = nullptr;
}
}
}

View File

@ -0,0 +1,89 @@
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* 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/. */
#ifndef GFX_FONT_INFO_LOADER_H
#define GFX_FONT_INFO_LOADER_H
#include "nsCOMPtr.h"
#include "nsIObserver.h"
#include "nsITimer.h"
// helper class for loading in font info spaced out at regular intervals
class gfxFontInfoLoader {
public:
// state transitions:
// initial ---StartLoader with delay---> timer on delay
// initial ---StartLoader without delay---> timer on interval
// timer on delay ---LoaderTimerFire---> timer on interval
// timer on delay ---CancelLoader---> timer off
// timer on interval ---CancelLoader---> timer off
// timer off ---StartLoader with delay---> timer on delay
// timer off ---StartLoader without delay---> timer on interval
typedef enum {
stateInitial,
stateTimerOnDelay,
stateTimerOnInterval,
stateTimerOff
} TimerState;
gfxFontInfoLoader() :
mInterval(0), mState(stateInitial)
{
}
virtual ~gfxFontInfoLoader();
// start timer with an initial delay, then call Run method at regular intervals
void StartLoader(uint32_t aDelay, uint32_t aInterval);
// cancel the timer and cleanup
void CancelLoader();
protected:
class ShutdownObserver : public nsIObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
ShutdownObserver(gfxFontInfoLoader *aLoader)
: mLoader(aLoader)
{ }
virtual ~ShutdownObserver()
{ }
protected:
gfxFontInfoLoader *mLoader;
};
// Init - initialization at start time after initial delay
virtual void InitLoader() = 0;
// Run - called at intervals, return true to indicate done
virtual bool RunLoader() = 0;
// Finish - cleanup after done
virtual void FinishLoader() = 0;
// Timer interval callbacks
static void LoaderTimerCallback(nsITimer *aTimer, void *aThis) {
gfxFontInfoLoader *loader = static_cast<gfxFontInfoLoader*>(aThis);
loader->LoaderTimerFire();
}
void LoaderTimerFire();
void RemoveShutdownObserver();
nsCOMPtr<nsITimer> mTimer;
nsCOMPtr<nsIObserver> mObserver;
uint32_t mInterval;
TimerState mState;
};
#endif /* GFX_FONT_INFO_LOADER_H */

View File

@ -18,10 +18,9 @@
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "nsCOMPtr.h"
#include "nsIUUIDGenerator.h"
#include "nsIObserverService.h"
#include "nsIUnicodeDecoder.h"
#include "nsCRT.h"
#include "harfbuzz/hb.h"
@ -38,7 +37,6 @@
#define UNICODE_BMP_LIMIT 0x10000
using namespace mozilla;
using mozilla::services::GetObserverService;
#pragma pack(1)
@ -1433,106 +1431,3 @@ gfxFontUtils::IsCffFont(const uint8_t* aFontData)
#endif
NS_IMPL_ISUPPORTS1(gfxFontInfoLoader::ShutdownObserver, nsIObserver)
NS_IMETHODIMP
gfxFontInfoLoader::ShutdownObserver::Observe(nsISupports *aSubject,
const char *aTopic,
const char16_t *someData)
{
if (!nsCRT::strcmp(aTopic, "quit-application")) {
mLoader->CancelLoader();
} else {
NS_NOTREACHED("unexpected notification topic");
}
return NS_OK;
}
void
gfxFontInfoLoader::StartLoader(uint32_t aDelay, uint32_t aInterval)
{
mInterval = aInterval;
// sanity check
if (mState != stateInitial && mState != stateTimerOff) {
CancelLoader();
}
// set up timer
if (!mTimer) {
mTimer = do_CreateInstance("@mozilla.org/timer;1");
if (!mTimer) {
NS_WARNING("Failure to create font info loader timer");
return;
}
}
// need an initial delay?
uint32_t timerInterval;
if (aDelay) {
mState = stateTimerOnDelay;
timerInterval = aDelay;
} else {
mState = stateTimerOnInterval;
timerInterval = mInterval;
}
InitLoader();
// start timer
mTimer->InitWithFuncCallback(LoaderTimerCallback, this, timerInterval,
nsITimer::TYPE_REPEATING_SLACK);
nsCOMPtr<nsIObserverService> obs = GetObserverService();
if (obs) {
mObserver = new ShutdownObserver(this);
obs->AddObserver(mObserver, "quit-application", false);
}
}
void
gfxFontInfoLoader::CancelLoader()
{
if (mState == stateInitial) {
return;
}
mState = stateTimerOff;
if (mTimer) {
mTimer->Cancel();
mTimer = nullptr;
}
RemoveShutdownObserver();
FinishLoader();
}
void
gfxFontInfoLoader::LoaderTimerFire()
{
if (mState == stateTimerOnDelay) {
mState = stateTimerOnInterval;
mTimer->SetDelay(mInterval);
}
bool done = RunLoader();
if (done) {
CancelLoader();
}
}
gfxFontInfoLoader::~gfxFontInfoLoader()
{
RemoveShutdownObserver();
}
void
gfxFontInfoLoader::RemoveShutdownObserver()
{
if (mObserver) {
nsCOMPtr<nsIObserverService> obs = GetObserverService();
if (obs) {
obs->RemoveObserver(mObserver, "quit-application");
mObserver = nullptr;
}
}
}

View File

@ -7,12 +7,9 @@
#define GFX_FONT_UTILS_H
#include "gfxPlatform.h"
#include "nsITimer.h"
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsTArray.h"
#include "nsAutoPtr.h"
#include "nsIObserver.h"
#include "mozilla/Likely.h"
#include "mozilla/Endian.h"
#include "mozilla/MemoryReporting.h"
@ -943,80 +940,5 @@ protected:
static const char* gMSFontNameCharsets[];
};
// helper class for loading in font info spaced out at regular intervals
class gfxFontInfoLoader {
public:
// state transitions:
// initial ---StartLoader with delay---> timer on delay
// initial ---StartLoader without delay---> timer on interval
// timer on delay ---LoaderTimerFire---> timer on interval
// timer on delay ---CancelLoader---> timer off
// timer on interval ---CancelLoader---> timer off
// timer off ---StartLoader with delay---> timer on delay
// timer off ---StartLoader without delay---> timer on interval
typedef enum {
stateInitial,
stateTimerOnDelay,
stateTimerOnInterval,
stateTimerOff
} TimerState;
gfxFontInfoLoader() :
mInterval(0), mState(stateInitial)
{
}
virtual ~gfxFontInfoLoader();
// start timer with an initial delay, then call Run method at regular intervals
void StartLoader(uint32_t aDelay, uint32_t aInterval);
// cancel the timer and cleanup
void CancelLoader();
protected:
class ShutdownObserver : public nsIObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
ShutdownObserver(gfxFontInfoLoader *aLoader)
: mLoader(aLoader)
{ }
virtual ~ShutdownObserver()
{ }
protected:
gfxFontInfoLoader *mLoader;
};
// Init - initialization at start time after initial delay
virtual void InitLoader() = 0;
// Run - called at intervals, return true to indicate done
virtual bool RunLoader() = 0;
// Finish - cleanup after done
virtual void FinishLoader() = 0;
// Timer interval callbacks
static void LoaderTimerCallback(nsITimer *aTimer, void *aThis) {
gfxFontInfoLoader *loader = static_cast<gfxFontInfoLoader*>(aThis);
loader->LoaderTimerFire();
}
void LoaderTimerFire();
void RemoveShutdownObserver();
nsCOMPtr<nsITimer> mTimer;
nsCOMPtr<nsIObserver> mObserver;
uint32_t mInterval;
TimerState mState;
};
#endif /* GFX_FONT_UTILS_H */

View File

@ -11,6 +11,7 @@
#include "nsTHashtable.h"
#include "gfxFontUtils.h"
#include "gfxFontInfoLoader.h"
#include "gfxFont.h"
#include "gfxPlatform.h"

View File

@ -20,6 +20,7 @@ EXPORTS += [
'gfxFont.h',
'gfxFontConstants.h',
'gfxFontFeatures.h',
'gfxFontInfoLoader.h',
'gfxFontTest.h',
'gfxFontUtils.h',
'gfxGradientCache.h',
@ -244,6 +245,7 @@ UNIFIED_SOURCES += [
'gfxCachedTempSurface.cpp',
'gfxContext.cpp',
'gfxFontFeatures.cpp',
'gfxFontInfoLoader.cpp',
'gfxFontMissingGlyphs.cpp',
'gfxFontTest.cpp',
'gfxGradientCache.cpp',