From e29205dc4d013704cd6b1ee2fd7b7dc0ef226c01 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sun, 23 Jun 2019 02:27:59 +0200 Subject: Move storage API Also remove optional cachestorage script. It doesn't exists and the cachedstorage, whatever it is, is defined as an alias for the normal storage system. --- js/vapi-storage.js | 339 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 339 insertions(+) create mode 100644 js/vapi-storage.js (limited to 'js/vapi-storage.js') diff --git a/js/vapi-storage.js b/js/vapi-storage.js new file mode 100644 index 0000000..768958c --- /dev/null +++ b/js/vapi-storage.js @@ -0,0 +1,339 @@ +/******************************************************************************* + + ηMatrix - a browser extension to black/white list requests. + Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors + Copyright (C) 2019 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 + 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/}. + + Home: https://gitlab.com/vannilla/ematrix + uMatrix Home: https://github.com/gorhill/uMatrix +*/ + +/* global self, Components */ + +// For background page (tabs management) + +'use strict'; + +/******************************************************************************/ + +(function () { + const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); + + let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js + + // API matches that of chrome.storage.local: + // https://developer.chrome.com/extensions/storage + vAPI.storage = (function () { + let db = null; + let vacuumTimer = null; + + let close = function () { + if (vacuumTimer !== null) { + clearTimeout(vacuumTimer); + vacuumTimer = null; + } + + if (db === null) { + return; + } + + db.asyncClose(); + db = null; + }; + + let open = function () { + if (db !== null) { + 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)); + } + if (!path.isDirectory()) { + 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'); + } + + path.append(location.host + '.sqlite'); + + // Open database + try { + db = Services.storage.openDatabase(path); + if (db.connectionReady === false) { + db.asyncClose(); + db = null; + } + } catch (ex) { + // Ignore + } + + if (db === null) { + return null; + } + + // Database was opened, register cleanup task + vAPI.addCleanUpTask(close); + + // Setup database + db.createAsyncStatement('CREATE TABLE IF NOT EXISTS ' + +'"settings" ("name" ' + +'TEXT PRIMARY KEY NOT NULL, ' + +'"value" TEXT);') + .executeAsync(); + + if (vacuum !== null) { + vacuumTimer = vAPI.setTimeout(vacuum, 60000); + } + + return db; + }; + + // Vacuum only once, and only while idle + let vacuum = function () { + vacuumTimer = null; + if (db === null) { + return; + } + let idleSvc = + Cc['@mozilla.org/widget/idleservice;1'] + .getService(Ci.nsIIdleService); + + if (idleSvc.idleTime < 60000) { + vacuumTimer = vAPI.setTimeout(vacuum, 60000); + return; + } + + db.createAsyncStatement('VACUUM').executeAsync(); + vacuum = null; + }; + + // Execute a query + let runStatement = function (stmt, callback) { + let result = {}; + + stmt.executeAsync({ + handleResult: function (rows) { + if (!rows || typeof callback !== 'function') { + 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); + } + }, + handleCompletion: function (reason) { + if (typeof callback === 'function' && reason === 0) { + callback(result); + } + }, + handleError: function (error) { + console.error('SQLite error ', error.result, error.message); + + // Caller expects an answer regardless of failure. + if (typeof callback === 'function' ) { + callback(null); + } + }, + }); + }; + + let bindNames = function (stmt, names) { + if (Array.isArray(names) === false || names.length === 0) { + 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); + } + + stmt.bindParameters(params); + }; + + let clear = function (callback) { + if (open() === null) { + if (typeof callback === 'function') { + callback(); + } + return; + } + + runStatement(db.createAsyncStatement('DELETE FROM "settings";'), + callback); + }; + + let getBytesInUse = function (keys, callback) { + if (typeof callback !== 'function') { + return; + } + + if (open() === null) { + 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); + } else { + stmt = db.createAsyncStatement('SELECT "size" AS "size", ' + +'SUM(LENGTH("value")) ' + +'FROM "settings"'); + } + + runStatement(stmt, function (result) { + callback(result.size); + }); + }; + + let read = function (details, callback) { + if (typeof callback !== 'function') { + return; + } + + let prepareResult = function (result) { + for (let key in result) { + if (result.hasOwnProperty(key) === false) { + continue; + } + + result[key] = JSON.parse(result[key]); + } + + if (typeof details === 'object' && details !== null) { + for (let key in details) { + if (result.hasOwnProperty(key) === false) { + result[key] = details[key]; + } + } + } + + callback(result); + }; + + if (open() === null) { + prepareResult({}); + return; + } + + let names = []; + if (details !== null) { + if (Array.isArray(details)) { + names = details; + } else if (typeof details === 'object') { + names = Object.keys(details); + } else { + names = [details.toString()]; + } + } + + let stmt; + if (names.length === 0) { + stmt = db.createAsyncStatement('SELECT * FROM "settings"'); + } else { + stmt = db.createAsyncStatement('SELECT * FROM "settings" ' + +'WHERE "name" = :name'); + bindNames(stmt, names); + } + + runStatement(stmt, prepareResult); + }; + + let remove = function (keys, callback) { + if (open() === null) { + if (typeof callback === 'function') { + callback(); + } + return; + } + + var stmt = db.createAsyncStatement('DELETE FROM "settings" ' + +'WHERE "name" = :name'); + bindNames(stmt, typeof keys === 'string' ? [keys] : keys); + runStatement(stmt, callback); + }; + + let write = function (details, callback) { + if (open() === null) { + if (typeof callback === 'function') { + callback(); + } + return; + } + + let stmt = db.createAsyncStatement('INSERT OR REPLACE INTO ' + +'"settings" ("name", "value") ' + +'VALUES(:name, :value)'); + let params = stmt.newBindingParamsArray(); + + for (let key in details) { + if (details.hasOwnProperty(key) === false) { + continue; + } + + let bp = params.newBindingParams(); + bp.bindByName('name', key); + bp.bindByName('value', JSON.stringify(details[key])); + params.addParams(bp); + } + + if (params.length === 0) { + return; + } + + stmt.bindParameters(params); + runStatement(stmt, callback); + }; + + // Export API + var api = { + QUOTA_BYTES: 100 * 1024 * 1024, + clear: clear, + get: read, + getBytesInUse: getBytesInUse, + remove: remove, + set: write + }; + + return api; + })(); + + vAPI.cacheStorage = vAPI.storage; +})(); -- cgit v1.2.3 From 194b9f768b7e8ea57217fa6cf7b501727e65b662 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sun, 23 Jun 2019 13:36:44 +0200 Subject: Remove some comments While they are technically informative, the splitting makes things easier to follow already (somewhat) and there's not really a need to list each global variable (there aren't many anyway.) --- js/vapi-storage.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'js/vapi-storage.js') diff --git a/js/vapi-storage.js b/js/vapi-storage.js index 768958c..75ed6ff 100644 --- a/js/vapi-storage.js +++ b/js/vapi-storage.js @@ -21,10 +21,6 @@ uMatrix Home: https://github.com/gorhill/uMatrix */ -/* global self, Components */ - -// For background page (tabs management) - 'use strict'; /******************************************************************************/ -- cgit v1.2.3 From 9d87f8f864b28d182af9659a3095433adb4b0126 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 17:33:06 +0200 Subject: Change how modules are imported I can't really find a reason why the returned value is preferred over the normal importing process. Additionally, there's a good chance importing Services.jsm can be done only once at the start of everything, instead of binding each object to a separate closure. --- js/vapi-storage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'js/vapi-storage.js') diff --git a/js/vapi-storage.js b/js/vapi-storage.js index 75ed6ff..6a6cb3b 100644 --- a/js/vapi-storage.js +++ b/js/vapi-storage.js @@ -27,7 +27,7 @@ (function () { const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); + Cu.import('resource://gre/modules/Services.jsm'); let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js -- cgit v1.2.3 From 51f5e899fff9e804d9c91e4fefdd57ea5a85e99c Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 18:06:54 +0200 Subject: Make components and services global Given that they are used a lot, at least in vAPI, let's just define/import them only once. --- js/vapi-storage.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'js/vapi-storage.js') diff --git a/js/vapi-storage.js b/js/vapi-storage.js index 6a6cb3b..6dbb955 100644 --- a/js/vapi-storage.js +++ b/js/vapi-storage.js @@ -26,9 +26,6 @@ /******************************************************************************/ (function () { - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - Cu.import('resource://gre/modules/Services.jsm'); - let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js // API matches that of chrome.storage.local: -- cgit v1.2.3 From f1f66637b814155c95a2bfddfdd9cd2973b86659 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 18:20:08 +0200 Subject: Revert "Make components and services global" This reverts commit 51f5e899fff9e804d9c91e4fefdd57ea5a85e99c. It seems to cause issues with the popup menu. --- js/vapi-storage.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'js/vapi-storage.js') diff --git a/js/vapi-storage.js b/js/vapi-storage.js index 6dbb955..6a6cb3b 100644 --- a/js/vapi-storage.js +++ b/js/vapi-storage.js @@ -26,6 +26,9 @@ /******************************************************************************/ (function () { + const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + Cu.import('resource://gre/modules/Services.jsm'); + let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js // API matches that of chrome.storage.local: -- cgit v1.2.3 From 6908a6c89f9f44380faa1831ec13cff4042b671a Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 18:41:18 +0200 Subject: Make vAPI definitely global At least for background.html, it can be defined once at the start of vapi-core and then populated. --- js/vapi-storage.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'js/vapi-storage.js') diff --git a/js/vapi-storage.js b/js/vapi-storage.js index 6a6cb3b..ce4ab72 100644 --- a/js/vapi-storage.js +++ b/js/vapi-storage.js @@ -29,8 +29,6 @@ const {classes: Cc, interfaces: Ci, utils: Cu} = Components; Cu.import('resource://gre/modules/Services.jsm'); - let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js - // API matches that of chrome.storage.local: // https://developer.chrome.com/extensions/storage vAPI.storage = (function () { -- cgit v1.2.3 From acd097e4733c106a15815c90e21c00d0c545e042 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Fri, 19 Jul 2019 16:00:08 +0200 Subject: Make components and Services.jsm global Once again, but this time it works. --- js/vapi-storage.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'js/vapi-storage.js') diff --git a/js/vapi-storage.js b/js/vapi-storage.js index ce4ab72..7aa873c 100644 --- a/js/vapi-storage.js +++ b/js/vapi-storage.js @@ -26,9 +26,6 @@ /******************************************************************************/ (function () { - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - Cu.import('resource://gre/modules/Services.jsm'); - // API matches that of chrome.storage.local: // https://developer.chrome.com/extensions/storage vAPI.storage = (function () { -- cgit v1.2.3