@@ -26,6 +26,8 @@ using v8::Object; |
26 | 26 | using v8::Uint8Array; |
27 | 27 | using v8::Value; |
28 | 28 |
|
| 29 | +constexpr size_t kDefaultMaxTotalBufferSize = 100 * 1024 * 1024; // 100MB |
| 30 | + |
29 | 31 | // Get a protocol string property from the object. |
30 | 32 | Maybe<protocol::String> ObjectGetProtocolString(v8::Local<v8::Context> context, |
31 | 33 | Local<Object> object, |
@@ -246,7 +248,8 @@ NetworkAgent::NetworkAgent( |
246 | 248 | : inspector_(inspector), |
247 | 249 | v8_inspector_(v8_inspector), |
248 | 250 | env_(env), |
249 | | - network_resource_manager_(std::move(network_resource_manager)) { |
| 251 | + network_resource_manager_(std::move(network_resource_manager)), |
| 252 | + requests_(kDefaultMaxTotalBufferSize) { |
250 | 253 | event_notifier_map_["requestWillBeSent"] = &NetworkAgent::requestWillBeSent; |
251 | 254 | event_notifier_map_["responseReceived"] = &NetworkAgent::responseReceived; |
252 | 255 | event_notifier_map_["loadingFailed"] = &NetworkAgent::loadingFailed; |
@@ -329,8 +332,15 @@ void NetworkAgent::Wire(protocol::UberDispatcher* dispatcher) { |
329 | 332 | protocol::Network::Dispatcher::wire(dispatcher, this); |
330 | 333 | } |
331 | 334 |
|
332 | | -protocol::DispatchResponse NetworkAgent::enable() { |
| 335 | +protocol::DispatchResponse NetworkAgent::enable( |
| 336 | + std::optional<int> in_maxTotalBufferSize, |
| 337 | + std::optional<int> in_maxResourceBufferSize) { |
333 | 338 | inspector_->Enable(); |
| 339 | + requests_ = RequestsBuffer( |
| 340 | + in_maxTotalBufferSize.value_or(kDefaultMaxTotalBufferSize)); |
| 341 | + if (in_maxResourceBufferSize) { |
| 342 | + max_resource_buffer_size_ = *in_maxResourceBufferSize; |
| 343 | + } |
334 | 344 | return protocol::DispatchResponse::Success(); |
335 | 345 | } |
336 | 346 |
|
@@ -341,7 +351,7 @@ protocol::DispatchResponse NetworkAgent::disable() { |
341 | 351 |
|
342 | 352 | protocol::DispatchResponse NetworkAgent::getRequestPostData( |
343 | 353 | const protocol::String& in_requestId, protocol::String* out_postData) { |
344 | | - auto request_entry = requests_.find(in_requestId); |
| 354 | + auto request_entry = requests_.cfind(in_requestId); |
345 | 355 | if (request_entry == requests_.end()) { |
346 | 356 | // Request not found, ignore it. |
347 | 357 | return protocol::DispatchResponse::InvalidParams("Request not found"); |
@@ -362,7 +372,7 @@ protocol::DispatchResponse NetworkAgent::getRequestPostData( |
362 | 372 |
|
363 | 373 | // Concat response bodies. |
364 | 374 | protocol::Binary buf = |
365 | | - protocol::Binary::concat(request_entry->second.request_data_blobs); |
| 375 | + protocol::Binary::concat(request_entry->second.request_data_blobs()); |
366 | 376 | *out_postData = protocol::StringUtil::fromUTF8(buf.data(), buf.size()); |
367 | 377 | return protocol::DispatchResponse::Success(); |
368 | 378 | } |
@@ -371,7 +381,7 @@ protocol::DispatchResponse NetworkAgent::getResponseBody( |
371 | 381 | const protocol::String& in_requestId, |
372 | 382 | protocol::String* out_body, |
373 | 383 | bool* out_base64Encoded) { |
374 | | - auto request_entry = requests_.find(in_requestId); |
| 384 | + auto request_entry = requests_.cfind(in_requestId); |
375 | 385 | if (request_entry == requests_.end()) { |
376 | 386 | // Request not found, ignore it. |
377 | 387 | return protocol::DispatchResponse::InvalidParams("Request not found"); |
@@ -391,7 +401,7 @@ protocol::DispatchResponse NetworkAgent::getResponseBody( |
391 | 401 |
|
392 | 402 | // Concat response bodies. |
393 | 403 | protocol::Binary buf = |
394 | | - protocol::Binary::concat(request_entry->second.response_data_blobs); |
| 404 | + protocol::Binary::concat(request_entry->second.response_data_blobs()); |
395 | 405 | if (request_entry->second.response_charset == Charset::kBinary) { |
396 | 406 | // If the response is binary, we return base64 encoded data. |
397 | 407 | *out_body = buf.toBase64(); |
@@ -410,22 +420,26 @@ protocol::DispatchResponse NetworkAgent::getResponseBody( |
410 | 420 |
|
411 | 421 | protocol::DispatchResponse NetworkAgent::streamResourceContent( |
412 | 422 | const protocol::String& in_requestId, protocol::Binary* out_bufferedData) { |
413 | | - auto it = requests_.find(in_requestId); |
414 | | - if (it == requests_.end()) { |
415 | | - // Request not found, ignore it. |
416 | | - return protocol::DispatchResponse::InvalidParams("Request not found"); |
417 | | - } |
418 | | - auto& request_entry = it->second; |
| 423 | + bool is_response_finished = false; |
| 424 | + { |
| 425 | + auto it = requests_.find(in_requestId); |
| 426 | + if (it == requests_.end()) { |
| 427 | + // Request not found, ignore it. |
| 428 | + return protocol::DispatchResponse::InvalidParams("Request not found"); |
| 429 | + } |
| 430 | + auto& request_entry = it->second; |
419 | 431 |
|
420 | | - request_entry.is_streaming = true; |
| 432 | + request_entry.is_streaming = true; |
421 | 433 |
|
422 | | - // Concat response bodies. |
423 | | - *out_bufferedData = |
424 | | - protocol::Binary::concat(request_entry.response_data_blobs); |
425 | | - // Clear buffered data. |
426 | | - request_entry.response_data_blobs.clear(); |
| 434 | + // Concat response bodies. |
| 435 | + *out_bufferedData = |
| 436 | + protocol::Binary::concat(request_entry.response_data_blobs()); |
| 437 | + // Clear buffered data. |
| 438 | + request_entry.clear_response_data_blobs(); |
| 439 | + is_response_finished = request_entry.is_response_finished; |
| 440 | + } |
427 | 441 |
|
428 | | - if (request_entry.is_response_finished) { |
| 442 | + if (is_response_finished) { |
429 | 443 | // If the request is finished, remove the entry. |
430 | 444 | requests_.erase(in_requestId); |
431 | 445 | } |
@@ -500,9 +514,11 @@ void NetworkAgent::requestWillBeSent(v8::Local<v8::Context> context, |
500 | 514 | } |
501 | 515 |
|
502 | 516 | auto request_charset = charset == "utf-8" ? Charset::kUTF8 : Charset::kBinary; |
503 | | - requests_.emplace( |
504 | | - request_id, |
505 | | - RequestEntry(timestamp, request_charset, request->getHasPostData())); |
| 517 | + requests_.emplace(request_id, |
| 518 | + RequestEntry(timestamp, |
| 519 | + request_charset, |
| 520 | + request->getHasPostData(), |
| 521 | + max_resource_buffer_size_)); |
506 | 522 | frontend_->requestWillBeSent(request_id, |
507 | 523 | std::move(request), |
508 | 524 | std::move(initiator), |
@@ -580,7 +596,7 @@ void NetworkAgent::loadingFinished(v8::Local<v8::Context> context, |
580 | 596 |
|
581 | 597 | frontend_->loadingFinished(request_id, timestamp); |
582 | 598 |
|
583 | | - auto request_entry = requests_.find(request_id); |
| 599 | + auto request_entry = requests_.cfind(request_id); |
584 | 600 | if (request_entry == requests_.end()) { |
585 | 601 | // No entry found. Ignore it. |
586 | 602 | return; |
@@ -590,7 +606,7 @@ void NetworkAgent::loadingFinished(v8::Local<v8::Context> context, |
590 | 606 | // Streaming finished, remove the entry. |
591 | 607 | requests_.erase(request_id); |
592 | 608 | } else { |
593 | | - request_entry->second.is_response_finished = true; |
| 609 | + requests_.find(request_id)->second.is_response_finished = true; |
594 | 610 | } |
595 | 611 | } |
596 | 612 |
|
@@ -631,7 +647,7 @@ void NetworkAgent::dataSent(v8::Local<v8::Context> context, |
631 | 647 | } |
632 | 648 | Local<Uint8Array> data = data_obj.As<Uint8Array>(); |
633 | 649 | auto data_bin = protocol::Binary::fromUint8Array(data); |
634 | | - request_entry->second.request_data_blobs.push_back(data_bin); |
| 650 | + request_entry->second.push_request_data_blob(data_bin); |
635 | 651 | } |
636 | 652 |
|
637 | 653 | void NetworkAgent::dataReceived(v8::Local<v8::Context> context, |
@@ -673,7 +689,7 @@ void NetworkAgent::dataReceived(v8::Local<v8::Context> context, |
673 | 689 | frontend_->dataReceived( |
674 | 690 | request_id, timestamp, data_length, encoded_data_length, data_bin); |
675 | 691 | } else { |
676 | | - request_entry.response_data_blobs.push_back(data_bin); |
| 692 | + request_entry.push_response_data_blob(data_bin); |
677 | 693 | } |
678 | 694 | } |
679 | 695 |
|
|