Skip to content

Commit

Permalink
[FB] PWA | Save data to external file
Browse files Browse the repository at this point in the history
  • Loading branch information
surapunoyousei committed Nov 2, 2023
1 parent f9014e5 commit 8616ea7
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 62 deletions.
156 changes: 105 additions & 51 deletions browser/base/content/browser-pageActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,117 @@
* 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/. */

let QRCodeGeneratePageActionButton = window.MozXULElement.parseXULToFragment(`
<hbox id="QRCodeGeneratePageAction" data-l10n-id="qrcode-generate-page-action"
class="urlbar-page-action" tooltiptext="qrcode-generate-page-action"
role="button" popup="qrcode-panel">
<image id="QRCodeGeneratePageAction-image" class="urlbar-icon"/>
<panel id="qrcode-panel" type="arrow" position="bottomright topright" onpopupshowing="generateCurrentTabQRCode()">
<vbox id="qrcode-box">
<vbox class="panel-header">
<html:h1>
<html:span data-l10n-id="qrcode-generate-page-action-title"></html:span>
</html:h1>
</vbox>
<toolbarseparator/>
<vbox id="qrcode-img-vbox">
</vbox>
</vbox>
</panel>
</hbox>
`);
/****************************************************** QR Code ******************************************************/

function generateCurrentTabQRCode() {
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",
},
cornersSquareOptions: {
type: "extra-rounded",
},
backgroundOptions: {
color: "#e9ebee",
},
imageOptions: {
crossOrigin: "anonymous",
margin: 10
let gFloorpPageAction = {
qrCode: {
QRCodeGeneratePageActionButton: window.MozXULElement.parseXULToFragment(`
<hbox id="QRCodeGeneratePageAction" data-l10n-id="qrcode-generate-page-action"
class="urlbar-page-action" tooltiptext="qrcode-generate-page-action"
role="button" popup="qrcode-panel">
<image id="QRCodeGeneratePageAction-image" class="urlbar-icon"/>
<panel id="qrcode-panel" type="arrow" position="bottomright topright" onpopupshowing="gFloorpPageAction.qrCode.onPopupShowing()">
<vbox id="qrcode-box">
<vbox class="panel-header">
<html:h1>
<html:span data-l10n-id="qrcode-generate-page-action-title"></html:span>
</html:h1>
</vbox>
<toolbarseparator/>
<vbox id="qrcode-img-vbox">
</vbox>
</vbox>
</panel>
</hbox>
`),
onPopupShowing() {
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",
},
cornersSquareOptions: {
type: "extra-rounded",
},
backgroundOptions: {
color: "#e9ebee",
},
imageOptions: {
crossOrigin: "anonymous",
margin: 10
}
});

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

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

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

//remove old qrcode
let QRCodeBox = document.getElementById("qrcode-img-vbox");
Ssb: {
SsbPageActionButton: window.MozXULElement.parseXULToFragment(`
<hbox id="ssbPageAction" data-l10n-id="ssb-page-action"
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.qrCode.generateCurrentTabQRCode()">
<vbox id="ssb-box">
<vbox class="panel-header">
<html:h1>
<html:span data-l10n-id="ssb-page-action-title"></html:span>
</html:h1>
</vbox>
<toolbarseparator/>
<vbox id="qrcode-img-vbox">
</vbox>
</vbox>
</panel>
</hbox>
`),

while (QRCodeBox.firstChild) {
QRCodeBox.firstChild.remove();
}
async onPopupShowing() {
let currentPageUrl = gBrowser.selectedBrowser.currentURI.spec;

qrCode.append(QRCodeBox);
let ssb = await SiteSpecificBrowser.createFromBrowser(
gBrowser.selectedBrowser
);
},

async onCommand(event, buttonNode) {
if (!gBrowser.currentURI.schemeIs("https")) {
return;
}

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

// Launching through the UI implies installing.
await ssb.install();

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

SessionStore.promiseInitialized.then(() => {
document.getElementById("star-button-box").before(QRCodeGeneratePageActionButton);
document.getElementById("star-button-box").before(gFloorpPageAction.qrCode.QRCodeGeneratePageActionButton);
document.getElementById("star-button-box").before(gFloorpPageAction.Ssb.SsbPageActionButton);
});
58 changes: 58 additions & 0 deletions browser/components/ssb/SiteSpecificBrowserExternalFileService.jsm
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* 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 = ["SiteSpecificBrowserExternalFileService"];

let SiteSpecificBrowserExternalFileService = {
get _saveFile() {
return PathUtils.join(PathUtils.profileDir, "ssb.json");
},

async getCurrentSsbData() {
let fileExists = await IOUtils.exists(this._saveFile);
if (!fileExists) {
IOUtils.writeJSON(this._saveFile, {});
return {};
};

let result = await IOUtils.readJSON(
this._saveFile
);

return result;
},

async saveSsbData(ssbData) {
await IOUtils.writeJSON(this._saveFile, ssbData);
},

get _saveSsbMap() {
return PathUtils.join(PathUtils.profileDir, "ssb-map.json");
},

async ssbMapFileExists() {
let fileExists = await IOUtils.exists(this._saveSsbMap);
return fileExists;
},

async getSsbMapData() {
let fileExists = await IOUtils.exists(this._saveSsbMap);
if (!fileExists) {
IOUtils.writeJSON(this._saveSsbMap, {});
return null;
};

let result = await IOUtils.readUTF8(
this._saveSsbMap
);

return result;
},

async saveSsbMapData(ssbMapData) {
await IOUtils.writeJSON(this._saveSsbMap, ssbMapData);
},
}
49 changes: 38 additions & 11 deletions browser/components/ssb/SiteSpecificBrowserService.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,23 @@ const { Services } = ChromeUtils.import(
let { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);

let { KeyValueService } = ChromeUtils.importESModule(
"resource://gre/modules/kvstore.sys.mjs"
);

const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);

const { SiteSpecificBrowserExternalFileService } = ChromeUtils.import(
"resource:///modules/SiteSpecificBrowserExternalFileService.jsm"
);


XPCOMUtils.defineLazyModuleGetters(this, {
ManifestObtainer: "resource://gre/modules/ManifestObtainer.jsm",
ManifestProcessor: "resource://gre/modules/ManifestProcessor.jsm",
KeyValueService: "resource://gre/modules/kvstore.jsm",
ImageTools: "resource:///modules/ssb/ImageTools.jsm",
});

Expand Down Expand Up @@ -270,17 +278,21 @@ async function buildManifestForBrowser(browser) {
*/
let SSBMap = new Map();

if (Services.prefs.prefHasUserValue("browser.ssb.SSBMap")) {
function loadMapFromLocalStorage() {
const mapJson = Services.prefs.getStringPref("browser.ssb.SSBMap");
const serializedMap = JSON.parse(mapJson);
const map = new Map(serializedMap);
return map;

async function loadMapFromLocalStorage() {
const mapJson = await SiteSpecificBrowserExternalFileService.getSsbMapData();
if (!mapJson) {
return new Map();
}

SSBMap = loadMapFromLocalStorage();
const serializedMap = JSON.parse(mapJson);
const map = new Map(serializedMap);
return map;
}

loadMapFromLocalStorage().then((map) => {
SSBMap = map;
});


/**
* The base contains the data about an SSB instance needed in content processes.
Expand Down Expand Up @@ -390,8 +402,8 @@ class SiteSpecificBrowser extends SiteSpecificBrowserBase {
SSBMap.set(id, this);

function saveMapToLocalStorage(map) {
let mapJson = JSON.stringify([...map]);
Services.prefs.setStringPref("browser.ssb.SSBMap", mapJson);
const serializedMap = [...map];
SiteSpecificBrowserExternalFileService.saveSsbMapData(serializedMap);
}

saveMapToLocalStorage(SSBMap);
Expand Down Expand Up @@ -542,6 +554,21 @@ class SiteSpecificBrowser extends SiteSpecificBrowserBase {

let kvstore = await SiteSpecificBrowserService.getKVStore();
await kvstore.put(storeKey(this.id), JSON.stringify(data));

let ssbData = await SiteSpecificBrowserExternalFileService.getCurrentSsbData();

if (!ssbData) {
ssbData = {};
}

ssbData[this.startURI.spec] = {
name: this.name,
icon: this.getIcon(128).src,
id: this.id,
startURI: this.startURI.spec,
};

await SiteSpecificBrowserExternalFileService.saveSsbData(ssbData);
}
}

Expand Down
1 change: 1 addition & 0 deletions browser/components/ssb/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ XPCOM_MANIFESTS += [
]

EXTRA_JS_MODULES += [
"SiteSpecificBrowserExternalFileService.jsm",
"SiteSpecificBrowserIdUtils.jsm",
"SiteSpecificBrowserService.jsm",
]
Expand Down

0 comments on commit 8616ea7

Please sign in to comment.