Bug 799816. Try to not have preload sheets (or other unreferenced sheets) hanging out in our mCompleteSheets hashtable. r=dbaron

This commit is contained in:
Boris Zbarsky 2013-04-24 14:59:14 -04:00
parent 109adf1697
commit b4d550d2d0

View File

@ -1083,12 +1083,15 @@ Loader::CreateSheet(nsIURI* aURI,
}
#endif
bool fromCompleteSheets = false;
if (!sheet) {
// Then our per-document complete sheets.
URIPrincipalAndCORSModeHashKey key(aURI, aLoaderPrincipal, aCORSMode);
mCompleteSheets.Get(&key, getter_AddRefs(sheet));
LOG((" From completed: %p", sheet.get()));
fromCompleteSheets = !!sheet;
}
if (sheet) {
@ -1156,6 +1159,15 @@ Loader::CreateSheet(nsIURI* aURI,
"Sheet thinks it's not complete while we think it is");
*aSheet = sheet->Clone(nullptr, nullptr, nullptr, nullptr).get();
if (*aSheet && fromCompleteSheets &&
!sheet->GetOwnerNode() && !sheet->GetParentSheet()) {
// The sheet we're cloning isn't actually referenced by
// anyone. Replace it in the cache, so that if our CSSOM is
// later modified we don't end up with two copies of our inner
// hanging around.
URIPrincipalAndCORSModeHashKey key(aURI, aLoaderPrincipal, aCORSMode);
mCompleteSheets.Put(&key, *aSheet);
}
}
}
@ -1759,13 +1771,26 @@ Loader::DoSheetComplete(SheetLoadData* aLoadData, nsresult aStatus,
// adjust the PostLoadEvent code that thinks anything already
// complete must have loaded succesfully.
if (NS_SUCCEEDED(aStatus) && aLoadData->mURI) {
// Pick our sheet to cache carefully. Ideally, we want to cache
// one of the sheets that will be kept alive by a document or
// parent sheet anyway, so that if someone then accesses it via
// CSSOM we won't have extra clones of the inner lying around.
data = aLoadData;
nsCSSStyleSheet* sheet = aLoadData->mSheet;
while (data) {
if (data->mSheet->GetParentSheet() || data->mSheet->GetOwnerNode()) {
sheet = data->mSheet;
break;
}
data = data->mNext;
}
#ifdef MOZ_XUL
if (IsChromeURI(aLoadData->mURI)) {
nsXULPrototypeCache* cache = nsXULPrototypeCache::GetInstance();
if (cache && cache->IsEnabled()) {
if (!cache->GetStyleSheet(aLoadData->mURI)) {
LOG((" Putting sheet in XUL prototype cache"));
cache->PutStyleSheet(aLoadData->mSheet);
cache->PutStyleSheet(sheet);
}
}
}
@ -1774,7 +1799,7 @@ Loader::DoSheetComplete(SheetLoadData* aLoadData, nsresult aStatus,
URIPrincipalAndCORSModeHashKey key(aLoadData->mURI,
aLoadData->mLoaderPrincipal,
aLoadData->mSheet->GetCORSMode());
mCompleteSheets.Put(&key, aLoadData->mSheet);
mCompleteSheets.Put(&key, sheet);
#ifdef MOZ_XUL
}
#endif