Skip to content

Commit

Permalink
[FB] PWA | Make donot show at unsecure sites
Browse files Browse the repository at this point in the history
  • Loading branch information
surapunoyousei committed Nov 13, 2023
1 parent 7cc538c commit f0b4b42
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 77 deletions.
109 changes: 34 additions & 75 deletions browser/base/content/browser-pageActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var { SiteSpecificBrowserIdUtils } = ChromeUtils.importESModule(
"resource:///modules/SiteSpecificBrowserIdUtils.sys.mjs"
);


let gFloorpPageAction = {
qrCode: {
QRCodeGeneratePageActionButton: window.MozXULElement.parseXULToFragment(`
Expand All @@ -41,41 +40,44 @@ let gFloorpPageAction = {
</hbox>
`),
onPopupShowing() {
Services.scriptloader.loadSubScript("chrome://browser/content/qr-code-styling/qr-code-styling.js", window);

Services.scriptloader.loadSubScript(
"chrome://browser/content/qr-code-styling/qr-code-styling.js",
window
);

let currentTab = gBrowser.selectedTab;
let currentTabURL = currentTab.linkedBrowser.currentURI.spec;

const qrCode = new QRCodeStyling({
width: 250,
height: 250,
type: "svg",
data: currentTabURL,
image: "chrome://branding/content/about-logo.png",
dotsOptions: {
color: "#4267b2",
color: "#4267b2",
},
cornersSquareOptions: {
type: "extra-rounded",
},
backgroundOptions: {
color: "#e9ebee",
color: "#e9ebee",
},
imageOptions: {
crossOrigin: "anonymous",
margin: 10
}
crossOrigin: "anonymous",
margin: 10,
},
});

//remove old qrcode
let QRCodeBox = document.getElementById("qrcode-img-vbox");

while (QRCodeBox.firstChild) {
QRCodeBox.firstChild.remove();
}

qrCode.append(QRCodeBox);
}
},
},

