2014-01-28 23:39:01 -08:00
|
|
|
/* -*- 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"
|
2014-01-28 23:39:01 -08:00
|
|
|
#include "nsThreadUtils.h" // for nsRunnable
|
|
|
|
#include "gfxPlatformFontList.h"
|
2014-01-28 23:39:01 -08:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-08-01 12:37:53 -07:00
|
|
|
using services::GetObserverService;
|
2014-01-28 23:39:01 -08:00
|
|
|
|
2014-01-28 23:39:01 -08:00
|
|
|
void
|
|
|
|
FontInfoData::Load()
|
|
|
|
{
|
|
|
|
TimeStamp start = TimeStamp::Now();
|
|
|
|
|
|
|
|
uint32_t i, n = mFontFamiliesToLoad.Length();
|
|
|
|
mLoadStats.families = n;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
LoadFontFamilyData(mFontFamiliesToLoad[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoadTime = TimeStamp::Now() - start;
|
|
|
|
}
|
|
|
|
|
|
|
|
class FontInfoLoadCompleteEvent : public nsRunnable {
|
2014-06-23 11:49:07 -07:00
|
|
|
virtual ~FontInfoLoadCompleteEvent() {}
|
|
|
|
|
2014-07-14 12:21:34 -07:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-01-28 23:39:01 -08:00
|
|
|
|
|
|
|
FontInfoLoadCompleteEvent(FontInfoData *aFontInfo) :
|
|
|
|
mFontInfo(aFontInfo)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run();
|
|
|
|
|
|
|
|
nsRefPtr<FontInfoData> mFontInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AsyncFontInfoLoader : public nsRunnable {
|
2014-06-23 11:49:07 -07:00
|
|
|
virtual ~AsyncFontInfoLoader() {}
|
|
|
|
|
2014-07-14 12:21:34 -07:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-01-28 23:39:01 -08:00
|
|
|
|
|
|
|
AsyncFontInfoLoader(FontInfoData *aFontInfo) :
|
|
|
|
mFontInfo(aFontInfo)
|
|
|
|
{
|
|
|
|
mCompleteEvent = new FontInfoLoadCompleteEvent(aFontInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run();
|
|
|
|
|
|
|
|
nsRefPtr<FontInfoData> mFontInfo;
|
|
|
|
nsRefPtr<FontInfoLoadCompleteEvent> mCompleteEvent;
|
|
|
|
};
|
|
|
|
|
|
|
|
// runs on main thread after async font info loading is done
|
|
|
|
nsresult
|
|
|
|
FontInfoLoadCompleteEvent::Run()
|
|
|
|
{
|
|
|
|
gfxFontInfoLoader *loader =
|
|
|
|
static_cast<gfxFontInfoLoader*>(gfxPlatformFontList::PlatformFontList());
|
|
|
|
|
|
|
|
loader->FinalizeLoader(mFontInfo);
|
|
|
|
|
|
|
|
mFontInfo = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-14 12:21:34 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(FontInfoLoadCompleteEvent, nsRunnable);
|
2014-01-28 23:39:01 -08:00
|
|
|
|
|
|
|
// runs on separate thread
|
|
|
|
nsresult
|
|
|
|
AsyncFontInfoLoader::Run()
|
|
|
|
{
|
|
|
|
// load platform-specific font info
|
|
|
|
mFontInfo->Load();
|
|
|
|
|
|
|
|
// post a completion event that transfer the data to the fontlist
|
2014-05-23 12:53:17 -07:00
|
|
|
NS_DispatchToMainThread(mCompleteEvent);
|
2014-01-28 23:39:01 -08:00
|
|
|
mFontInfo = nullptr;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-14 12:21:34 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(AsyncFontInfoLoader, nsRunnable);
|
2014-01-28 23:39:01 -08:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(gfxFontInfoLoader::ShutdownObserver, nsIObserver)
|
2014-01-28 23:39:01 -08:00
|
|
|
|
|
|
|
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
|
2014-01-28 23:39:01 -08:00
|
|
|
if (mState != stateInitial &&
|
|
|
|
mState != stateTimerOff &&
|
|
|
|
mState != stateTimerOnDelay) {
|
2014-01-28 23:39:01 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 23:39:01 -08:00
|
|
|
AddShutdownObserver();
|
2014-01-28 23:39:01 -08:00
|
|
|
|
2014-01-28 23:39:01 -08:00
|
|
|
// delay? ==> start async thread after a delay
|
2014-01-28 23:39:01 -08:00
|
|
|
if (aDelay) {
|
|
|
|
mState = stateTimerOnDelay;
|
2014-01-28 23:39:01 -08:00
|
|
|
mTimer->InitWithFuncCallback(DelayedStartCallback, this, aDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
return;
|
2014-01-28 23:39:01 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 23:39:01 -08:00
|
|
|
mFontInfo = CreateFontInfoData();
|
|
|
|
|
|
|
|
// initialize
|
2014-01-28 23:39:01 -08:00
|
|
|
InitLoader();
|
|
|
|
|
2014-01-28 23:39:01 -08:00
|
|
|
// start async load
|
|
|
|
mState = stateAsyncLoad;
|
|
|
|
nsresult rv = NS_NewNamedThread("Font Loader",
|
|
|
|
getter_AddRefs(mFontLoaderThread),
|
|
|
|
nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-28 23:39:01 -08:00
|
|
|
|
2014-01-28 23:39:01 -08:00
|
|
|
nsCOMPtr<nsIRunnable> loadEvent = new AsyncFontInfoLoader(mFontInfo);
|
|
|
|
|
|
|
|
mFontLoaderThread->Dispatch(loadEvent, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontInfoLoader::FinalizeLoader(FontInfoData *aFontInfo)
|
|
|
|
{
|
|
|
|
// avoid loading data if loader has already been canceled
|
|
|
|
if (mState != stateAsyncLoad) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoadTime = mFontInfo->mLoadTime;
|
|
|
|
|
|
|
|
// try to load all font data immediately
|
|
|
|
if (LoadFontInfo()) {
|
|
|
|
CancelLoader();
|
|
|
|
return;
|
2014-01-28 23:39:01 -08:00
|
|
|
}
|
2014-01-28 23:39:01 -08:00
|
|
|
|
|
|
|
// not all work completed ==> run load on interval
|
|
|
|
mState = stateTimerOnInterval;
|
|
|
|
mTimer->InitWithFuncCallback(LoadFontInfoCallback, this, mInterval,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK);
|
2014-01-28 23:39:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontInfoLoader::CancelLoader()
|
|
|
|
{
|
|
|
|
if (mState == stateInitial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mState = stateTimerOff;
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
2014-01-28 23:39:01 -08:00
|
|
|
if (mFontLoaderThread) {
|
|
|
|
mFontLoaderThread->Shutdown();
|
|
|
|
mFontLoaderThread = nullptr;
|
|
|
|
}
|
2014-01-28 23:39:01 -08:00
|
|
|
RemoveShutdownObserver();
|
2014-01-28 23:39:01 -08:00
|
|
|
CleanupLoader();
|
2014-01-28 23:39:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-28 23:39:01 -08:00
|
|
|
gfxFontInfoLoader::LoadFontInfoTimerFire()
|
2014-01-28 23:39:01 -08:00
|
|
|
{
|
|
|
|
if (mState == stateTimerOnDelay) {
|
|
|
|
mState = stateTimerOnInterval;
|
|
|
|
mTimer->SetDelay(mInterval);
|
|
|
|
}
|
|
|
|
|
2014-01-28 23:39:01 -08:00
|
|
|
bool done = LoadFontInfo();
|
2014-01-28 23:39:01 -08:00
|
|
|
if (done) {
|
|
|
|
CancelLoader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontInfoLoader::~gfxFontInfoLoader()
|
|
|
|
{
|
|
|
|
RemoveShutdownObserver();
|
2014-08-01 12:37:53 -07:00
|
|
|
MOZ_COUNT_DTOR(gfxFontInfoLoader);
|
2014-01-28 23:39:01 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 23:39:01 -08:00
|
|
|
void
|
|
|
|
gfxFontInfoLoader::AddShutdownObserver()
|
|
|
|
{
|
|
|
|
if (mObserver) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
mObserver = new ShutdownObserver(this);
|
|
|
|
obs->AddObserver(mObserver, "quit-application", false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 23:39:01 -08:00
|
|
|
void
|
|
|
|
gfxFontInfoLoader::RemoveShutdownObserver()
|
|
|
|
{
|
|
|
|
if (mObserver) {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(mObserver, "quit-application");
|
|
|
|
mObserver = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|