cdp-tunnel 2.7.10 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,168 +1,9 @@
1
1
  var State = (function() {
2
2
  var _state = {
3
- ws: null,
4
- reconnectTimer: null,
5
- hasConnectedClient: false,
6
- cdpClients: [],
7
- sessionIdToTabId: new Map(),
8
- sessionIdToTargetId: new Map(),
9
- attachedTabIds: new Set(),
10
- emittedTargets: new Set(),
11
- browserContextIds: new Set(['default']),
12
- autoAttachConfig: {
13
- autoAttach: false,
14
- waitForDebuggerOnStart: false,
15
- flatten: true
16
- },
17
- discoverTargetsEnabled: false,
18
- pendingDebuggerTabs: new Set(),
19
- screencastPollingSessions: new Map(),
20
- automatedTabs: new Set(),
21
3
  currentTabId: null,
22
- isAttached: false,
23
- pendingCreatedTabUrls: new Set(),
24
- clientIdToTabId: new Map(),
25
- clientIdToSessionId: new Map(),
26
- tabIdToClientId: new Map(),
27
- clientIdToGroupId: new Map(),
28
- preExistingTabIds: new Set(),
29
- cdpCreatedTabIds: new Set()
4
+ isAttached: false
30
5
  };
31
6
 
32
- function mapSession(sessionId, tabId, targetId) {
33
- _state.sessionIdToTabId.set(sessionId, tabId);
34
- _state.sessionIdToTargetId.set(sessionId, targetId);
35
- _state.attachedTabIds.add(tabId);
36
- }
37
-
38
- function unmapSession(sessionId) {
39
- var tabId = _state.sessionIdToTabId.get(sessionId);
40
- _state.sessionIdToTabId.delete(sessionId);
41
- _state.sessionIdToTargetId.delete(sessionId);
42
- if (tabId && !hasOtherSessionForTab(tabId)) {
43
- _state.attachedTabIds.delete(tabId);
44
- }
45
- return tabId;
46
- }
47
-
48
- function getTabIdBySession(sessionId) {
49
- return _state.sessionIdToTabId.get(sessionId);
50
- }
51
-
52
- function getTargetIdBySession(sessionId) {
53
- return _state.sessionIdToTargetId.get(sessionId);
54
- }
55
-
56
- function findSessionByTabId(tabId) {
57
- var lastMatch = null;
58
- _state.sessionIdToTabId.forEach(function(mappedTabId, sessionId) {
59
- if (mappedTabId === tabId) {
60
- lastMatch = sessionId;
61
- }
62
- });
63
- return lastMatch;
64
- }
65
-
66
- function findSessionsByTabId(tabId) {
67
- var sessions = [];
68
- _state.sessionIdToTabId.forEach(function(mappedTabId, sessionId) {
69
- if (mappedTabId === tabId) {
70
- sessions.push(sessionId);
71
- }
72
- });
73
- return sessions;
74
- }
75
-
76
- function findSessionByTargetId(targetId) {
77
- var entries = _state.sessionIdToTargetId.entries();
78
- var entry = entries.next();
79
- while (!entry.done) {
80
- if (entry.value[1] === targetId) {
81
- return entry.value[0];
82
- }
83
- entry = entries.next();
84
- }
85
- return null;
86
- }
87
-
88
- function getTabIdByTargetId(targetId) {
89
- var sessionId = findSessionByTargetId(targetId);
90
- if (sessionId) {
91
- return _state.sessionIdToTabId.get(sessionId);
92
- }
93
- return null;
94
- }
95
-
96
- function hasOtherSessionForTab(tabId) {
97
- var count = 0;
98
- _state.sessionIdToTabId.forEach(function(mappedTabId) {
99
- if (mappedTabId === tabId) {
100
- count++;
101
- }
102
- });
103
- return count > 0;
104
- }
105
-
106
- function addAttachedTab(tabId) {
107
- _state.attachedTabIds.add(tabId);
108
- }
109
-
110
- function removeAttachedTab(tabId) {
111
- _state.attachedTabIds.delete(tabId);
112
- }
113
-
114
- function isTabAttached(tabId) {
115
- return _state.attachedTabIds.has(tabId);
116
- }
117
-
118
- function getAttachedTabIds() {
119
- return Array.from(_state.attachedTabIds);
120
- }
121
-
122
- function addEmittedTarget(targetId) {
123
- _state.emittedTargets.add(targetId);
124
- }
125
-
126
- function hasEmittedTarget(targetId) {
127
- return _state.emittedTargets.has(targetId);
128
- }
129
-
130
- function setAutoAttachConfig(config) {
131
- Object.assign(_state.autoAttachConfig, config);
132
- }
133
-
134
- function getAutoAttachConfig() {
135
- return Object.assign({}, _state.autoAttachConfig);
136
- }
137
-
138
- function setDiscoverTargets(enabled) {
139
- _state.discoverTargetsEnabled = enabled;
140
- }
141
-
142
- function addPendingDebuggerTab(tabId) {
143
- _state.pendingDebuggerTabs.add(tabId);
144
- }
145
-
146
- function removePendingDebuggerTab(tabId) {
147
- _state.pendingDebuggerTabs.delete(tabId);
148
- }
149
-
150
- function isPendingDebuggerTab(tabId) {
151
- return _state.pendingDebuggerTabs.has(tabId);
152
- }
153
-
154
- function addBrowserContext(id) {
155
- _state.browserContextIds.add(id);
156
- }
157
-
158
- function removeBrowserContext(id) {
159
- _state.browserContextIds.delete(id);
160
- }
161
-
162
- function getBrowserContexts() {
163
- return Array.from(_state.browserContextIds);
164
- }
165
-
166
7
  function loadPersisted() {
167
8
  return new Promise(function(resolve) {
168
9
  chrome.storage.local.get(['currentTabId', 'isAttached'], function(result) {
@@ -189,337 +30,145 @@ var State = (function() {
189
30
  _state.currentTabId = tabId;
190
31
  }
191
32
 
192
- function getWs() {
193
- return _state.ws;
194
- }
195
-
196
- function setWs(ws) {
197
- _state.ws = ws;
198
- }
199
-
200
- function getReconnectTimer() {
201
- return _state.reconnectTimer;
202
- }
203
-
204
- function setReconnectTimer(timer) {
205
- _state.reconnectTimer = timer;
206
- }
207
-
208
- function clearReconnectTimer() {
209
- if (_state.reconnectTimer) {
210
- clearTimeout(_state.reconnectTimer);
211
- _state.reconnectTimer = null;
212
- }
213
- }
214
-
215
- function hasConnectedClient() {
216
- return _state.hasConnectedClient;
217
- }
218
-
219
- function setHasConnectedClient(value) {
220
- _state.hasConnectedClient = value;
221
- }
222
-
223
- function addPendingCreatedTabUrl(url) {
224
- _state.pendingCreatedTabUrls.add(url);
225
- }
226
-
227
- function removePendingCreatedTabUrl(url) {
228
- _state.pendingCreatedTabUrls.delete(url);
229
- }
230
-
231
- function hasPendingCreatedTabUrl(url) {
232
- return _state.pendingCreatedTabUrls.has(url);
233
- }
234
-
235
- function getScreencastSession(tabId) {
236
- return _state.screencastPollingSessions.get(tabId);
237
- }
238
-
239
- function setScreencastSession(tabId, session) {
240
- _state.screencastPollingSessions.set(tabId, session);
241
- }
242
-
243
- function deleteScreencastSession(tabId) {
244
- _state.screencastPollingSessions.delete(tabId);
245
- }
246
-
247
- function addAutomatedTab(tabId) {
248
- _state.automatedTabs.add(tabId);
249
- }
250
-
251
- function removeAutomatedTab(tabId) {
252
- _state.automatedTabs.delete(tabId);
253
- }
254
-
255
- function getAutomatedTabs() {
256
- return Array.from(_state.automatedTabs);
257
- }
258
-
259
- function clearSessionState() {
260
- _state.sessionIdToTabId.clear();
261
- _state.sessionIdToTargetId.clear();
262
- _state.pendingDebuggerTabs.clear();
263
- _state.emittedTargets.clear();
33
+ function getAttachedTabIds() {
34
+ var tabs = [];
35
+ ConnectionManager.forEachConnection(function(entry) {
36
+ tabs = tabs.concat(entry.state.getAttachedTabIds());
37
+ });
38
+ return tabs;
264
39
  }
265
40
 
266
- function addCDPCreatedTab(tabId) {
267
- _state.cdpCreatedTabIds.add(tabId);
41
+ function getCDPClients() {
42
+ var clients = [];
43
+ ConnectionManager.forEachConnection(function(entry) {
44
+ clients = clients.concat(entry.state.getCDPClients() || []);
45
+ });
46
+ return clients;
268
47
  }
269
48
 
270
- function isCDPCreatedTab(tabId) {
271
- return _state.cdpCreatedTabIds.has(tabId);
49
+ function findSessionByTabId(tabId) {
50
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
51
+ return entry ? entry.state.findSessionByTabId(tabId) : null;
272
52
  }
273
53
 
274
- function getCDPCreatedTabIds() {
275
- return Array.from(_state.cdpCreatedTabIds);
54
+ function getWs() {
55
+ var entry = ConnectionManager.getPrimaryConnection();
56
+ return entry ? entry.state.getWs() : null;
276
57
  }
277
58
 
278
59
  function clearAllState() {
279
- clearSessionState();
280
- _state.attachedTabIds.clear();
281
- _state.emittedTargets.clear();
282
- _state.screencastPollingSessions.clear();
283
- _state.browserContextIds = new Set(['default']);
284
- _state.autoAttachConfig = {
285
- autoAttach: false,
286
- waitForDebuggerOnStart: false,
287
- flatten: true
288
- };
289
- _state.discoverTargetsEnabled = false;
290
- _state.hasConnectedClient = false;
291
- _state.tabIdToClientId.clear();
292
- _state.clientIdToGroupId.clear();
293
- _state.clientIdToTabId.clear();
294
- _state.clientIdToSessionId.clear();
295
- _state.preExistingTabIds.clear();
296
- _state.pendingDebuggerTabs.clear();
297
- _state.automatedTabs.clear();
298
- _state.pendingCreatedTabUrls.clear();
299
- _state.cdpCreatedTabIds.clear();
300
- _state.cdpClients = [];
60
+ ConnectionManager.forEachConnection(function(entry) {
61
+ entry.state.clearAllState();
62
+ });
301
63
  }
302
64
 
303
- function cleanupAllTabs() {
65
+ function persist(tabId, attached) {
66
+ _state.currentTabId = tabId;
67
+ _state.isAttached = attached;
304
68
  return new Promise(function(resolve) {
305
- var tabIds = Array.from(_state.attachedTabIds);
306
-
307
- var detachPromises = tabIds.map(function(tabId) {
308
- return chrome.debugger.detach({ tabId: tabId }).catch(function() {});
309
- });
310
-
311
- Promise.all(detachPromises).then(function() {
312
- chrome.tabGroups.query({}, function(groups) {
313
- if (!groups || groups.length === 0) {
314
- clearAllState();
315
- persist(null, false).then(resolve);
316
- return;
317
- }
318
-
319
- var cdpGroups = groups.filter(function(g) {
320
- return g.title && (g.title.indexOf('CDP-') === 0 || g.title.indexOf('CDP #') === 0);
321
- });
322
-
323
- if (cdpGroups.length === 0) {
324
- clearAllState();
325
- persist(null, false).then(resolve);
326
- return;
327
- }
328
-
329
- var closePromises = cdpGroups.map(function(group) {
330
- return new Promise(function(res) {
331
- chrome.tabs.query({ groupId: group.id }, function(tabs) {
332
- if (!tabs || tabs.length === 0) {
333
- res();
334
- return;
335
- }
336
- var ids = tabs.map(function(t) { return t.id; });
337
- chrome.tabs.remove(ids, function() {
338
- if (chrome.runtime.lastError) {
339
- console.error('[State] Failed to close tabs:', chrome.runtime.lastError.message);
340
- }
341
- res();
342
- });
343
- });
344
- });
345
- });
346
-
347
- Promise.all(closePromises).then(function() {
348
- clearAllState();
349
- persist(null, false).then(resolve);
350
- });
351
- });
352
- });
69
+ chrome.storage.local.set({ currentTabId: tabId, isAttached: attached }, resolve);
353
70
  });
354
71
  }
355
72
 
356
- function mapClientIdToTab(clientId, tabId, sessionId) {
357
- _state.clientIdToTabId.set(clientId, tabId);
358
- if (sessionId) {
359
- _state.clientIdToSessionId.set(clientId, sessionId);
360
- }
73
+ function isTabAttached(tabId) {
74
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
75
+ return entry ? entry.state.isTabAttached(tabId) : false;
361
76
  }
362
77
 
363
- function setTabIdToClientId(tabId, clientId) {
364
- _state.tabIdToClientId.set(tabId, clientId);
78
+ function removeAttachedTab(tabId) {
79
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
80
+ if (entry) entry.state.removeAttachedTab(tabId);
365
81
  }
366
82
 
367
- function removeTabIdToClientId(tabId) {
368
- _state.tabIdToClientId.delete(tabId);
83
+ function hasConnectedClient() {
84
+ var result = false;
85
+ ConnectionManager.forEachConnection(function(entry) {
86
+ if (entry.state.hasConnectedClient()) result = true;
87
+ });
88
+ return result;
369
89
  }
370
90
 
371
91
  function getClientIdByTabId(tabId) {
372
- return _state.tabIdToClientId.get(tabId);
373
- }
374
-
375
- function setGroupIdForClient(clientId, groupId) {
376
- _state.clientIdToGroupId.set(clientId, groupId);
92
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
93
+ return entry ? entry.state.getClientIdByTabId(tabId) : undefined;
377
94
  }
378
95
 
379
- function getGroupIdForClient(clientId) {
380
- return _state.clientIdToGroupId.get(clientId);
96
+ function isCDPCreatedTab(tabId) {
97
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
98
+ return entry ? entry.state.isCDPCreatedTab(tabId) : false;
381
99
  }
382
100
 
383
- function removeGroupForClient(clientId) {
384
- _state.clientIdToGroupId.delete(clientId);
101
+ function isPreExistingTab(tabId) {
102
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
103
+ return entry ? entry.state.isPreExistingTab(tabId) : false;
385
104
  }
386
105
 
387
- function addPreExistingTab(tabId) {
388
- _state.preExistingTabIds.add(tabId);
106
+ function addAttachedTab(tabId) {
107
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
108
+ if (entry) entry.state.addAttachedTab(tabId);
389
109
  }
390
110
 
391
- function isPreExistingTab(tabId) {
392
- return _state.preExistingTabIds.has(tabId);
111
+ function addCDPCreatedTab(tabId) {
112
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
113
+ if (entry) entry.state.addCDPCreatedTab(tabId);
393
114
  }
394
115
 
395
- function getPreExistingTabs() {
396
- return Array.from(_state.preExistingTabIds);
116
+ function getScreencastSession(tabId) {
117
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
118
+ return entry ? entry.state.getScreencastSession(tabId) : null;
397
119
  }
398
120
 
399
- function removePreExistingTab(tabId) {
400
- _state.preExistingTabIds.delete(tabId);
121
+ function setScreencastSession(tabId, session) {
122
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
123
+ if (entry) entry.state.setScreencastSession(tabId, session);
401
124
  }
402
125
 
403
- function clearPreExistingTabsForClient(clientId) {
404
- _state.preExistingTabIds.forEach(function(tabId) {
405
- if (State.getClientIdByTabId(tabId) === clientId) {
406
- _state.preExistingTabIds.delete(tabId);
407
- }
408
- });
126
+ function deleteScreencastSession(tabId) {
127
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
128
+ if (entry) entry.state.deleteScreencastSession(tabId);
409
129
  }
410
130
 
411
- function getTabIdByClientId(clientId) {
412
- return _state.clientIdToTabId.get(clientId);
131
+ function removeAutomatedTab(tabId) {
132
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
133
+ if (entry) entry.state.removeAutomatedTab(tabId);
413
134
  }
414
135
 
415
- function getSessionIdByClientId(clientId) {
416
- return _state.clientIdToSessionId.get(clientId);
136
+ function addAutomatedTab(tabId) {
137
+ var entry = ConnectionManager.getConnectionByTabId(tabId);
138
+ if (entry) entry.state.addAutomatedTab(tabId);
417
139
  }
418
140
 
419
- function unmapClientId(clientId) {
420
- _state.clientIdToTabId.delete(clientId);
421
- _state.clientIdToSessionId.delete(clientId);
422
- }
423
-
424
- function addCDPClient(clientId, info) {
425
- var exists = false;
426
- for (var i = 0; i < _state.cdpClients.length; i++) {
427
- if (_state.cdpClients[i].id === clientId) {
428
- exists = true;
429
- break;
430
- }
431
- }
432
- if (!exists) {
433
- _state.cdpClients.push({
434
- id: clientId,
435
- connectedAt: Date.now()
436
- });
437
- }
438
- }
439
-
440
- function removeCDPClient(clientId) {
441
- var newClients = [];
442
- for (var i = 0; i < _state.cdpClients.length; i++) {
443
- if (_state.cdpClients[i].id !== clientId) {
444
- newClients.push(_state.cdpClients[i]);
445
- }
446
- }
447
- _state.cdpClients = newClients;
448
- }
449
-
450
- function getCDPClients() {
451
- return _state.cdpClients;
452
- }
453
-
454
- function setCDPClients(clients) {
455
- _state.cdpClients = clients || [];
141
+ function getAutomatedTabs() {
142
+ var tabs = [];
143
+ ConnectionManager.forEachConnection(function(entry) {
144
+ tabs = tabs.concat(entry.state.getAutomatedTabs());
145
+ });
146
+ return tabs;
456
147
  }
457
148
 
458
149
  return {
459
- mapSession: mapSession,
460
- unmapSession: unmapSession,
461
- getTabIdBySession: getTabIdBySession,
462
- getTargetIdBySession: getTargetIdBySession,
463
- findSessionByTabId: findSessionByTabId,
464
- findSessionsByTabId: findSessionsByTabId,
465
- findSessionByTargetId: findSessionByTargetId,
466
- getTabIdByTargetId: getTabIdByTargetId,
467
- hasOtherSessionForTab: hasOtherSessionForTab,
468
- addAttachedTab: addAttachedTab,
469
- removeAttachedTab: removeAttachedTab,
470
- isTabAttached: isTabAttached,
471
- getAttachedTabIds: getAttachedTabIds,
472
- addEmittedTarget: addEmittedTarget,
473
- hasEmittedTarget: hasEmittedTarget,
474
- setAutoAttachConfig: setAutoAttachConfig,
475
- getAutoAttachConfig: getAutoAttachConfig,
476
- setDiscoverTargets: setDiscoverTargets,
477
- addPendingDebuggerTab: addPendingDebuggerTab,
478
- removePendingDebuggerTab: removePendingDebuggerTab,
479
- isPendingDebuggerTab: isPendingDebuggerTab,
480
- addBrowserContext: addBrowserContext,
481
- removeBrowserContext: removeBrowserContext,
482
- getBrowserContexts: getBrowserContexts,
483
150
  loadPersisted: loadPersisted,
484
151
  persist: persist,
485
152
  getCurrentTabId: getCurrentTabId,
486
153
  setCurrentTabId: setCurrentTabId,
487
- getWs: getWs,
488
- setWs: setWs,
489
- getReconnectTimer: getReconnectTimer,
490
- setReconnectTimer: setReconnectTimer,
491
- clearReconnectTimer: clearReconnectTimer,
492
- hasConnectedClient: hasConnectedClient,
493
- setHasConnectedClient: setHasConnectedClient,
494
- addCDPClient: addCDPClient,
495
- removeCDPClient: removeCDPClient,
154
+ getAttachedTabIds: getAttachedTabIds,
496
155
  getCDPClients: getCDPClients,
497
- setCDPClients: setCDPClients,
498
- addPendingCreatedTabUrl: addPendingCreatedTabUrl,
499
- removePendingCreatedTabUrl: removePendingCreatedTabUrl,
500
- hasPendingCreatedTabUrl: hasPendingCreatedTabUrl,
501
- getScreencastSession: getScreencastSession,
502
- setScreencastSession: setScreencastSession,
503
- deleteScreencastSession: deleteScreencastSession,
504
- addAutomatedTab: addAutomatedTab,
505
- removeAutomatedTab: removeAutomatedTab,
506
- getAutomatedTabs: getAutomatedTabs,
507
- clearSessionState: clearSessionState,
156
+ findSessionByTabId: findSessionByTabId,
157
+ getWs: getWs,
508
158
  clearAllState: clearAllState,
509
- cleanupAllTabs: cleanupAllTabs,
510
- setTabIdToClientId: setTabIdToClientId,
511
- removeTabIdToClientId: removeTabIdToClientId,
159
+ isTabAttached: isTabAttached,
160
+ removeAttachedTab: removeAttachedTab,
161
+ hasConnectedClient: hasConnectedClient,
512
162
  getClientIdByTabId: getClientIdByTabId,
513
- setGroupIdForClient: setGroupIdForClient,
514
- getGroupIdForClient: getGroupIdForClient,
515
- removeGroupForClient: removeGroupForClient,
516
- addPreExistingTab: addPreExistingTab,
163
+ isCDPCreatedTab: isCDPCreatedTab,
517
164
  isPreExistingTab: isPreExistingTab,
518
- getPreExistingTabs: getPreExistingTabs,
519
- removePreExistingTab: removePreExistingTab,
520
- clearPreExistingTabsForClient: clearPreExistingTabsForClient,
165
+ addAttachedTab: addAttachedTab,
521
166
  addCDPCreatedTab: addCDPCreatedTab,
522
- isCDPCreatedTab: isCDPCreatedTab,
523
- getCDPCreatedTabIds: getCDPCreatedTabIds
167
+ removeAutomatedTab: removeAutomatedTab,
168
+ addAutomatedTab: addAutomatedTab,
169
+ getAutomatedTabs: getAutomatedTabs,
170
+ getScreencastSession: getScreencastSession,
171
+ setScreencastSession: setScreencastSession,
172
+ deleteScreencastSession: deleteScreencastSession
524
173
  };
525
174
  })();