Bug 1571530 - Make Loader APIs return a Result. r=heycam

On the fence on this one, but I do think it's nicer.

Differential Revision: https://phabricator.services.mozilla.com/D40850

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Emilio Cobos Álvarez 2019-08-16 10:56:03 +00:00
Родитель 353176ffe4
Коммит 2353359865
8 изменённых файлов: 118 добавлений и 167 удалений

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

@ -6485,9 +6485,12 @@ nsresult Document::LoadAdditionalStyleSheet(additionalSheetType aType,
MOZ_CRASH("impossible value for aType");
}
RefPtr<StyleSheet> sheet;
nsresult rv = loader->LoadSheetSync(aSheetURI, parsingMode, true, &sheet);
NS_ENSURE_SUCCESS(rv, rv);
auto result = loader->LoadSheetSync(aSheetURI, parsingMode, true);
if (result.isErr()) {
return result.unwrapErr();
}
RefPtr<StyleSheet> sheet = result.unwrap();
sheet->SetAssociatedDocumentOrShadowRoot(
this, StyleSheet::OwnedByDocumentOrShadowRoot);
@ -11442,15 +11445,15 @@ void Document::PreloadStyle(
ReferrerInfo::CreateFromDocumentAndPolicyOverride(this, aReferrerPolicy);
// Charset names are always ASCII.
CSSLoader()->LoadSheet(uri, true, NodePrincipal(), aEncoding, referrerInfo,
obs, Element::StringToCORSMode(aCrossOriginAttr),
aIntegrity);
Unused << CSSLoader()->LoadSheet(
uri, true, NodePrincipal(), aEncoding, referrerInfo, obs,
Element::StringToCORSMode(aCrossOriginAttr), aIntegrity);
}
RefPtr<StyleSheet> Document::LoadChromeSheetSync(nsIURI* uri) {
RefPtr<StyleSheet> sheet;
CSSLoader()->LoadSheetSync(uri, css::eAuthorSheetFeatures, false, &sheet);
return sheet;
return CSSLoader()
->LoadSheetSync(uri, css::eAuthorSheetFeatures, false)
.unwrapOr(nullptr);
}
void Document::ResetDocumentDirection() {

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

@ -3107,18 +3107,16 @@ nsresult HTMLEditor::AddOverrideStyleSheetInternal(const nsAString& aURL) {
// We MUST ONLY load synchronous local files (no @import)
// XXXbz Except this will actually try to load remote files
// synchronously, of course..
RefPtr<StyleSheet> sheet;
// Editor override style sheets may want to style Gecko anonymous boxes
DebugOnly<nsresult> ignoredRv =
presShell->GetDocument()->CSSLoader()->LoadSheetSync(
uaURI, css::eAgentSheetFeatures, true, &sheet);
NS_WARNING_ASSERTION(NS_SUCCEEDED(ignoredRv), "LoadSheetSync() failed");
auto result = presShell->GetDocument()->CSSLoader()->LoadSheetSync(
uaURI, css::eAgentSheetFeatures, true);
// Synchronous loads should ALWAYS return completed
if (NS_WARN_IF(!sheet)) {
return NS_ERROR_FAILURE;
if (NS_WARN_IF(result.isErr())) {
return result.unwrapErr();
}
RefPtr<StyleSheet> sheet = result.unwrap();
// Add the override style sheet
// (This checks if already exists)
presShell->AddOverrideStyleSheet(sheet);

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

@ -181,12 +181,6 @@ nsStyleSheetService::LoadAndRegisterSheet(nsIURI* aSheetURI,
return rv;
}
static nsresult LoadSheet(nsIURI* aURI, css::SheetParsingMode aParsingMode,
RefPtr<StyleSheet>* aResult) {
RefPtr<css::Loader> loader = new css::Loader;
return loader->LoadSheetSync(aURI, aParsingMode, true, aResult);
}
nsresult nsStyleSheetService::LoadAndRegisterSheetInternal(
nsIURI* aSheetURI, uint32_t aSheetType) {
NS_ENSURE_ARG_POINTER(aSheetURI);
@ -210,12 +204,12 @@ nsresult nsStyleSheetService::LoadAndRegisterSheetInternal(
return NS_ERROR_INVALID_ARG;
}
RefPtr<StyleSheet> sheet;
nsresult rv = LoadSheet(aSheetURI, parsingMode, &sheet);
NS_ENSURE_SUCCESS(rv, rv);
MOZ_ASSERT(sheet);
mSheets[aSheetType].AppendElement(sheet);
RefPtr<css::Loader> loader = new css::Loader;
auto result = loader->LoadSheetSync(aSheetURI, parsingMode, true);
if (result.isErr()) {
return result.unwrapErr();
}
mSheets[aSheetType].AppendElement(result.unwrap());
return NS_OK;
}

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

@ -2149,70 +2149,60 @@ nsresult Loader::LoadChildSheet(StyleSheet* aParentSheet,
return rv;
}
nsresult Loader::LoadSheetSync(nsIURI* aURL, SheetParsingMode aParsingMode,
bool aUseSystemPrincipal,
RefPtr<StyleSheet>* aSheet) {
Result<RefPtr<StyleSheet>, nsresult> Loader::LoadSheetSync(
nsIURI* aURL, SheetParsingMode aParsingMode, bool aUseSystemPrincipal) {
LOG(("css::Loader::LoadSheetSync"));
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo(nullptr);
return InternalLoadNonDocumentSheet(aURL, false, aParsingMode,
aUseSystemPrincipal, nullptr, nullptr,
aSheet, referrerInfo, nullptr);
return InternalLoadNonDocumentSheet(
aURL, false, aParsingMode, aUseSystemPrincipal, nullptr, nullptr,
referrerInfo, nullptr, CORS_NONE, EmptyString());
}
nsresult Loader::LoadSheet(nsIURI* aURL, SheetParsingMode aParsingMode,
bool aUseSystemPrincipal,
nsICSSLoaderObserver* aObserver,
RefPtr<StyleSheet>* aSheet) {
LOG(
("css::Loader::LoadSheet(aURL, aParsingMode, aUseSystemPrincipal, "
"aObserver, aSheet)"));
Result<RefPtr<StyleSheet>, nsresult> Loader::LoadSheet(
nsIURI* aURI, SheetParsingMode aParsingMode, bool aUseSystemPrincipal,
nsICSSLoaderObserver* aObserver) {
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo(nullptr);
return InternalLoadNonDocumentSheet(aURL, false, aParsingMode,
aUseSystemPrincipal, nullptr, nullptr,
aSheet, referrerInfo, aObserver);
return InternalLoadNonDocumentSheet(
aURI, false, aParsingMode, aUseSystemPrincipal, nullptr, nullptr,
referrerInfo, aObserver, CORS_NONE, EmptyString());
}
nsresult Loader::LoadSheet(nsIURI* aURL, bool aIsPreload,
nsIPrincipal* aOriginPrincipal,
const Encoding* aPreloadEncoding,
nsIReferrerInfo* aReferrerInfo,
nsICSSLoaderObserver* aObserver, CORSMode aCORSMode,
const nsAString& aIntegrity) {
Result<RefPtr<StyleSheet>, nsresult> Loader::LoadSheet(
nsIURI* aURL, bool aIsPreload, nsIPrincipal* aOriginPrincipal,
const Encoding* aPreloadEncoding, nsIReferrerInfo* aReferrerInfo,
nsICSSLoaderObserver* aObserver, CORSMode aCORSMode,
const nsAString& aIntegrity) {
LOG(("css::Loader::LoadSheet(aURL, aObserver) api call"));
return InternalLoadNonDocumentSheet(aURL, aIsPreload, eAuthorSheetFeatures,
false, aOriginPrincipal, aPreloadEncoding,
nullptr, aReferrerInfo, aObserver,
aCORSMode, aIntegrity);
return InternalLoadNonDocumentSheet(
aURL, aIsPreload, eAuthorSheetFeatures, false, aOriginPrincipal,
aPreloadEncoding, aReferrerInfo, aObserver, aCORSMode, aIntegrity);
}
nsresult Loader::InternalLoadNonDocumentSheet(
Result<RefPtr<StyleSheet>, nsresult> Loader::InternalLoadNonDocumentSheet(
nsIURI* aURL, bool aIsPreload, SheetParsingMode aParsingMode,
bool aUseSystemPrincipal, nsIPrincipal* aOriginPrincipal,
const Encoding* aPreloadEncoding, RefPtr<StyleSheet>* aSheet,
nsIReferrerInfo* aReferrerInfo, nsICSSLoaderObserver* aObserver,
CORSMode aCORSMode, const nsAString& aIntegrity) {
const Encoding* aPreloadEncoding, nsIReferrerInfo* aReferrerInfo,
nsICSSLoaderObserver* aObserver, CORSMode aCORSMode,
const nsAString& aIntegrity) {
MOZ_ASSERT(aURL, "Must have a URI to load");
MOZ_ASSERT(aSheet || aObserver, "Sheet and observer can't both be null");
MOZ_ASSERT(!aUseSystemPrincipal || !aObserver,
"Shouldn't load system-principal sheets async");
MOZ_ASSERT(aReferrerInfo, "Must have referrerInfo");
LOG_URI(" Non-document sheet uri: '%s'", aURL);
if (aSheet) {
*aSheet = nullptr;
}
if (!mEnabled) {
LOG_WARN((" Not enabled"));
return NS_ERROR_NOT_AVAILABLE;
return Err(NS_ERROR_NOT_AVAILABLE);
}
nsCOMPtr<nsIPrincipal> loadingPrincipal =
(aOriginPrincipal && mDocument ? mDocument->NodePrincipal() : nullptr);
nsresult rv = CheckContentPolicy(loadingPrincipal, aOriginPrincipal, aURL,
mDocument, aIsPreload);
NS_ENSURE_SUCCESS(rv, rv);
if (NS_FAILED(rv)) {
return Err(rv);
}
bool syncLoad = !aObserver;
@ -2230,11 +2220,11 @@ nsresult Loader::InternalLoadNonDocumentSheet(
if (aObserver || !mObservers.IsEmpty()) {
rv = PostLoadEvent(aURL, sheet, aObserver, IsAlternate::No,
MediaMatched::Yes, aReferrerInfo, nullptr);
if (NS_FAILED(rv)) {
return Err(rv);
}
}
if (aSheet) {
sheet.swap(*aSheet);
}
return rv;
return sheet;
}
SheetLoadData* data = new SheetLoadData(
@ -2243,16 +2233,13 @@ nsresult Loader::InternalLoadNonDocumentSheet(
NS_ADDREF(data);
rv = LoadSheet(data, state, aIsPreload);
NS_ENSURE_SUCCESS(rv, rv);
if (aSheet) {
sheet.swap(*aSheet);
if (NS_FAILED(rv)) {
return Err(rv);
}
if (aObserver) {
data->mMustNotify = true;
}
return rv;
return sheet;
}
nsresult Loader::PostLoadEvent(nsIURI* aURI, StyleSheet* aSheet,

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

@ -190,7 +190,6 @@ class Loader final {
* (see comments at enum SheetParsingMode, above).
* @param aUseSystemPrincipal if true, give the resulting sheet the system
* principal no matter where it's being loaded from.
* @param [out] aSheet the loaded, complete sheet.
*
* NOTE: At the moment, this method assumes the sheet will be UTF-8, but
* ideally it would allow arbitrary encodings. Callers should NOT depend on
@ -200,16 +199,9 @@ class Loader final {
* whether the data could be parsed as CSS and doesn't indicate anything
* about the status of child sheets of the returned sheet.
*/
nsresult LoadSheetSync(nsIURI* aURL, SheetParsingMode aParsingMode,
bool aUseSystemPrincipal, RefPtr<StyleSheet>* aSheet);
/**
* As above, but defaults aParsingMode to eAuthorSheetFeatures and
* aUseSystemPrincipal to false.
*/
nsresult LoadSheetSync(nsIURI* aURL, RefPtr<StyleSheet>* aSheet) {
return LoadSheetSync(aURL, eAuthorSheetFeatures, false, aSheet);
}
Result<RefPtr<StyleSheet>, nsresult> LoadSheetSync(
nsIURI*, SheetParsingMode = eAuthorSheetFeatures,
bool aUseSystemPrincipal = false);
/**
* Asynchronously load the stylesheet at aURL. If a successful result is
@ -225,28 +217,25 @@ class Loader final {
* @param aReferrerInfo referrer information of the sheet.
* @param aObserver the observer to notify when the load completes.
* Must not be null.
* @param [out] aSheet the sheet to load. Note that the sheet may well
* not be loaded by the time this method returns.
* @return the sheet to load. Note that the sheet may well not be loaded by
* the time this method returns.
*
* NOTE: At the moment, this method assumes the sheet will be UTF-8, but
* ideally it would allow arbitrary encodings. Callers should NOT depend on
* non-UTF8 sheets being treated as UTF-8 by this method.
*/
nsresult LoadSheet(nsIURI* aURL, SheetParsingMode aParsingMode,
bool aUseSystemPrincipal, nsICSSLoaderObserver* aObserver,
RefPtr<StyleSheet>* aSheet);
Result<RefPtr<StyleSheet>, nsresult> LoadSheet(
nsIURI* aURI, bool aIsPreLoad, nsIPrincipal* aOriginPrincipal,
const Encoding* aPreloadEncoding, nsIReferrerInfo* aReferrerInfo,
nsICSSLoaderObserver* aObserver, CORSMode aCORSMode = CORS_NONE,
const nsAString& aIntegrity = EmptyString());
/**
* Same as above, to be used when the caller doesn't care about the
* not-yet-loaded sheet.
* As above, but without caring for a couple things.
*/
nsresult LoadSheet(nsIURI* aURL, bool aIsPreload,
nsIPrincipal* aOriginPrincipal,
const Encoding* aPreloadEncoding,
nsIReferrerInfo* aReferrerInfo,
nsICSSLoaderObserver* aObserver,
CORSMode aCORSMode = CORS_NONE,
const nsAString& aIntegrity = EmptyString());
Result<RefPtr<StyleSheet>, nsresult> LoadSheet(nsIURI*, SheetParsingMode,
bool aUseSystemPrincipal,
nsICSSLoaderObserver*);
/**
* Stop loading all sheets. All nsICSSLoaderObservers involved will be
@ -369,13 +358,12 @@ class Loader final {
// Inserts a style sheet into a parent style sheet.
void InsertChildSheet(StyleSheet& aSheet, StyleSheet& aParentSheet);
nsresult InternalLoadNonDocumentSheet(
Result<RefPtr<StyleSheet>, nsresult> InternalLoadNonDocumentSheet(
nsIURI* aURL, bool aIsPreload, SheetParsingMode aParsingMode,
bool aUseSystemPrincipal, nsIPrincipal* aOriginPrincipal,
const Encoding* aPreloadEncoding, RefPtr<StyleSheet>* aSheet,
nsIReferrerInfo* aReferrerInfo, nsICSSLoaderObserver* aObserver,
CORSMode aCORSMode = CORS_NONE,
const nsAString& aIntegrity = EmptyString());
const Encoding* aPreloadEncoding, nsIReferrerInfo* aReferrerInfo,
nsICSSLoaderObserver* aObserver, CORSMode aCORSMode,
const nsAString& aIntegrity);
// Post a load event for aObserver to be notified about aSheet. The
// notification will be sent with status NS_OK unless the load event is

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

@ -36,9 +36,11 @@ nsresult PreloadedStyleSheet::GetSheet(StyleSheet** aResult) {
if (!mSheet) {
RefPtr<css::Loader> loader = new css::Loader;
nsresult rv = loader->LoadSheetSync(mURI, mParsingMode, true, &mSheet);
NS_ENSURE_SUCCESS(rv, rv);
MOZ_ASSERT(mSheet);
auto result = loader->LoadSheetSync(mURI, mParsingMode, true);
if (result.isErr()) {
return result.unwrapErr();
}
mSheet = result.unwrap();
}
*aResult = mSheet;
@ -47,21 +49,7 @@ nsresult PreloadedStyleSheet::GetSheet(StyleSheet** aResult) {
nsresult PreloadedStyleSheet::Preload() {
MOZ_DIAGNOSTIC_ASSERT(!mLoaded);
// The nsIStyleSheetService.preloadSheet API doesn't tell us which backend
// the sheet will be used with, and it seems wasteful to eagerly create
// both a CSSStyleSheet and a ServoStyleSheet. So instead, we guess that
// the sheet type we will want matches the current value of the stylo pref,
// and preload a sheet of that type.
//
// If we guess wrong, we will re-load the sheet later with the requested type,
// and we won't really have front loaded the loading time as the name
// "preload" might suggest. Also, in theory we could get different data from
// fetching the URL again, but for the usage patterns of this API this is
// unlikely, and it doesn't seem worth trying to store the contents of the URL
// and duplicating a bunch of css::Loader's logic.
mLoaded = true;
StyleSheet* sheet;
return GetSheet(&sheet);
}
@ -90,11 +78,14 @@ nsresult PreloadedStyleSheet::PreloadAsync(NotNull<dom::Promise*> aPromise) {
MOZ_DIAGNOSTIC_ASSERT(!mLoaded);
RefPtr<css::Loader> loader = new css::Loader;
RefPtr<StylesheetPreloadObserver> obs =
new StylesheetPreloadObserver(aPromise, this);
return loader->LoadSheet(mURI, mParsingMode, false, obs, &mSheet);
auto result = loader->LoadSheet(mURI, mParsingMode, false, obs);
if (result.isErr()) {
return result.unwrapErr();
}
mSheet = result.unwrap();
return NS_OK;
}
} // namespace mozilla

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

@ -125,7 +125,7 @@ nsresult nsLayoutStylesheetCache::Observe(nsISupports* aSubject,
#define STYLE_SHEET(identifier_, url_, shared_) \
NotNull<StyleSheet*> nsLayoutStylesheetCache::identifier_##Sheet() { \
if (!m##identifier_##Sheet) { \
LoadSheetURL(url_, &m##identifier_##Sheet, eAgentSheetFeatures, eCrash); \
m##identifier_##Sheet = LoadSheetURL(url_, eAgentSheetFeatures, eCrash); \
} \
return WrapNotNull(m##identifier_##Sheet); \
}
@ -241,8 +241,8 @@ nsLayoutStylesheetCache::nsLayoutStylesheetCache() : mUsedSharedMemory(0) {
if (gUserContentSheetURL) {
MOZ_ASSERT(XRE_IsContentProcess(), "Only used in content processes.");
LoadSheet(gUserContentSheetURL, &mUserContentSheet, eUserSheetFeatures,
eLogToConsole);
mUserContentSheet =
LoadSheet(gUserContentSheetURL, eUserSheetFeatures, eLogToConsole);
gUserContentSheetURL = nullptr;
}
@ -446,37 +446,29 @@ void nsLayoutStylesheetCache::InitFromProfile() {
contentFile->Append(NS_LITERAL_STRING("userContent.css"));
chromeFile->Append(NS_LITERAL_STRING("userChrome.css"));
LoadSheetFile(contentFile, &mUserContentSheet, eUserSheetFeatures,
eLogToConsole);
LoadSheetFile(chromeFile, &mUserChromeSheet, eUserSheetFeatures,
eLogToConsole);
mUserContentSheet = LoadSheetFile(contentFile, eUserSheetFeatures);
mUserChromeSheet = LoadSheetFile(chromeFile, eUserSheetFeatures);
}
void nsLayoutStylesheetCache::LoadSheetURL(const char* aURL,
RefPtr<StyleSheet>* aSheet,
SheetParsingMode aParsingMode,
FailureAction aFailureAction) {
RefPtr<StyleSheet> nsLayoutStylesheetCache::LoadSheetURL(
const char* aURL, SheetParsingMode aParsingMode,
FailureAction aFailureAction) {
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), aURL);
LoadSheet(uri, aSheet, aParsingMode, aFailureAction);
if (!aSheet) {
NS_ERROR(nsPrintfCString("Could not load %s", aURL).get());
}
return LoadSheet(uri, aParsingMode, aFailureAction);
}
void nsLayoutStylesheetCache::LoadSheetFile(nsIFile* aFile,
RefPtr<StyleSheet>* aSheet,
SheetParsingMode aParsingMode,
FailureAction aFailureAction) {
RefPtr<StyleSheet> nsLayoutStylesheetCache::LoadSheetFile(
nsIFile* aFile, SheetParsingMode aParsingMode) {
bool exists = false;
aFile->Exists(&exists);
if (!exists) return;
if (!exists) {
return nullptr;
}
nsCOMPtr<nsIURI> uri;
NS_NewFileURI(getter_AddRefs(uri), aFile);
LoadSheet(uri, aSheet, aParsingMode, aFailureAction);
return LoadSheet(uri, aParsingMode, eLogToConsole);
}
static void ErrorLoadingSheet(nsIURI* aURI, const char* aMsg,
@ -495,20 +487,18 @@ static void ErrorLoadingSheet(nsIURI* aURI, const char* aMsg,
MOZ_CRASH_UNSAFE(errorMessage.get());
}
void nsLayoutStylesheetCache::LoadSheet(nsIURI* aURI,
RefPtr<StyleSheet>* aSheet,
SheetParsingMode aParsingMode,
FailureAction aFailureAction) {
RefPtr<StyleSheet> nsLayoutStylesheetCache::LoadSheet(
nsIURI* aURI, SheetParsingMode aParsingMode, FailureAction aFailureAction) {
if (!aURI) {
ErrorLoadingSheet(aURI, "null URI", eCrash);
return;
return nullptr;
}
if (!gCSSLoader) {
gCSSLoader = new Loader;
if (!gCSSLoader) {
ErrorLoadingSheet(aURI, "no Loader", eCrash);
return;
return nullptr;
}
}
@ -517,15 +507,16 @@ void nsLayoutStylesheetCache::LoadSheet(nsIURI* aURI,
// parallel parsing on them. If that ever changes, we'll either need to find a
// different way to prohibit parallel parsing for UA sheets, or handle
// -moz-bool-pref and various other things in the parallel parsing code.
nsresult rv = gCSSLoader->LoadSheetSync(aURI, aParsingMode, true, aSheet);
if (NS_FAILED(rv)) {
auto result = gCSSLoader->LoadSheetSync(aURI, aParsingMode, true);
if (MOZ_UNLIKELY(result.isErr())) {
ErrorLoadingSheet(
aURI,
nsPrintfCString("LoadSheetSync failed with error %" PRIx32,
static_cast<uint32_t>(rv))
static_cast<uint32_t>(result.unwrapErr()))
.get(),
aFailureAction);
}
return result.unwrapOr(nullptr);
}
/* static */

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

@ -114,15 +114,14 @@ class nsLayoutStylesheetCache final : public nsIObserver,
void InitSharedSheetsInParent();
void InitSharedSheetsInChild(already_AddRefed<Shm> aSharedMemory);
void InitMemoryReporter();
void LoadSheetURL(const char* aURL, RefPtr<mozilla::StyleSheet>* aSheet,
mozilla::css::SheetParsingMode aParsingMode,
mozilla::css::FailureAction aFailureAction);
void LoadSheetFile(nsIFile* aFile, RefPtr<mozilla::StyleSheet>* aSheet,
mozilla::css::SheetParsingMode aParsingMode,
mozilla::css::FailureAction aFailureAction);
void LoadSheet(nsIURI* aURI, RefPtr<mozilla::StyleSheet>* aSheet,
mozilla::css::SheetParsingMode aParsingMode,
mozilla::css::FailureAction aFailureAction);
RefPtr<mozilla::StyleSheet> LoadSheetURL(
const char* aURL, mozilla::css::SheetParsingMode aParsingMode,
mozilla::css::FailureAction aFailureAction);
RefPtr<mozilla::StyleSheet> LoadSheetFile(
nsIFile* aFile, mozilla::css::SheetParsingMode aParsingMode);
RefPtr<mozilla::StyleSheet> LoadSheet(
nsIURI* aURI, mozilla::css::SheetParsingMode aParsingMode,
mozilla::css::FailureAction aFailureAction);
void LoadSheetFromSharedMemory(const char* aURL,
RefPtr<mozilla::StyleSheet>* aSheet,
mozilla::css::SheetParsingMode aParsingMode,