Bug 1602168: Add a command line argument to launch an ssb. r=Gijs

Allows launching a site browser from the command line (see https://bugzilla.mozilla.org/show_bug.cgi?id=1283670).
Moves the code for launching an SSB into a shared module.

Differential Revision: https://phabricator.services.mozilla.com/D56284

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Dave Townsend 2019-12-12 20:23:00 +00:00
Родитель c696551b78
Коммит b6cef263d5
7 изменённых файлов: 147 добавлений и 22 удалений

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

@ -2,6 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
ChromeUtils.defineModuleGetter(
this,
"SiteSpecificBrowserService",
"resource:///modules/SiteSpecificBrowserService.jsm"
);
var BrowserPageActions = { var BrowserPageActions = {
/** /**
* The main page action button in the urlbar (DOM node) * The main page action button in the urlbar (DOM node)
@ -1098,20 +1104,7 @@ BrowserPageActions.launchSSB = {
return; return;
} }
let sa = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray); SiteSpecificBrowserService.launchFromURI(gBrowser.currentURI);
let uri = Cc["@mozilla.org/supports-string;1"].createInstance(
Ci.nsISupportsString
);
uri.data = gBrowser.currentURI.spec;
sa.appendElement(uri);
Services.ww.openWindow(
null,
"chrome://browser/content/ssb/ssb.html",
"_blank",
"chrome,dialog=no,all",
sa
);
gBrowser.removeTab(gBrowser.selectedTab, { closeWindowWithLastTab: false }); gBrowser.removeTab(gBrowser.selectedTab, { closeWindowWithLastTab: false });
}, },
}; };

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

@ -0,0 +1,95 @@
/* 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/. */
var EXPORTED_SYMBOLS = ["SiteSpecificBrowserService", "SSBCommandLineHandler"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const SiteSpecificBrowserService = {
/**
* Given a URI launches the SSB UI to display it.
*
* @param {nsIURI} uri the URI to display.
*/
launchFromURI(uri) {
if (!this.isEnabled) {
throw new Error("Site specific browsing is disabled.");
}
if (!uri.schemeIs("https")) {
throw new Error(
"Site specific browsers can only be opened for secure sites."
);
}
let sa = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
let uristr = Cc["@mozilla.org/supports-string;1"].createInstance(
Ci.nsISupportsString
);
uristr.data = uri.spec;
sa.appendElement(uristr);
Services.ww.openWindow(
null,
"chrome://browser/content/ssb/ssb.html",
"_blank",
"chrome,dialog=no,all",
sa
);
},
};
XPCOMUtils.defineLazyPreferenceGetter(
SiteSpecificBrowserService,
"isEnabled",
"browser.ssb.enabled",
false
);
class SSBCommandLineHandler {
/* nsICommandLineHandler */
handle(cmdLine) {
if (!SiteSpecificBrowserService.isEnabled) {
return;
}
let site = cmdLine.handleFlagWithParam("ssb", false);
if (site) {
cmdLine.preventDefault = true;
try {
let fixupInfo = Services.uriFixup.getFixupURIInfo(
site,
Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS
);
let uri = fixupInfo.preferredURI;
if (!uri) {
dump(`Unable to parse '${site}' as a URI.\n`);
return;
}
if (fixupInfo.fixupChangedProtocol && uri.schemeIs("http")) {
uri = uri
.mutate()
.setScheme("https")
.finalize();
}
SiteSpecificBrowserService.launchFromURI(uri);
} catch (e) {
dump(`Unable to parse '${site}' as a URI: ${e}\n`);
}
}
}
get helpInfo() {
return " --ssb <uri> Open a site specific browser for <uri>.\n";
}
}
SSBCommandLineHandler.prototype.QueryInterface = ChromeUtils.generateQI([
Ci.nsICommandLineHandler,
]);

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

@ -0,0 +1,19 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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/.
Classes = [
{
'cid': '{6344f783-c893-4db8-91ec-7d43a46bd6f4}',
'contract_ids': [
'@mozilla.org/browser/ssb/clh;1',
],
'jsm': 'resource:///modules/SiteSpecificBrowserService.jsm',
'constructor': 'SSBCommandLineHandler',
'categories': {
'command-line-handler': 'e-ssb',
},
},
]

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

@ -6,3 +6,11 @@
JAR_MANIFESTS += ['content/jar.mn'] JAR_MANIFESTS += ['content/jar.mn']
BROWSER_CHROME_MANIFESTS += ['tests/browser/browser.ini'] BROWSER_CHROME_MANIFESTS += ['tests/browser/browser.ini']
XPCOM_MANIFESTS += [
'components.conf',
]
EXTRA_JS_MODULES += [
'SiteSpecificBrowserService.jsm',
]

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

@ -13,20 +13,23 @@ add_task(async () => {
Assert.equal(win.gBrowser.tabs.length, 1, "Should be only one tab."); Assert.equal(win.gBrowser.tabs.length, 1, "Should be only one tab.");
let tab = win.gBrowser.selectedTab; let tab = win.gBrowser.selectedTab;
let loaded = BrowserTestUtils.browserLoaded(
win.gBrowser.selectedBrowser,
false,
gHttpsTestRoot + "test_page.html"
);
BrowserTestUtils.loadURI( BrowserTestUtils.loadURI(
win.gBrowser.selectedBrowser, win.gBrowser.selectedBrowser,
gHttpsTestRoot + "test_page.html" gHttpsTestRoot + "test_page.html"
); );
await loaded; await BrowserTestUtils.browserLoaded(
win.gBrowser.selectedBrowser,
true,
gHttpsTestRoot + "test_page.html"
);
let ssb = await openSSBFromBrowserWindow(win); let ssb = await openSSBFromBrowserWindow(win);
Assert.equal(win.gBrowser.tabs.length, 1, "Should still be only one tab."); Assert.equal(win.gBrowser.tabs.length, 1, "Should still be only one tab.");
Assert.notEqual(tab, win.gBrowser.selectedTab, "Should be a new tab."); Assert.notEqual(tab, win.gBrowser.selectedTab, "Should be a new tab.");
Assert.equal(
getBrowser(ssb).currentURI.spec,
gHttpsTestRoot + "test_page.html"
);
Assert.ok(!windowClosed, "Should not have seen the window close."); Assert.ok(!windowClosed, "Should not have seen the window close.");
await BrowserTestUtils.closeWindow(ssb); await BrowserTestUtils.closeWindow(ssb);

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

@ -27,6 +27,8 @@ async function openSSBFromBrowserWindow(win = window) {
EventUtils.synthesizeMouseAtCenter(pageActionButton, {}, win); EventUtils.synthesizeMouseAtCenter(pageActionButton, {}, win);
await popupShown; await popupShown;
let expectedUri = win.gBrowser.selectedBrowser.currentURI;
let openItem = doc.getElementById("pageAction-panel-launchSSB"); let openItem = doc.getElementById("pageAction-panel-launchSSB");
Assert.ok(!openItem.disabled, "Open menu item should not be disabled"); Assert.ok(!openItem.disabled, "Open menu item should not be disabled");
Assert.ok(!openItem.hidden, "Open menu item should not be hidden"); Assert.ok(!openItem.hidden, "Open menu item should not be hidden");
@ -39,7 +41,7 @@ async function openSSBFromBrowserWindow(win = window) {
EventUtils.synthesizeMouseAtCenter(openItem, {}, win); EventUtils.synthesizeMouseAtCenter(openItem, {}, win);
let ssbwin = await openPromise; let ssbwin = await openPromise;
let browser = ssbwin.document.getElementById("browser"); let browser = ssbwin.document.getElementById("browser");
await BrowserTestUtils.browserLoaded(browser, true); await BrowserTestUtils.browserLoaded(browser, true, expectedUri.spec);
return ssbwin; return ssbwin;
} }

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

@ -36,6 +36,11 @@ ChromeUtils.defineModuleGetter(
"PrivateBrowsingUtils", "PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm" "resource://gre/modules/PrivateBrowsingUtils.jsm"
); );
ChromeUtils.defineModuleGetter(
this,
"SiteSpecificBrowserService",
"resource:///modules/SiteSpecificBrowserService.jsm"
);
const ACTION_ID_BOOKMARK = "bookmark"; const ACTION_ID_BOOKMARK = "bookmark";
const ACTION_ID_PIN_TAB = "pinTab"; const ACTION_ID_PIN_TAB = "pinTab";
@ -1280,7 +1285,7 @@ if (Services.prefs.getBoolPref("identity.fxaccounts.enabled")) {
}); });
} }
if (Services.prefs.getBoolPref("browser.ssb.enabled", false)) { if (SiteSpecificBrowserService.isEnabled) {
gBuiltInActions.push({ gBuiltInActions.push({
id: "launchSSB", id: "launchSSB",
// Hardcoded for now. Localization tracked in bug 1602528. // Hardcoded for now. Localization tracked in bug 1602528.