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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
loader.lazyRequireGetter(
this,
"NetworkEventActor",
"resource://devtools/server/actors/network-monitor/network-event-actor.js",
true
);
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
NetworkUtils:
"resource://devtools/shared/network-observer/NetworkUtils.sys.mjs",
});
/**
* Handles network events from the content process
* This currently only handles events for requests (js/css) blocked by CSP.
*/
class NetworkEventContentWatcher {
/**
* Start watching for all network events related to a given Target Actor.
*
* @param TargetActor targetActor
* The target actor in the content process from which we should
* observe network events.
* @param Object options
* Dictionary object with following attributes:
* - onAvailable: mandatory function
* This will be called for each resource.
* - onUpdated: optional function
* This would be called multiple times for each resource.
*/
async watch(targetActor, { onAvailable, onUpdated }) {
// Map from channelId to network event objects.
this.networkEvents = new Map();
this.targetActor = targetActor;
this.onAvailable = onAvailable;
this.onUpdated = onUpdated;
this.httpFailedOpeningRequest = this.httpFailedOpeningRequest.bind(this);
this.httpOnImageCacheResponse = this.httpOnImageCacheResponse.bind(this);
Services.obs.addObserver(
this.httpFailedOpeningRequest,
"http-on-failed-opening-request"
);
Services.obs.addObserver(
this.httpOnImageCacheResponse,
"http-on-image-cache-response"
);
}
/**
* Allows clearing of network events
*/
clear() {
this.networkEvents.clear();
}
httpFailedOpeningRequest(subject, topic) {
const channel = subject.QueryInterface(Ci.nsIHttpChannel);
// Ignore preload requests to avoid duplicity request entries in
// the Network panel. If a preload fails (for whatever reason)
// then the platform kicks off another 'real' request.
if (lazy.NetworkUtils.isPreloadRequest(channel)) {
return;
}
if (
!lazy.NetworkUtils.matchRequest(channel, {
targetActor: this.targetActor,
})
) {
return;
}
this.onNetworkEventAvailable(channel, {
networkEventOptions: {
blockedReason: channel.loadInfo.requestBlockingReason,
},
});
}
httpOnImageCacheResponse(subject, topic) {
if (
topic != "http-on-image-cache-response" ||
!(subject instanceof Ci.nsIHttpChannel)
) {
return;
}
const channel = subject.QueryInterface(Ci.nsIHttpChannel);
if (
!lazy.NetworkUtils.matchRequest(channel, {
targetActor: this.targetActor,
})
) {
return;
}
// Only one network request should be created per URI for images from the cache
const hasURI = Array.from(this.networkEvents.values()).some(
networkEvent => networkEvent.uri === channel.URI.spec
);
if (hasURI) {
return;
}
this.onNetworkEventAvailable(channel, {
networkEventOptions: { fromCache: true },
});
}
onNetworkEventAvailable(channel, { networkEventOptions }) {
const actor = new NetworkEventActor(
this.targetActor.conn,
this.targetActor.sessionContext,
{
onNetworkEventUpdate: this.onNetworkEventUpdate.bind(this),
onNetworkEventDestroy: this.onNetworkEventDestroyed.bind(this),
},
networkEventOptions,
channel
);
this.targetActor.manage(actor);
const resource = actor.asResource();
const networkEvent = {
browsingContextID: resource.browsingContextID,
innerWindowId: resource.innerWindowId,
resourceId: resource.resourceId,
resourceType: resource.resourceType,
receivedUpdates: [],
resourceUpdates: {
// Requests already come with request cookies and headers, so those
// should always be considered as available. But the client still
// heavily relies on those `Available` flags to fetch additional data,
// so it is better to keep them for consistency.
requestCookiesAvailable: true,
requestHeadersAvailable: true,
},
uri: channel.URI.spec,
};
this.networkEvents.set(resource.resourceId, networkEvent);
this.onAvailable([resource]);
const isBlocked = !!resource.blockedReason;
if (isBlocked) {
this._emitUpdate(networkEvent);
} else {
actor.addResponseStart({ channel, fromCache: true });
actor.addEventTimings(
0 /* totalTime */,
{} /* timings */,
{} /* offsets */
);
actor.addResponseContent(
{
mimeType: channel.contentType,
size: channel.contentLength,
text: "",
transferredSize: 0,
},
{}
);
}
}
onNetworkEventUpdate(updateResource) {
const networkEvent = this.networkEvents.get(updateResource.resourceId);
if (!networkEvent) {
return;
}
const { resourceUpdates, receivedUpdates } = networkEvent;
switch (updateResource.updateType) {
case "responseStart":
// For cached image requests channel.responseStatus is set to 200 as
// expected. However responseStatusText is empty. In this case fallback
// to the expected statusText "OK".
let statusText = updateResource.statusText;
if (!statusText && updateResource.status === "200") {
statusText = "OK";
}
resourceUpdates.httpVersion = updateResource.httpVersion;
resourceUpdates.status = updateResource.status;
resourceUpdates.statusText = statusText;
resourceUpdates.remoteAddress = updateResource.remoteAddress;
resourceUpdates.remotePort = updateResource.remotePort;
resourceUpdates.waitingTime = updateResource.waitingTime;
resourceUpdates.responseHeadersAvailable = true;
resourceUpdates.responseCookiesAvailable = true;
break;
case "responseContent":
resourceUpdates.contentSize = updateResource.contentSize;
resourceUpdates.mimeType = updateResource.mimeType;
resourceUpdates.transferredSize = updateResource.transferredSize;
break;
case "eventTimings":
resourceUpdates.totalTime = updateResource.totalTime;
break;
}
resourceUpdates[`${updateResource.updateType}Available`] = true;
receivedUpdates.push(updateResource.updateType);
// Here we explicitly call all three `add` helpers on each network event
// actor so in theory we could check only the last one to be called, ie
// responseContent.
const isComplete =
receivedUpdates.includes("responseStart") &&
receivedUpdates.includes("responseContent") &&
receivedUpdates.includes("eventTimings");
if (isComplete) {
this._emitUpdate(networkEvent);
}
}
_emitUpdate(networkEvent) {
this.onUpdated([
{
resourceType: networkEvent.resourceType,
resourceId: networkEvent.resourceId,
resourceUpdates: networkEvent.resourceUpdates,
browsingContextID: networkEvent.browsingContextID,
innerWindowId: networkEvent.innerWindowId,
},
]);
}
onNetworkEventDestroyed(channelId) {
if (this.networkEvents.has(channelId)) {
this.networkEvents.delete(channelId);
}
}
destroy() {
this.clear();
Services.obs.removeObserver(
this.httpFailedOpeningRequest,
"http-on-failed-opening-request"
);
Services.obs.removeObserver(
this.httpOnImageCacheResponse,
"http-on-image-cache-response"
);
}
}
module.exports = NetworkEventContentWatcher;
|