Resource manager, which handles loading resources from URLs and caching results. More...
#include "donner/svg/components/resources/ResourceManagerContext.h"
Classes | |
| struct | FetchSecurityStats |
| External resource fetch attempts, cache use, and limit state. More... | |
Public Member Functions | |
| ResourceManagerContext (Registry ®istry, size_t maximumAggregateResourceSize=kDefaultMaximumAggregateResourceSize, size_t maximumExternalFetchAttempts=kDefaultMaximumExternalFetchAttempts) | |
| Constructor. | |
| void | loadResources (ParseWarningSink &warningSink) |
| Load resources such as images. | |
| void | setResourceLoader (std::unique_ptr< ResourceLoaderInterface > &&loader) |
| Set the user-supplied ResourceLoaderInterface which handles loading URLs and returning their contents. | |
| const FetchSecurityStats & | fetchSecurityStats () const |
| Return external callback/cache resource-limit accounting. | |
| void | setProcessingMode (ProcessingMode mode) |
| Set the processing mode for this document. | |
| uint64_t | cacheGeneration () const |
| Identity of the resource loader, processing mode and SVG parse callback for derived caches. | |
| ProcessingMode | processingMode () const |
| Current processing policy, shared with non-resource document systems. | |
| void | setSvgParseCallback (SubDocumentCache::ParseCallback callback) |
| Set the callback used to parse SVG content into sub-documents. | |
| std::optional< SVGDocumentHandle > | loadExternalSVG (std::string_view url, ParseWarningSink &warningSink) |
| Load an external SVG document by URL, for use by <use> elements referencing external files. | |
| std::optional< SVGDocumentHandle > | loadAdmittedExternalSVG (std::string_view url, ParseWarningSink &warningSink) |
| Load an external SVG document like loadExternalSVG, admitting each URL at most once per admission pass. | |
| void | beginExternalSVGAdmissionPass () |
| Start a new admission pass for loadAdmittedExternalSVG: every URL is admitted again. Call this at the start of each full render-tree rebuild. | |
| bool | loadStylesheetImports (ParseWarningSink &warningSink) |
| Resolve and load the @import rules of every author stylesheet in the document, storing the imported stylesheets on each StylesheetComponent in cascade order. | |
| std::optional< Vector2i > | getImageSize (Entity entity) const |
| Get the size of an image resource for an entity, if it has one and successfully loaded. | |
| void | addFontFaces (std::span< const css::FontFace > fontFaces) |
| Add a list of css::FontFace objects to be loaded. | |
| void | synchronizeStylesheetFontFaces (Entity stylesheetEntity, std::span< const css::FontFace > fontFaces, std::uint64_t revision=0) |
| Register a stylesheet's font faces once for its current parsed storage. | |
| const std::vector< css::FontFace > & | fontFaces () const |
| Get all registered @font-face declarations. | |
| std::optional< std::size_t > | registerStylesheetFontFace (const css::FontFace &face) |
| Admit an applicable stylesheet face and return its index, or nullopt on refusal. | |
| void | setLayeredStylesheetFontOrder (std::vector< std::size_t > authorOrder, std::optional< std::uint64_t > documentRevision) |
| Install active document faces in ascending cascade precedence. | |
| bool | hasLayeredFontOrderFor (std::uint64_t documentRevision) const |
| A repeated text query can reuse the live attachment and stylesheet order at this revision. | |
| const std::optional< std::vector< std::size_t > > & | layeredFontFaceOrder () const |
| Programmatic and active ranked stylesheet faces, as indexes into fontFaces(). | |
| size_t | pendingFontFaceCount () const |
| Font faces still awaiting loading or registration. | |
| bool | stylesheetFontFaceLimitRejected () const |
| Whether stylesheet font-face registration exceeded its document limit. | |
| size_t | stylesheetFontFaceCountForTesting () const |
| Current stylesheet font-face count exposed to tests. | |
| size_t | stylesheetFontFaceRegistrationCountForTesting () const |
| Number of stylesheet registration records exposed to tests. | |
Static Public Attributes | |
| static constexpr size_t | kDefaultMaximumAggregateResourceSize = 64 * 1024 * 1024 |
| Default aggregate byte budget for raw and decoded document resources. | |
| static constexpr size_t | kMaximumResourceFetchAttempts = 256 |
| Maximum number of application resource-loader calls for one document. | |
| static constexpr size_t | kDefaultMaximumExternalFetchAttempts = kMaximumResourceFetchAttempts |
| Default number of unique external resource callbacks allowed per document. | |
| static constexpr size_t | kMaximumStylesheetFontFaces = 1024 |
| Maximum stylesheet-provided font faces retained by one untrusted document. | |
Resource manager, which handles loading resources from URLs and caching results.
| struct donner::svg::components::ResourceManagerContext::FetchSecurityStats |
External resource fetch attempts, cache use, and limit state.
| void donner::svg::components::ResourceManagerContext::addFontFaces | ( | std::span< const css::FontFace > | fontFaces | ) |
Add a list of css::FontFace objects to be loaded.
Registration is idempotent. The style pass re-announces every stylesheet's @font-face rules on each recompute, so a declaration already registered is not stored a second time; it is only re-queued for loading, which retries a source that has not resolved yet and does nothing for one that has. Storing duplicates instead would re-fetch each URL and re-wrap its bytes in a fresh buffer every recompute, which in turn gives the font layer a new identity for a font that never changed.
| fontFaces | Font faces to load. |
| std::optional< Vector2i > donner::svg::components::ResourceManagerContext::getImageSize | ( | Entity | entity | ) | const |
Get the size of an image resource for an entity, if it has one and successfully loaded.
| entity | Entity to get the image size for. |
| std::optional< SVGDocumentHandle > donner::svg::components::ResourceManagerContext::loadAdmittedExternalSVG | ( | std::string_view | url, |
| ParseWarningSink & | warningSink ) |
Load an external SVG document like loadExternalSVG, admitting each URL at most once per admission pass.
A later request for an admitted URL reuses that admission while the loader and SubDocumentCache::admissionFingerprint are unchanged: the document's size, the families of this document and of the admitted one, and their limits. A nested document of the admitted one that grew or changed families is only re-checked when the next pass starts. A full render-tree rebuild starts a new pass with beginExternalSVGAdmissionPass. Only successful loads are reused; a failed one goes through loadExternalSVG again, which applies its own negative cache. Admitted documents stay referenced here until the next pass starts.
| url | URL of the external SVG to load. |
| warningSink | Sink to collect warnings. |
| std::optional< SVGDocumentHandle > donner::svg::components::ResourceManagerContext::loadExternalSVG | ( | std::string_view | url, |
| ParseWarningSink & | warningSink ) |
Load an external SVG document by URL, for use by <use> elements referencing external files.
The document is cached in the \ref SubDocumentCache.
| url | URL of the external SVG to load. |
| warningSink | Sink to collect warnings. |
| void donner::svg::components::ResourceManagerContext::loadResources | ( | ParseWarningSink & | warningSink | ) |
Load resources such as images.
Note that this doesn't issue network calls directly, but relies on the user's application to handle callbacks for loading URLs and returning their contents.
| warningSink | Sink to collect warnings. |
| bool donner::svg::components::ResourceManagerContext::loadStylesheetImports | ( | ParseWarningSink & | warningSink | ) |
Resolve and load the @import rules of every author stylesheet in the document, storing the imported stylesheets on each StylesheetComponent in cascade order.
Imports are fetched only through the application's ResourceLoaderInterface, sharing the external-fetch cache, fetch-attempt limit, and aggregate byte budget used by images, fonts, and external SVG documents; data: URLs are decoded without the loader. With no loader, or in secure processing modes, external imports are diagnosed and ignored without any fetch. One pass is additionally bounded by StylesheetImportBudget for depth, import count, bytes, and parsed size. Stylesheets already resolved for the current cacheGeneration are not reloaded.
The resource loader runs during this call, so it must not be called while document write access is held.
| warningSink | Sink to collect warnings, mapped to the importing <style> source. |
|
inline |
Set the processing mode for this document.
In secure modes (ProcessingMode::SecureStatic, ProcessingMode::SecureAnimated), external resource loading is disabled per SVG2 §2.7.1.
| mode | Processing mode to set. |
| void donner::svg::components::ResourceManagerContext::setResourceLoader | ( | std::unique_ptr< ResourceLoaderInterface > && | loader | ) |
Set the user-supplied ResourceLoaderInterface which handles loading URLs and returning their contents.
| loader | Resource loader interface, which will be held until overridden. Call this API again with nullptr to unset. |
|
inline |
Set the callback used to parse SVG content into sub-documents.
This is called when an <image> element references an SVG file. The callback is injected to avoid circular build dependencies between the component layer and SVGParser.
| callback | Callback that parses SVG bytes into an SVGDocumentHandle. |
| void donner::svg::components::ResourceManagerContext::synchronizeStylesheetFontFaces | ( | Entity | stylesheetEntity, |
| std::span< const css::FontFace > | fontFaces, | ||
| std::uint64_t | revision = 0 ) |
Register a stylesheet's font faces once for its current parsed storage.
| stylesheetEntity | Entity owning the stylesheet. |
| fontFaces | Font faces the stylesheet contributes. |
| revision | Content revision of fontFaces; a change re-registers even when the storage address and size are reused. |