@@ -15,6 +15,7 @@ using v8::Maybe;
15
15
using v8::MaybeLocal;
16
16
using v8::Nothing;
17
17
using v8::Object;
18
+ using v8::Uint8Array;
18
19
using v8::Value;
19
20
20
21
// Get a protocol string property from the object.
@@ -183,6 +184,7 @@ NetworkAgent::NetworkAgent(NetworkInspector* inspector,
183
184
event_notifier_map_[" responseReceived" ] = &NetworkAgent::responseReceived;
184
185
event_notifier_map_[" loadingFailed" ] = &NetworkAgent::loadingFailed;
185
186
event_notifier_map_[" loadingFinished" ] = &NetworkAgent::loadingFinished;
187
+ event_notifier_map_[" dataReceived" ] = &NetworkAgent::dataReceived;
186
188
}
187
189
188
190
void NetworkAgent::emitNotification (v8::Local<v8::Context> context,
@@ -211,6 +213,30 @@ protocol::DispatchResponse NetworkAgent::disable() {
211
213
return protocol::DispatchResponse::Success ();
212
214
}
213
215
216
+ protocol::DispatchResponse NetworkAgent::streamResourceContent (
217
+ const protocol::String& in_requestId, protocol::Binary* out_bufferedData) {
218
+ if (!requests_.contains (in_requestId)) {
219
+ // Request not found, ignore it.
220
+ return protocol::DispatchResponse::InvalidParams (" Request not found" );
221
+ }
222
+
223
+ auto & it = requests_[in_requestId];
224
+
225
+ it.is_streaming = true ;
226
+
227
+ // Concat response bodies.
228
+ *out_bufferedData = protocol::Binary::concat (it.response_data_blobs );
229
+ // Clear buffered data.
230
+ it.response_data_blobs .clear ();
231
+
232
+ if (it.is_finished ) {
233
+ // If the request is finished, remove the entry.
234
+ requests_.erase (in_requestId);
235
+ }
236
+
237
+ return protocol::DispatchResponse::Success ();
238
+ }
239
+
214
240
void NetworkAgent::requestWillBeSent (v8::Local<v8::Context> context,
215
241
v8::Local<v8::Object> params) {
216
242
protocol::String request_id;
@@ -247,6 +273,12 @@ void NetworkAgent::requestWillBeSent(v8::Local<v8::Context> context,
247
273
std::move (initiator),
248
274
timestamp,
249
275
wall_time);
276
+
277
+ if (requests_.contains (request_id)) {
278
+ // Duplicate entry, ignore it.
279
+ return ;
280
+ }
281
+ requests_.emplace (request_id, RequestEntry{timestamp, false , false , {}});
250
282
}
251
283
252
284
void NetworkAgent::responseReceived (v8::Local<v8::Context> context,
@@ -295,6 +327,8 @@ void NetworkAgent::loadingFailed(v8::Local<v8::Context> context,
295
327
}
296
328
297
329
frontend_->loadingFailed (request_id, timestamp, type, error_text);
330
+
331
+ requests_.erase (request_id);
298
332
}
299
333
300
334
void NetworkAgent::loadingFinished (v8::Local<v8::Context> context,
@@ -309,6 +343,63 @@ void NetworkAgent::loadingFinished(v8::Local<v8::Context> context,
309
343
}
310
344
311
345
frontend_->loadingFinished (request_id, timestamp);
346
+
347
+ auto request_entry = requests_.find (request_id);
348
+ if (request_entry == requests_.end ()) {
349
+ // No entry found. Ignore it.
350
+ return ;
351
+ }
352
+
353
+ if (request_entry->second .is_streaming ) {
354
+ // Streaming finished, remove the entry.
355
+ requests_.erase (request_id);
356
+ } else {
357
+ request_entry->second .is_finished = true ;
358
+ }
359
+ }
360
+
361
+ void NetworkAgent::dataReceived (v8::Local<v8::Context> context,
362
+ v8::Local<v8::Object> params) {
363
+ protocol::String request_id;
364
+ if (!ObjectGetProtocolString (context, params, " requestId" ).To (&request_id)) {
365
+ return ;
366
+ }
367
+
368
+ auto request_entry = requests_.find (request_id);
369
+ if (request_entry == requests_.end ()) {
370
+ // No entry found. Ignore it.
371
+ return ;
372
+ }
373
+
374
+ double timestamp;
375
+ if (!ObjectGetDouble (context, params, " timestamp" ).To (×tamp)) {
376
+ return ;
377
+ }
378
+ int data_length;
379
+ if (!ObjectGetInt (context, params, " dataLength" ).To (&data_length)) {
380
+ return ;
381
+ }
382
+ int encoded_data_length;
383
+ if (!ObjectGetInt (context, params, " encodedDataLength" )
384
+ .To (&encoded_data_length)) {
385
+ return ;
386
+ }
387
+ Local<Object> data_obj;
388
+ if (!ObjectGetObject (context, params, " data" ).ToLocal (&data_obj)) {
389
+ return ;
390
+ }
391
+ if (!data_obj->IsUint8Array ()) {
392
+ return ;
393
+ }
394
+ Local<Uint8Array> data = data_obj.As <Uint8Array>();
395
+ auto data_bin = protocol::Binary::fromUint8Array (data);
396
+
397
+ if (request_entry->second .is_streaming ) {
398
+ frontend_->dataReceived (
399
+ request_id, timestamp, data_length, encoded_data_length, data_bin);
400
+ } else {
401
+ requests_[request_id].response_data_blobs .push_back (data_bin);
402
+ }
312
403
}
313
404
314
405
} // namespace inspector
0 commit comments