-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathindex.js
More file actions
245 lines (206 loc) · 6.19 KB
/
index.js
File metadata and controls
245 lines (206 loc) · 6.19 KB
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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
/* global chrome, ExtensionRuntimePort */
'use strict';
import './dynamicallyInjectContentScripts';
import './tabsManager';
import {
handleDevToolsPageMessage,
handleBackendManagerMessage,
handleReactDevToolsHookMessage,
handleFetchResourceContentScriptMessage,
} from './messageHandlers';
const ports: {
// TODO: Check why we convert tab IDs to strings, and if we can avoid it
[tabId: string]: {
extension: ExtensionRuntimePort | null,
proxy: ExtensionRuntimePort | null,
disconnectPipe: Function | null,
},
} = {};
function registerTab(tabId: number) {
// $FlowFixMe[incompatible-type]
if (!ports[tabId]) {
// $FlowFixMe[incompatible-type]
ports[tabId] = {
extension: null,
proxy: null,
disconnectPipe: null,
};
}
}
function registerExtensionPort(port: ExtensionRuntimePort, tabId: number) {
// $FlowFixMe[incompatible-type]
ports[tabId].extension = port;
port.onDisconnect.addListener(() => {
// This should delete disconnectPipe from ports dictionary
// $FlowFixMe[incompatible-type]
ports[tabId].disconnectPipe?.();
// $FlowFixMe[incompatible-type]
ports[tabId].extension = null;
});
}
function registerProxyPort(port: ExtensionRuntimePort, tabId: string) {
ports[tabId].proxy = port;
// In case proxy port was disconnected from the other end, from content script
// This can happen if content script was detached, when user does in-tab navigation
// This listener should never be called when we call port.disconnect() from this (background/index.js) script
port.onDisconnect.addListener(() => {
ports[tabId].disconnectPipe?.();
ports[tabId].proxy = null;
});
}
function isNumeric(str: string): boolean {
return +str + '' === str;
}
chrome.runtime.onConnect.addListener(port => {
if (port.name === 'proxy') {
// Might not be present for restricted pages in Firefox
if (port.sender?.tab?.id == null) {
// Not disconnecting it, so it would not reconnect
return;
}
// Proxy content script is executed in tab, so it should have it specified.
const tabId = port.sender.tab.id;
// $FlowFixMe[incompatible-type]
const registeredPort = ports[tabId];
const proxy = registeredPort?.proxy;
if (proxy) {
registeredPort.disconnectPipe?.();
proxy.disconnect();
}
registerTab(tabId);
registerProxyPort(
port,
// $FlowFixMe[incompatible-call]
tabId,
);
// $FlowFixMe[incompatible-type]
if (ports[tabId].extension) {
connectExtensionAndProxyPorts(
ports[tabId].extension,
ports[tabId].proxy,
tabId,
);
}
return;
}
if (isNumeric(port.name)) {
// DevTools page port doesn't have tab id specified, because its sender is the extension.
const tabId = +port.name;
registerTab(tabId);
registerExtensionPort(
port,
// $FlowFixMe[incompatible-call]
tabId,
);
// $FlowFixMe[incompatible-type]
if (ports[tabId].proxy) {
connectExtensionAndProxyPorts(
ports[tabId].extension,
ports[tabId].proxy,
tabId,
);
}
return;
}
// I am not sure if we should throw here
console.warn(`Unknown port ${port.name} connected`);
});
function connectExtensionAndProxyPorts(
maybeExtensionPort: ExtensionRuntimePort | null,
maybeProxyPort: ExtensionRuntimePort | null,
tabId: number,
) {
if (!maybeExtensionPort) {
throw new Error(
`Attempted to connect ports, when extension port is not present`,
);
}
const extensionPort = maybeExtensionPort;
if (!maybeProxyPort) {
throw new Error(
`Attempted to connect ports, when proxy port is not present`,
);
}
const proxyPort = maybeProxyPort;
// $FlowFixMe[incompatible-type]
if (ports[tabId].disconnectPipe) {
throw new Error(
`Attempted to connect already connected ports for tab with id ${tabId}`,
);
}
function extensionPortMessageListener(message: any) {
try {
proxyPort.postMessage(message);
} catch (e) {
if (__DEV__) {
console.log(`Broken pipe ${tabId}: `, e);
}
disconnectListener();
}
}
function proxyPortMessageListener(message: any) {
try {
extensionPort.postMessage(message);
} catch (e) {
if (__DEV__) {
console.log(`Broken pipe ${tabId}: `, e);
}
disconnectListener();
}
}
function disconnectListener() {
extensionPort.onMessage.removeListener(extensionPortMessageListener);
proxyPort.onMessage.removeListener(proxyPortMessageListener);
// We handle disconnect() calls manually, based on each specific case
// No need to disconnect other port here
// $FlowFixMe[incompatible-type]
delete ports[tabId].disconnectPipe;
}
ports[tabId].disconnectPipe = disconnectListener;
extensionPort.onMessage.addListener(extensionPortMessageListener);
proxyPort.onMessage.addListener(proxyPortMessageListener);
extensionPort.onDisconnect.addListener(disconnectListener);
proxyPort.onDisconnect.addListener(disconnectListener);
}
chrome.runtime.onMessage.addListener((message, sender) => {
switch (message?.source) {
case 'devtools-page': {
handleDevToolsPageMessage(message);
break;
}
case 'react-devtools-fetch-resource-content-script': {
handleFetchResourceContentScriptMessage(message);
break;
}
case 'react-devtools-backend-manager': {
handleBackendManagerMessage(message, sender);
break;
}
case 'react-devtools-hook': {
handleReactDevToolsHookMessage(message, sender);
}
}
});
chrome.tabs.onActivated.addListener(({tabId: activeTabId}) => {
for (const registeredTabId in ports) {
if (
ports[registeredTabId].proxy != null &&
ports[registeredTabId].extension != null
) {
const numericRegisteredTabId = +registeredTabId;
const event =
activeTabId === numericRegisteredTabId
? 'resumeElementPolling'
: 'pauseElementPolling';
ports[registeredTabId].extension.postMessage({event});
}
}
});