Project

General

Profile

Download (3.7 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / common / message_server.js @ f8dedf60

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Message server.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior
7
 *
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
 * license. Although I request that you do not make use of this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
#FROM common/browser.js IMPORT browser
45

    
46
let listeners = {};
47
let listening = false;
48

    
49
function raw_listen(port)
50
{
51
    if (listeners[port.name] === undefined)
52
	return;
53

    
54
    listeners[port.name](port);
55
}
56

    
57
/* magic should be one of the constants from /common/connection_types.js */
58
function listen_for_connection(magic, cb)
59
{
60
    if (!listening) {
61
	listening = true;
62
	browser.runtime.onConnect.addListener(raw_listen);
63
    }
64
    listeners[magic] = cb;
65
}
66
#EXPORT listen_for_connection
67

    
68
/*
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
{
76
    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

    
82
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
}
90

    
91
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

    
99
    if (!(magic in listeners))
100
	throw `no listener for '${magic}'`;
101

    
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
(6-6/11)