Bug 1731553 - [remote] Add shared module to handle stack frames. r=webdriver-reviewers,jdescottes

Differential Revision: https://phabricator.services.mozilla.com/D133087
This commit is contained in:
Henrik Skupin 2021-12-09 20:24:09 +00:00
Родитель 9a89d1dcce
Коммит 2973a301fe
4 изменённых файлов: 199 добавлений и 0 удалений

Просмотреть файл

@ -16,6 +16,7 @@ remote.jar:
content/shared/Format.jsm (shared/Format.jsm) content/shared/Format.jsm (shared/Format.jsm)
content/shared/Log.jsm (shared/Log.jsm) content/shared/Log.jsm (shared/Log.jsm)
content/shared/RecommendedPreferences.jsm (shared/RecommendedPreferences.jsm) content/shared/RecommendedPreferences.jsm (shared/RecommendedPreferences.jsm)
content/shared/Stack.jsm (shared/Stack.jsm)
content/shared/Sync.jsm (shared/Sync.jsm) content/shared/Sync.jsm (shared/Sync.jsm)
content/shared/TabManager.jsm (shared/TabManager.jsm) content/shared/TabManager.jsm (shared/TabManager.jsm)
content/shared/WebSocketConnection.jsm (shared/WebSocketConnection.jsm) content/shared/WebSocketConnection.jsm (shared/WebSocketConnection.jsm)

77
remote/shared/Stack.jsm Normal file
Просмотреть файл

@ -0,0 +1,77 @@
/* 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 = ["getFramesFromStack", "isChromeFrame"];
/**
* An object that contains details of a stack frame.
*
* @typedef {Object} StackFrame
* @see nsIStackFrame
*
* @property {String=} asyncCause
* Type of asynchronous call by which this frame was invoked.
* @property {Number} columnNumber
* The column number for this stack frame.
* @property {String} filename
* The source URL for this stack frame.
* @property {String} function
* SpiderMonkeys inferred name for this stack frames function, or null.
* @property {Number} lineNumber
* The line number for this stack frame (starts with 1).
* @property {Number} sourceId
* The process-unique internal integer ID of this source.
*/
/**
* Return a list of stack frames from the given stack.
*
* Convert stack objects to the JSON attributes expected by consumers.
*
* @param {Object} stack
* The native stack object to process.
*
* @returns {Array<StackFrame>=}
*/
function getFramesFromStack(stack) {
if (!stack || (Cu && Cu.isDeadWrapper(stack))) {
// If the global from which this error came from has been nuked,
// stack is going to be a dead wrapper.
return null;
}
const frames = [];
while (stack) {
frames.push({
asyncCause: stack.asyncCause,
columnNumber: stack.column,
filename: stack.source,
functionName: stack.functionDisplayName || "",
lineNumber: stack.line,
sourceId: stack.sourceId,
});
stack = stack.parent || stack.asyncParent;
}
return frames;
}
/**
* Check if a frame is from chrome scope.
*
* @param {Object} frame
* The frame to check
*
* @returns {boolean}
* True, if frame is from chrome scope
*/
function isChromeFrame(frame) {
return (
frame.filename.startsWith("chrome://") ||
frame.filename.startsWith("resource://")
);
}

Просмотреть файл

@ -0,0 +1,120 @@
/* 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 { getFramesFromStack, isChromeFrame } = ChromeUtils.import(
"chrome://remote/content/shared/Stack.jsm"
);
const sourceFrames = [
{
column: 1,
functionDisplayName: "foo",
line: 2,
source: "cheese",
sourceId: 1,
},
{
column: 3,
functionDisplayName: null,
line: 4,
source: "cake",
sourceId: 2,
},
{
column: 5,
functionDisplayName: "chrome",
line: 6,
source: "chrome://foo",
sourceId: 3,
},
];
const targetFrames = [
{
columnNumber: 1,
functionName: "foo",
lineNumber: 2,
filename: "cheese",
sourceId: 1,
},
{
columnNumber: 3,
functionName: "",
lineNumber: 4,
filename: "cake",
sourceId: 2,
},
{
columnNumber: 5,
functionName: "chrome",
lineNumber: 6,
filename: "chrome://foo",
sourceId: 3,
},
];
add_task(async function test_getFramesFromStack() {
const stack = buildStack(sourceFrames);
const frames = getFramesFromStack(stack, { includeChrome: false });
ok(Array.isArray(frames), "frames is of expected type Array");
equal(frames.length, 3, "Got expected amount of frames");
checkFrame(frames.at(0), targetFrames.at(0));
checkFrame(frames.at(1), targetFrames.at(1));
checkFrame(frames.at(2), targetFrames.at(2));
});
add_task(async function test_getFramesFromStack_asyncStack() {
const stack = buildStack(sourceFrames, true);
const frames = getFramesFromStack(stack);
ok(Array.isArray(frames), "frames is of expected type Array");
equal(frames.length, 3, "Got expected amount of frames");
checkFrame(frames.at(0), targetFrames.at(0));
checkFrame(frames.at(1), targetFrames.at(1));
checkFrame(frames.at(2), targetFrames.at(2));
});
add_task(async function test_isChromeFrame() {
for (const filename of ["chrome://foo/bar", "resource://foo/bar"]) {
ok(isChromeFrame({ filename }), "Frame is of expected chrome scope");
}
for (const filename of ["http://foo.bar", "about:blank"]) {
ok(!isChromeFrame({ filename }), "Frame is of expected content scope");
}
});
function buildStack(frames, async = false) {
const parent = async ? "asyncParent" : "parent";
let currentFrame, stack;
for (const frame of frames) {
if (currentFrame) {
currentFrame[parent] = Object.assign({}, frame);
currentFrame = currentFrame[parent];
} else {
stack = Object.assign({}, frame);
currentFrame = stack;
}
}
return stack;
}
function checkFrame(frame, expectedFrame) {
equal(
frame.columnNumber,
expectedFrame.columnNumber,
"Got expected column number"
);
equal(
frame.functionName,
expectedFrame.functionName,
"Got expected function name"
);
equal(frame.lineNumber, expectedFrame.lineNumber, "Got expected line number");
equal(frame.filename, expectedFrame.filename, "Got expected filename");
equal(frame.sourceId, expectedFrame.sourceId, "Got expected source id");
}

Просмотреть файл

@ -4,4 +4,5 @@
[test_Format.js] [test_Format.js]
[test_RecommendedPreferences.js] [test_RecommendedPreferences.js]
[test_Stack.js]
[test_Sync.js] [test_Sync.js]