Commit 64d6bab1 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇
Browse files

Add SettingsUtils utility class

For reading and writing of settings.
Promise-based API taking into account defaults values.
Also drop unused loadSettings callback in options page.

Differential Revision: https://phabricator.kde.org/D24165
parent bc9d26da
......@@ -58,13 +58,13 @@ chrome.runtime.onMessage.addListener(function (message, sender) {
}
});
var storage = (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync);
storage.get(DEFAULT_EXTENSION_SETTINGS, function (items) {
SettingsUtils.get().then((items) => {
if (items.breezeScrollBars.enabled) {
loadBreezeScrollBars();
}
if (items.mpris.enabled) {
const mpris = items.mpris;
if (mpris.enabled) {
loadMpris();
if (items.mprisMediaSessions.enabled) {
loadMediaSessionsShim();
......
......@@ -23,8 +23,6 @@ var runtimeCallbacks = {};
let currentMessageSerial = 0;
let pendingMessageReplyResolvers = {};
var storage = (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync);
// Callback is called with following arguments (in that order);
// - The actual message data/payload
// - The name of the action triggered
......
......@@ -41,12 +41,7 @@ function sendEnvironment() {
}
function sendSettings() {
storage.get(DEFAULT_EXTENSION_SETTINGS, function (items) {
if (chrome.runtime.lastError) {
console.warn("Failed to load settings");
return;
}
SettingsUtils.get().then((items) => {
sendPortMessage("settings", "changed", items);
});
}
......
......@@ -22,6 +22,7 @@
"background": {
"scripts": [
"constants.js",
"utils.js",
"extension-utils.js",
"extension-kdeconnect.js",
......@@ -43,7 +44,7 @@
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["constants.js", "content-utils.js", "content-script.js"],
"js": ["constants.js", "utils.js", "content-utils.js", "content-script.js"],
"run_at":"document_start",
"all_frames": true,
"match_about_blank": true
......
......@@ -7,6 +7,7 @@
<link rel="stylesheet" href="options.css">
<script src="constants.js"></script>
<script src="i18n.js"></script>
<script src="utils.js"></script>
<script src="content-utils.js"></script>
<script src="options.js"></script>
</head>
......
......@@ -16,8 +16,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var storage = (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync);
function tabClicked(tabbar, tabbutton) {
tabbar.buttons.forEach(function (button) {
var tablink = button.dataset.tabLink
......@@ -34,15 +32,8 @@ function tabClicked(tabbar, tabbutton) {
});
}
function loadSettings(cb) {
storage.get(DEFAULT_EXTENSION_SETTINGS, function (items) {
if (chrome.runtime.lastError) {
if (typeof cb === "function") {
cb(false);
}
return;
}
function loadSettings() {
SettingsUtils.get().then((items) => {
for (let key in items) {
if (!items.hasOwnProperty(key)) {
continue;
......@@ -94,10 +85,6 @@ function loadSettings(cb) {
});
}
}
if (typeof cb === "function") {
cb(true);
}
});
}
......@@ -141,14 +128,11 @@ function saveSettings(cb) {
}
}
try {
storage.set(settings, function () {
return cb(chrome.runtime.lastError);
});
// When the extension is reloaded, any call to extension APIs throws
} catch (e) {
cb(e);
}
SettingsUtils.set(settings).then(() => {
cb();
}, (err) => {
cb(err);
});
}
function updateDependencies(control, extension, settingsKey) {
......
/*
Copyright (C) 2019 Kai Uwe Broulik <kde@privat.broulik.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class SettingsUtils {
static storage() {
return (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync);
}
static get() {
return new Promise((resolve, reject) => {
SettingsUtils.storage().get(DEFAULT_EXTENSION_SETTINGS, (items) => {
const error = chrome.runtime.lastError;
if (error) {
return reject(error);
}
resolve(items);
});
});
}
static set(settings) {
return new Promise((resolve, reject) => {
try {
SettingsUtils.storage().set(settings, () => {
const error = chrome.runtime.lastError;
if (error) {
return reject(error);
}
resolve();
});
} catch (e) {
reject(e);
}
});
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment