aboutsummaryrefslogtreecommitdiffstats
path: root/js/vapi-storage.js
diff options
context:
space:
mode:
authorJesús <heckyel@hyperbola.info>2022-04-06 10:38:06 +0800
committerJesús <heckyel@hyperbola.info>2022-04-06 10:38:06 +0800
commit8aeb670be1d252807252ee2bfe99b15b81c3e28d (patch)
treeced0973165449d620c5c40876c37ab16b17655f9 /js/vapi-storage.js
parent5aa99a2ea2b683ba35eb36dfc54efd79f3cfcb85 (diff)
downloadematrix-8aeb670be1d252807252ee2bfe99b15b81c3e28d.tar.lz
ematrix-8aeb670be1d252807252ee2bfe99b15b81c3e28d.tar.xz
ematrix-8aeb670be1d252807252ee2bfe99b15b81c3e28d.zip
update from upstream
Diffstat (limited to 'js/vapi-storage.js')
-rw-r--r--js/vapi-storage.js260
1 files changed, 130 insertions, 130 deletions
diff --git a/js/vapi-storage.js b/js/vapi-storage.js
index 8e46b90..bcc3a9a 100644
--- a/js/vapi-storage.js
+++ b/js/vapi-storage.js
@@ -2,7 +2,7 @@
ηMatrix - a browser extension to black/white list requests.
Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors
- Copyright (C) 2019-2020 Alessio Vanni
+ Copyright (C) 2019-2022 Alessio Vanni
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
@@ -17,7 +17,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
- Home: https://libregit.spks.xyz/heckyel/ematrix
+ Home: https://gitlab.com/vannilla/ematrix
uMatrix Home: https://github.com/gorhill/uMatrix
*/
@@ -29,60 +29,60 @@
// API matches that of chrome.storage.local:
// https://developer.chrome.com/extensions/storage
vAPI.storage = (function () {
- let db = null;
- let vacuumTimer = null;
+ let db = null;
+ let vacuumTimer = null;
- let close = function () {
+ let close = function () {
if (vacuumTimer !== null) {
- clearTimeout(vacuumTimer);
- vacuumTimer = null;
+ clearTimeout(vacuumTimer);
+ vacuumTimer = null;
}
if (db === null) {
- return;
+ return;
}
db.asyncClose();
db = null;
- };
+ };
- let open = function () {
+ let open = function () {
if (db !== null) {
- return db;
+ return db;
}
// Create path
let path = Services.dirsvc.get('ProfD', Ci.nsIFile);
path.append('ematrix-data');
if (!path.exists()) {
- path.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0774', 8));
+ path.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0774', 8));
}
if (!path.isDirectory()) {
- throw Error('Should be a directory...');
+ throw Error('Should be a directory...');
}
- let path2 = Services.dirsvc.get('ProfD', Ci.nsIFile);
- path2.append('extension-data');
- path2.append(location.host + '.sqlite');
- if (path2.exists()) {
- path2.moveTo(path, location.host+'.sqlite');
- }
+ let path2 = Services.dirsvc.get('ProfD', Ci.nsIFile);
+ path2.append('extension-data');
+ path2.append(location.host + '.sqlite');
+ if (path2.exists()) {
+ path2.moveTo(path, location.host+'.sqlite');
+ }
- path.append(location.host + '.sqlite');
+ path.append(location.host + '.sqlite');
// Open database
try {
- db = Services.storage.openDatabase(path);
- if (db.connectionReady === false) {
+ db = Services.storage.openDatabase(path);
+ if (db.connectionReady === false) {
db.asyncClose();
db = null;
- }
+ }
} catch (ex) {
- // Ignore
+ // Ignore
}
if (db === null) {
- return null;
+ return null;
}
// Database was opened, register cleanup task
@@ -90,240 +90,240 @@
// Setup database
db.createAsyncStatement('CREATE TABLE IF NOT EXISTS '
- +'"settings" ("name" '
- +'TEXT PRIMARY KEY NOT NULL, '
- +'"value" TEXT);')
- .executeAsync();
+ +'"settings" ("name" '
+ +'TEXT PRIMARY KEY NOT NULL, '
+ +'"value" TEXT);')
+ .executeAsync();
if (vacuum !== null) {
- vacuumTimer = vAPI.setTimeout(vacuum, 60000);
+ vacuumTimer = vAPI.setTimeout(vacuum, 60000);
}
return db;
- };
+ };
- // Vacuum only once, and only while idle
- let vacuum = function () {
+ // Vacuum only once, and only while idle
+ let vacuum = function () {
vacuumTimer = null;
if (db === null) {
- return;
+ return;
}
let idleSvc =
- Cc['@mozilla.org/widget/idleservice;1']
- .getService(Ci.nsIIdleService);
+ Cc['@mozilla.org/widget/idleservice;1']
+ .getService(Ci.nsIIdleService);
if (idleSvc.idleTime < 60000) {
- vacuumTimer = vAPI.setTimeout(vacuum, 60000);
- return;
+ vacuumTimer = vAPI.setTimeout(vacuum, 60000);
+ return;
}
db.createAsyncStatement('VACUUM').executeAsync();
vacuum = null;
- };
+ };
- // Execute a query
- let runStatement = function (stmt, callback) {
+ // Execute a query
+ let runStatement = function (stmt, callback) {
let result = {};
stmt.executeAsync({
- handleResult: function (rows) {
+ handleResult: function (rows) {
if (!rows || typeof callback !== 'function') {
- return;
+ return;
}
let row;
while ((row = rows.getNextRow())) {
- // we assume that there will be two columns, since we're
- // using it only for preferences
- // eMatrix: the above comment is obsolete
- // (it's not used just for preferences
- // anymore), but we still expect two columns.
- let res = row.getResultByIndex(0);
- result[res] = row.getResultByIndex(1);
+ // we assume that there will be two columns, since we're
+ // using it only for preferences
+ // eMatrix: the above comment is obsolete
+ // (it's not used just for preferences
+ // anymore), but we still expect two columns.
+ let res = row.getResultByIndex(0);
+ result[res] = row.getResultByIndex(1);
}
- },
- handleCompletion: function (reason) {
+ },
+ handleCompletion: function (reason) {
if (typeof callback === 'function' && reason === 0) {
- callback(result);
+ callback(result);
}
- },
- handleError: function (error) {
+ },
+ handleError: function (error) {
console.error('SQLite error ', error.result, error.message);
// Caller expects an answer regardless of failure.
if (typeof callback === 'function' ) {
- callback(null);
+ callback(null);
}
- },
+ },
});
- };
+ };
- let bindNames = function (stmt, names) {
+ let bindNames = function (stmt, names) {
if (Array.isArray(names) === false || names.length === 0) {
- return;
+ return;
}
let params = stmt.newBindingParamsArray();
- for (let i=names.length-1; i>=0; --i) {
- let bp = params.newBindingParams();
- bp.bindByName('name', names[i]);
- params.addParams(bp);
+ for (let i=names.length-1; i>=0; --i) {
+ let bp = params.newBindingParams();
+ bp.bindByName('name', names[i]);
+ params.addParams(bp);
}
stmt.bindParameters(params);
- };
+ };
- let clear = function (callback) {
+ let clear = function (callback) {
if (open() === null) {
- if (typeof callback === 'function') {
+ if (typeof callback === 'function') {
callback();
- }
- return;
+ }
+ return;
}
runStatement(db.createAsyncStatement('DELETE FROM "settings";'),
- callback);
- };
+ callback);
+ };
- let getBytesInUse = function (keys, callback) {
+ let getBytesInUse = function (keys, callback) {
if (typeof callback !== 'function') {
- return;
+ return;
}
if (open() === null) {
- callback(0);
- return;
+ callback(0);
+ return;
}
let stmt;
if (Array.isArray(keys)) {
- stmt = db.createAsyncStatement('SELECT "size" AS "size", '
- +'SUM(LENGTH("value")) '
- +'FROM "settings" WHERE '
- +'"name" = :name');
- bindNames(keys);
+ stmt = db.createAsyncStatement('SELECT "size" AS "size", '
+ +'SUM(LENGTH("value")) '
+ +'FROM "settings" WHERE '
+ +'"name" = :name');
+ bindNames(keys);
} else {
- stmt = db.createAsyncStatement('SELECT "size" AS "size", '
- +'SUM(LENGTH("value")) '
- +'FROM "settings"');
+ stmt = db.createAsyncStatement('SELECT "size" AS "size", '
+ +'SUM(LENGTH("value")) '
+ +'FROM "settings"');
}
runStatement(stmt, function (result) {
- callback(result.size);
+ callback(result.size);
});
- };
+ };
- let read = function (details, callback) {
+ let read = function (details, callback) {
if (typeof callback !== 'function') {
- return;
+ return;
}
let prepareResult = function (result) {
- for (let key in result) {
+ for (let key in result) {
if (result.hasOwnProperty(key) === false) {
- continue;
+ continue;
}
result[key] = JSON.parse(result[key]);
- }
+ }
- if (typeof details === 'object' && details !== null) {
+ if (typeof details === 'object' && details !== null) {
for (let key in details) {
- if (result.hasOwnProperty(key) === false) {
+ if (result.hasOwnProperty(key) === false) {
result[key] = details[key];
- }
+ }
}
- }
+ }
- callback(result);
+ callback(result);
};
if (open() === null) {
- prepareResult({});
- return;
+ prepareResult({});
+ return;
}
let names = [];
if (details !== null) {
- if (Array.isArray(details)) {
+ if (Array.isArray(details)) {
names = details;
- } else if (typeof details === 'object') {
+ } else if (typeof details === 'object') {
names = Object.keys(details);
- } else {
+ } else {
names = [details.toString()];
- }
+ }
}
let stmt;
if (names.length === 0) {
- stmt = db.createAsyncStatement('SELECT * FROM "settings"');
+ stmt = db.createAsyncStatement('SELECT * FROM "settings"');
} else {
- stmt = db.createAsyncStatement('SELECT * FROM "settings" '
- +'WHERE "name" = :name');
- bindNames(stmt, names);
+ stmt = db.createAsyncStatement('SELECT * FROM "settings" '
+ +'WHERE "name" = :name');
+ bindNames(stmt, names);
}
runStatement(stmt, prepareResult);
- };
+ };
- let remove = function (keys, callback) {
+ let remove = function (keys, callback) {
if (open() === null) {
- if (typeof callback === 'function') {
+ if (typeof callback === 'function') {
callback();
- }
- return;
+ }
+ return;
}
var stmt = db.createAsyncStatement('DELETE FROM "settings" '
- +'WHERE "name" = :name');
+ +'WHERE "name" = :name');
bindNames(stmt, typeof keys === 'string' ? [keys] : keys);
runStatement(stmt, callback);
- };
+ };
- let write = function (details, callback) {
+ let write = function (details, callback) {
if (open() === null) {
- if (typeof callback === 'function') {
+ if (typeof callback === 'function') {
callback();
- }
- return;
+ }
+ return;
}
let stmt = db.createAsyncStatement('INSERT OR REPLACE INTO '
- +'"settings" ("name", "value") '
- +'VALUES(:name, :value)');
+ +'"settings" ("name", "value") '
+ +'VALUES(:name, :value)');
let params = stmt.newBindingParamsArray();
for (let key in details) {
- if (details.hasOwnProperty(key) === false) {
+ if (details.hasOwnProperty(key) === false) {
continue;
- }
+ }
- let bp = params.newBindingParams();
- bp.bindByName('name', key);
- bp.bindByName('value', JSON.stringify(details[key]));
- params.addParams(bp);
+ let bp = params.newBindingParams();
+ bp.bindByName('name', key);
+ bp.bindByName('value', JSON.stringify(details[key]));
+ params.addParams(bp);
}
if (params.length === 0) {
- return;
+ return;
}
stmt.bindParameters(params);
runStatement(stmt, callback);
- };
+ };
- // Export API
- var api = {
+ // Export API
+ var api = {
QUOTA_BYTES: 100 * 1024 * 1024,
clear: clear,
get: read,
getBytesInUse: getBytesInUse,
remove: remove,
set: write
- };
+ };
- return api;
+ return api;
})();
vAPI.cacheStorage = vAPI.storage;