-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchrome_api.py
141 lines (123 loc) · 8.16 KB
/
chrome_api.py
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
# Copyright (C) 2021 Aurore Fass
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Message passing APIs for Chromium-based extensions.
"""
import browser_api
import handle_messages as m
GLOBAL_PM = browser_api.GLOBAL_PM
def global_post_message(node_value):
""" Checks if the postMessage is the window.postMessage (WA - CS communication). """
if any(pm in node_value for pm in GLOBAL_PM):
return {'mess_type': 'C', 'fun': m.post_message} # Sends
if '.postMessage' in node_value:
return {'mess_type': 'Trash', 'fun': m.do_nothing} # Do nothing as port.PM
return {'mess_type': 'C', 'fun': m.post_message} # Sends
def port_post_message(node_value):
""" Checks if the postMessage is the port.postMessage (WA - BP and CS - BP communication). """
if any(pm in node_value for pm in GLOBAL_PM):
return {'mess_type': 'Trash', 'fun': m.do_nothing} # Do nothing, wrong PM
if '.postMessage' in node_value:
return {'mess_type': 'C2', 'fun': m.post_message} # port.postMessage to send messages
return {'mess_type': 'Trash', 'fun': m.do_nothing} # Do nothing, wrong PM
CS2BP = {'chrome.runtime.sendMessage': {'mess_type': 'C1',
'fun': m.chrome_runtime_sendMessage}, # Sends
'chrome.runtime.connect': {'mess_type': 'C2',
'fun': m.chrome_runtime_connect}, # Gets port + message
'chrome.extension.sendMessage': {'mess_type': 'C1',
'fun': m.chrome_runtime_sendMessage}, # Sends
'chrome.extension.connect': {'mess_type': 'C2',
'fun': m.chrome_runtime_connect}, # Gets port + message
'chrome.runtime.sendRequest': {'mess_type': 'C1-d',
'fun': m.chrome_runtime_sendRequest}, # Sends
'chrome.extension.sendRequest': {'mess_type': 'C1-d',
'fun': m.chrome_runtime_sendRequest}} # Sends
BP2CS = {'chrome.tabs.sendMessage': {'mess_type': 'C1',
'fun': m.chrome_tabs_sendMessage}, # Sends
'chrome.tabs.connect': {'mess_type': 'C2',
'fun': m.chrome_tabs_connect}, # Gets port + message
'chrome.tabs.sendRequest': {'mess_type': 'C1-d',
'fun': m.chrome_tabs_sendRequest}} # Sends
CS_BP = {'chrome.runtime.onMessage.addListener': {'mess_type': 'C1',
'fun': m.chrome_runtime_onMessage_addListener},
'}.runtime.onMessage.addListener': {'mess_type': 'C1',
'fun': m.chrome_runtime_onMessage_addListener},
# Receives
'chrome.runtime.onConnect.addListener': {'mess_type': 'C2',
'fun': m.chrome_runtime_onConnect_addListener},
'chrome.extension.onMessage.addListener': {'mess_type': 'C1',
'fun': m.chrome_runtime_onMessage_addListener},
'}.extension.onMessage.addListener': {'mess_type': 'C1',
'fun': m.chrome_runtime_onMessage_addListener},
# Receives
'chrome.extension.onConnect.addListener': {'mess_type': 'C2',
'fun': m.chrome_runtime_onConnect_addListener},
'chrome.runtime.onRequest.addListener': {'mess_type': 'C1-d',
'fun': m.chrome_runtime_onRequest_addListener},
'}.runtime.onRequest.addListener': {'mess_type': 'C1-d',
'fun': m.chrome_runtime_onRequest_addListener},
'chrome.extension.onRequest.addListener': {'mess_type': 'C1-d',
'fun': m.chrome_runtime_onRequest_addListener},
'}.extension.onRequest.addListener': {'mess_type': 'C1-d',
'fun': m.chrome_runtime_onRequest_addListener},
# Gets port --> port.postMessage / port.onMessage.addListener
'postMessage': False, # Checks which postMessage
# port.onMessage.addListener to receive messages
'.onMessage.addListener': {'mess_type': 'C2',
'fun': m.onMessage_addListener}}
WA_CS = {'postMessage': True, # Checks which postMessage
'addEventListener': {'mess_type': 'C', 'fun': m.add_event_listener}, # Receives
'onmessage': {'mess_type': 'C', 'fun': m.onmessage}} # Receives
WA2BP = {'chrome.runtime.sendMessage': {'mess_type': 'C1',
'fun': m.chrome_runtime_sendMessage}, # Sends
'chrome.extension.sendMessage': {'mess_type': 'C1',
'fun': m.chrome_runtime_sendMessage}, # Sends
'chrome.runtime.sendRequest': {'mess_type': 'C1-d',
'fun': m.chrome_runtime_sendRequest}, # Sends
'chrome.extension.sendRequest': {'mess_type': 'C1-d',
'fun': m.chrome_runtime_sendRequest}, # Sends
'chrome.runtime.connect': {'mess_type': 'C2',
'fun': m.chrome_runtime_connect}, # Gets port + message
'chrome.extension.connect': {'mess_type': 'C2',
'fun': m.chrome_runtime_connect}} # Gets port + message
BP2WA = {'chrome.runtime.onMessageExternal.addListener':
{'mess_type': 'C1', 'fun': m.chrome_runtime_onMessageExternal_addListener}, # Receives
'}.runtime.onMessageExternal.addListener':
{'mess_type': 'C1', 'fun': m.chrome_runtime_onMessageExternal_addListener}, # Receives
'chrome.runtime.onConnectExternal.addListener':
{'mess_type': 'C2', 'fun': m.chrome_runtime_onConnectExternal_addListener}, # Get port
'chrome.extension.onMessageExternal.addListener':
{'mess_type': 'C1', 'fun': m.chrome_runtime_onMessageExternal_addListener}, # Receives
'}.extension.onMessageExternal.addListener':
{'mess_type': 'C1', 'fun': m.chrome_runtime_onMessageExternal_addListener}, # Receives
'chrome.extension.onConnectExternal.addListener':
{'mess_type': 'C2', 'fun': m.chrome_runtime_onConnectExternal_addListener}, # Get port
'chrome.runtime.onRequestExternal.addListener':
{'mess_type': 'C1-d', 'fun': m.chrome_runtime_onRequestExternal_addListener}, # Receiv
'}.runtime.onRequestExternal.addListener':
{'mess_type': 'C1-d', 'fun': m.chrome_runtime_onRequestExternal_addListener}, # Receiv
'chrome.extension.onRequestExternal.addListener':
{'mess_type': 'C1-d', 'fun': m.chrome_runtime_onRequestExternal_addListener}, # Receiv
'}.extension.onRequestExternal.addListener':
{'mess_type': 'C1-d', 'fun': m.chrome_runtime_onRequestExternal_addListener}, # Receiv
'postMessage': False, # Checks which postMessage
'.runtime.onMessage.addListener': {'mess_type': 'Trash', 'fun': m.do_nothing},
'.extension.onMessage.addListener': {'mess_type': 'Trash', 'fun': m.do_nothing},
# So that cannot be confounded with .onMessage.addListener
# port.onMessage.addListener to receive messages
'.onMessage.addListener': {'mess_type': 'C2', 'fun': m.onMessage_addListener}}
# C1: Chrome one-time channel
# C2: Chrome long-lived channel
# Trash: Empty, just to ensure that we are not matching wrong substrings