aboutsummaryrefslogtreecommitdiffstats
path: root/platform/firefox/vapi-client.js
blob: bd6facd2950711def12a42ee16779a5c0ffb56b8 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*******************************************************************************

    µBlock - a browser extension to block requests.
    Copyright (C) 2014 The µBlock authors

    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://github.com/gorhill/uBlock
*/

/* global addMessageListener, removeMessageListener, sendAsyncMessage, outerShutdown */

// For non background pages

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

(function(self) {

'use strict';

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

var vAPI = self.vAPI = self.vAPI || {};
vAPI.firefox = true;
vAPI.sessionId = String.fromCharCode(Date.now() % 26 + 97) +
    Math.random().toString(36).slice(2);

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

vAPI.setTimeout = vAPI.setTimeout || function(callback, delay, extra) {
    return setTimeout(function(a) { callback(a); }, delay, extra);
};

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

vAPI.shutdown = (function() {
    var jobs = [];

    var add = function(job) {
        jobs.push(job);
    };

    var exec = function() {
        //console.debug('Shutting down...');
        var job;
        while ( (job = jobs.pop()) ) {
            job();
        }
    };

    return {
        add: add,
        exec: exec
    };
})();

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

vAPI.messaging = {
    channels: {},
    pending: {},
    pendingCount: 0,
    auxProcessId: 1,
    connected: false,
    connector: function(msg) {
        messagingConnector(JSON.parse(msg));
    },

    builtinChannelHandler: function(msg) {
        if ( msg.cmd === 'injectScript' ) {
            // injectScript is not always present.
            // - See contentObserver.initContentScripts in frameModule.js
            if ( typeof self.injectScript !== 'function' )  {
                return;
            }
            var details = msg.details;
            if ( !details.allFrames && window !== window.top ) {
                return;
            }
            self.injectScript(details.file);
            return;
        }
        if ( msg.cmd === 'shutdownSandbox' ) {
            vAPI.shutdown.exec();
            vAPI.messaging.shutdown();
            if ( typeof self.outerShutdown === 'function' ) {
                outerShutdown();
            }
            return;
        }
    },

    setup: function() {
        this.channels['vAPI'] = new MessagingChannel('vAPI', this.builtinChannelHandler);
        window.addEventListener('pagehide', this.toggleListener, true);
        window.addEventListener('pageshow', this.toggleListener, true);
    },

    shutdown: function() {
        this.close();
        window.removeEventListener('pagehide', this.toggleListener, true);
        window.removeEventListener('pageshow', this.toggleListener, true);
        vAPI.messaging = null;
    },

    close: function() {
        this.disconnect();
        this.channels = {};
        // service pending callbacks
        var pending = this.pending, listener;
        this.pending = {};
        this.pendingCount = 0;
        for ( var auxId in pending ) {
            if ( this.pending.hasOwnProperty(auxId) ) {
                listener = pending[auxId];
                if ( typeof listener === 'function' ) {
                    listener(null);
                }
            }
        }
    },

    channel: function(channelName, callback) {
        if ( !channelName ) {
            return;
        }
        var channel = this.channels[channelName];
        if ( channel instanceof MessagingChannel ) {
            channel.addListener(callback);
            channel.refCount += 1;
        } else {
            channel = this.channels[channelName] = new MessagingChannel(channelName, callback);
        }
        return channel;
    },

    connect: function() {
        if ( !this.connected ) {
            addMessageListener(this.connector);
            this.connected = true;
        }
    },

    disconnect: function() {
        if ( this.connected ) {
            removeMessageListener();
            this.connected = false;
        }
    },

    toggleListener: function({type, persisted}) {
        var me = vAPI.messaging;
        if ( !me ) {
            return;
        }

        if ( type === 'pagehide' ) {
            if ( !persisted ) {
                vAPI.shutdown.exec();
                vAPI.messaging.shutdown();
                if ( typeof self.outerShutdown === 'function' ) {
                    outerShutdown();
                }
            }
            me.disconnect();
            return;
        }

        me.connect();
    }
};

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

var messagingConnector = function(details) {
    if ( !details ) {
        return;
    }

    var messaging = vAPI.messaging;

    // Sandbox might have been shutdown
    if ( !messaging ) {
        return;
    }

    var channels = messaging.channels;
    var channel;

    // Sent to all channels
    if ( details.broadcast && !details.channelName ) {
        for ( channel in channels ) {
            if ( channels[channel] instanceof MessagingChannel === false ) {
                continue;
            }
            channels[channel].sendToListeners(details.msg);
        }
        return;
    }

    // Response to specific message previously sent
    if ( details.auxProcessId ) {
        var listener = messaging.pending[details.auxProcessId];
        delete messaging.pending[details.auxProcessId];
        delete details.auxProcessId; // TODO: why?
        if ( listener ) {
            messaging.pendingCount -= 1;
            listener(details.msg);
            return;
        }
    }

    // Sent to a specific channel
    var response;
    channel = channels[details.channelName];
    if ( channel instanceof MessagingChannel ) {
        response = channel.sendToListeners(details.msg);
    }

    // Respond back if required
    if ( details.mainProcessId !== undefined ) {
        sendAsyncMessage('ublock0:background', {
            mainProcessId: details.mainProcessId,
            msg: response
        });
    }
};

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

var MessagingChannel = function(name, callback) {
    this.channelName = name;
    this.listeners = typeof callback === 'function' ? [callback] : [];
    this.refCount = 1;
    if ( typeof callback === 'function' ) {
        vAPI.messaging.connect();
    }
};

MessagingChannel.prototype.send = function(message, callback) {
    this.sendTo(message, undefined, undefined, callback);
};

MessagingChannel.prototype.sendTo = function(message, toTabId, toChannel, callback) {
    var messaging = vAPI.messaging;
    // Too large a gap between the last request and the last response means
    // the main process is no longer reachable: memory leaks and bad
    // performance become a risk -- especially for long-lived, dynamic
    // pages. Guard against this.
    if ( messaging.pendingCount > 25 ) {
        //console.error('uBlock> Sigh. Main process is sulking. Will try to patch things up.');
        messaging.close();
    }
    messaging.connect();
    var auxProcessId;
    if ( callback ) {
        auxProcessId = messaging.auxProcessId++;
        messaging.pending[auxProcessId] = callback;
        messaging.pendingCount += 1;
    }
    sendAsyncMessage('ublock0:background', {
        channelName: self._sandboxId_ + '|' + this.channelName,
        auxProcessId: auxProcessId,
        toTabId: toTabId,
        toChannel: toChannel,
        msg: message
    });
};

MessagingChannel.prototype.close = function() {
    this.refCount -= 1;
    if ( this.refCount !== 0 ) {
        return;
    }
    delete vAPI.messaging.channels[this.channelName];
};

MessagingChannel.prototype.addListener = function(callback) {
    if ( typeof callback !== 'function' ) {
        return;
    }
    if ( this.listeners.indexOf(callback) !== -1 ) {
        throw new Error('Duplicate listener.');
    }
    this.listeners.push(callback);
    vAPI.messaging.connect();
};

MessagingChannel.prototype.removeListener = function(callback) {
    if ( typeof callback !== 'function' ) {
        return;
    }
    var pos = this.listeners.indexOf(callback);
    if ( pos === -1 ) {
        throw new Error('Listener not found.');
    }
    this.listeners.splice(pos, 1);
};

MessagingChannel.prototype.removeAllListeners = function() {
    this.listeners = [];
};

MessagingChannel.prototype.sendToListeners = function(msg) {
    var response;
    var listeners = this.listeners;
    for ( var i = 0, n = listeners.length; i < n; i++ ) {
        response = listeners[i](msg);
        if ( response !== undefined ) {
            break;
        }
    }
    return response;
};

// https://www.youtube.com/watch?v=Cg0cmhjdiLs

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

vAPI.messaging.setup();

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

// No need to have vAPI client linger around after shutdown if
// we are not a top window (because element picker can still
// be injected in top window).
if ( window !== window.top ) {
    // Can anything be done?
}

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

})(this);

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