aboutsummaryrefslogtreecommitdiffstats
path: root/js/vapi-core.js
blob: 5d33a60684204144ad37324b13d150be0c9aefe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************

    ηMatrix - a browser extension to black/white list requests.
    Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors
    Copyright (C) 2019-2020 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://libregit.spks.xyz/heckyel/ematrix
    uMatrix Home: https://github.com/gorhill/uMatrix
*/

'use strict';

/******************************************************************************/

(function (self) {
    vAPI.modernFirefox =
        Services.appinfo.ID === '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}'
        && Services.vc.compare(Services.appinfo.version, '44') > 0;

    vAPI.app = {
        name: 'eMatrix',
        version: location.hash.slice(1),

        start: function () {
            return;
        },
        stop: function () {
            return;
        },
        restart: function () {
            Cc['@mozilla.org/childprocessmessagemanager;1']
                .getService(Ci.nsIMessageSender)
                .sendAsyncMessage(location.host + '-restart');
        },
    };

    // List of things that needs to be destroyed when disabling the extension
    // Only functions should be added to it
    // eMatrix: taken care by vAPI.addCleanUpTask --- use that function
    let cleanupTasks = [];

    // This must be updated manually, every time a new task is added/removed
    // eMatrix: do we?
    let expectedNumberOfCleanups = 7;

    vAPI.addCleanUpTask = function (task) {
        if (typeof task !== 'function') {
            return;
        }

        cleanupTasks.push(task);
    };

    vAPI.deferUntil = function (testFn, mainFn, details) {
        let dtls = (typeof details !== 'object') ? {} : details;
        let now = 0;
        let next = dtls.next || 200;
        let until = dtls.until || 2000;

        let check = function () {
            if (testFn() === true || now >= until) {
                mainFn();
                return;
            }
            now += next;
            vAPI.setTimeout(check, next);
        };

        if ('sync' in dtls && dtls.sync === true) {
            check();
        } else {
            vAPI.setTimeout(check, 1);
        }
    };

    window.addEventListener('unload', function () {
        // if (typeof vAPI.app.onShutdown === 'function') {
        //     vAPI.app.onShutdown();
        // }

        // IMPORTANT: cleanup tasks must be executed using LIFO order.
        for (let i=cleanupTasks.length-1; i>=0; --i) {
            try {
                cleanupTasks[i]();
            } catch (e) {
                // Just in case a clean up task ends up throwing for
                // no reason
                console.error(e);
            }
        }

        // eMatrix: temporarily disabled
        // if (cleanupTasks.length < expectedNumberOfCleanups) {
        //     console.error
        //     ('eMatrix> Cleanup tasks performed: %s (out of %s)',
        //      cleanupTasks.length,
        //      expectedNumberOfCleanups);
        // }

        // frameModule needs to be cleared too
        Cu.import('chrome://ematrix/content/lib/FrameModule.jsm');
        contentObserver.unregister();
        Cu.unload('chrome://ematrix/content/lib/FrameModule.jsm');
    });

    vAPI.noTabId = '-1';

    vAPI.isBehindTheSceneTabId = function (tabId) {
        return tabId.toString() === '-1';
    };

    vAPI.lastError = function () {
        return null;
    };
})(this);