Bug 1946184 - Fix computing the CSD margin right after calling HideWindowChrome(...
[gecko.git] / devtools / shared / platform / CacheEntry.sys.mjs
blobc417489addb1180ddd8f56e0d5e5980dd2bf549d
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/. */
5 const lazy = {};
7 ChromeUtils.defineESModuleGetters(lazy, {
8   NetworkHelper:
9     "resource://devtools/shared/network-observer/NetworkHelper.sys.mjs",
10 });
12 /**
13  * Global cache session object.
14  */
15 let gCacheSession = null;
17 /**
18  * Get (and create if necessary) a cache session / cache storage session.
19  *
20  * @param {nsIRequest} request
21  */
22 function getCacheSession(request) {
23   if (!gCacheSession) {
24     try {
25       const cacheService = Services.cache2;
26       if (cacheService) {
27         let loadContext = lazy.NetworkHelper.getRequestLoadContext(request);
28         if (!loadContext) {
29           // Get default load context if we can't fetch.
30           loadContext = Services.loadContextInfo.default;
31         }
32         gCacheSession = cacheService.diskCacheStorage(loadContext);
33       }
34     } catch (e) {
35       gCacheSession = null;
36     }
37   }
39   return gCacheSession;
42 /**
43  * Parses a cache entry returned from the backend to build a response cache
44  * object.
45  *
46  * @param {nsICacheEntry} cacheEntry
47  *     The cache entry from the backend.
48  *
49  * @returns {Object}
50  *     A responseCache object expected by RDP.
51  */
52 function buildResponseCacheObject(cacheEntry) {
53   const cacheObject = {};
54   try {
55     if (cacheEntry.storageDataSize) {
56       cacheObject.storageDataSize = cacheEntry.storageDataSize;
57     }
58   } catch (e) {
59     // We just need to handle this in case it's a js file of 0B.
60   }
61   if (cacheEntry.expirationTime) {
62     cacheObject.expirationTime = cacheEntry.expirationTime;
63   }
64   if (cacheEntry.fetchCount) {
65     cacheObject.fetchCount = cacheEntry.fetchCount;
66   }
67   if (cacheEntry.lastFetched) {
68     cacheObject.lastFetched = cacheEntry.lastFetched;
69   }
70   if (cacheEntry.lastModified) {
71     cacheObject.lastModified = cacheEntry.lastModified;
72   }
73   if (cacheEntry.deviceID) {
74     cacheObject.deviceID = cacheEntry.deviceID;
75   }
76   return cacheObject;
79 /**
80  * Does the fetch for the cache entry from the session.
81  *
82  * @param {nsIRequest} request
83  *     The request object.
84  *
85  * @returns {Promise}
86  *     Promise which resolve a response cache object object, or null if none
87  *     was available.
88  */
89 export function getResponseCacheObject(request) {
90   const cacheSession = getCacheSession(request);
91   if (!cacheSession) {
92     return null;
93   }
95   return new Promise(resolve => {
96     cacheSession.asyncOpenURI(
97       request.URI,
98       "",
99       Ci.nsICacheStorage.OPEN_SECRETLY,
100       {
101         onCacheEntryCheck: () => {
102           return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
103         },
104         onCacheEntryAvailable: cacheEntry => {
105           if (cacheEntry) {
106             const cacheObject = buildResponseCacheObject(cacheEntry);
107             resolve(cacheObject);
108           } else {
109             resolve(null);
110           }
111         },
112       }
113     );
114   });