From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- devtools/shared/platform/CacheEntry.sys.mjs | 115 ++++++++++++++++++++++++++++ devtools/shared/platform/clipboard.js | 59 ++++++++++++++ devtools/shared/platform/moz.build | 11 +++ devtools/shared/platform/stack.js | 64 ++++++++++++++++ 4 files changed, 249 insertions(+) create mode 100644 devtools/shared/platform/CacheEntry.sys.mjs create mode 100644 devtools/shared/platform/clipboard.js create mode 100644 devtools/shared/platform/moz.build create mode 100644 devtools/shared/platform/stack.js (limited to 'devtools/shared/platform') 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); + } + }, + } + ); + }); +} diff --git a/devtools/shared/platform/clipboard.js b/devtools/shared/platform/clipboard.js new file mode 100644 index 0000000000..46ea6c5fe6 --- /dev/null +++ b/devtools/shared/platform/clipboard.js @@ -0,0 +1,59 @@ +/* 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/. */ + +// Helpers for clipboard handling. + +"use strict"; + +const clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService( + Ci.nsIClipboardHelper +); + +function copyString(string) { + clipboardHelper.copyString(string); +} + +/** + * Retrieve the current clipboard data matching the flavor "text/plain". + * + * @return {String} Clipboard text content, null if no text clipboard data is available. + */ +function getText() { + const flavor = "text/plain"; + + const xferable = Cc["@mozilla.org/widget/transferable;1"].createInstance( + Ci.nsITransferable + ); + + if (!xferable) { + throw new Error( + "Couldn't get the clipboard data due to an internal error " + + "(couldn't create a Transferable object)." + ); + } + + xferable.init(null); + xferable.addDataFlavor(flavor); + + // Get the data into our transferable. + Services.clipboard.getData(xferable, Services.clipboard.kGlobalClipboard); + + const data = {}; + try { + xferable.getTransferData(flavor, data); + } catch (e) { + // Clipboard doesn't contain data in flavor, return null. + return null; + } + + // There's no data available, return. + if (!data.value) { + return null; + } + + return data.value.QueryInterface(Ci.nsISupportsString).data; +} + +exports.copyString = copyString; +exports.getText = getText; diff --git a/devtools/shared/platform/moz.build b/devtools/shared/platform/moz.build new file mode 100644 index 0000000000..edbf580c1c --- /dev/null +++ b/devtools/shared/platform/moz.build @@ -0,0 +1,11 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +DevToolsModules( + "CacheEntry.sys.mjs", + "clipboard.js", + "stack.js", +) diff --git a/devtools/shared/platform/stack.js b/devtools/shared/platform/stack.js new file mode 100644 index 0000000000..65ff644091 --- /dev/null +++ b/devtools/shared/platform/stack.js @@ -0,0 +1,64 @@ +/* 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/. */ + +// A few wrappers for stack-manipulation. This version of the module +// is used in chrome code. + +"use strict"; + +/** + * Return the Nth path from the stack excluding substr. + * + * @param {Number} + * n the Nth path from the stack to describe. + * @param {String} substr + * A segment of the path that should be excluded. + */ +function getNthPathExcluding(n, substr) { + let stack = Components.stack.formattedStack.split("\n"); + + // Remove this method from the stack + stack = stack.splice(1); + + stack = stack.map(line => { + if (line.includes(" -> ")) { + return line.split(" -> ")[1]; + } + return line; + }); + + if (substr) { + stack = stack.filter(line => { + return line && !line.includes(substr); + }); + } + + if (!stack[n]) { + n = 0; + } + return stack[n] || ""; +} + +/** + * Return a stack object that can be serialized and, when + * deserialized, passed to callFunctionWithAsyncStack. + */ +function getStack() { + return Components.stack.caller; +} + +/** + * Like Cu.callFunctionWithAsyncStack but handles the isWorker case + * -- |Cu| isn't defined in workers. + */ +function callFunctionWithAsyncStack(callee, stack, id) { + if (isWorker) { + return callee(); + } + return Cu.callFunctionWithAsyncStack(callee, stack, id); +} + +exports.callFunctionWithAsyncStack = callFunctionWithAsyncStack; +exports.getNthPathExcluding = getNthPathExcluding; +exports.getStack = getStack; -- cgit v1.2.3