Bug 1880914 - Move view source functions. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D206198
This commit is contained in:
Yi Xiong Wong 2024-04-08 17:21:01 +00:00
parent 3839c61dbc
commit 87a29d2ac8
11 changed files with 116 additions and 118 deletions

View file

@ -414,4 +414,111 @@ var BrowserCommands = {
window.close();
} // WindowIsClosing does all the necessary checks
},
/**
* Open the View Source dialog.
*
* @param args
* An object with the following properties:
*
* URL (required):
* A string URL for the page we'd like to view the source of.
* browser (optional):
* The browser containing the document that we would like to view the
* source of. This is required if outerWindowID is passed.
* outerWindowID (optional):
* The outerWindowID of the content window containing the document that
* we want to view the source of. You only need to provide this if you
* want to attempt to retrieve the document source from the network
* cache.
* lineNumber (optional):
* The line number to focus on once the source is loaded.
*/
async viewSourceOfDocument(args) {
// Check if external view source is enabled. If so, try it. If it fails,
// fallback to internal view source.
if (Services.prefs.getBoolPref("view_source.editor.external")) {
try {
await top.gViewSourceUtils.openInExternalEditor(args);
return;
} catch (data) {}
}
let tabBrowser = gBrowser;
let preferredRemoteType;
let initialBrowsingContextGroupId;
if (args.browser) {
preferredRemoteType = args.browser.remoteType;
initialBrowsingContextGroupId = args.browser.browsingContext.group.id;
} else {
if (!tabBrowser) {
throw new Error(
"viewSourceOfDocument should be passed the " +
"subject browser if called from a window without " +
"gBrowser defined."
);
}
// Some internal URLs (such as specific chrome: and about: URLs that are
// not yet remote ready) cannot be loaded in a remote browser. View
// source in tab expects the new view source browser's remoteness to match
// that of the original URL, so disable remoteness if necessary for this
// URL.
const oa = E10SUtils.predictOriginAttributes({ window });
preferredRemoteType = E10SUtils.getRemoteTypeForURI(
args.URL,
gMultiProcessBrowser,
gFissionBrowser,
E10SUtils.DEFAULT_REMOTE_TYPE,
null,
oa
);
}
// In the case of popups, we need to find a non-popup browser window.
if (!tabBrowser || !window.toolbar.visible) {
// This returns only non-popup browser windows by default.
const browserWindow = BrowserWindowTracker.getTopWindow();
tabBrowser = browserWindow.gBrowser;
}
const inNewWindow = !Services.prefs.getBoolPref("view_source.tab");
// `viewSourceInBrowser` will load the source content from the page
// descriptor for the tab (when possible) or fallback to the network if
// that fails. Either way, the view source module will manage the tab's
// location, so use "about:blank" here to avoid unnecessary redundant
// requests.
const tab = tabBrowser.addTab("about:blank", {
relatedToCurrent: true,
inBackground: inNewWindow,
skipAnimation: inNewWindow,
preferredRemoteType,
initialBrowsingContextGroupId,
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
skipLoad: true,
});
args.viewSourceBrowser = tabBrowser.getBrowserForTab(tab);
top.gViewSourceUtils.viewSourceInBrowser(args);
if (inNewWindow) {
tabBrowser.hideTab(tab);
tabBrowser.replaceTabWithWindow(tab);
}
},
/**
* Opens the View Source dialog for the source loaded in the root
* top-level document of the browser. This is really just a
* convenience wrapper around viewSourceOfDocument.
*
* @param browser
* The browser that we want to load the source of.
*/
viewSource(browser) {
this.viewSourceOfDocument({
browser,
outerWindowID: browser.outerWindowID,
URL: browser.currentURI.spec,
});
},
};

View file

@ -439,7 +439,7 @@
oncommand="gContextMenu.viewPartialSource();"/>
<menuitem id="context-viewsource"
data-l10n-id="main-context-menu-view-page-source"
oncommand="BrowserViewSource(gContextMenu.browser);"/>
oncommand="BrowserCommands.viewSource(gContextMenu.browser);"/>
<menuitem id="context-inspect-a11y"
hidden="true"
data-l10n-id="main-context-menu-inspect-a11y-properties"

View file

@ -40,8 +40,8 @@
<command id="cmd_quitApplication" oncommand="goQuitApplication(event)"/>
<command id="View:AboutProcesses" oncommand="switchToTabHavingURI('about:processes', true)"/>
<command id="View:PageSource" oncommand="BrowserViewSource(window.gBrowser.selectedBrowser);"/>
<command id="View:PageInfo" oncommand="BrowserPageInfo();"/>
<command id="View:PageSource" oncommand="BrowserCommands.viewSource(window.gBrowser.selectedBrowser);"/>
<command id="View:FullScreen" oncommand="BrowserFullScreen();"/>
<command id="View:ReaderView" oncommand="AboutReaderParent.toggleReaderMode(event);"/>
<command id="View:PictureInPicture" oncommand="PictureInPicture.onCommand(event);"/>

View file

