DevTools: fix bookkeeping when evicting POST data

Due to the bug, free space size was not updated when the POST data was
evicted, resulting in crash.

TBR=eostroukhov@chromium.org

(cherry picked from commit c9a324bf2fb731f1f9ef94dc4068cdc77140a5d6)

Bug: 813187
Change-Id: I8c4a3762eaa2ecf6ce58fccc639846c24cc9d6c8
Reviewed-on: https://chromium-review.googlesource.com/927643
Commit-Queue: Eugene Ostroukhov <eostroukhov@chromium.org>
Reviewed-by: Eugene Ostroukhov <eostroukhov@chromium.org>
Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#537977}
Reviewed-on: https://chromium-review.googlesource.com/932005
Cr-Commit-Position: refs/branch-heads/3325@{#550}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/get-request-post-data-expected.txt b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/get-request-post-data-expected.txt
index d973a0b..dbbb20c 100644
--- a/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/get-request-post-data-expected.txt
+++ b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/get-request-post-data-expected.txt
@@ -1,6 +1,6 @@
 Tests fetching POST request body.
 Data included: false, has post data: true
-Data length: 1024
+Did not fetch data: No post data available for the request
 Data included: true, has post data: true
 Data length: 128
 Data included: false, has post data: undefined
@@ -8,4 +8,28 @@
 Data included: false, has post data: undefined
 Did not fetch data: No post data available for the request
 Error is: No resource with given id was found
+Data included: true, has post data: true
+Data length: 64
+Data included: true, has post data: true
+Data length: 64
+Data included: true, has post data: true
+Data length: 64
+Data included: true, has post data: true
+Data length: 64
+Data included: true, has post data: true
+Data length: 64
+Data included: true, has post data: true
+Data length: 64
+Data included: true, has post data: true
+Data length: 64
+Data included: true, has post data: true
+Data length: 64
+Data included: true, has post data: true
+Data length: 64
+Data included: true, has post data: true
+Data length: 64
+Testing request body eviction
+Data included: true, has post data: true
+Data length: 512
+Did not fetch data: No post data available for the request
 
diff --git a/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/get-request-post-data.js b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/get-request-post-data.js
index d469488..748d76c 100644
--- a/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/get-request-post-data.js
+++ b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/get-request-post-data.js
@@ -1,5 +1,5 @@
 (async function(testRunner) {
-  var {page, session, dp} = await testRunner.startBlank(
+  const {page, session, dp} = await testRunner.startBlank(
       `Tests fetching POST request body.`);
 
   function SendRequest(method, body) {
@@ -13,26 +13,38 @@
         });`);
   }
 
-  async function SendAndReportRequest(method, body = '') {
-    var requestWillBeSentPromise = dp.Network.onceRequestWillBeSent();
-    await SendRequest(method, body);
-    var notification = (await requestWillBeSentPromise).params;
-    var request = notification.request;
-    testRunner.log(`Data included: ${request.postData !== undefined}, has post data: ${request.hasPostData}`);
-    var { result, error } = await dp.Network.getRequestPostData({ requestId: notification.requestId });
+  async function ReportRequest(requestId) {
+    const {result, error} = await dp.Network.getRequestPostData({requestId});
     if (error) {
       testRunner.log(`Did not fetch data: ${error.message}`);
-    }
-    else
+    } else {
       testRunner.log(`Data length: ${result.postData.length}`);
+    }
   }
 
-  await dp.Network.enable({ maxPostDataSize: 512 });
+  async function SendAndReportRequest(method, body = '') {
+    SendRequest(method, body);
+    const notification = (await dp.Network.onceRequestWillBeSent()).params;
+    const request = notification.request;
+    testRunner.log(`Data included: ${request.postData !== undefined}, has post data: ${request.hasPostData}`);
+    await ReportRequest(notification.requestId);
+    return notification.requestId;
+  }
+
+  await dp.Network.enable({ maxPostDataSize: 512, maxTotalBufferSize: 1025 });
   await SendAndReportRequest('POST', 'new Uint8Array(1024)');
   await SendAndReportRequest('POST', 'new Uint8Array(128)');
   await SendAndReportRequest('POST', '');
   await SendAndReportRequest('GET', 'new Uint8Array(1024)');
-  var result = await dp.Network.getRequestPostData({ requestId: 'fake-id' });
+  const result = await dp.Network.getRequestPostData({ requestId: 'fake-id' });
   testRunner.log(`Error is: ${result.error.message}`);
+
+  let most_recent_request;
+  for (let i = 0; i < 10; i++)
+    most_recent_request = await SendAndReportRequest('POST', 'new Uint8Array(64)');
+
+  testRunner.log('Testing request body eviction');
+  await SendAndReportRequest('POST', 'new Uint8Array(512)');
+  await ReportRequest(most_recent_request);
   testRunner.completeTest();
 })
diff --git a/third_party/WebKit/Source/core/inspector/NetworkResourcesData.cpp b/third_party/WebKit/Source/core/inspector/NetworkResourcesData.cpp
index 597c0be..332f342 100644
--- a/third_party/WebKit/Source/core/inspector/NetworkResourcesData.cpp
+++ b/third_party/WebKit/Source/core/inspector/NetworkResourcesData.cpp
@@ -116,6 +116,11 @@
     result = content_.CharactersSizeInBytes();
     content_ = String();
   }
+
+  if (post_data_ && post_data_->SizeInBytes()) {
+    result += post_data_->SizeInBytes();
+    post_data_ = nullptr;
+  }
   return result;
 }
 
@@ -435,8 +440,7 @@
   ResourceData* resource_data = ResourceDataForRequestId(request_id);
   if (!resource_data)
     return;
-  if (resource_data->HasContent() || resource_data->HasData())
-    content_size_ -= resource_data->EvictContent();
+  content_size_ -= resource_data->EvictContent();
   request_id_to_resource_data_map_.erase(request_id);
 }