Files
desktop/src/zen/mods/ZenStyleSheetCache.cpp
mr. m 199da1f824 chore: Updated to firefox 142.0, p=#9870
* chore: Updateed to firefox 142.0, b=no-bug, c=l10n, folders

* chore: Finish updating to firefox, b=no-bug, c=tabs

* chore: Fixed mods builds, b=no-bug, c=mods

* feat: Small changes to tabs layout, b=no-bug, c=tabs, compact-mode, folders, workspaces

* test: Fixed tests, b=no-bug, c=scripts, tests, folders

* test: Fixed tests, b=no-bug, c=tabs, tests, welcome
2025-08-13 23:21:38 +02:00

97 lines
2.6 KiB
C++

/* 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 "ZenStyleSheetCache.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsCOMPtr.h"
#include "nsIFile.h"
#include "nsNetUtil.h"
#include "nsStyleSheetService.h"
#include "mozilla/css/SheetParsingMode.h"
#include "mozilla/GlobalStyleSheetCache.h"
#define GET_MODS_FILE(chromeFile, err) \
NS_GetSpecialDirectory(NS_APP_USER_CHROME_DIR, getter_AddRefs(chromeFile)); \
if (!chromeFile) { \
return err; \
} \
chromeFile->Append(ZEN_MODS_FILENAME);
namespace zen {
using namespace mozilla;
NS_IMPL_ISUPPORTS(ZenStyleSheetCache, nsISupports)
auto ZenStyleSheetCache::GetModsSheet() -> StyleSheet* {
if (mModsSheet) {
// If the mods stylesheet is already loaded, return it.
return mModsSheet;
}
nsCOMPtr<nsIFile> chromeFile;
GET_MODS_FILE(chromeFile, nullptr);
// Create the mods stylesheet if it doesn't exist.
bool exists;
chromeFile->Exists(&exists);
if (!exists) {
nsresult rv = chromeFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644);
if (NS_FAILED(rv)) {
return nullptr;
}
}
LoadSheetFile(chromeFile, css::eUserSheetFeatures);
return mModsSheet;
}
auto ZenStyleSheetCache::LoadSheetFile(nsIFile* aFile,
css::SheetParsingMode aParsingMode)
-> void {
nsCOMPtr<nsIURI> uri;
NS_NewFileURI(getter_AddRefs(uri), aFile);
if (!uri) {
return;
}
auto loader = new mozilla::css::Loader;
auto result = loader->LoadSheetSync(uri, aParsingMode,
css::Loader::UseSystemPrincipal::Yes);
if (MOZ_UNLIKELY(result.isErr())) {
return;
}
mModsSheet = result.unwrapOr(nullptr);
}
/* static */
auto ZenStyleSheetCache::Singleton() -> ZenStyleSheetCache* {
MOZ_ASSERT(NS_IsMainThread());
if (!gZenModsCache) {
gZenModsCache = new ZenStyleSheetCache;
}
return gZenModsCache;
}
nsresult ZenStyleSheetCache::RebuildModsStylesheets(const nsACString& aContents) {
// Re-parse the mods stylesheet. By doing so, we read
// Once we have the data as a nsACString, we call ReparseSheet from the
// StyleSheet class to re-parse the stylesheet.
auto sheet = GetModsSheet();
if (!sheet) {
return NS_ERROR_FAILURE;
}
ErrorResult aRv;
sheet->ReparseSheet(aContents, aRv);
if (auto sss = nsStyleSheetService::GetInstance()) {
sss->ZenMarkStylesAsChanged();
}
return aRv.StealNSResult();
}
mozilla::StaticRefPtr<ZenStyleSheetCache> ZenStyleSheetCache::gZenModsCache;
} // namespace: zen