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 --- .../extensions/WebNavigationFrames.sys.mjs | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 toolkit/components/extensions/WebNavigationFrames.sys.mjs (limited to 'toolkit/components/extensions/WebNavigationFrames.sys.mjs') diff --git a/toolkit/components/extensions/WebNavigationFrames.sys.mjs b/toolkit/components/extensions/WebNavigationFrames.sys.mjs new file mode 100644 index 0000000000..211698a88e --- /dev/null +++ b/toolkit/components/extensions/WebNavigationFrames.sys.mjs @@ -0,0 +1,90 @@ +/* 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/. */ + +/** + * 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). + */ + +/** + * 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 BrowsingContext into internal FrameDetail json. + * + * @param {BrowsingContext} bc + * @returns {FrameDetail} + */ +function getFrameDetail(bc) { + return { + frameId: getFrameId(bc), + parentFrameId: getParentFrameId(bc), + url: bc.currentURI?.spec, + }; +} + +export var WebNavigationFrames = { + getFrame(bc, frameId) { + // frameId 0 means the top-level frame; anything else is a child frame. + let frame = BrowsingContext.get(frameId || bc.id); + if (frame && frame.top === bc) { + return getFrameDetail(frame); + } + return null; + }, + + getFrameId, + getParentFrameId, + + getAllFrames(bc) { + let frames = []; + + // Recursively walk the BC tree, find all frames. + function visit(bc) { + frames.push(bc); + bc.children.forEach(visit); + } + visit(bc); + return frames.map(getFrameDetail); + }, + + getFromWindow(target) { + if (Window.isInstance(target)) { + return getFrameId(BrowsingContext.getFromWindow(target)); + } + return -1; + }, +}; -- cgit v1.2.3