1 |
7f368d46
|
Wojtek Kosior
|
/**
|
2 |
2bd35bc4
|
Wojtek Kosior
|
* This file is part of Haketilo.
|
3 |
|
|
*
|
4 |
|
|
* Function: Message server.
|
5 |
6bae771d
|
Wojtek Kosior
|
*
|
6 |
|
|
* Copyright (C) 2021 Wojtek Kosior
|
7 |
263d03d5
|
jahoti
|
*
|
8 |
|
|
* This program is free software: you can redistribute it and/or modify
|
9 |
|
|
* it under the terms of the GNU General Public License as published by
|
10 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
11 |
|
|
* (at your option) any later version.
|
12 |
|
|
*
|
13 |
|
|
* This program is distributed in the hope that it will be useful,
|
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
|
|
* GNU General Public License for more details.
|
17 |
|
|
*
|
18 |
|
|
* As additional permission under GNU GPL version 3 section 7, you
|
19 |
|
|
* may distribute forms of that code without the copy of the GNU
|
20 |
|
|
* GPL normally required by section 4, provided you include this
|
21 |
|
|
* license notice and, in case of non-source distribution, a URL
|
22 |
|
|
* through which recipients can access the Corresponding Source.
|
23 |
|
|
* If you modify file(s) with this exception, you may extend this
|
24 |
|
|
* exception to your version of the file(s), but you are not
|
25 |
|
|
* obligated to do so. If you do not wish to do so, delete this
|
26 |
|
|
* exception statement from your version.
|
27 |
|
|
*
|
28 |
|
|
* As a special exception to the GPL, any HTML file which merely
|
29 |
|
|
* makes function calls to this code, and for that purpose
|
30 |
|
|
* includes it by reference shall be deemed a separate work for
|
31 |
|
|
* copyright law purposes. If you modify this code, you may extend
|
32 |
|
|
* this exception to your version of the code, but you are not
|
33 |
|
|
* obligated to do so. If you do not wish to do so, delete this
|
34 |
|
|
* exception statement from your version.
|
35 |
|
|
*
|
36 |
|
|
* You should have received a copy of the GNU General Public License
|
37 |
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
38 |
|
|
*
|
39 |
|
|
* I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
40 |
372d24ea
|
Wojtek Kosior
|
* license. Although I request that you do not make use of this code in a
|
41 |
263d03d5
|
jahoti
|
* proprietary program, I am not going to enforce this in court.
|
42 |
6bae771d
|
Wojtek Kosior
|
*/
|
43 |
7f368d46
|
Wojtek Kosior
|
|
44 |
b590eaa2
|
Wojtek Kosior
|
#FROM common/browser.js IMPORT browser
|
45 |
7f368d46
|
Wojtek Kosior
|
|
46 |
01e977f9
|
Wojtek Kosior
|
let listeners = {};
|
47 |
|
|
let listening = false;
|
48 |
7f368d46
|
Wojtek Kosior
|
|
49 |
01e977f9
|
Wojtek Kosior
|
function raw_listen(port)
|
50 |
|
|
{
|
51 |
|
|
if (listeners[port.name] === undefined)
|
52 |
|
|
return;
|
53 |
|
|
|
54 |
|
|
listeners[port.name](port);
|
55 |
|
|
}
|
56 |
7f368d46
|
Wojtek Kosior
|
|
57 |
01e977f9
|
Wojtek Kosior
|
/* magic should be one of the constants from /common/connection_types.js */
|
58 |
261548ff
|
Wojtek Kosior
|
function listen_for_connection(magic, cb)
|
59 |
|
|
{
|
60 |
01e977f9
|
Wojtek Kosior
|
if (!listening) {
|
61 |
|
|
listening = true;
|
62 |
|
|
browser.runtime.onConnect.addListener(raw_listen);
|
63 |
|
|
}
|
64 |
261548ff
|
Wojtek Kosior
|
listeners[magic] = cb;
|
65 |
|
|
}
|
66 |
01e977f9
|
Wojtek Kosior
|
#EXPORT listen_for_connection
|
67 |
7f368d46
|
Wojtek Kosior
|
|
68 |
01e977f9
|
Wojtek Kosior
|
/*
|
69 |
|
|
* Messaging background page from itself might result in messages being silently
|
70 |
|
|
* discarded. Here we implement an interface (somewhat) compatible with the one
|
71 |
|
|
* provided by the browser, but which allows for background page to communicate
|
72 |
|
|
* with itself.
|
73 |
|
|
*/
|
74 |
|
|
function EvTarget()
|
75 |
261548ff
|
Wojtek Kosior
|
{
|
76 |
01e977f9
|
Wojtek Kosior
|
this.listeners = new Set();
|
77 |
|
|
this.addListener = cb => this.listeners.add(cb);
|
78 |
|
|
this.removeListener = cb => this.listeners.delete(cb);
|
79 |
|
|
this.dispatch = msg => this.listeners.forEach(l => l(msg));
|
80 |
|
|
}
|
81 |
7f368d46
|
Wojtek Kosior
|
|
82 |
01e977f9
|
Wojtek Kosior
|
function Port(magic)
|
83 |
|
|
{
|
84 |
|
|
this.name = magic;
|
85 |
|
|
this.onDisconnect = new EvTarget();
|
86 |
|
|
this.onMessage = new EvTarget();
|
87 |
|
|
this.postMessage = msg => this.other.onMessage.dispatch(msg);
|
88 |
|
|
this.disconnect = () => this.other.onDisconnect.dispatch(this.other);
|
89 |
261548ff
|
Wojtek Kosior
|
}
|
90 |
7f368d46
|
Wojtek Kosior
|
|
91 |
01e977f9
|
Wojtek Kosior
|
let bg_page_url;
|
92 |
|
|
function connect_to_background(magic)
|
93 |
|
|
{
|
94 |
|
|
if (bg_page_url === undefined)
|
95 |
|
|
bg_page_url = browser.runtime.getURL("_generated_background_page.html");
|
96 |
|
|
if (typeof document === "undefined" || document.URL !== bg_page_url)
|
97 |
|
|
return browser.runtime.connect({name: magic});
|
98 |
7f368d46
|
Wojtek Kosior
|
|
99 |
01e977f9
|
Wojtek Kosior
|
if (!(magic in listeners))
|
100 |
4c6a2323
|
Wojtek Kosior
|
throw `no listener for '${magic}'`;
|
101 |
01e977f9
|
Wojtek Kosior
|
|
102 |
|
|
const ports = [new Port(magic), new Port(magic)];
|
103 |
|
|
ports[0].other = ports[1];
|
104 |
|
|
ports[1].other = ports[0];
|
105 |
|
|
|
106 |
|
|
listeners[magic](ports[0]);
|
107 |
|
|
return ports[1];
|
108 |
|
|
}
|
109 |
|
|
#EXPORT connect_to_background
|