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-10 22:33:06 +00:00
Родитель 597e62a81e
Коммит e891cab2a3
5 изменённых файлов: 135 добавлений и 15 удалений

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

@ -2,6 +2,12 @@
* 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/. */
ChromeUtils.defineModuleGetter(
this,
"SiteSpecificBrowserService",
"resource:///modules/SiteSpecificBrowserService.jsm"
);
var BrowserPageActions = {
/**
* The main page action button in the urlbar (DOM node)
@ -1098,20 +1104,7 @@ BrowserPageActions.launchSSB = {
return;
}
let sa = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
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
);
SiteSpecificBrowserService.launchFromURI(gBrowser.currentURI);
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']
BROWSER_CHROME_MANIFESTS += ['tests/browser/browser.ini']
XPCOM_MANIFESTS += [
'components.conf',
]
EXTRA_JS_MODULES += [
'SiteSpecificBrowserService.jsm',
]

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

@ -36,6 +36,11 @@ ChromeUtils.defineModuleGetter(
"PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"SiteSpecificBrowserService",
"resource:///modules/SiteSpecificBrowserService.jsm"
);
const ACTION_ID_BOOKMARK = "bookmark";
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({
id: "launchSSB",
// Hardcoded for now. Localization tracked in bug 1602528.