1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 ChromeUtils.defineESModuleGetters(lazy, {
9 "resource://devtools/shared/network-observer/NetworkHelper.sys.mjs",
13 * Global cache session object.
15 let gCacheSession = null;
18 * Get (and create if necessary) a cache session / cache storage session.
20 * @param {nsIRequest} request
22 function getCacheSession(request) {
25 const cacheService = Services.cache2;
27 let loadContext = lazy.NetworkHelper.getRequestLoadContext(request);
29 // Get default load context if we can't fetch.
30 loadContext = Services.loadContextInfo.default;
32 gCacheSession = cacheService.diskCacheStorage(loadContext);
43 * Parses a cache entry returned from the backend to build a response cache
46 * @param {nsICacheEntry} cacheEntry
47 * The cache entry from the backend.
50 * A responseCache object expected by RDP.
52 function buildResponseCacheObject(cacheEntry) {
53 const cacheObject = {};
55 if (cacheEntry.storageDataSize) {
56 cacheObject.storageDataSize = cacheEntry.storageDataSize;
59 // We just need to handle this in case it's a js file of 0B.
61 if (cacheEntry.expirationTime) {
62 cacheObject.expirationTime = cacheEntry.expirationTime;
64 if (cacheEntry.fetchCount) {
65 cacheObject.fetchCount = cacheEntry.fetchCount;
67 if (cacheEntry.lastFetched) {
68 cacheObject.lastFetched = cacheEntry.lastFetched;
70 if (cacheEntry.lastModified) {
71 cacheObject.lastModified = cacheEntry.lastModified;
73 if (cacheEntry.deviceID) {
74 cacheObject.deviceID = cacheEntry.deviceID;
80 * Does the fetch for the cache entry from the session.
82 * @param {nsIRequest} request
86 * Promise which resolve a response cache object object, or null if none
89 export function getResponseCacheObject(request) {
90 const cacheSession = getCacheSession(request);
95 return new Promise(resolve => {
96 cacheSession.asyncOpenURI(
99 Ci.nsICacheStorage.OPEN_SECRETLY,
101 onCacheEntryCheck: () => {
102 return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
104 onCacheEntryAvailable: cacheEntry => {
106 const cacheObject = buildResponseCacheObject(cacheEntry);
107 resolve(cacheObject);