@ -2801,113 +2801,6 @@ function readFromClipboard() {
return url;
}
/**
* Open the View Source dialog.
*
* @param args
* An object with the following properties:
*
* URL (required):
* A string URL for the page we'd like to view the source of.
* browser (optional):
* The browser containing the document that we would like to view the
* source of. This is required if outerWindowID is passed.
* outerWindowID (optional):
* The outerWindowID of the content window containing the document that
* we want to view the source of. You only need to provide this if you
* want to attempt to retrieve the document source from the network
* cache.
* lineNumber (optional):
* The line number to focus on once the source is loaded.
*/
async function BrowserViewSourceOfDocument(args) {
// Check if external view source is enabled. If so, try it. If it fails,
// fallback to internal view source.
if (Services.prefs.getBoolPref("view_source.editor.external")) {
try {
await top.gViewSourceUtils.openInExternalEditor(args);
return;
} catch (data) {}
}
let tabBrowser = gBrowser;
let preferredRemoteType;
let initialBrowsingContextGroupId;
if (args.browser) {
preferredRemoteType = args.browser.remoteType;
initialBrowsingContextGroupId = args.browser.browsingContext.group.id;
} else {
if (!tabBrowser) {
throw new Error(
"BrowserViewSourceOfDocument should be passed the " +
"subject browser if called from a window without " +
"gBrowser defined."
);
}
// Some internal URLs (such as specific chrome: and about: URLs that are
// not yet remote ready) cannot be loaded in a remote browser. View
// source in tab expects the new view source browser's remoteness to match
// that of the original URL, so disable remoteness if necessary for this
// URL.
var oa = E10SUtils.predictOriginAttributes({ window });
preferredRemoteType = E10SUtils.getRemoteTypeForURI(
args.URL,
gMultiProcessBrowser,
gFissionBrowser,
E10SUtils.DEFAULT_REMOTE_TYPE,
null,
oa
);
}
// In the case of popups, we need to find a non-popup browser window.
if (!tabBrowser || !window.toolbar.visible) {
// This returns only non-popup browser windows by default.
let browserWindow = BrowserWindowTracker.getTopWindow();
tabBrowser = browserWindow.gBrowser;
}
const inNewWindow = !Services.prefs.getBoolPref("view_source.tab");
// `viewSourceInBrowser` will load the source content from the page
// descriptor for the tab (when possible) or fallback to the network if
// that fails. Either way, the view source module will manage the tab's
// location, so use "about:blank" here to avoid unnecessary redundant
// requests.
let tab = tabBrowser.addTab("about:blank", {
relatedToCurrent: true,
inBackground: inNewWindow,
skipAnimation: inNewWindow,
preferredRemoteType,
initialBrowsingContextGroupId,
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
skipLoad: true,
});
args.viewSourceBrowser = tabBrowser.getBrowserForTab(tab);
top.gViewSourceUtils.viewSourceInBrowser(args);
if (inNewWindow) {
tabBrowser.hideTab(tab);
tabBrowser.replaceTabWithWindow(tab);
}
}
/**
* Opens the View Source dialog for the source loaded in the root
* top-level document of the browser. This is really just a
* convenience wrapper around BrowserViewSourceOfDocument.
*
* @param browser
* The browser that we want to load the source of.
*/
function BrowserViewSource(browser) {
BrowserViewSourceOfDocument({
browser,
outerWindowID: browser.outerWindowID,
URL: browser.currentURI.spec,
});
}
// documentURL - URL of the document to view, or null for this window's document
// initialTab - name of the initial tab to display, or null for the first tab
// imageElement - image to load in the Media Tab of the Page Info window; can be null/omitted

View file

@ -39,8 +39,6 @@
"gLastOpenDirectory",
"getLoadContext",
"readFromClipboard",
"BrowserViewSourceOfDocument",
"BrowserViewSource",
"BrowserPageInfo",
"UpdateUrlbarSearchSplitterState",
"UpdatePopupNotificationsVisibility",

View file

@ -1619,7 +1619,7 @@ class nsContextMenu {
// Open new "view source" window with the frame's URL.
viewFrameSource() {
BrowserViewSourceOfDocument({
BrowserCommands.viewSourceOfDocument({
browser: this.browser,
URL: this.contentData.docLocation,
outerWindowID: this.frameOuterWindowID,

View file

@ -13,7 +13,7 @@ add_task(async function () {
});
let promiseTab = BrowserTestUtils.waitForNewTab(gBrowser, DATA_URI_SOURCE);
BrowserViewSource(tab.linkedBrowser);
BrowserCommands.viewSource(tab.linkedBrowser);
let viewSourceTab = await promiseTab;
registerCleanupFunction(async function () {
BrowserTestUtils.removeTab(viewSourceTab);

View file

@ -33,7 +33,7 @@ add_task(async function () {
// Make sure we can view-source on the data URI page.
let promiseTab = BrowserTestUtils.waitForNewTab(gBrowser, DATA_URI_SOURCE);
BrowserViewSource(fileBrowser);
BrowserCommands.viewSource(fileBrowser);
let viewSourceTab = await promiseTab;
registerCleanupFunction(async function () {
BrowserTestUtils.removeTab(viewSourceTab);

View file

@ -62,8 +62,8 @@ var gViewSourceUtils = {
}
// Try existing browsers first.
let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
if (browserWin && browserWin.BrowserViewSourceOfDocument) {
browserWin.BrowserViewSourceOfDocument(aArgs);
if (browserWin && browserWin.BrowserCommands.viewSourceOfDocument) {
browserWin.BrowserCommands.viewSourceOfDocument(aArgs);
return;
}
// No browser window created yet, try to create one.

View file

@ -33,7 +33,7 @@ add_task(async function () {
},
async browser => {
let winPromise = waitForNewViewSourceWindow("view-source:" + PAGE);
BrowserViewSource(browser);
BrowserCommands.viewSource(browser);
let win = await winPromise;
ok(win, "View Source opened up in a new window.");

View file

@ -43,7 +43,7 @@ async function waitForViewSourceTab(open) {
*/
function openViewSourceForBrowser(browser) {
return waitForViewSourceTab(() => {
window.BrowserViewSource(browser);
window.BrowserCommands.viewSource(browser);
});
}