Files
palemoon27/dom/xbl/nsXBLResourceLoader.cpp
T
roytam1 fe0509a62e import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 904479 - Added createPromiseWithId() that returns id of resolver r=kanru,nsm (2ac672d882)
- Bug 1166580 - Disable mozHasPendingMessage tests on non-browser platform. r=me (03c689964b)
- Bug 1162281 - Invalid system message handler in an App Manifest can break the entire system. r=fabrice (e192a95f9c)
- Bug 1198988 - Turn off some useless dump() calls r=ferjm (34fc83b236)
- Bug 1164498: Remove |DispatchBluetoothReply|, r=btian (6143335efa)
- Bug 1001757 - Add ability to store core apps outside of profile on desktop b2g; r=fabrice (f6b605e7aa)
- Bug 1155245 - Set the app status correctly for hosted certified apps in developer mode. r=fabrice (131178b80e)
- Bug 1179052 - Add some raptor markers to b2g gecko startup r=gwagner (222256fad8)
- Bug 1163904 - handle -url command line argument. r=fabrice (ee61af1ff9)
- Bug 1167275 - JS error in shell.js handleCmdLine() r=me (32e75c604f)
- Bug 1167197 - Fix GMPProvider on Android r=cpearce Bug 1181209 - Make changes to Gecko needed for b2gdroid to boot. r=fabrice (b35d3a372f)
- Bug 1158544 - Remove FTPChannelChild::mWasOpened and make the base class mWasOpened protected; r=mcmanus (9111e1bc00)
- Bug 1171716 - Part 2: Use NS_ReleaseOnMainThread in nsBaseChannel. r=froydnj (f138124f14)
- partial of Bug 1177175 - Add a UITour target inside the TP panel. (603cc719b3)
- Bug 1175545 - Dont process alt-svc on 421 r=hurley (ad0f2f6e91)
- Bug 1191291 - convert nsHttpChannel::RetargetDeliveryTo warning to log r=michal.novotny (b9c6003df8)
- Bug 1182487 - Don't try to write to HTTP cache entry in nsHttpChannel when entry is open for reading only. r=michal (b36d7014a0)
- Bug 1173069 - Don't accumulate the cache hit telemetry for intercepted channels; r=mayhemer,jdm (aaed79183d)
- Bug 1208755 HttpBaseChannel::ShouldIntercept() should not assume every channel has a LoadInfo. r=ckerschb (d55be94901)
- Bug 1201229 - Return an empty string for a header when an error occurs; r=dragana (256d0462c8)
- Bug 1048048 - add preload content policy types - web platform test updates (r=dveditz) (baa1004dd6)
- Bug 1048048 - add preload content policy types - csp changes (r=dveditz) (17914dadba)
- Bug 1048048 - add preload content policy types for stylesheets (r=cam) (29af13263a)
- Bug 1048048 - add preload content policy types (r=ehsan) (f58a32d51b)
- Bug 1201747 - Don't inspect the subject principal in StorageAllowedForPrincipal. r=mystor (4f2c100882)
- Bug 1176829 - Remove custom elements base element queue. r=smaug (03a520c13d)
- Bug 1176829 follow-up, finish removing unused member to fix bustage. CLOSED TREE (29c6150af8)
- Bug 1179909: Build fix. r=me CLOSED TREE (40e3bdb971)
- Bug 1188932 - Allow the User-Agent header to be explicitly set by requests, r=bkelly, r=jgraham (37aacbd37d)
2022-04-01 23:06:55 +08:00

