@whotracksme/reporting 5.1.26 → 5.1.27
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.
- package/CHANGELOG.md +18 -0
- package/package.json +1 -1
- package/reporting/example/index.js +14 -3
- package/reporting/example/manifests/chromium.json +1 -2
- package/reporting/src/request/index.js +4 -43
- package/reporting/src/request/page-telemetry.js +2 -2
- package/reporting/src/request/steps/page-logger.js +1 -6
- package/reporting/src/request/steps/redirect-tagger.js +0 -24
- package/reporting/src/webrequest-pipeline/page-store.js +219 -150
- package/reporting/src/webrequest-pipeline/webrequest-context.js +1 -33
- package/reporting/test/helpers/scenarios.js +19 -0
- package/reporting/test/request/index.spec.js +2 -9
- package/reporting/test/request-reporter.test.js +18 -14
- package/reporting/test/webrequest-pipeline/index.test.js +28 -15
- package/reporting/test/webrequest-pipeline/page-store.test.js +36 -17
- package/reporting/src/request/dnt.js +0 -24
- package/reporting/src/webrequest-pipeline/page.js +0 -117
|
@@ -9,277 +9,346 @@
|
|
|
9
9
|
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import logger from './logger.js';
|
|
13
|
-
import {
|
|
14
|
-
setActive,
|
|
15
|
-
create as makeTabContext,
|
|
16
|
-
PAGE_LOADING_STATE,
|
|
17
|
-
} from './page.js';
|
|
18
12
|
import ChromeStorageMap from '../request/utils/chrome-storage-map.js';
|
|
19
13
|
|
|
20
14
|
const PAGE_TTL = 1000 * 60 * 60; // 1 hour
|
|
21
15
|
|
|
16
|
+
const PAGE_LOADING_STATE = {
|
|
17
|
+
CREATED: 'created',
|
|
18
|
+
NAVIGATING: 'navigating',
|
|
19
|
+
COMMITTED: 'committed',
|
|
20
|
+
COMPLETE: 'complete',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
class Page {
|
|
24
|
+
constructor(page) {
|
|
25
|
+
this.id = page.id;
|
|
26
|
+
this.url = page.url;
|
|
27
|
+
this.isPrivate = page.incognito || false;
|
|
28
|
+
this.isPrivateServer = page.isPrivateServer || false;
|
|
29
|
+
this.created = page.created || Date.now();
|
|
30
|
+
this.destroyed = page.destroyed || null;
|
|
31
|
+
this.frames = page.frames || {
|
|
32
|
+
0: {
|
|
33
|
+
parentFrameId: -1,
|
|
34
|
+
url: page.url,
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
this.state = page.state || PAGE_LOADING_STATE.CREATED;
|
|
38
|
+
|
|
39
|
+
this.activeTime = page.activeTime || 0;
|
|
40
|
+
this.activeFrom = page.active ? Date.now() : 0;
|
|
41
|
+
|
|
42
|
+
this.requestStats = page.requestStats || {};
|
|
43
|
+
this.annotations = page.annotations || {};
|
|
44
|
+
this.counter = page.counter || 0;
|
|
45
|
+
this.previous = page.previous;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
setActive(active) {
|
|
49
|
+
if (active && this.activeFrom === 0) {
|
|
50
|
+
this.activeFrom = Date.now();
|
|
51
|
+
} else if (!active && this.activeFrom > 0) {
|
|
52
|
+
this.activeTime += Date.now() - this.activeFrom;
|
|
53
|
+
this.activeFrom = 0;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getStatsForDomain(domain) {
|
|
58
|
+
return (this.requestStats[domain] ||= {});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
22
62
|
export default class PageStore {
|
|
63
|
+
#notifyPageStageListeners;
|
|
64
|
+
#pages;
|
|
65
|
+
|
|
23
66
|
constructor({ notifyPageStageListeners }) {
|
|
24
|
-
this
|
|
67
|
+
this.#pages = new ChromeStorageMap({
|
|
25
68
|
storageKey: 'wtm-url-reporting:page-store:tabs',
|
|
26
69
|
ttlInMs: PAGE_TTL,
|
|
27
70
|
});
|
|
28
|
-
this
|
|
29
|
-
this.notifyPageStageListeners = notifyPageStageListeners;
|
|
71
|
+
this.#notifyPageStageListeners = notifyPageStageListeners;
|
|
30
72
|
}
|
|
31
73
|
|
|
32
74
|
async init() {
|
|
33
|
-
await this.
|
|
34
|
-
chrome.tabs.onCreated.addListener(this
|
|
35
|
-
chrome.tabs.onUpdated.addListener(this
|
|
36
|
-
chrome.tabs.onRemoved.addListener(this
|
|
37
|
-
chrome.tabs.onActivated.addListener(this
|
|
38
|
-
chrome.webNavigation.onBeforeNavigate.addListener(this
|
|
39
|
-
chrome.webNavigation.onCommitted.addListener(this
|
|
40
|
-
chrome.webNavigation.onCompleted.addListener(this
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
75
|
+
await this.#pages.isReady;
|
|
76
|
+
chrome.tabs.onCreated.addListener(this.#onTabCreated);
|
|
77
|
+
chrome.tabs.onUpdated.addListener(this.#onTabUpdated);
|
|
78
|
+
chrome.tabs.onRemoved.addListener(this.#onTabRemoved);
|
|
79
|
+
chrome.tabs.onActivated.addListener(this.#onTabActivated);
|
|
80
|
+
chrome.webNavigation.onBeforeNavigate.addListener(this.#onBeforeNavigate);
|
|
81
|
+
chrome.webNavigation.onCommitted.addListener(this.#onNavigationCommitted);
|
|
82
|
+
chrome.webNavigation.onCompleted.addListener(this.#onNavigationCompleted);
|
|
83
|
+
chrome.windows.onFocusChanged?.addListener(this.#onWindowFocusChanged);
|
|
84
|
+
|
|
44
85
|
// popupate initially open tabs
|
|
45
|
-
chrome.tabs.query({}
|
|
46
|
-
openTabs.forEach((tab) => {
|
|
47
|
-
this.onTabCreated(tab);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
86
|
+
(await chrome.tabs.query({})).forEach((tab) => this.#onTabCreated(tab));
|
|
50
87
|
}
|
|
51
88
|
|
|
52
89
|
unload() {
|
|
53
|
-
this.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
chrome.tabs.
|
|
60
|
-
chrome.
|
|
61
|
-
chrome.
|
|
62
|
-
chrome.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
90
|
+
this.#pages.forEach((serializedPage) => {
|
|
91
|
+
const page = new Page(serializedPage);
|
|
92
|
+
this.#stagePage(page);
|
|
93
|
+
});
|
|
94
|
+
this.#pages.clear();
|
|
95
|
+
|
|
96
|
+
chrome.tabs.onCreated.removeListener(this.#onTabCreated);
|
|
97
|
+
chrome.tabs.onUpdated.removeListener(this.#onTabUpdated);
|
|
98
|
+
chrome.tabs.onRemoved.removeListener(this.#onTabRemoved);
|
|
99
|
+
chrome.tabs.onActivated.removeListener(this.#onTabActivated);
|
|
100
|
+
chrome.webNavigation.onBeforeNavigate.removeListener(
|
|
101
|
+
this.#onBeforeNavigate,
|
|
102
|
+
);
|
|
103
|
+
chrome.webNavigation.onCommitted.removeListener(
|
|
104
|
+
this.#onNavigationCommitted,
|
|
105
|
+
);
|
|
106
|
+
chrome.webNavigation.onCompleted.removeListener(
|
|
107
|
+
this.#onNavigationCompleted,
|
|
108
|
+
);
|
|
109
|
+
chrome.windows.onFocusChanged?.removeListener(this.#onWindowFocusChanged);
|
|
66
110
|
}
|
|
67
111
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
112
|
+
checkIfEmpty() {
|
|
113
|
+
// this operations can potentially be expensive
|
|
114
|
+
return this.#pages.countNonExpiredKeys() === 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
#stagePage(page) {
|
|
118
|
+
page.setActive(false);
|
|
73
119
|
page.destroyed = Date.now();
|
|
74
120
|
// unset previous (to prevent history chain memory leak)
|
|
75
121
|
page.previous = undefined;
|
|
76
|
-
this.
|
|
122
|
+
this.#pages.set(page.id, page);
|
|
123
|
+
this.#notifyPageStageListeners(page);
|
|
77
124
|
}
|
|
78
125
|
|
|
79
126
|
/**
|
|
80
127
|
* Create a new `tabContext` for the new tab
|
|
81
128
|
*/
|
|
82
|
-
onTabCreated = (tab) => {
|
|
83
|
-
this.
|
|
129
|
+
#onTabCreated = (tab) => {
|
|
130
|
+
this.#pages.set(tab.id, new Page(tab));
|
|
84
131
|
};
|
|
85
132
|
|
|
86
133
|
/**
|
|
87
134
|
* Update an existing tab or create it if we do not have a context yet.
|
|
88
135
|
*/
|
|
89
|
-
onTabUpdated = (tabId, info, tab) => {
|
|
90
|
-
|
|
91
|
-
if (!tabContext) {
|
|
92
|
-
tabContext = makeTabContext(tab);
|
|
93
|
-
this.tabs.set(tabId, tabContext);
|
|
94
|
-
}
|
|
136
|
+
#onTabUpdated = (tabId, info, tab) => {
|
|
137
|
+
const page = new Page(this.#pages.get(tabId) || tab);
|
|
95
138
|
|
|
96
139
|
// Update `isPrivate` and `url` if available
|
|
97
|
-
|
|
98
|
-
setActive(
|
|
140
|
+
page.isPrivate = tab.incognito;
|
|
141
|
+
page.setActive(tab.active);
|
|
99
142
|
if (info.url !== undefined) {
|
|
100
|
-
|
|
143
|
+
page.url = info.url;
|
|
101
144
|
}
|
|
145
|
+
|
|
146
|
+
this.#pages.set(tabId, page);
|
|
102
147
|
};
|
|
103
148
|
|
|
104
149
|
/**
|
|
105
150
|
* Remove tab context for `tabId`.
|
|
106
151
|
*/
|
|
107
|
-
onTabRemoved = (tabId) => {
|
|
108
|
-
const
|
|
109
|
-
if (
|
|
110
|
-
|
|
152
|
+
#onTabRemoved = (tabId) => {
|
|
153
|
+
const serializedPage = this.#pages.get(tabId);
|
|
154
|
+
if (!serializedPage) {
|
|
155
|
+
return;
|
|
111
156
|
}
|
|
112
|
-
|
|
157
|
+
const page = new Page(serializedPage);
|
|
158
|
+
if (page.state === PAGE_LOADING_STATE.COMPLETE) {
|
|
159
|
+
this.#stagePage(page);
|
|
160
|
+
}
|
|
161
|
+
this.#pages.delete(tabId);
|
|
113
162
|
};
|
|
114
163
|
|
|
115
|
-
onTabActivated = (
|
|
164
|
+
#onTabActivated = (details) => {
|
|
165
|
+
const { previousTabId, tabId } = details;
|
|
116
166
|
// if previousTabId is not set (e.g. on chrome), set all tabs to inactive
|
|
117
167
|
// otherwise, we only have to mark the previous tab as inactive
|
|
118
168
|
if (!previousTabId) {
|
|
119
|
-
for (const
|
|
120
|
-
|
|
169
|
+
for (const serializedPage of this.#pages.values()) {
|
|
170
|
+
const page = new Page(serializedPage);
|
|
171
|
+
page.setActive(false);
|
|
172
|
+
this.#pages.set(page.id, page);
|
|
121
173
|
}
|
|
122
|
-
} else if (this.
|
|
123
|
-
|
|
174
|
+
} else if (this.#pages.has(previousTabId)) {
|
|
175
|
+
const page = new Page(previousTabId);
|
|
176
|
+
page.setActive(false);
|
|
177
|
+
this.#pages.set(page.id, page);
|
|
124
178
|
}
|
|
125
|
-
|
|
126
|
-
|
|
179
|
+
|
|
180
|
+
if (this.#pages.has(tabId)) {
|
|
181
|
+
const page = new Page(this.#pages.get(tabId));
|
|
182
|
+
page.setActive(true);
|
|
183
|
+
this.#pages.set(page.id, page);
|
|
127
184
|
}
|
|
128
185
|
};
|
|
129
186
|
|
|
130
|
-
onWindowFocusChanged = (
|
|
131
|
-
chrome.tabs.query({ active: true }
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
});
|
|
187
|
+
#onWindowFocusChanged = async (focusedWindowId) => {
|
|
188
|
+
const activeTabs = await chrome.tabs.query({ active: true });
|
|
189
|
+
for (const { id, windowId } of activeTabs) {
|
|
190
|
+
const serializedPage = this.#pages.get(id);
|
|
191
|
+
if (!serializedPage) {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const page = new Page(serializedPage);
|
|
195
|
+
page.setActive(windowId === focusedWindowId);
|
|
196
|
+
this.#pages.set(id, page);
|
|
197
|
+
}
|
|
144
198
|
};
|
|
145
199
|
|
|
146
|
-
onBeforeNavigate = (details) => {
|
|
200
|
+
#onBeforeNavigate = (details) => {
|
|
147
201
|
const { frameId, tabId, url, timeStamp } = details;
|
|
148
|
-
|
|
149
|
-
if (frameId
|
|
150
|
-
|
|
202
|
+
|
|
203
|
+
if (frameId !== 0) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const page = this.#pages.has(tabId)
|
|
208
|
+
? new Page(this.#pages.get(tabId))
|
|
209
|
+
: null;
|
|
210
|
+
|
|
211
|
+
if (page) {
|
|
212
|
+
// ignore duplicated #onBeforeNavigate https://bugzilla.mozilla.org/show_bug.cgi?id=1732564
|
|
151
213
|
if (
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
tabContext.created + 200 > timeStamp
|
|
214
|
+
page.id === tabId &&
|
|
215
|
+
page.url === url &&
|
|
216
|
+
page.created + 200 > timeStamp
|
|
156
217
|
) {
|
|
157
218
|
return;
|
|
158
219
|
}
|
|
159
220
|
// We are starting a navigation to a new page - if the previous page is complete (i.e. fully
|
|
160
221
|
// loaded), stage it before we create the new page info.
|
|
161
|
-
if (
|
|
162
|
-
this
|
|
222
|
+
if (page.state === PAGE_LOADING_STATE.COMPLETE) {
|
|
223
|
+
this.#stagePage(page);
|
|
163
224
|
}
|
|
164
|
-
// create a new page for the navigation
|
|
165
|
-
this.tabs.delete(tabId);
|
|
166
|
-
const nextContext = makeTabContext({
|
|
167
|
-
id: tabId,
|
|
168
|
-
active: false,
|
|
169
|
-
url,
|
|
170
|
-
incognito: tabContext ? tabContext.isPrivate : false,
|
|
171
|
-
created: timeStamp,
|
|
172
|
-
});
|
|
173
|
-
nextContext.previous = tabContext;
|
|
174
|
-
this.tabs.set(tabId, nextContext);
|
|
175
|
-
nextContext.state = PAGE_LOADING_STATE.NAVIGATING;
|
|
176
225
|
}
|
|
226
|
+
|
|
227
|
+
// create a new page for the navigation
|
|
228
|
+
this.#pages.delete(tabId);
|
|
229
|
+
|
|
230
|
+
const nextPage = new Page({
|
|
231
|
+
id: tabId,
|
|
232
|
+
active: false,
|
|
233
|
+
url,
|
|
234
|
+
incognito: page ? page.isPrivate : false,
|
|
235
|
+
created: timeStamp,
|
|
236
|
+
});
|
|
237
|
+
nextPage.previous = page;
|
|
238
|
+
nextPage.state = PAGE_LOADING_STATE.NAVIGATING;
|
|
239
|
+
this.#pages.set(tabId, nextPage);
|
|
177
240
|
};
|
|
178
241
|
|
|
179
|
-
onNavigationCommitted = (details) => {
|
|
242
|
+
#onNavigationCommitted = (details) => {
|
|
180
243
|
const { frameId, tabId } = details;
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
244
|
+
const serializedPage = this.#pages.get(tabId);
|
|
245
|
+
|
|
246
|
+
if (!serializedPage) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const page = new Page(serializedPage);
|
|
251
|
+
|
|
252
|
+
if (frameId === 0) {
|
|
253
|
+
page.state = PAGE_LOADING_STATE.COMMITTED;
|
|
254
|
+
this.#pages.set(tabId, page);
|
|
255
|
+
} else if (!page.frames[frameId]) {
|
|
185
256
|
// frame created without request
|
|
186
257
|
this.onSubFrame(details);
|
|
187
258
|
}
|
|
188
259
|
};
|
|
189
260
|
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
261
|
+
#onNavigationCompleted = (details) => {
|
|
262
|
+
const { frameId, tabId } = details;
|
|
263
|
+
const serializedPage = this.#pages.get(tabId);
|
|
264
|
+
if (!serializedPage) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const page = new Page(serializedPage);
|
|
268
|
+
if (frameId === 0) {
|
|
269
|
+
page.state = PAGE_LOADING_STATE.COMPLETE;
|
|
194
270
|
}
|
|
271
|
+
this.#pages.set(tabId, page);
|
|
195
272
|
};
|
|
196
273
|
|
|
197
|
-
onMainFrame = (
|
|
274
|
+
onMainFrame = (details, event) => {
|
|
275
|
+
const { tabId, url } = details;
|
|
198
276
|
// main frame from tabId -1 is from service worker and should not be saved
|
|
199
277
|
if (tabId === -1) {
|
|
200
278
|
return;
|
|
201
279
|
}
|
|
280
|
+
|
|
202
281
|
// Update last request id from the tab
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
this.tabs.set(tabId, tabContext);
|
|
207
|
-
}
|
|
282
|
+
const page = new Page(
|
|
283
|
+
this.#pages.get(tabId) || { url, incognito: false, id: tabId },
|
|
284
|
+
);
|
|
208
285
|
|
|
209
286
|
if (event === 'onBeforeRequest') {
|
|
210
|
-
|
|
211
|
-
// Detect redirect: if the last request on this tab had the same id and
|
|
212
|
-
// this was from the same `onBeforeRequest` hook, we can assume this is a
|
|
213
|
-
// redirection.
|
|
214
|
-
if (tabContext.lastRequestId === requestId) {
|
|
215
|
-
tabContext.isRedirect = true;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// Only keep track of `lastRequestId` with `onBeforeRequest` listener
|
|
219
|
-
// since we need this information for redirect detection only and this can
|
|
220
|
-
// be detected with this hook.
|
|
221
|
-
tabContext.lastRequestId = requestId;
|
|
287
|
+
page.frames = {};
|
|
222
288
|
}
|
|
223
289
|
|
|
224
290
|
// Update context of tab with `url` and main frame information
|
|
225
|
-
|
|
226
|
-
|
|
291
|
+
page.url = url;
|
|
292
|
+
page.frames[0] = {
|
|
227
293
|
parentFrameId: -1,
|
|
228
294
|
url,
|
|
229
295
|
};
|
|
296
|
+
|
|
297
|
+
this.#pages.set(tabId, page);
|
|
230
298
|
};
|
|
231
299
|
|
|
232
300
|
onSubFrame = (details) => {
|
|
233
301
|
const { tabId, frameId, parentFrameId, url } = details;
|
|
234
|
-
const
|
|
235
|
-
if (!
|
|
236
|
-
logger.log('Could not find tab for sub_frame request', details);
|
|
302
|
+
const serializedPage = this.#pages.get(tabId);
|
|
303
|
+
if (!serializedPage) {
|
|
237
304
|
return;
|
|
238
305
|
}
|
|
239
|
-
|
|
306
|
+
const page = new Page(serializedPage);
|
|
240
307
|
// Keep track of frameUrl as well as parent frame
|
|
241
|
-
|
|
308
|
+
page.frames[frameId] = {
|
|
242
309
|
parentFrameId,
|
|
243
310
|
url,
|
|
244
311
|
};
|
|
312
|
+
this.#pages.set(tabId, page);
|
|
245
313
|
};
|
|
246
314
|
|
|
247
315
|
getPageForRequest(context) {
|
|
248
316
|
const { tabId, frameId, originUrl, type, initiator } = context;
|
|
249
|
-
const
|
|
250
|
-
if (!
|
|
317
|
+
const serializedPage = this.#pages.get(tabId);
|
|
318
|
+
if (!serializedPage) {
|
|
251
319
|
return null;
|
|
252
320
|
}
|
|
321
|
+
const page = new Page(serializedPage);
|
|
253
322
|
// check if the current page has the given frame id, otherwise check if it belongs to the
|
|
254
323
|
// previous page
|
|
255
|
-
if (!
|
|
256
|
-
if (
|
|
257
|
-
return
|
|
324
|
+
if (!page.frames[frameId]) {
|
|
325
|
+
if (page.previous && page.previous.frames[frameId]) {
|
|
326
|
+
return page.previous;
|
|
258
327
|
}
|
|
259
328
|
return null;
|
|
260
329
|
}
|
|
261
330
|
|
|
262
331
|
const couldBePreviousPage =
|
|
263
|
-
frameId === 0 && type !== 'main_frame' &&
|
|
332
|
+
frameId === 0 && type !== 'main_frame' && page.previous;
|
|
264
333
|
|
|
265
334
|
// for main frame requests: check if the origin url is from the previous page (Firefox)
|
|
266
335
|
if (
|
|
267
336
|
couldBePreviousPage &&
|
|
268
|
-
|
|
269
|
-
|
|
337
|
+
page.url !== originUrl &&
|
|
338
|
+
page.previous.url === originUrl
|
|
270
339
|
) {
|
|
271
|
-
return
|
|
340
|
+
return page.previous;
|
|
272
341
|
}
|
|
273
342
|
// on Chrome we have `initiator` which only contains the origin. In this case, check for a
|
|
274
343
|
// different origin
|
|
275
344
|
if (
|
|
276
345
|
couldBePreviousPage &&
|
|
277
346
|
initiator &&
|
|
278
|
-
!
|
|
279
|
-
|
|
347
|
+
!page.url.startsWith(initiator) &&
|
|
348
|
+
page.previous.url.startsWith(initiator)
|
|
280
349
|
) {
|
|
281
|
-
return
|
|
350
|
+
return page.previous;
|
|
282
351
|
}
|
|
283
|
-
return
|
|
352
|
+
return page;
|
|
284
353
|
}
|
|
285
354
|
}
|
|
@@ -11,7 +11,6 @@
|
|
|
11
11
|
|
|
12
12
|
import { parse } from '../utils/url.js';
|
|
13
13
|
import logger from './logger.js';
|
|
14
|
-
import { getFrameUrl, getFrameAncestors } from './page.js';
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* Transform an array of headers (i.e.: `{ name, value }`) into a `Map`.
|
|
@@ -59,38 +58,17 @@ export default class WebRequestContext {
|
|
|
59
58
|
// Get context on this page
|
|
60
59
|
const page = pageStore.getPageForRequest(context);
|
|
61
60
|
|
|
62
|
-
//
|
|
63
|
-
// frameAncestors
|
|
64
|
-
if (context.frameAncestors === undefined) {
|
|
65
|
-
context.frameAncestors = page ? getFrameAncestors(page, context) : [];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Cliqz-specific extensions to webRequest details
|
|
61
|
+
// Ghostery-specific extensions to webRequest details
|
|
69
62
|
context.page = page;
|
|
70
63
|
context.tabUrl = context.tabUrl || (page && page.url);
|
|
71
|
-
context.frameUrl = context.frameUrl || (page && getFrameUrl(page, context));
|
|
72
64
|
context.isPrivate = page ? page.isPrivate : null;
|
|
73
65
|
context.isMainFrame = context.type === 'main_frame';
|
|
74
|
-
context.isRedirect = page && context.isMainFrame && page.isRedirect;
|
|
75
66
|
|
|
76
67
|
if (!context.tabUrl) {
|
|
77
68
|
context.tabUrl =
|
|
78
69
|
context.originUrl || context.initiator || context.documentUrl;
|
|
79
70
|
}
|
|
80
71
|
|
|
81
|
-
if (!context.frameUrl) {
|
|
82
|
-
context.frameUrl =
|
|
83
|
-
context.documentUrl || context.originUrl || context.initiator;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (!context.originUrl) {
|
|
87
|
-
context.originUrl =
|
|
88
|
-
context.initiator ||
|
|
89
|
-
context.documentUrl ||
|
|
90
|
-
context.frameUrl ||
|
|
91
|
-
context.tabUrl;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
72
|
return new WebRequestContext(context);
|
|
95
73
|
}
|
|
96
74
|
|
|
@@ -98,15 +76,11 @@ export default class WebRequestContext {
|
|
|
98
76
|
Object.assign(this, details);
|
|
99
77
|
|
|
100
78
|
// Lazy attributes
|
|
101
|
-
this._frameUrlParts = null;
|
|
102
|
-
|
|
103
79
|
this._requestHeadersMap = null;
|
|
104
80
|
this._responseHeadersMap = null;
|
|
105
81
|
|
|
106
82
|
this.urlParts = parse(this.url);
|
|
107
|
-
this.frameUrlParts = parse(this.frameUrl);
|
|
108
83
|
this.tabUrlParts = parse(this.tabUrl);
|
|
109
|
-
this.originUrlParts = parse(this.originUrl);
|
|
110
84
|
}
|
|
111
85
|
|
|
112
86
|
/**
|
|
@@ -145,10 +119,4 @@ export default class WebRequestContext {
|
|
|
145
119
|
getReferrer() {
|
|
146
120
|
return this.getRequestHeader('Referer');
|
|
147
121
|
}
|
|
148
|
-
|
|
149
|
-
isBackgroundRequest() {
|
|
150
|
-
return (
|
|
151
|
-
this.tabId === -1 && this.originUrlParts?.protocol.endsWith('extension:')
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
122
|
}
|
|
@@ -72,14 +72,33 @@ export async function playScenario(
|
|
|
72
72
|
throw new Error('specify scenario release');
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
const seenTabIds = new Set();
|
|
75
76
|
const scenarioPath = await download(scenarioRelease, scenarioName, browser);
|
|
76
77
|
const events = loadScenario(scenarioPath);
|
|
77
78
|
for (const event of events) {
|
|
78
79
|
try {
|
|
79
80
|
const args = event.args.map(rewriteIp);
|
|
81
|
+
|
|
82
|
+
if (events.api === 'tabs' && event.event === 'onCreated') {
|
|
83
|
+
seenTabIds.add(args[0].id);
|
|
84
|
+
} else {
|
|
85
|
+
if (Object.prototype.hasOwnProperty.call(args[0], 'tabId')) {
|
|
86
|
+
seenTabIds.add(args[0].tabId);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (
|
|
90
|
+
Object.prototype.hasOwnProperty.call(args[0], 'successorTabId') &&
|
|
91
|
+
args[0].successorTabId !== -1
|
|
92
|
+
) {
|
|
93
|
+
seenTabIds.add(args[0].successorTabId);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
80
97
|
chrome[event.api][event.event].dispatch(...args);
|
|
81
98
|
} catch (e) {
|
|
82
99
|
console.error(`Could not dispatch event`, event, e);
|
|
83
100
|
}
|
|
84
101
|
}
|
|
102
|
+
|
|
103
|
+
return { seenTabIds };
|
|
85
104
|
}
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import * as chai from 'chai';
|
|
13
13
|
import sinon from 'sinon';
|
|
14
|
+
|
|
14
15
|
// import FakeSessionApi from '../helpers/fake-session-storage.js'; // TODO: see comments below
|
|
15
16
|
|
|
16
17
|
import testPages from './test-pages.js';
|
|
@@ -82,7 +83,6 @@ describe('request/index', function () {
|
|
|
82
83
|
afterEach(() => {
|
|
83
84
|
pipeline?.unload();
|
|
84
85
|
pipeline = null;
|
|
85
|
-
|
|
86
86
|
if (monkeyPatchedChromeStorage) {
|
|
87
87
|
chrome.storage.session = oldChromeStorageSession;
|
|
88
88
|
chrome.storage = oldChromeStorage;
|
|
@@ -139,9 +139,7 @@ describe('request/index', function () {
|
|
|
139
139
|
});
|
|
140
140
|
|
|
141
141
|
function simulatePageLoad(pageSpec) {
|
|
142
|
-
|
|
143
|
-
...pageSpec.tab,
|
|
144
|
-
});
|
|
142
|
+
chrome.tabs.onCreated.dispatch(pageSpec.tab);
|
|
145
143
|
return {
|
|
146
144
|
onBeforeRequest: pageSpec.onBeforeRequest.map(function (reqData) {
|
|
147
145
|
const response = pipeline.onBeforeRequest(reqData);
|
|
@@ -296,7 +294,6 @@ describe('request/index', function () {
|
|
|
296
294
|
type: 'main_frame',
|
|
297
295
|
url: 'http://cliqztest.com/',
|
|
298
296
|
requestHeaders: mockRequestHeaders,
|
|
299
|
-
frameAncestors: [],
|
|
300
297
|
});
|
|
301
298
|
chai.expect(mainDoc).to.not.have.property('cancel');
|
|
302
299
|
chai.expect(mainDoc).to.not.have.property('redirectUrl');
|
|
@@ -313,7 +310,6 @@ describe('request/index', function () {
|
|
|
313
310
|
requestHeaders: mockRequestHeaders,
|
|
314
311
|
initiator: 'http://cliqztest.com',
|
|
315
312
|
isPrivate: false,
|
|
316
|
-
frameAncestors: [],
|
|
317
313
|
});
|
|
318
314
|
chai.expect(response).to.have.property('redirectUrl');
|
|
319
315
|
chai.expect(response.redirectUrl).to.not.contain(uid);
|
|
@@ -331,7 +327,6 @@ describe('request/index', function () {
|
|
|
331
327
|
requestHeaders: mockRequestHeaders,
|
|
332
328
|
originUrl: '',
|
|
333
329
|
tabUrl: '',
|
|
334
|
-
frameAncestors: [],
|
|
335
330
|
});
|
|
336
331
|
chai.expect(mainDoc).to.not.have.property('cancel');
|
|
337
332
|
chai.expect(mainDoc).to.not.have.property('redirectUrl');
|
|
@@ -349,7 +344,6 @@ describe('request/index', function () {
|
|
|
349
344
|
originUrl: 'http://cliqztest.com',
|
|
350
345
|
tabUrl: 'http://cliqztest.com',
|
|
351
346
|
isPrivate: false,
|
|
352
|
-
frameAncestors: [],
|
|
353
347
|
});
|
|
354
348
|
chai.expect(response).to.have.property('redirectUrl');
|
|
355
349
|
chai.expect(response.redirectUrl).to.not.contain(uid);
|
|
@@ -368,7 +362,6 @@ describe('request/index', function () {
|
|
|
368
362
|
originUrl: 'http://cliqztest.com',
|
|
369
363
|
tabUrl: 'http://cliqztest.com',
|
|
370
364
|
isPrivate: false,
|
|
371
|
-
frameAncestors: [],
|
|
372
365
|
});
|
|
373
366
|
chai.expect(response).to.have.property('redirectUrl');
|
|
374
367
|
chai.expect(response.redirectUrl).to.not.contain(uid);
|