diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /devtools/shared/platform/CacheEntry.sys.mjs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/shared/platform/CacheEntry.sys.mjs')
-rw-r--r-- | devtools/shared/platform/CacheEntry.sys.mjs | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/devtools/shared/platform/CacheEntry.sys.mjs b/devtools/shared/platform/CacheEntry.sys.mjs new file mode 100644 index 0000000000..761f0d2946 --- /dev/null +++ b/devtools/shared/platform/CacheEntry.sys.mjs @@ -0,0 +1,115 @@ +/* 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/. */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + NetworkHelper: + "resource://devtools/shared/network-observer/NetworkHelper.sys.mjs", +}); + +/** + * Global cache session object. + */ +let gCacheSession = null; + +/** + * Get (and create if necessary) a cache session / cache storage session. + * + * @param {nsIRequest} request + */ +function getCacheSession(request) { + if (!gCacheSession) { + try { + const cacheService = Services.cache2; + if (cacheService) { + let loadContext = lazy.NetworkHelper.getRequestLoadContext(request); + if (!loadContext) { + // Get default load context if we can't fetch. + loadContext = Services.loadContextInfo.default; + } + gCacheSession = cacheService.diskCacheStorage(loadContext); + } + } catch (e) { + gCacheSession = null; + } + } + + return gCacheSession; +} + +/** + * Parses a cache entry returned from the backend to build a response cache + * object. + * + * @param {nsICacheEntry} cacheEntry + * The cache entry from the backend. + * + * @returns {Object} + * A responseCache object expected by RDP. + */ +function buildResponseCacheObject(cacheEntry) { + const cacheObject = {}; + try { + if (cacheEntry.storageDataSize) { + cacheObject.storageDataSize = cacheEntry.storageDataSize; + } + } catch (e) { + // We just need to handle this in case it's a js file of 0B. + } + if (cacheEntry.expirationTime) { + cacheObject.expirationTime = cacheEntry.expirationTime; + } + if (cacheEntry.fetchCount) { + cacheObject.fetchCount = cacheEntry.fetchCount; + } + if (cacheEntry.lastFetched) { + cacheObject.lastFetched = cacheEntry.lastFetched; + } + if (cacheEntry.lastModified) { + cacheObject.lastModified = cacheEntry.lastModified; + } + if (cacheEntry.deviceID) { + cacheObject.deviceID = cacheEntry.deviceID; + } + return cacheObject; +} + +/** + * Does the fetch for the cache entry from the session. + * + * @param {nsIRequest} request + * The request object. + * + * @returns {Promise} + * Promise which resolve a response cache object object, or null if none + * was available. + */ +export function getResponseCacheObject(request) { + const cacheSession = getCacheSession(request); + if (!cacheSession) { + return null; + } + + return new Promise(resolve => { + cacheSession.asyncOpenURI( + request.URI, + "", + Ci.nsICacheStorage.OPEN_SECRETLY, + { + onCacheEntryCheck: entry => { + return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED; + }, + onCacheEntryAvailable: (cacheEntry, isnew, status) => { + if (cacheEntry) { + const cacheObject = buildResponseCacheObject(cacheEntry); + resolve(cacheObject); + } else { + resolve(null); + } + }, + } + ); + }); +} |