302 lines
8.4 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "nsTArray.h"
#include "nsString.h"
#include "nsIStyleRuleProcessor.h"
#include "nsIDocument.h"
#include "nsIContent.h"
#include "nsIPresShell.h"
#include "nsXBLService.h"
#include "nsIServiceManager.h"
#include "nsXBLResourceLoader.h"
#include "nsXBLPrototypeResources.h"
#include "nsIDocumentObserver.h"
#include "imgILoader.h"
#include "imgRequestProxy.h"
#include "mozilla/CSSStyleSheet.h"
#include "mozilla/css/Loader.h"
#include "nsIURI.h"
#include "nsNetUtil.h"
#include "nsGkAtoms.h"
#include "nsFrameManager.h"
#include "nsStyleContext.h"
#include "nsXBLPrototypeBinding.h"
#include "nsCSSRuleProcessor.h"
#include "nsContentUtils.h"
#include "nsStyleSet.h"
#include "nsIScriptSecurityManager.h"
using namespace mozilla;
NS_IMPL_CYCLE_COLLECTION(nsXBLResourceLoader, mBoundElements)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXBLResourceLoader)
NS_INTERFACE_MAP_ENTRY(nsICSSLoaderObserver)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXBLResourceLoader)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXBLResourceLoader)
struct nsXBLResource
{
nsXBLResource* mNext;
nsIAtom* mType;
nsString mSrc;
nsXBLResource(nsIAtom* aType, const nsAString& aSrc)
{
MOZ_COUNT_CTOR(nsXBLResource);
mNext = nullptr;
mType = aType;
mSrc = aSrc;
}
~nsXBLResource()
{
MOZ_COUNT_DTOR(nsXBLResource);
NS_CONTENT_DELETE_LIST_MEMBER(nsXBLResource, this, mNext);
}
};
nsXBLResourceLoader::nsXBLResourceLoader(nsXBLPrototypeBinding* aBinding,
nsXBLPrototypeResources* aResources)
:mBinding(aBinding),
mResources(aResources),
mResourceList(nullptr),
mLastResource(nullptr),
mLoadingResources(false),
mInLoadResourcesFunc(false),
mPendingSheets(0)
{
}
nsXBLResourceLoader::~nsXBLResourceLoader()
{
delete mResourceList;
}
void
nsXBLResourceLoader::LoadResources(bool* aResult)
{
mInLoadResourcesFunc = true;
if (mLoadingResources) {
*aResult = (mPendingSheets == 0);
mInLoadResourcesFunc = false;
return;
}
mLoadingResources = true;
*aResult = true;
// Declare our loaders.
nsCOMPtr<nsIDocument> doc = mBinding->XBLDocumentInfo()->GetDocument();
mozilla::css::Loader* cssLoader = doc->CSSLoader();
nsIURI *docURL = doc->GetDocumentURI();
nsIPrincipal* docPrincipal = doc->NodePrincipal();
nsCOMPtr<nsIURI> url;
for (nsXBLResource* curr = mResourceList; curr; curr = curr->mNext) {
if (curr->mSrc.IsEmpty())
continue;
if (NS_FAILED(NS_NewURI(getter_AddRefs(url), curr->mSrc,
doc->GetDocumentCharacterSet().get(), docURL)))
continue;
if (curr->mType == nsGkAtoms::image) {
if (!nsContentUtils::CanLoadImage(url, doc, doc, docPrincipal)) {
// We're not permitted to load this image, move on...
continue;
}
// Now kick off the image load...
// Passing nullptr for pretty much everything -- cause we don't care!
// XXX: initialDocumentURI is nullptr!
nsRefPtr<imgRequestProxy> req;
nsContentUtils::LoadImage(url, doc, docPrincipal, docURL,
doc->GetReferrerPolicy(), nullptr,
nsIRequest::LOAD_BACKGROUND, EmptyString(),
getter_AddRefs(req));
}
else if (curr->mType == nsGkAtoms::stylesheet) {
// Kick off the load of the stylesheet.
// Always load chrome synchronously
// XXXbz should that still do a content policy check?
bool chrome;
nsresult rv;
if (NS_SUCCEEDED(url->SchemeIs("chrome", &chrome)) && chrome)
{
rv = nsContentUtils::GetSecurityManager()->
CheckLoadURIWithPrincipal(docPrincipal, url,
nsIScriptSecurityManager::ALLOW_CHROME);
if (NS_SUCCEEDED(rv)) {
nsRefPtr<CSSStyleSheet> sheet;
rv = cssLoader->LoadSheetSync(url, getter_AddRefs(sheet));
NS_ASSERTION(NS_SUCCEEDED(rv), "Load failed!!!");
if (NS_SUCCEEDED(rv))
{
rv = StyleSheetLoaded(sheet, false, NS_OK);
NS_ASSERTION(NS_SUCCEEDED(rv), "Processing the style sheet failed!!!");
}
}
}
else
{
rv = cssLoader->LoadSheet(url, false, docPrincipal, EmptyCString(), this);
if (NS_SUCCEEDED(rv))
++mPendingSheets;
}
}
}
*aResult = (mPendingSheets == 0);
mInLoadResourcesFunc = false;
// Destroy our resource list.
delete mResourceList;
mResourceList = nullptr;
}
// nsICSSLoaderObserver
NS_IMETHODIMP
nsXBLResourceLoader::StyleSheetLoaded(CSSStyleSheet* aSheet,
bool aWasAlternate,
nsresult aStatus)
{
if (!mResources) {
// Our resources got destroyed -- just bail out
return NS_OK;
}
mResources->AppendStyleSheet(aSheet);
if (!mInLoadResourcesFunc)
mPendingSheets--;
if (mPendingSheets == 0) {
// All stylesheets are loaded.
mResources->GatherRuleProcessor();
// XXX Check for mPendingScripts when scripts also come online.
if (!mInLoadResourcesFunc)
NotifyBoundElements();
}
return NS_OK;
}
void
nsXBLResourceLoader::AddResource(nsIAtom* aResourceType, const nsAString& aSrc)
{
nsXBLResource* res = new nsXBLResource(aResourceType, aSrc);
if (!res)
return;
if (!mResourceList)
mResourceList = res;
else
mLastResource->mNext = res;
mLastResource = res;
}
void
nsXBLResourceLoader::AddResourceListener(nsIContent* aBoundElement)
{
if (aBoundElement) {
mBoundElements.AppendObject(aBoundElement);
}
}
void
nsXBLResourceLoader::NotifyBoundElements()
{
nsXBLService* xblService = nsXBLService::GetInstance();
if (!xblService)
return;
nsIURI* bindingURI = mBinding->BindingURI();
uint32_t eltCount = mBoundElements.Count();
for (uint32_t j = 0; j < eltCount; j++) {
nsCOMPtr<nsIContent> content = mBoundElements.ObjectAt(j);
bool ready = false;
xblService->BindingReady(content, bindingURI, &ready);
if (ready) {
// We need the document to flush out frame construction and
// such, so we want to use the current document.
nsIDocument* doc = content->GetCurrentDoc();
if (doc) {
// Flush first to make sure we can get the frame for content
doc->FlushPendingNotifications(Flush_Frames);
// If |content| is (in addition to having binding |mBinding|)
// also a descendant of another element with binding |mBinding|,
// then we might have just constructed it due to the
// notification of its parent. (We can know about both if the
// binding loads were triggered from the DOM rather than frame
// construction.) So we have to check both whether the element
// has a primary frame and whether it's in the undisplayed map
// before sending a ContentInserted notification, or bad things
// will happen.
nsIPresShell *shell = doc->GetShell();
if (shell) {
nsIFrame* childFrame = content->GetPrimaryFrame();
if (!childFrame) {
// Check to see if it's in the undisplayed content map.
nsStyleContext* sc =
shell->FrameManager()->GetUndisplayedContent(content);
if (!sc) {
shell->RecreateFramesFor(content);
}
}
}
// Flush again
// XXXbz why is this needed?
doc->FlushPendingNotifications(Flush_ContentAndNotify);
}
}
}
// Clear out the whole array.
mBoundElements.Clear();
// Delete ourselves.
mResources->ClearLoader();
}
nsresult
nsXBLResourceLoader::Write(nsIObjectOutputStream* aStream)
{
nsresult rv;
for (nsXBLResource* curr = mResourceList; curr; curr = curr->mNext) {
if (curr->mType == nsGkAtoms::image)
rv = aStream->Write8(XBLBinding_Serialize_Image);
else if (curr->mType == nsGkAtoms::stylesheet)
rv = aStream->Write8(XBLBinding_Serialize_Stylesheet);
else
continue;
NS_ENSURE_SUCCESS(rv, rv);
rv = aStream->WriteWStringZ(curr->mSrc.get());
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}