Bug 1864861: part 1) Factor general font loading code out to FontLoaderUtils. r=jfkthame

Removes the dependency of font-face loading code to `FontPreloader`.
The latter's purpose is to preload fonts.

The code was just moved with includes updated.

Differential Revision: https://phabricator.services.mozilla.com/D193789
This commit is contained in:
Mirko Brodesser 2023-11-20 10:38:22 +00:00
Родитель ee05426167
Коммит ba9f4a7916
7 изменённых файлов: 251 добавлений и 205 удалений

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

@ -5,7 +5,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "FontFaceSetDocumentImpl.h"
#include "FontPreloader.h"
#include "mozilla/FontLoaderUtils.h"
#include "mozilla/LoadInfo.h"
#include "mozilla/PresShell.h"
#include "mozilla/PresShellInlines.h"
@ -282,7 +282,7 @@ nsresult FontFaceSetDocumentImpl::StartLoad(gfxUserFontEntry* aUserFontEntry,
nsCOMPtr<nsILoadGroup> loadGroup(mDocument->GetDocumentLoadGroup());
if (NS_FAILED(rv)) {
nsCOMPtr<nsIChannel> channel;
rv = FontPreloader::BuildChannel(
rv = FontLoaderUtils::BuildChannel(
getter_AddRefs(channel), src.mURI->get(), CORS_ANONYMOUS,
dom::ReferrerPolicy::_empty /* not used */, aUserFontEntry, &src,
mDocument, loadGroup, nullptr, false);

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

@ -5,7 +5,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "FontFaceSetWorkerImpl.h"
#include "FontPreloader.h"
#include "mozilla/FontLoaderUtils.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRef.h"
#include "mozilla/dom/WorkerRunnable.h"
@ -255,7 +255,7 @@ nsresult FontFaceSetWorkerImpl::StartLoad(gfxUserFontEntry* aUserFontEntry,
nsCOMPtr<nsILoadGroup> loadGroup(mWorkerRef->Private()->GetLoadGroup());
nsCOMPtr<nsIChannel> channel;
rv = FontPreloader::BuildChannel(
rv = FontLoaderUtils::BuildChannel(
getter_AddRefs(channel), src.mURI->get(), CORS_ANONYMOUS,
dom::ReferrerPolicy::_empty /* not used */, aUserFontEntry, &src,
mWorkerRef->Private(), loadGroup, nullptr, false);

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

@ -0,0 +1,181 @@
/* -*- 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 "mozilla/FontLoaderUtils.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "gfxUserFontSet.h"
#include "nsCOMPtr.h"
#include "nsIChannel.h"
#include "nsIClassOfService.h"
#include "nsIContentPolicy.h"
#include "nsIHttpChannel.h"
#include "nsILoadInfo.h"
#include "nsIReferrerInfo.h"
#include "nsISupportsPriority.h"
#include "nsIURI.h"
#include "nsNetUtil.h"
namespace mozilla {
/* static */ void FontLoaderUtils::BuildChannelFlags(
nsIURI* aURI, bool aIsPreload,
nsContentSecurityManager::CORSSecurityMapping& aCorsMapping,
nsSecurityFlags& aSecurityFlags, nsContentPolicyType& aContentPolicyType) {
// aCORSMode is ignored. We always load as crossorigin=anonymous, but a
// preload started with anything other then "anonymous" will never be found.
aCorsMapping =
aURI->SchemeIs("file")
? nsContentSecurityManager::CORSSecurityMapping::
CORS_NONE_MAPS_TO_INHERITED_CONTEXT
: nsContentSecurityManager::CORSSecurityMapping::REQUIRE_CORS_CHECKS;
aSecurityFlags = nsContentSecurityManager::ComputeSecurityFlags(
CORSMode::CORS_NONE, aCorsMapping);
aContentPolicyType = aIsPreload ? nsIContentPolicy::TYPE_INTERNAL_FONT_PRELOAD
: nsIContentPolicy::TYPE_FONT;
}
/* static */ nsresult FontLoaderUtils::BuildChannelSetup(
nsIChannel* aChannel, nsIHttpChannel* aHttpChannel,
nsIReferrerInfo* aReferrerInfo, const gfxFontFaceSrc* aFontFaceSrc) {
if (aHttpChannel) {
nsresult rv = aHttpChannel->SetRequestHeader(
"Accept"_ns,
"application/font-woff2;q=1.0,application/font-woff;q=0.9,*/*;q=0.8"_ns,
false);
NS_ENSURE_SUCCESS(rv, rv);
if (aReferrerInfo) {
rv = aHttpChannel->SetReferrerInfoWithoutClone(aReferrerInfo);
MOZ_ASSERT(NS_SUCCEEDED(rv));
} else {
MOZ_ASSERT(aFontFaceSrc);
rv = aHttpChannel->SetReferrerInfo(aFontFaceSrc->mReferrerInfo);
Unused << NS_WARN_IF(NS_FAILED(rv));
// For WOFF and WOFF2, we should tell servers/proxies/etc NOT to try
// and apply additional compression at the content-encoding layer
if (aFontFaceSrc->mFormatHint == StyleFontFaceSourceFormatKeyword::Woff ||
aFontFaceSrc->mFormatHint ==
StyleFontFaceSourceFormatKeyword::Woff2) {
rv = aHttpChannel->SetRequestHeader("Accept-Encoding"_ns, "identity"_ns,
false);
NS_ENSURE_SUCCESS(rv, rv);
}
}
}
nsCOMPtr<nsISupportsPriority> priorityChannel(do_QueryInterface(aChannel));
if (priorityChannel) {
priorityChannel->AdjustPriority(nsISupportsPriority::PRIORITY_HIGH);
}
nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(aChannel));
if (cos) {
cos->AddClassFlags(nsIClassOfService::TailForbidden);
}
return NS_OK;
}
// static
nsresult FontLoaderUtils::BuildChannel(
nsIChannel** aChannel, nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
gfxUserFontEntry* aUserFontEntry, const gfxFontFaceSrc* aFontFaceSrc,
dom::Document* aDocument, nsILoadGroup* aLoadGroup,
nsIInterfaceRequestor* aCallbacks, bool aIsPreload) {
nsresult rv;
nsIPrincipal* principal =
aUserFontEntry ? (aUserFontEntry->GetPrincipal()
? aUserFontEntry->GetPrincipal()->NodePrincipal()
: nullptr)
: aDocument->NodePrincipal();
nsContentSecurityManager::CORSSecurityMapping corsMapping;
nsSecurityFlags securityFlags;
nsContentPolicyType contentPolicyType;
BuildChannelFlags(aURI, aIsPreload, corsMapping, securityFlags,
contentPolicyType);
nsCOMPtr<nsIChannel> channel;
// Note we are calling NS_NewChannelWithTriggeringPrincipal() with both a
// node and a principal. This is because the document where the font is
// being loaded might have a different origin from the principal of the
// stylesheet that initiated the font load.
rv = NS_NewChannelWithTriggeringPrincipal(getter_AddRefs(channel), aURI,
aDocument, principal, securityFlags,
contentPolicyType,
nullptr, // PerformanceStorage
aLoadGroup);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
nsCOMPtr<nsIReferrerInfo> referrerInfo;
if (httpChannel && !aFontFaceSrc) {
referrerInfo = new dom::ReferrerInfo(aDocument->GetDocumentURIAsReferrer(),
aReferrerPolicy);
rv = httpChannel->SetReferrerInfoWithoutClone(referrerInfo);
MOZ_ASSERT(NS_SUCCEEDED(rv));
}
rv = BuildChannelSetup(channel, httpChannel, referrerInfo, aFontFaceSrc);
NS_ENSURE_SUCCESS(rv, rv);
channel.forget(aChannel);
return NS_OK;
}
// static
nsresult FontLoaderUtils::BuildChannel(
nsIChannel** aChannel, nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
gfxUserFontEntry* aUserFontEntry, const gfxFontFaceSrc* aFontFaceSrc,
dom::WorkerPrivate* aWorkerPrivate, nsILoadGroup* aLoadGroup,
nsIInterfaceRequestor* aCallbacks, bool aIsPreload) {
nsresult rv;
nsIPrincipal* principal =
aUserFontEntry ? (aUserFontEntry->GetPrincipal()
? aUserFontEntry->GetPrincipal()->NodePrincipal()
: nullptr)
: aWorkerPrivate->GetPrincipal();
nsContentSecurityManager::CORSSecurityMapping corsMapping;
nsSecurityFlags securityFlags;
nsContentPolicyType contentPolicyType;
BuildChannelFlags(aURI, aIsPreload, corsMapping, securityFlags,
contentPolicyType);
nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannelWithTriggeringPrincipal(
getter_AddRefs(channel), aURI, aWorkerPrivate->GetLoadingPrincipal(),
principal, securityFlags, contentPolicyType, nullptr, nullptr,
aLoadGroup);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
nsCOMPtr<nsIReferrerInfo> referrerInfo;
if (httpChannel && !aFontFaceSrc) {
referrerInfo =
static_cast<dom::ReferrerInfo*>(aWorkerPrivate->GetReferrerInfo())
->CloneWithNewPolicy(aReferrerPolicy);
}
rv = BuildChannelSetup(channel, httpChannel, referrerInfo, aFontFaceSrc);
NS_ENSURE_SUCCESS(rv, rv);
channel.forget(aChannel);
return NS_OK;
}
} // namespace mozilla

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

@ -0,0 +1,59 @@
/* -*- 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/. */
#ifndef GECKO_LAYOUT_STYLE_FONTLOADERUTILS_H_
#define GECKO_LAYOUT_STYLE_FONTLOADERUTILS_H_
#include "ErrorList.h"
#include "nsContentSecurityManager.h"
class gfxUserFontEntry;
class nsIChannel;
class nsIHttpChannel;
class nsIInterfaceRequestor;
class nsILoadGroup;
class nsIURI;
struct gfxFontFaceSrc;
namespace mozilla {
enum CORSMode : uint8_t;
namespace dom {
class Document;
class WorkerPrivate;
enum class ReferrerPolicy : uint8_t;
} // namespace dom
class FontLoaderUtils {
public:
static nsresult BuildChannel(
nsIChannel** aChannel, nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
gfxUserFontEntry* aUserFontEntry, const gfxFontFaceSrc* aFontFaceSrc,
dom::Document* aDocument, nsILoadGroup* aLoadGroup,
nsIInterfaceRequestor* aCallbacks, bool aIsPreload);
static nsresult BuildChannel(
nsIChannel** aChannel, nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
gfxUserFontEntry* aUserFontEntry, const gfxFontFaceSrc* aFontFaceSrc,
dom::WorkerPrivate* aWorkerPrivate, nsILoadGroup* aLoadGroup,
nsIInterfaceRequestor* aCallbacks, bool aIsPreload);
private:
static void BuildChannelFlags(
nsIURI* aURI, bool aIsPreload,
nsContentSecurityManager::CORSSecurityMapping& aCorsMapping,
nsSecurityFlags& aSecurityFlags, nsContentPolicyType& aContentPolicyType);
static nsresult BuildChannelSetup(nsIChannel* aChannel,
nsIHttpChannel* aHttpChannel,
nsIReferrerInfo* aReferrerInfo,
const gfxFontFaceSrc* aFontFaceSrc);
};
} // namespace mozilla
#endif // GECKO_LAYOUT_STYLE_FONTLOADERUTILS_H_

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

@ -6,15 +6,8 @@
#include "FontPreloader.h"
#include "gfxUserFontSet.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "nsContentSecurityManager.h"
#include "nsIClassOfService.h"
#include "nsIHttpChannel.h"
#include "nsISupportsPriority.h"
#include "nsNetUtil.h"
#include "mozilla/FontLoaderUtils.h"
#include "gfxPlatform.h"
namespace mozilla {
@ -31,163 +24,9 @@ nsresult FontPreloader::CreateChannel(
return NS_ERROR_NOT_AVAILABLE;
}
return BuildChannel(aChannel, aURI, aCORSMode, aReferrerPolicy, nullptr,
nullptr, aDocument, aLoadGroup, aCallbacks, true);
}
/* static */ void FontPreloader::BuildChannelFlags(
nsIURI* aURI, bool aIsPreload,
nsContentSecurityManager::CORSSecurityMapping& aCorsMapping,
nsSecurityFlags& aSecurityFlags, nsContentPolicyType& aContentPolicyType) {
// aCORSMode is ignored. We always load as crossorigin=anonymous, but a
// preload started with anything other then "anonymous" will never be found.
aCorsMapping =
aURI->SchemeIs("file")
? nsContentSecurityManager::CORSSecurityMapping::
CORS_NONE_MAPS_TO_INHERITED_CONTEXT
: nsContentSecurityManager::CORSSecurityMapping::REQUIRE_CORS_CHECKS;
aSecurityFlags = nsContentSecurityManager::ComputeSecurityFlags(
CORSMode::CORS_NONE, aCorsMapping);
aContentPolicyType = aIsPreload ? nsIContentPolicy::TYPE_INTERNAL_FONT_PRELOAD
: nsIContentPolicy::TYPE_FONT;
}
/* static */ nsresult FontPreloader::BuildChannelSetup(
nsIChannel* aChannel, nsIHttpChannel* aHttpChannel,
nsIReferrerInfo* aReferrerInfo, const gfxFontFaceSrc* aFontFaceSrc) {
if (aHttpChannel) {
nsresult rv = aHttpChannel->SetRequestHeader(
"Accept"_ns,
"application/font-woff2;q=1.0,application/font-woff;q=0.9,*/*;q=0.8"_ns,
false);
NS_ENSURE_SUCCESS(rv, rv);
if (aReferrerInfo) {
rv = aHttpChannel->SetReferrerInfoWithoutClone(aReferrerInfo);
MOZ_ASSERT(NS_SUCCEEDED(rv));
} else {
MOZ_ASSERT(aFontFaceSrc);
rv = aHttpChannel->SetReferrerInfo(aFontFaceSrc->mReferrerInfo);
Unused << NS_WARN_IF(NS_FAILED(rv));
// For WOFF and WOFF2, we should tell servers/proxies/etc NOT to try
// and apply additional compression at the content-encoding layer
if (aFontFaceSrc->mFormatHint == StyleFontFaceSourceFormatKeyword::Woff ||
aFontFaceSrc->mFormatHint ==
StyleFontFaceSourceFormatKeyword::Woff2) {
rv = aHttpChannel->SetRequestHeader("Accept-Encoding"_ns, "identity"_ns,
false);
NS_ENSURE_SUCCESS(rv, rv);
}
}
}
nsCOMPtr<nsISupportsPriority> priorityChannel(do_QueryInterface(aChannel));
if (priorityChannel) {
priorityChannel->AdjustPriority(nsISupportsPriority::PRIORITY_HIGH);
}
nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(aChannel));
if (cos) {
cos->AddClassFlags(nsIClassOfService::TailForbidden);
}
return NS_OK;
}
// static
nsresult FontPreloader::BuildChannel(
nsIChannel** aChannel, nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
gfxUserFontEntry* aUserFontEntry, const gfxFontFaceSrc* aFontFaceSrc,
dom::Document* aDocument, nsILoadGroup* aLoadGroup,
nsIInterfaceRequestor* aCallbacks, bool aIsPreload) {
nsresult rv;
nsIPrincipal* principal =
aUserFontEntry ? (aUserFontEntry->GetPrincipal()
? aUserFontEntry->GetPrincipal()->NodePrincipal()
: nullptr)
: aDocument->NodePrincipal();
nsContentSecurityManager::CORSSecurityMapping corsMapping;
nsSecurityFlags securityFlags;
nsContentPolicyType contentPolicyType;
BuildChannelFlags(aURI, aIsPreload, corsMapping, securityFlags,
contentPolicyType);
nsCOMPtr<nsIChannel> channel;
// Note we are calling NS_NewChannelWithTriggeringPrincipal() with both a
// node and a principal. This is because the document where the font is
// being loaded might have a different origin from the principal of the
// stylesheet that initiated the font load.
rv = NS_NewChannelWithTriggeringPrincipal(getter_AddRefs(channel), aURI,
aDocument, principal, securityFlags,
contentPolicyType,
nullptr, // PerformanceStorage
aLoadGroup);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
nsCOMPtr<nsIReferrerInfo> referrerInfo;
if (httpChannel && !aFontFaceSrc) {
referrerInfo = new dom::ReferrerInfo(aDocument->GetDocumentURIAsReferrer(),
aReferrerPolicy);
rv = httpChannel->SetReferrerInfoWithoutClone(referrerInfo);
MOZ_ASSERT(NS_SUCCEEDED(rv));
}
rv = BuildChannelSetup(channel, httpChannel, referrerInfo, aFontFaceSrc);
NS_ENSURE_SUCCESS(rv, rv);
channel.forget(aChannel);
return NS_OK;
}
// static
nsresult FontPreloader::BuildChannel(
nsIChannel** aChannel, nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
gfxUserFontEntry* aUserFontEntry, const gfxFontFaceSrc* aFontFaceSrc,
dom::WorkerPrivate* aWorkerPrivate, nsILoadGroup* aLoadGroup,
nsIInterfaceRequestor* aCallbacks, bool aIsPreload) {
nsresult rv;
nsIPrincipal* principal =
aUserFontEntry ? (aUserFontEntry->GetPrincipal()
? aUserFontEntry->GetPrincipal()->NodePrincipal()
: nullptr)
: aWorkerPrivate->GetPrincipal();
nsContentSecurityManager::CORSSecurityMapping corsMapping;
nsSecurityFlags securityFlags;
nsContentPolicyType contentPolicyType;
BuildChannelFlags(aURI, aIsPreload, corsMapping, securityFlags,
contentPolicyType);
nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannelWithTriggeringPrincipal(
getter_AddRefs(channel), aURI, aWorkerPrivate->GetLoadingPrincipal(),
principal, securityFlags, contentPolicyType, nullptr, nullptr,
aLoadGroup);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
nsCOMPtr<nsIReferrerInfo> referrerInfo;
if (httpChannel && !aFontFaceSrc) {
referrerInfo =
static_cast<dom::ReferrerInfo*>(aWorkerPrivate->GetReferrerInfo())
->CloneWithNewPolicy(aReferrerPolicy);
}
rv = BuildChannelSetup(channel, httpChannel, referrerInfo, aFontFaceSrc);
NS_ENSURE_SUCCESS(rv, rv);
channel.forget(aChannel);
return NS_OK;
return FontLoaderUtils::BuildChannel(aChannel, aURI, aCORSMode,
aReferrerPolicy, nullptr, nullptr,
aDocument, aLoadGroup, aCallbacks, true);
}
} // namespace mozilla

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

@ -8,38 +8,13 @@
#define FontPreloader_h_
#include "mozilla/FetchPreloader.h"
#include "nsContentSecurityManager.h"
#include "nsIContentPolicy.h"
#include "nsILoadInfo.h"
class gfxUserFontEntry;
struct gfxFontFaceSrc;
class nsIHttpChannel;
class nsIReferrerInfo;
namespace mozilla {
namespace dom {
class WorkerPrivate;
}
class FontPreloader final : public FetchPreloader {
public:
FontPreloader();
static nsresult BuildChannel(
nsIChannel** aChannel, nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
gfxUserFontEntry* aUserFontEntry, const gfxFontFaceSrc* aFontFaceSrc,
dom::Document* aDocument, nsILoadGroup* aLoadGroup,
nsIInterfaceRequestor* aCallbacks, bool aIsPreload);
static nsresult BuildChannel(
nsIChannel** aChannel, nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
gfxUserFontEntry* aUserFontEntry, const gfxFontFaceSrc* aFontFaceSrc,
dom::WorkerPrivate* aWorkerPrivate, nsILoadGroup* aLoadGroup,
nsIInterfaceRequestor* aCallbacks, bool aIsPreload);
protected:
nsresult CreateChannel(nsIChannel** aChannel, nsIURI* aURI,
const CORSMode aCORSMode,
@ -47,16 +22,6 @@ class FontPreloader final : public FetchPreloader {
dom::Document* aDocument, nsILoadGroup* aLoadGroup,
nsIInterfaceRequestor* aCallbacks,
uint64_t aEarlyHintPreloaderId) override;
static void BuildChannelFlags(
nsIURI* aURI, bool aIsPreload,
nsContentSecurityManager::CORSSecurityMapping& aCorsMapping,
nsSecurityFlags& aSecurityFlags, nsContentPolicyType& aContentPolicyType);
static nsresult BuildChannelSetup(nsIChannel* aChannel,
nsIHttpChannel* aHttpChannel,
nsIReferrerInfo* aReferrerInfo,
const gfxFontFaceSrc* aFontFaceSrc);
};
} // namespace mozilla

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

@ -76,6 +76,7 @@ EXPORTS.mozilla += [
"CSSPropFlags.h",
"DeclarationBlock.h",
"DocumentStyleRootIterator.h",
"FontLoaderUtils.h",
"FontPreloader.h",
"GeckoBindings.h",
"GlobalStyleSheetCache.h",
@ -200,6 +201,7 @@ UNIFIED_SOURCES += [
"FontFaceSetImpl.cpp",
"FontFaceSetIterator.cpp",
"FontFaceSetWorkerImpl.cpp",
"FontLoaderUtils.cpp",
"FontPreloader.cpp",
"GeckoBindings.cpp",
"GlobalStyleSheetCache.cpp",