Donner SVG main
0.8.0 · 382bd0190.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::svg::components::ResourceManagerContext Class Reference

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 &registry, 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< 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)
 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.
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.

Detailed Description

Resource manager, which handles loading resources from URLs and caching results.


Class Documentation

◆ donner::svg::components::ResourceManagerContext::FetchSecurityStats

struct donner::svg::components::ResourceManagerContext::FetchSecurityStats

External resource fetch attempts, cache use, and limit state.

Class Members
size_t attempts = 0 Fetch attempts charged against the document limit.
size_t cachedBytes = 0 Cumulative response bytes admitted to the cache.
size_t cacheHits = 0 Requests answered by cached positive or failed outcomes.
bool rejected = false Whether URL validation or a fetch budget rejected a request.

Member Function Documentation

◆ addFontFaces()

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.

Parameters
fontFacesFont faces to load.

◆ getImageSize()

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.

Parameters
entityEntity to get the image size for.

◆ loadExternalSVG()

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.

Parameters
urlURL of the external SVG to load.
warningSinkSink to collect warnings.
Returns
Parsed document handle, or std::nullopt on failure.

◆ loadResources()

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.

Parameters
warningSinkSink to collect warnings.

◆ setProcessingMode()

void donner::svg::components::ResourceManagerContext::setProcessingMode ( ProcessingMode mode)
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.

Parameters
modeProcessing mode to set.

◆ setResourceLoader()

void donner::svg::components::ResourceManagerContext::setResourceLoader ( std::unique_ptr< ResourceLoaderInterface > && loader)

Set the user-supplied ResourceLoaderInterface which handles loading URLs and returning their contents.

Parameters
loaderResource loader interface, which will be held until overridden. Call this API again with nullptr to unset.

◆ setSvgParseCallback()

void donner::svg::components::ResourceManagerContext::setSvgParseCallback ( SubDocumentCache::ParseCallback callback)
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.

Parameters
callbackCallback that parses SVG bytes into an SVGDocumentHandle.

The documentation for this class was generated from the following file: