Commit 543f84f3 authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Background Fetch] Extract Request body blobs.

This is the first step in supporting uploads for Background Fetch

Bug: 774054
Change-Id: I13188181573fcd8464df0729a09190a42b5bd2d0
Reviewed-on: https://chromium-review.googlesource.com/1160848Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581349}
parent 5f4901a6
......@@ -4,11 +4,14 @@
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_manager.h"
#include "base/memory/scoped_refptr.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"
......@@ -22,6 +25,7 @@
#include "third_party/blink/renderer/modules/service_worker/service_worker_registration.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"
......@@ -156,6 +160,31 @@ bool ShouldBlockCORSPreflight(ExecutionContext* execution_context,
return false;
}
scoped_refptr<BlobDataHandle> ExtractBlobHandle(
Request* request,
ExceptionState& exception_state) {
DCHECK(request);
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;
}
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(
......@@ -383,11 +412,15 @@ Vector<WebServiceWorkerRequest> BackgroundFetchManager::CreateWebRequestVector(
DCHECK(request);
request->PopulateWebServiceWorkerRequest(web_requests[i]);
web_requests[i].SetBlobDataHandle(
ExtractBlobHandle(request, exception_state));
}
} else if (requests.IsRequest()) {
DCHECK(requests.GetAsRequest());
web_requests.resize(1);
requests.GetAsRequest()->PopulateWebServiceWorkerRequest(web_requests[0]);
web_requests[0].SetBlobDataHandle(
ExtractBlobHandle(requests.GetAsRequest(), exception_state));
} 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 {
......@@ -175,4 +176,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;
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