From 9a5c1c75f96b26041443309a8bfa977da8950c38 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sun, 23 Jun 2019 13:34:42 +0200 Subject: Get cookie management out of vapi-background --- js/vapi-cookies.js | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 js/vapi-cookies.js (limited to 'js/vapi-cookies.js') diff --git a/js/vapi-cookies.js b/js/vapi-cookies.js new file mode 100644 index 0000000..be989b2 --- /dev/null +++ b/js/vapi-cookies.js @@ -0,0 +1,127 @@ +/******************************************************************************* + + η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 + + vAPI.cookies = {}; + + vAPI.cookies.CookieEntry = function (ffCookie) { + this.domain = ffCookie.host; + this.name = ffCookie.name; + this.path = ffCookie.path; + this.secure = ffCookie.isSecure === true; + this.session = ffCookie.expires === 0; + this.value = ffCookie.value; + }; + + vAPI.cookies.start = function () { + Services.obs.addObserver(this, 'cookie-changed', false); + Services.obs.addObserver(this, 'private-cookie-changed', false); + vAPI.addCleanUpTask(this.stop.bind(this)); + }; + + vAPI.cookies.stop = function () { + Services.obs.removeObserver(this, 'cookie-changed'); + Services.obs.removeObserver(this, 'private-cookie-changed'); + }; + + vAPI.cookies.observe = function (subject, topic, reason) { + //if ( topic !== 'cookie-changed' && topic !== 'private-cookie-changed' ) { + // return; + //} + // + if (reason === 'cleared' && typeof this.onAllRemoved === 'function') { + this.onAllRemoved(); + return; + } + if (subject === null) { + return; + } + if (subject instanceof Ci.nsICookie2 === false) { + try { + subject = subject.QueryInterface(Ci.nsICookie2); + } catch (ex) { + return; + } + } + if (reason === 'deleted') { + if (typeof this.onRemoved === 'function') { + this.onRemoved(new this.CookieEntry(subject)); + } + return; + } + if (typeof this.onChanged === 'function') { + this.onChanged(new this.CookieEntry(subject)); + } + }; + + vAPI.cookies.getAll = function(callback) { + // Meant and expected to be asynchronous. + if (typeof callback !== 'function') { + return; + } + + let onAsync = function () { + let out = []; + let enumerator = Services.cookies.enumerator; + let ffcookie; + while (enumerator.hasMoreElements()) { + ffcookie = enumerator.getNext(); + if (ffcookie instanceof Ci.nsICookie) { + out.push(new this.CookieEntry(ffcookie)); + } + } + + callback(out); + }; + + vAPI.setTimeout(onAsync.bind(this), 0); + }; + + vAPI.cookies.remove = function (details, callback) { + let uri = Services.io.newURI(details.url, null, null); + let cookies = Services.cookies; + cookies.remove(uri.asciiHost, details.name, uri.path, false, {}); + cookies.remove( '.' + uri.asciiHost, details.name, uri.path, false, {}); + + if (typeof callback === 'function') { + callback({ + domain: uri.asciiHost, + name: details.name, + path: uri.path + }); + } + }; +})(); -- 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-cookies.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'js/vapi-cookies.js') diff --git a/js/vapi-cookies.js b/js/vapi-cookies.js index be989b2..500d27b 100644 --- a/js/vapi-cookies.js +++ b/js/vapi-cookies.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-cookies.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'js/vapi-cookies.js') diff --git a/js/vapi-cookies.js b/js/vapi-cookies.js index 500d27b..1f19c49 100644 --- a/js/vapi-cookies.js +++ b/js/vapi-cookies.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-cookies.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'js/vapi-cookies.js') diff --git a/js/vapi-cookies.js b/js/vapi-cookies.js index 1f19c49..9211c48 100644 --- a/js/vapi-cookies.js +++ b/js/vapi-cookies.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 vAPI.cookies = {}; -- 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-cookies.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'js/vapi-cookies.js') diff --git a/js/vapi-cookies.js b/js/vapi-cookies.js index 9211c48..1f19c49 100644 --- a/js/vapi-cookies.js +++ b/js/vapi-cookies.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 vAPI.cookies = {}; -- 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-cookies.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'js/vapi-cookies.js') diff --git a/js/vapi-cookies.js b/js/vapi-cookies.js index 1f19c49..21f5d2e 100644 --- a/js/vapi-cookies.js +++ b/js/vapi-cookies.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 - vAPI.cookies = {}; vAPI.cookies.CookieEntry = function (ffCookie) { -- 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-cookies.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'js/vapi-cookies.js') diff --git a/js/vapi-cookies.js b/js/vapi-cookies.js index 21f5d2e..ff36d97 100644 --- a/js/vapi-cookies.js +++ b/js/vapi-cookies.js @@ -26,9 +26,6 @@ /******************************************************************************/ (function () { - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - Cu.import('resource://gre/modules/Services.jsm'); - vAPI.cookies = {}; vAPI.cookies.CookieEntry = function (ffCookie) { -- cgit v1.2.3