summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/WebNavigationFrames.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /toolkit/components/extensions/WebNavigationFrames.sys.mjs
parentInitial commit. (diff)
downloadfirefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz
firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/extensions/WebNavigationFrames.sys.mjs')
-rw-r--r--toolkit/components/extensions/WebNavigationFrames.sys.mjs90
1 files changed, 90 insertions, 0 deletions
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;
+ },
+};