Skip to content

Commit

Permalink
Implement quit-requested and quit-granted warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jph-sendlater committed Dec 9, 2020
1 parent c782744 commit fda5964
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
7 changes: 7 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ const SendLater = {
const { preferences } = await browser.storage.local.get({ preferences: {} });
const activeSchedules = await this.getActiveSchedules(preferences.instanceUUID);
const nActive = activeSchedules.length;
browser.SL3U.setSendLaterVar("messages_pending", nActive);
if (nActive > 0) {
statusMsg = browser.i18n.getMessage("PendingMessage", [nActive]);
} else {
Expand Down Expand Up @@ -1342,6 +1343,12 @@ function mainLoop() {
clearTimeout(SendLater.loopTimeout);
}

// This variable gets set to "true" when a user is warned about
// leaving TB open, but it may not get set back to false if
// some other quit-requested-observer aborts the quit. So just
// just in case, we'll periodically set it back to "false".
browser.SL3U.setSendLaterVar("quit_confirmed", false);

browser.storage.local.get({ preferences: {} }).then((storage) => {
let interval = +storage.preferences.checkTimePref || 0;
if (storage.preferences.checkTimePref_isMilliseconds)
Expand Down
145 changes: 145 additions & 0 deletions experiments/sl3u.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,66 @@ const SendLaterFunctions = {
debug(...msg) { this.logger(msg, "debug", console.debug) },
trace(...msg) { this.logger(msg, "trace", console.trace) },

getMessage(context, messageName, substitutions) {
// from static.js
try {
messageName = messageName.toLowerCase();

let messages, str;

const ext = context.extension;
const selectedLocale = ext.localeData.selectedLocale;
if (ext.localeData.messages.has(selectedLocale)) {
messages = ext.localeData.messages.get(selectedLocale);
if (messages.has(messageName)) {
str = messages.get(messageName);
}
}

if (str === undefined) {
SendLaterFunctions.warn(`Unable to find message ${messageName} in locale ${selectedLocale}`);
for (let locale of ext.localeData.availableLocales) {
if (ext.localeData.messages.has(locale)) {
messages = ext.localeData.messages.get(locale);
if (messages.has(messageName)) {
str = messages.get(messageName);
break;
}
}
}
}

if (str === undefined) {
str = messageName;
}

if (!str.includes("$")) {
return str;
}

if (!Array.isArray(substitutions)) {
substitutions = [substitutions];
}

let replacer = (matched, index, dollarSigns) => {
if (index) {
// This is not quite Chrome-compatible. Chrome consumes any number
// of digits following the $, but only accepts 9 substitutions. We
// accept any number of substitutions.
index = parseInt(index, 10) - 1;
return index in substitutions ? substitutions[index] : "";
}
// For any series of contiguous `$`s, the first is dropped, and
// the rest remain in the output string.
return dollarSigns;
};
return str.replace(/\$(?:([1-9]\d*)|(\$+))/g, replacer);
} catch (e) {
console.warn("Unable to get localized message.",e);
}
return "";
},

waitAndDelete(file_arg) {
const timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
const callback = {
Expand Down Expand Up @@ -1450,6 +1510,7 @@ var SL3U = class extends ExtensionCommon.ExtensionAPI {
try {
const data = JSON.parse(dataStr);
SendLaterVars.logConsoleLevel = (data.logConsoleLevel||"all").toLowerCase();
SendLaterVars.ask_quit = data.askQuit;
} catch (ex) {
SendLaterFunctions.warn(
`SL3U.notifyStorageLocal Unable to set SendLaterVars.logConsoleLevel`
Expand Down Expand Up @@ -1489,6 +1550,10 @@ var SL3U = class extends ExtensionCommon.ExtensionAPI {
});
},

async setSendLaterVar(key, value) {
SendLaterVars[key] = value;
},

async startObservers() {
try {
const loadPrefs = async () => {
Expand All @@ -1503,6 +1568,7 @@ var SL3U = class extends ExtensionCommon.ExtensionAPI {
);
SendLaterVars.logConsoleLevel =
(preferences.logConsoleLevel||"all").toLowerCase();
SendLaterVars.ask_quit = preferences.askQuit;
return true;
} catch (err) {
// SendLaterFunctions.warn("Could not fetch preferences", err);
Expand All @@ -1516,6 +1582,85 @@ var SL3U = class extends ExtensionCommon.ExtensionAPI {
}
} catch {}

const QuitObserver = {
observe(subject, topic, data) {
const appName = Services.appinfo.name;
const scheduledMessagesWarningTitle =
SendLaterFunctions.getMessage(context, "scheduledMessagesWarningTitle");
const scheduledMessagesWarningQuitRequested =
SendLaterFunctions.getMessage(context, "scheduledMessagesWarningQuitRequested", [appName]);
const scheduledMessagesWarningQuit =
SendLaterFunctions.getMessage(context, "ScheduledMessagesWarningQuit", [appName]);
const confirmAgain =
SendLaterFunctions.getMessage(context, "confirmAgain");

const localStorage = context.apiCan.findAPIPath("storage.local");
let check, result;
switch (topic) {
case "quit-application-requested":
if (!SendLaterVars.messages_pending ||
!SendLaterVars.ask_quit) {
return;
}

SendLaterVars.quit_confirmed = true;
check = { value: true };
result = Services.prompt.confirmCheck(
null, scheduledMessagesWarningTitle,
scheduledMessagesWarningQuitRequested,
confirmAgain, check
);
if (!check.value) {
localStorage.callMethodInParentProcess(
"get", [{ "preferences": {} }]
).then(({ preferences }) => {
preferences.askQuit = false;
localStorage.callMethodInParentProcess(
"set", [{ preferences }]
).then(
() => { console.log("Successfully set preferences.askQuit = false"); }
);
}).catch(SendLaterFunctions.error);
}
if (!result) {
subject.QueryInterface(Ci.nsISupportsPRBool);
subject.data = true;
}
break;
case "quit-application-granted":
if (SendLaterVars.quit_confirmed ||
!SendLaterVars.messages_pending ||
!SendLaterVars.ask_quit) {
return;
}
check = { value: true };
result = Services.prompt.alertCheck(
null, scheduledMessagesWarningTitle,
scheduledMessagesWarningQuit,
confirmAgain, check
);
if (!check.value) {
localStorage.callMethodInParentProcess(
"get", [{ "preferences": {} }]
).then(({ preferences }) => {
preferences.askQuit = false;
localStorage.callMethodInParentProcess(
"set", [{ preferences }]
).then(
() => { console.log("Successfully set preferences.askQuit = false"); }
);
}).catch(SendLaterFunctions.error);
}
break;
default:
break;
}
},
}

Services.obs.addObserver(QuitObserver, "quit-application-requested");
Services.obs.addObserver(QuitObserver, "quit-application-granted");

// Setup various observers.
SendLaterBackgrounding();
},
Expand Down
15 changes: 15 additions & 0 deletions experiments/sl3u.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@
}
]
},
{
"name":"setSendLaterVar",
"type":"function",
"async":true,
"parameters":[
{
"name":"key",
"type":"string"
},
{
"name":"value",
"type":"any"
}
]
},
{
"name": "generateMsgId",
"type": "function",
Expand Down

0 comments on commit fda5964

Please sign in to comment.