diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/components/extensions/WebNavigationFrames.jsm | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/extensions/WebNavigationFrames.jsm')
-rw-r--r-- | toolkit/components/extensions/WebNavigationFrames.jsm | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/toolkit/components/extensions/WebNavigationFrames.jsm b/toolkit/components/extensions/WebNavigationFrames.jsm new file mode 100644 index 0000000000..26f49f8e91 --- /dev/null +++ b/toolkit/components/extensions/WebNavigationFrames.jsm @@ -0,0 +1,122 @@ +/* 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/. */ + +"use strict"; + +const EXPORTED_SYMBOLS = ["WebNavigationFrames"]; + +/* exported WebNavigationFrames */ + +/** + * The FrameDetail object which represents a frame in WebExtensions APIs. + * + * @typedef {Object} FrameDetail + * @inner + * @property {number} frameId - Represents the numeric id which identify the frame in its tab. + * @property {number} parentFrameId - Represents the numeric id which identify the parent frame. + * @property {string} url - Represents the current location URL loaded in the frame. + * @property {boolean} errorOccurred - Indicates whether an error is occurred during the last load + * happened on this frame (NOT YET SUPPORTED). + */ + +/** + * A generator function which iterates over a docShell tree, given a root docShell. + * + * @param {nsIDocShell} docShell - the root docShell object + * @returns {Iterator<nsIDocShell>} + */ +function iterateDocShellTree(docShell) { + return docShell.getAllDocShellsInSubtree( + docShell.typeContent, + docShell.ENUMERATE_FORWARDS + ); +} + +/** + * Returns the frame ID of the given window. If the window is the + * top-level content window, its frame ID is 0. Otherwise, its frame ID + * is its outer window ID. + * + * @param {Window|BrowsingContext} bc - The window to retrieve the frame ID for. + * @returns {number} + */ +function getFrameId(bc) { + if (!BrowsingContext.isInstance(bc)) { + bc = bc.browsingContext; + } + return bc.parent ? bc.id : 0; +} + +/** + * Returns the frame ID of the given window's parent. + * + * @param {Window|BrowsingContext} bc - The window to retrieve the parent frame ID for. + * @returns {number} + */ +function getParentFrameId(bc) { + if (!BrowsingContext.isInstance(bc)) { + bc = bc.browsingContext; + } + return bc.parent ? getFrameId(bc.parent) : -1; +} + +/** + * Convert a docShell object into its internal FrameDetail representation. + * + * @param {nsIDocShell} docShell - the docShell object to be converted into a FrameDetail JSON object. + * @returns {FrameDetail} the FrameDetail JSON object which represents the docShell. + */ +function convertDocShellToFrameDetail(docShell) { + let { browsingContext, domWindow: window } = docShell; + + return { + frameId: getFrameId(browsingContext), + parentFrameId: getParentFrameId(browsingContext), + url: window.location.href, + }; +} + +/** + * Search for a frame starting from the passed root docShell and + * convert it to its related frame detail representation. + * + * @param {number} frameId - the frame ID of the frame to retrieve, as + * described in getFrameId. + * @param {nsIDocShell} rootDocShell - the root docShell object + * @returns {nsIDocShell?} the docShell with the given frameId, or null + * if no match. + */ +function findDocShell(frameId, rootDocShell) { + for (let docShell of iterateDocShellTree(rootDocShell)) { + if (frameId == getFrameId(docShell.browsingContext)) { + return docShell; + } + } + + return null; +} + +var WebNavigationFrames = { + iterateDocShellTree, + + findDocShell, + + getFrame(docShell, frameId) { + let result = findDocShell(frameId, docShell); + if (result) { + return convertDocShellToFrameDetail(result); + } + return null; + }, + + getFrameId, + getParentFrameId, + + getAllFrames(docShell) { + return Array.from( + iterateDocShellTree(docShell), + convertDocShellToFrameDetail + ); + }, +}; |