MCPcopy Create free account
hub / github.com/Snapchat/Valdi / getArchiveForModule

Method getArchiveForModule

valdi/src/valdi/runtime/Resources/ResourceManager.cpp:80–151  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78 return modulePath.append(".valdimodule");
79}
80
81static StringBox resolveSourceMapFilePath(const StringBox& modulePath) {
82 return modulePath.append(".map.json");
83}
84
85static int32_t parseApiVersion(const BytesView& bytes, ILogger& logger) {
86 TextParser parser(std::string_view(reinterpret_cast<const char*>(bytes.data()), bytes.size()));
87
88 parser.tryParseWhitespaces();
89 auto version = parser.parseUInt();
90 parser.tryParseWhitespaces();
91
92 if (!version.has_value() || !parser.isAtEnd() || version.value() > std::numeric_limits<int32_t>::max()) {
93 VALDI_WARN(logger, "Invalid valdi_api_version resource content. Expected a non-negative integer.");
94 return 0;
95 }
96
97 return static_cast<int32_t>(version.value());
98}
99
100int32_t ResourceManager::getApiVersion() {
101 {
102 std::lock_guard<Mutex> guard(_mutex);
103 if (_apiVersion.has_value()) {
104 return _apiVersion.value();
105 }
106 }
107
108 // Loaded outside _mutex: the resource loader crosses into the platform and on the first call can
109 // initialize the Dynamic Delivery archive, which waits on the content manager. Holding _mutex
110 // across it blocked the main thread in preloadForComponentPath for 10s+ (MUSIC-13144,
111 // SHARING-35215). Concurrent first callers may both reach the loader; the Dynamic Delivery
112 // archive init behind it is a std::call_once, so the heavy work still runs once.
113 auto content = _resourceLoader->loadModuleContent(STRING_LITERAL("valdi_api_version"));
114 int32_t apiVersion = content ? parseApiVersion(content.value(), _logger) : 0;
115
116 std::lock_guard<Mutex> guard(_mutex);
117 if (!_apiVersion.has_value()) {
118 _apiVersion = apiVersion;
119 }
120 return _apiVersion.value();
121}
122
123Result<Ref<ValdiModuleArchive>> ResourceManager::getArchiveForModule(const StringBox& modulePath,
124 Ref<ModuleArchiveCache> archiveCache,
125 const Ref<Metrics>& metrics) {
126 auto bundleFilePath = resolveModuleArchiveFilePath(modulePath);
127 // A pack that cannot be used (disabled after a crash loop, locked by another process) costs nothing.
128 if (archiveCache != nullptr && !archiveCache->isUsable(metrics, _logger)) {
129 archiveCache = nullptr;
130 }
131
132 // NOTE: do not acquire _mutex anywhere in this function. It is called from getBundle while the
133 // BundleInitializer holds the Bundle's mutex; the cleanup path (removeUnusedResources) takes
134 // _mutex and then a Bundle mutex, so taking _mutex here inverts that order and can deadlock.
135 // The cache and metrics are snapshotted by getBundle under _mutex and passed in.
136
137 std::optional<ModuleContentIdentity> sourceIdentity;

Callers

nothing calls this directly

Calls 15

rethrowMethod · 0.80
emitModuleArchiveHeapMethod · 0.80
appendMethod · 0.65
errorMethod · 0.65
valueMethod · 0.65
loadModuleContentMethod · 0.45
toStringViewMethod · 0.45

Tested by

no test coverage detected