Bug 1378718 - pt 2 - Remove support for persistently caching omnijar data-URI fonts in the user-font cache. r=heycam

This commit is contained in:
Jonathan Kew 2017-07-06 15:06:53 +01:00
Родитель d99da5c4f5
Коммит 4e933d3a3f
2 изменённых файлов: 31 добавлений и 133 удалений

Просмотреть файл

@ -8,13 +8,7 @@
#include "gfxUserFontSet.h"
#include "gfxPlatform.h"
#include "gfxPrefs.h"
#include "nsContentPolicyUtils.h"
#include "nsUnicharUtils.h"
#include "nsNetUtil.h"
#include "nsIJARChannel.h"
#include "nsIProtocolHandler.h"
#include "nsIPrincipal.h"
#include "nsIZipReader.h"
#include "gfxFontConstants.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
@ -1120,9 +1114,7 @@ gfxUserFontSet::UserFontCache::Flusher::Observe(nsISupports* aSubject,
if (!strcmp(aTopic, "cacheservice:empty-cache")) {
for (auto i = sUserFonts->Iter(); !i.Done(); i.Next()) {
if (!i.Get()->IsPersistent()) {
i.Remove();
}
i.Remove();
}
} else if (!strcmp(aTopic, "last-pb-context-exited")) {
for (auto i = sUserFonts->Iter(); !i.Done(); i.Next()) {
@ -1156,33 +1148,26 @@ bool
gfxUserFontSet::UserFontCache::Entry::KeyEquals(const KeyTypePointer aKey) const
{
const gfxFontEntry* fe = aKey->mFontEntry;
// CRC32 checking mode
if (mLength || aKey->mLength) {
if (aKey->mLength != mLength ||
aKey->mCRC32 != mCRC32) {
return false;
}
} else {
bool result;
if (NS_FAILED(mURI->Equals(aKey->mURI, &result)) || !result) {
return false;
}
// For data: URIs, we don't care about the principal; otherwise, check it.
if (!IgnorePrincipal(mURI)) {
NS_ASSERTION(mPrincipal && aKey->mPrincipal,
"only data: URIs are allowed to omit the principal");
if (NS_FAILED(mPrincipal->Equals(aKey->mPrincipal, &result)) ||
!result) {
return false;
}
}
bool result;
if (NS_FAILED(mURI->Equals(aKey->mURI, &result)) || !result) {
return false;
}
if (mPrivate != aKey->mPrivate) {
// For data: URIs, we don't care about the principal; otherwise, check it.
if (!IgnorePrincipal(mURI)) {
NS_ASSERTION(mPrincipal && aKey->mPrincipal,
"only data: URIs are allowed to omit the principal");
if (NS_FAILED(mPrincipal->Equals(aKey->mPrincipal, &result)) ||
!result) {
return false;
}
}
if (mPrivate != aKey->mPrivate) {
return false;
}
if (mFontEntry->mStyle != fe->mStyle ||
mFontEntry->mWeight != fe->mWeight ||
mFontEntry->mStretch != fe->mStretch ||
@ -1196,8 +1181,7 @@ gfxUserFontSet::UserFontCache::Entry::KeyEquals(const KeyTypePointer aKey) const
}
void
gfxUserFontSet::UserFontCache::CacheFont(gfxFontEntry* aFontEntry,
EntryPersistence aPersistence)
gfxUserFontSet::UserFontCache::CacheFont(gfxFontEntry* aFontEntry)
{
NS_ASSERTION(aFontEntry->mFamilyName.Length() != 0,
"caching a font associated with no family yet");
@ -1237,25 +1221,17 @@ gfxUserFontSet::UserFontCache::CacheFont(gfxFontEntry* aFontEntry,
RegisterStrongMemoryReporter(new MemoryReporter());
}
if (data->mLength) {
MOZ_ASSERT(aPersistence == kPersistent);
MOZ_ASSERT(!data->mPrivate);
sUserFonts->PutEntry(Key(data->mCRC32, data->mLength, aFontEntry,
data->mPrivate, aPersistence));
// For data: URIs, the principal is ignored; anyone who has the same
// data: URI is able to load it and get an equivalent font.
// Otherwise, the principal is used as part of the cache key.
nsIPrincipal* principal;
if (IgnorePrincipal(data->mURI)) {
principal = nullptr;
} else {
MOZ_ASSERT(aPersistence == kDiscardable);
// For data: URIs, the principal is ignored; anyone who has the same
// data: URI is able to load it and get an equivalent font.
// Otherwise, the principal is used as part of the cache key.
nsIPrincipal* principal;
if (IgnorePrincipal(data->mURI)) {
principal = nullptr;
} else {
principal = data->mPrincipal;
}
sUserFonts->PutEntry(Key(data->mURI, principal, aFontEntry,
data->mPrivate, aPersistence));
principal = data->mPrincipal;
}
sUserFonts->PutEntry(Key(data->mURI, principal, aFontEntry,
data->mPrivate));
#ifdef DEBUG_USERFONT_CACHE
printf("userfontcache added fontentry: %p\n", aFontEntry);
@ -1319,36 +1295,6 @@ gfxUserFontSet::UserFontCache::GetFont(nsIURI* aSrcURI,
return entry->GetFontEntry();
}
// The channel is never openend; to be conservative we use the most
// restrictive security flag: SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS.
nsCOMPtr<nsIChannel> chan;
if (NS_FAILED(NS_NewChannel(getter_AddRefs(chan),
aSrcURI,
aPrincipal,
nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS,
nsIContentPolicy::TYPE_FONT))) {
return nullptr;
}
nsCOMPtr<nsIJARChannel> jarchan = do_QueryInterface(chan);
if (!jarchan) {
return nullptr;
}
nsCOMPtr<nsIZipEntry> zipentry;
if (NS_FAILED(jarchan->GetZipEntry(getter_AddRefs(zipentry)))) {
return nullptr;
}
uint32_t crc32, length;
zipentry->GetCRC32(&crc32);
zipentry->GetRealSize(&length);
entry = sUserFonts->GetEntry(Key(crc32, length, aUserFontEntry, aPrivate));
if (entry) {
return entry->GetFontEntry();
}
return nullptr;
}

Просмотреть файл

@ -99,7 +99,7 @@ class gfxUserFontData {
public:
gfxUserFontData()
: mSrcIndex(0), mFormat(0), mMetaOrigLen(0),
mCRC32(0), mLength(0), mCompression(kUnknownCompression),
mCompression(kUnknownCompression),
mPrivate(false), mIsBuffer(false)
{ }
virtual ~gfxUserFontData() { }
@ -114,8 +114,6 @@ public:
uint32_t mSrcIndex; // index in the rule's source list
uint32_t mFormat; // format hint for the source used, if any
uint32_t mMetaOrigLen; // length needed to decompress metadata
uint32_t mCRC32; // Checksum
uint32_t mLength; // Font length
uint8_t mCompression; // compression type
bool mPrivate; // whether font belongs to a private window
bool mIsBuffer; // whether the font source was a buffer
@ -284,21 +282,10 @@ public:
class UserFontCache {
public:
// Flag passed when caching a font entry, to specify whether the entry
// should persist in the cache or be discardable.
typedef enum {
kDiscardable,
kPersistent
} EntryPersistence;
// Record a loaded user-font in the cache. This requires that the
// font-entry's userFontData has been set up already, as it relies
// on the URI and Principal recorded there.
// If aPersistence is Persistent, the entry will remain in the cache
// across cacheservice:empty-cache notifications. This is used for
// "preloaded hidden fonts" on FxOS.
static void CacheFont(gfxFontEntry* aFontEntry,
EntryPersistence aPersistence = kDiscardable);
static void CacheFont(gfxFontEntry* aFontEntry);
// The given gfxFontEntry is being destroyed, so remove any record that
// refers to it.
@ -357,33 +344,14 @@ public:
// The font entry MUST notify the cache when it is destroyed
// (by calling ForgetFont()).
gfxFontEntry* MOZ_NON_OWNING_REF mFontEntry;
uint32_t mCRC32;
uint32_t mLength;
bool mPrivate;
EntryPersistence mPersistence;
Key(nsIURI* aURI, nsIPrincipal* aPrincipal,
gfxFontEntry* aFontEntry, bool aPrivate,
EntryPersistence aPersistence = kDiscardable)
gfxFontEntry* aFontEntry, bool aPrivate)
: mURI(aURI),
mPrincipal(aPrincipal),
mFontEntry(aFontEntry),
mCRC32(0),
mLength(0),
mPrivate(aPrivate),
mPersistence(aPersistence)
{ }
Key(uint32_t aCRC32, uint32_t aLength,
gfxFontEntry* aFontEntry, bool aPrivate,
EntryPersistence aPersistence = kDiscardable)
: mURI(nullptr),
mPrincipal(nullptr),
mFontEntry(aFontEntry),
mCRC32(aCRC32),
mLength(aLength),
mPrivate(aPrivate),
mPersistence(aPersistence)
mPrivate(aPrivate)
{ }
};
@ -395,21 +363,15 @@ public:
explicit Entry(KeyTypePointer aKey)
: mURI(aKey->mURI),
mPrincipal(aKey->mPrincipal),
mCRC32(aKey->mCRC32),
mLength(aKey->mLength),
mFontEntry(aKey->mFontEntry),
mPrivate(aKey->mPrivate),
mPersistence(aKey->mPersistence)
mPrivate(aKey->mPrivate)
{ }
Entry(const Entry& aOther)
: mURI(aOther.mURI),
mPrincipal(aOther.mPrincipal),
mCRC32(aOther.mCRC32),
mLength(aOther.mLength),
mFontEntry(aOther.mFontEntry),
mPrivate(aOther.mPrivate),
mPersistence(aOther.mPersistence)
mPrivate(aOther.mPrivate)
{ }
~Entry() { }
@ -419,9 +381,6 @@ public:
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
static PLDHashNumber HashKey(const KeyTypePointer aKey) {
if (aKey->mLength) {
return aKey->mCRC32;
}
uint32_t principalHash = 0;
if (aKey->mPrincipal) {
aKey->mPrincipal->GetHashValue(&principalHash);
@ -440,7 +399,6 @@ public:
gfxFontEntry* GetFontEntry() const { return mFontEntry; }
bool IsPersistent() const { return mPersistence == kPersistent; }
bool IsPrivate() const { return mPrivate; }
void ReportMemory(nsIHandleReportCallback* aHandleReport,
@ -460,9 +418,6 @@ public:
nsCOMPtr<nsIURI> mURI;
nsCOMPtr<nsIPrincipal> mPrincipal; // or nullptr for data: URLs
uint32_t mCRC32;
uint32_t mLength;
// The "real" font entry corresponding to this downloaded font.
// The font entry MUST notify the cache when it is destroyed
// (by calling ForgetFont()).
@ -470,9 +425,6 @@ public:
// Whether this font was loaded from a private window.
bool mPrivate;
// Whether this entry should survive cache-flushing.
EntryPersistence mPersistence;
};
static nsTHashtable<Entry>* sUserFonts;