Ssb: {
Expand All @@ -84,7 +86,7 @@ let gFloorpPageAction = {
class="urlbar-page-action" tooltiptext="ssb-page-action"
role="button" popup="ssb-panel">
<image id="ssbPageAction-image" class="urlbar-icon"/>
<panel id="ssb-panel" type="arrow" position="bottomright topright" onpopupshowing="gFloorpPageAction.Ssb.onPopupShowing()">
<panel id="ssb-panel" type="arrow" position="bottomright topright" onpopupshowing="gSsbInstallSupport.functions.setImageToInstallButton();">
<vbox id="ssb-box">
<vbox class="panel-header">
<html:h1>
Expand Down Expand Up @@ -112,48 +114,9 @@ let gFloorpPageAction = {
</hbox>
`),

async currentTabSsb () {
let currentURISsbObj = await SiteSpecificBrowser.createFromBrowser(gBrowser.selectedBrowser);

return currentURISsbObj;
},

async onPopupShowing() {
let currentURISsbObj = await gFloorpPageAction.Ssb.currentTabSsb();
let isInstalled = await gFloorpPageAction.Ssb.checkCurrentPageIsInstalled();

let currentTabTitle = currentURISsbObj.name;
let currentTabURL = currentURISsbObj._scope.displayHost;

let ssbContentLabel = document.getElementById("ssb-content-label");
let ssbContentDescription = document.getElementById("ssb-content-description");
let ssbContentIcon = document.getElementById("ssb-content-icon");

let installButton = document.querySelector(".ssb-app-install-button");

if (ssbContentLabel) {
ssbContentLabel.textContent = currentTabTitle;
}

if (ssbContentDescription) {
ssbContentDescription.textContent = currentTabURL;
}

if (installButton) {
if (isInstalled) {
document.l10n.setAttributes(installButton, "ssb-app-open-button");
} else {
document.l10n.setAttributes(installButton, "ssb-app-install-button");
}
}

if (ssbContentIcon) {
ssbContentIcon.src = document.querySelector(".tab-icon-image[selected=true]").src;
}
},

async onCommand() {
let isInstalled = await gFloorpPageAction.Ssb.checkCurrentPageIsInstalled();
let isInstalled =
await gFloorpPageAction.Ssb.checkCurrentPageIsInstalled();

this.closePopup();

Expand All @@ -162,7 +125,7 @@ let gFloorpPageAction = {
}

if (isInstalled) {
let currentTabSsb = await this.currentTabSsb();
let currentTabSsb = await this.getCurrentTabSsb();
let ssbObj = await SiteSpecificBrowserIdUtils.getIdByUrl(
currentTabSsb._manifest.start_url
);
Expand All @@ -172,38 +135,34 @@ let gFloorpPageAction = {
await SiteSpecificBrowserIdUtils.runSSBWithId(id);
}
} else {
let ssb = await SiteSpecificBrowser.createFromBrowser(gBrowser.selectedBrowser)

let ssb = await SiteSpecificBrowser.manifestIsExsitingFromBrowser(
gBrowser.selectedBrowser
);

await ssb.install();
await SiteSpecificBrowserIdUtils.runSSBWithId(ssb.id);
}
// The site's manifest may point to a different start page so explicitly
// open the SSB to the current page.
// gBrowser.removeTab(gBrowser.selectedTab, { closeWindowWithLastTab: false });
gBrowser.removeTab(gBrowser.selectedTab, {
closeWindowWithLastTab: false,
});
},

closePopup() {
document.getElementById("ssb-panel").hidePopup();
},

async checkCurrentPageIsInstalled() {
let currentTabSsb = await gFloorpPageAction.Ssb.currentTabSsb();
let ssbData = await SiteSpecificBrowserExternalFileService.getCurrentSsbData();

for (let key in ssbData) {
if (key === currentTabSsb._manifest.start_url) {
return true;
}
}
return false;
},
}
}
},
};

SessionStore.promiseInitialized.then(() => {
document.getElementById("star-button-box").before(gFloorpPageAction.qrCode.QRCodeGeneratePageActionButton);
document
.getElementById("star-button-box")
.before(gFloorpPageAction.qrCode.QRCodeGeneratePageActionButton);

if (Services.prefs.getBoolPref("browser.ssb.enabled")) {
document.getElementById("star-button-box").before(gFloorpPageAction.Ssb.SsbPageActionButton);
document
.getElementById("star-button-box")
.before(gFloorpPageAction.Ssb.SsbPageActionButton);
}
});
191 changes: 191 additions & 0 deletions browser/base/content/browser-ssb-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/* eslint-disable no-undef */
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* 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 { SiteSpecificBrowserExternalFileService } = ChromeUtils.importESModule(
"resource:///modules/SiteSpecificBrowserExternalFileService.sys.mjs"
);

var { SiteSpecificBrowser } = ChromeUtils.importESModule(
"resource:///modules/SiteSpecificBrowserService.sys.mjs"
);

var { SiteSpecificBrowserIdUtils } = ChromeUtils.importESModule(
"resource:///modules/SiteSpecificBrowserIdUtils.sys.mjs"
);

const gSsbInstallSupport = {
_initialized: false,

init() {
if (this._initialized) {
return;
}

// Use internal APIs to detect when the current tab changes.
let currentURL = gBrowser.currentURI.spec;
function checkURLChange() {
const newURL = gBrowser.currentURI.spec;

if (newURL !== currentURL) {
currentURL = newURL;

gSsbInstallSupport.eventListeners.onCurrentTabChangedOrLoaded();
}
}
setInterval(checkURLChange, 1000);

let events = ["TabSelect"];

for (let event of events) {
gBrowser.tabContainer.addEventListener(
event,
this.eventListeners.onCurrentTabChangedOrLoaded
);
}

// This is needed to handle the case when the user opens a new tab in the same window.
window.setTimeout(() => {
this.eventListeners.onCurrentTabChangedOrLoaded();
}, 1000);

this._initialized = true;
},

functions: {
async checkCurrentPageCanBeInstalled() {
let currentURI = gBrowser.currentURI;
let currentTab = gBrowser.selectedTab;
let currentTabURL = currentTab.linkedBrowser.currentURI.spec;

if (
currentTabURL.startsWith("https://") ||
currentTabURL.startsWith("file://") ||
currentURI.asciiHost === "localhost"
) {
return true;
}

return false;
},

async checkCurrentPageHasSsbManifest() {
if (
gBrowser.currentURI.schemeIs("about") ||
gBrowser.currentURI.schemeIs("chrome") ||
gBrowser.currentURI.schemeIs("resource") ||
gBrowser.currentURI.schemeIs("view-source") ||
gBrowser.currentURI.schemeIs("moz-extension") ||
// Exlude "about:blank"
gBrowser.currentURI.spec === "about:blank"
) {
return null;
}

let actor =
gBrowser.selectedBrowser.browsingContext.currentWindowGlobal.getActor(
"SiteSpecificBrowser"
);
// If true, return the manifest href, otherwise return null
let result = await actor.sendQuery("checkSsbManifestIsExistent");

return result;
},

async checkCurrentPageIsInstalled() {
let currentTabSsb = await this.getCurrentTabSsb();
let ssbData =
await SiteSpecificBrowserExternalFileService.getCurrentSsbData();

for (let key in ssbData) {
if (key === currentTabSsb._manifest.start_url) {
return true;
}
}
return false;
},

enableInstallButton() {
let installButton = document.getElementById("ssbPageAction");
installButton.removeAttribute("hidden");
},

disableInstallButton() {
let installButton = document.getElementById("ssbPageAction");
installButton.setAttribute("hidden", true);
},

async getCurrentTabSsb() {
let currentURISsbObj = await SiteSpecificBrowser.createFromBrowser(
gBrowser.selectedBrowser
);

return currentURISsbObj;
},

async setImageToInstallButton() {
gBrowser.currentURI;

let currentURISsbObj = await this.getCurrentTabSsb();
let isInstalled = await this.checkCurrentPageIsInstalled();

let currentTabTitle = currentURISsbObj.name;
let currentTabURL = currentURISsbObj._scope.displayHost;

let ssbContentLabel = document.getElementById("ssb-content-label");
let ssbContentDescription = document.getElementById(
"ssb-content-description"
);
let ssbContentIcon = document.getElementById("ssb-content-icon");

let installButton = document.querySelector(".ssb-app-install-button");

if (ssbContentLabel) {
ssbContentLabel.textContent = currentTabTitle;
}

if (ssbContentDescription) {
ssbContentDescription.textContent = currentTabURL;
}

if (installButton) {
if (isInstalled) {
document.l10n.setAttributes(installButton, "ssb-app-open-button");
} else {
document.l10n.setAttributes(installButton, "ssb-app-install-button");
}
}

if (ssbContentIcon) {
ssbContentIcon.src = document.querySelector(
".tab-icon-image[selected=true]"
).src;
}
},
},

eventListeners: {
async onCurrentTabChangedOrLoaded() {
// set image to the install button
let currentPageCanBeInstalled =
await gSsbInstallSupport.functions.checkCurrentPageCanBeInstalled();
let currentPageHasSsbManifest =
await gSsbInstallSupport.functions.checkCurrentPageHasSsbManifest();

if (!currentPageCanBeInstalled || currentPageHasSsbManifest === null) {
gSsbInstallSupport.functions.disableInstallButton();
return;
}

gSsbInstallSupport.functions.setImageToInstallButton();

window.setTimeout(() => {
gSsbInstallSupport.functions.enableInstallButton();
}, 100);
},
},
};

gSsbInstallSupport.init();
Loading

0 comments on commit f0b4b42

Please sign in to comment.