Commit 19947f44 authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Background Fetch] Extract Request body blobs.

This was previously submitted as cl/1160848
This is the first step in supporting uploads for Background Fetch

Bug: 774054
Change-Id: I325302b77313b38f211cb035522f80308cc83e14
Reviewed-on: https://chromium-review.googlesource.com/c/1297973
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604635}
parent f86f9f4a
......@@ -4,12 +4,15 @@
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_manager.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_macros.h"
#include "third_party/blink/public/platform/modules/service_worker/web_service_worker_request.h"
#include "third_party/blink/renderer/bindings/core/v8/request_or_usv_string.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/modules/v8/request_or_usv_string_or_request_or_usv_string_sequence.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/fetch/body.h"
#include "third_party/blink/renderer/core/fetch/body_stream_buffer.h"
#include "third_party/blink/renderer/core/fetch/request.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy.h"
#include "third_party/blink/renderer/core/frame/deprecation.h"
......@@ -23,6 +26,7 @@
#include "third_party/blink/renderer/platform/bindings/exception_code.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/bindings/v8_throw_exception.h"
#include "third_party/blink/renderer/platform/blob/blob_data.h"
#include "third_party/blink/renderer/platform/loader/cors/cors.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_utils.h"
#include "third_party/blink/renderer/platform/network/network_utils.h"
......@@ -116,6 +120,37 @@ bool ShouldBlockGateWayAttacks(ExecutionContext* execution_context,
return false;
}
scoped_refptr<BlobDataHandle> ExtractBlobHandle(
Request* request,
ExceptionState& exception_state) {
DCHECK(request);
if (!RuntimeEnabledFeatures::BackgroundFetchUploadsEnabled())
return nullptr;
if (request->IsBodyLocked(exception_state) == Body::BodyLocked::kLocked ||
request->IsBodyUsed(exception_state) == Body::BodyUsed::kUsed) {
DCHECK(!exception_state.HadException());
exception_state.ThrowTypeError("Request body is already used");
return nullptr;
}
if (exception_state.HadException())
return nullptr;
BodyStreamBuffer* buffer = request->BodyBuffer();
if (!buffer)
return nullptr;
auto blob_handle = buffer->DrainAsBlobDataHandle(
BytesConsumer::BlobSizePolicy::kDisallowBlobWithInvalidSize,
exception_state);
if (exception_state.HadException())
return nullptr;
return blob_handle;
}
} // namespace
BackgroundFetchManager::BackgroundFetchManager(
......@@ -414,6 +449,10 @@ Vector<WebServiceWorkerRequest> BackgroundFetchManager::CreateWebRequestVector(
// TODO(crbug.com/774054): Set blob data handle when adding support for
// requests with body.
request->PopulateWebServiceWorkerRequest(web_requests[i]);
web_requests[i].SetBlobDataHandle(
ExtractBlobHandle(request, exception_state));
if (exception_state.HadException())
return Vector<WebServiceWorkerRequest>();
}
} else if (requests.IsRequest()) {
auto* request = requests.GetAsRequest();
......@@ -425,6 +464,10 @@ Vector<WebServiceWorkerRequest> BackgroundFetchManager::CreateWebRequestVector(
*has_requests_with_body = request->HasBody();
web_requests.resize(1);
request->PopulateWebServiceWorkerRequest(web_requests[0]);
web_requests[0].SetBlobDataHandle(
ExtractBlobHandle(requests.GetAsRequest(), exception_state));
if (exception_state.HadException())
return Vector<WebServiceWorkerRequest>();
} else if (requests.IsUSVString()) {
Request* request = Request::Create(script_state, requests.GetAsUSVString(),
exception_state);
......
......@@ -14,6 +14,7 @@
#include "third_party/blink/renderer/core/fetch/request_init.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/blob/blob_data.h"
namespace blink {
......@@ -177,4 +178,48 @@ TEST_F(BackgroundFetchManagerTest, SequenceWithNullValue) {
ESErrorType::kTypeError);
}
TEST_F(BackgroundFetchManagerTest, BlobsExtracted) {
V8TestingScope scope;
KURL image_url("https://www.example.com/my_image.png");
KURL icon_url("https://www.example.com/my_icon.jpg");
// Create first request with a body.
String body_text = "cat_pic";
RequestInit* request_init = RequestInit::Create();
request_init->setMethod("POST");
request_init->setBody(blink::ScriptValue(
scope.GetScriptState(), ToV8(body_text, scope.GetScriptState())));
Request* image_request =
Request::Create(scope.GetScriptState(), image_url.GetString(),
request_init, scope.GetExceptionState());
ASSERT_FALSE(scope.GetExceptionState().HadException());
ASSERT_TRUE(image_request);
ASSERT_TRUE(image_request->HasBody());
// Create second request without a body.
RequestOrUSVString icon_request =
RequestOrUSVString::FromUSVString(icon_url.GetString());
// Create a request sequence with both requests.
HeapVector<RequestOrUSVString> request_sequence;
request_sequence.push_back(RequestOrUSVString::FromRequest(image_request));
request_sequence.push_back(icon_request);
RequestOrUSVStringOrRequestOrUSVStringSequence requests =
RequestOrUSVStringOrRequestOrUSVStringSequence::
FromRequestOrUSVStringSequence(request_sequence);
// Extract the blobs.
auto web_requests = CreateWebRequestVector(scope, requests);
ASSERT_FALSE(scope.GetExceptionState().HadException());
ASSERT_EQ(web_requests.size(), 2u);
ASSERT_TRUE(web_requests[0].GetBlobDataHandle());
EXPECT_EQ(web_requests[0].GetBlobDataHandle()->size(), body_text.length());
EXPECT_FALSE(web_requests[1].GetBlobDataHandle());
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment