1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Function: Broadcast messages to different execution contexts of the extension
|
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/message_server.js IMPORT connect_to_background
|
45
|
|
46
|
function sender_connection()
|
47
|
{
|
48
|
return {
|
49
|
type: "sender",
|
50
|
port: connect_to_background("broadcast_send")
|
51
|
};
|
52
|
}
|
53
|
#EXPORT sender_connection
|
54
|
|
55
|
function out(sender_conn, channel_name, value)
|
56
|
{
|
57
|
sender_conn.port.postMessage(["broadcast", channel_name, value]);
|
58
|
}
|
59
|
#EXPORT out
|
60
|
|
61
|
/*
|
62
|
* prepare()'d message will be broadcasted if the connection is closed or when
|
63
|
* flush() is called. All messages prepared by given sender will be discarded
|
64
|
* when discard() is called.
|
65
|
*
|
66
|
* Timeout will cause the prepared message to be broadcasted after given number
|
67
|
* of miliseconds unless discard() is called in the meantime. It is mostly
|
68
|
* useful as a fallback in case this connection end gets nuked (e.g. because
|
69
|
* browser window is closed) and onDisconnect event is not dispatched to
|
70
|
* background script's Port object. This is only an issue for browsers as old as
|
71
|
* IceCat 60: https://bugzilla.mozilla.org/show_bug.cgi?id=1392067
|
72
|
*
|
73
|
* Timeout won't be scheduled at all if its value is given as 0.
|
74
|
*/
|
75
|
function prepare(sender_conn, channel_name, value, timeout=5000)
|
76
|
{
|
77
|
sender_conn.port.postMessage(["prepare", channel_name, value, timeout]);
|
78
|
}
|
79
|
#EXPORT prepare
|
80
|
|
81
|
function discard(sender_conn)
|
82
|
{
|
83
|
sender_conn.port.postMessage(["discard"]);
|
84
|
}
|
85
|
#EXPORT discard
|
86
|
|
87
|
function flush(sender_conn)
|
88
|
{
|
89
|
sender_conn.port.postMessage(["flush"]);
|
90
|
}
|
91
|
#EXPORT flush
|
92
|
|
93
|
function listener_connection(cb)
|
94
|
{
|
95
|
const conn = {
|
96
|
type: "listener",
|
97
|
port: connect_to_background("broadcast_listen")
|
98
|
};
|
99
|
|
100
|
conn.port.onMessage.addListener(cb);
|
101
|
|
102
|
return conn;
|
103
|
}
|
104
|
#EXPORT listener_connection
|
105
|
|
106
|
function subscribe(listener_conn, channel_name)
|
107
|
{
|
108
|
listener_conn.port.postMessage(["subscribe", channel_name]);
|
109
|
}
|
110
|
#EXPORT subscribe
|
111
|
|
112
|
function unsubscribe(listener_conn, channel_name)
|
113
|
{
|
114
|
listener_conn.port.postMessage(["unsubscribe", channel_name]);
|
115
|
}
|
116
|
#EXPORT unsubscribe
|
117
|
|
118
|
function close(conn)
|
119
|
{
|
120
|
if (conn.type === "sender")
|
121
|
flush(conn);
|
122
|
setTimeout(() => conn.port.disconnect());
|
123
|
}
|
124
|
#EXPORT close
|