[Beacon] Update sendBeacon to use the CachedResourceLoader
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Aug 2017 23:09:24 +0000 (23:09 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Aug 2017 23:09:24 +0000 (23:09 +0000)
https://bugs.webkit.org/show_bug.cgi?id=175192
<rdar://problem/33725923>

Reviewed by Youenn Fablet.

LayoutTests/imported/w3c:

Rebaseline test as our Content-Type header has changed for ArrayBuffer / ArrayBufferView
payloads.

* web-platform-tests/beacon/headers/header-content-type-expected.txt:

Source/WebCore:

Update sendBeacon to use the FetchRequest / CachedResourceLoader instead of
the PingLoader. This gets us closer to the specification which is based on
Fetch and reduces code duplication. This also fixes an issue where our
Origin header was not properly set on Beacon resquests.

In a follow-up, we will implement in CachedResourceLoader Fetch's quota for
inflight keepalive requests which is needed to fully support sendBeacon().

* Modules/beacon/NavigatorBeacon.cpp:
(WebCore::NavigatorBeacon::sendBeacon):
* Modules/beacon/NavigatorBeacon.h:
* loader/LinkLoader.cpp:
(WebCore::createLinkPreloadResourceClient):
* loader/PingLoader.cpp:
* loader/PingLoader.h:
* loader/ResourceLoadInfo.cpp:
(WebCore::toResourceType):
* loader/SubresourceLoader.cpp:
(WebCore::logResourceLoaded):
* loader/cache/CachedResource.cpp:
(WebCore::CachedResource::defaultPriorityForResourceType):
(WebCore::CachedResource::load):
* loader/cache/CachedResource.h:
* loader/cache/CachedResourceLoader.cpp:
(WebCore::createResource):
(WebCore::CachedResourceLoader::requestBeaconResource):
(WebCore::contentTypeFromResourceType):
(WebCore::CachedResourceLoader::checkInsecureContent const):
(WebCore::CachedResourceLoader::allowedByContentSecurityPolicy const):
(WebCore::isResourceSuitableForDirectReuse):
* loader/cache/CachedResourceLoader.h:

Source/WebKit:

Deal with new Beacon CachedResource type.

* WebProcess/Network/WebLoaderStrategy.cpp:
(WebKit::maximumBufferingTime):

LayoutTests:

Rebaseline a few tests now that the Origin header is properly set of our Beacon
requests. This is a progression and matches the results from Blink.

Our Content-Type header for ArrayBuffer / ArrayBufferView payloads has also
changed. It is unclear which one is best but at least we are now consistent
with Fetch.

* http/tests/blink/sendbeacon/beacon-cookie-expected.txt:
* http/tests/blink/sendbeacon/beacon-cross-origin-expected.txt:
* http/tests/blink/sendbeacon/beacon-same-origin-expected.txt:
* http/wpt/beacon/headers/header-content-type-same-origin.html:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@220303 268f45cc-cd09-0410-ab3c-d52691b4dbfc

21 files changed:
LayoutTests/ChangeLog
LayoutTests/http/tests/blink/sendbeacon/beacon-cookie-expected.txt
LayoutTests/http/tests/blink/sendbeacon/beacon-cross-origin-expected.txt
LayoutTests/http/tests/blink/sendbeacon/beacon-same-origin-expected.txt
LayoutTests/http/wpt/beacon/headers/header-content-type-same-origin.html
LayoutTests/imported/w3c/ChangeLog
LayoutTests/imported/w3c/web-platform-tests/beacon/headers/header-content-type-expected.txt
Source/WebCore/ChangeLog
Source/WebCore/Modules/beacon/NavigatorBeacon.cpp
Source/WebCore/Modules/beacon/NavigatorBeacon.h
Source/WebCore/loader/LinkLoader.cpp
Source/WebCore/loader/PingLoader.cpp
Source/WebCore/loader/PingLoader.h
Source/WebCore/loader/ResourceLoadInfo.cpp
Source/WebCore/loader/SubresourceLoader.cpp
Source/WebCore/loader/cache/CachedResource.cpp
Source/WebCore/loader/cache/CachedResource.h
Source/WebCore/loader/cache/CachedResourceLoader.cpp
Source/WebCore/loader/cache/CachedResourceLoader.h
Source/WebKit/ChangeLog
Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp

index 141d57a3d18a21f67ca99fd0731880a9f1f24162..7a96ed6ad7f72e173d430bbcb7e6cbaa799d047e 100644 (file)
@@ -1,3 +1,23 @@
+2017-08-04  Chris Dumez  <cdumez@apple.com>
+
+        [Beacon] Update sendBeacon to use the CachedResourceLoader
+        https://bugs.webkit.org/show_bug.cgi?id=175192
+        <rdar://problem/33725923>
+
+        Reviewed by Youenn Fablet.
+
+        Rebaseline a few tests now that the Origin header is properly set of our Beacon
+        requests. This is a progression and matches the results from Blink.
+
+        Our Content-Type header for ArrayBuffer / ArrayBufferView payloads has also
+        changed. It is unclear which one is best but at least we are now consistent
+        with Fetch.
+
+        * http/tests/blink/sendbeacon/beacon-cookie-expected.txt:
+        * http/tests/blink/sendbeacon/beacon-cross-origin-expected.txt:
+        * http/tests/blink/sendbeacon/beacon-same-origin-expected.txt:
+        * http/wpt/beacon/headers/header-content-type-same-origin.html:
+
 2017-08-04  John Wilander  <wilander@apple.com>
 
         Resource Load Statistics: Report user interaction immediately, but only when needed
index b35b4571f0f6c711d6f1dac81e41211c2b0271fa..f1afd7ff1e3f49c9fece1aa79883fdc286f8e876 100644 (file)
@@ -7,7 +7,7 @@ PASS navigator.sendBeacon("resources/save-beacon.php?name=cookie", "Blip"); is t
 PASS Beacon sent successfully
 PASS Content-Type: text/plain;charset=UTF-8
 PASS Cookie: hello=world
-PASS Origin: null
+PASS Origin: http://127.0.0.1:8000
 PASS Referer: http://127.0.0.1:8000/blink/sendbeacon/beacon-cookie.html
 PASS Request-Method: POST
 PASS Length: 4
index 774e1a61661a27ae48f779c2fc94cca92e299a84..76c01a5e9bca57e12e170d3c9dbfe22c104cf2c3 100644 (file)
@@ -6,7 +6,7 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE
 PASS navigator.sendBeacon("http://localhost:8000/blink/sendbeacon/resources/save-beacon.php?name=cross-origin", "CrossOrigin"); is true
 PASS Beacon sent successfully
 PASS Content-Type: text/plain;charset=UTF-8
-PASS Origin: null
+PASS Origin: http://127.0.0.1:8000
 PASS Referer: http://127.0.0.1:8000/blink/sendbeacon/beacon-cross-origin.html
 PASS Request-Method: POST
 PASS Length: 11
index b2e1e803f29c2a082a1543a71c418d40a98a855d..f37bc1a7009bcda63bf5e8373b2eb4e6f0901c7f 100644 (file)
@@ -7,7 +7,7 @@ Sending beacon with type: [object String]
 PASS navigator.sendBeacon("resources/save-beacon.php?name=same-origin", payload); is true
 PASS Beacon sent successfully
 PASS Content-Type: text/plain;charset=UTF-8
-PASS Origin: null
+PASS Origin: http://127.0.0.1:8000
 PASS Referer: http://127.0.0.1:8000/blink/sendbeacon/beacon-same-origin.html
 PASS Request-Method: POST
 PASS Length: 10
@@ -16,8 +16,8 @@ PASS
 Sending beacon with type: [object Uint32Array]
 PASS navigator.sendBeacon("resources/save-beacon.php?name=same-origin", payload); is true
 PASS Beacon sent successfully
-PASS Content-Type: application/octet-stream
-PASS Origin: null
+PASS Content-Type: application/x-www-form-urlencoded
+PASS Origin: http://127.0.0.1:8000
 PASS Referer: http://127.0.0.1:8000/blink/sendbeacon/beacon-same-origin.html
 PASS Request-Method: POST
 PASS Length: 40
@@ -27,7 +27,7 @@ Sending beacon with type: [object Blob]
 PASS navigator.sendBeacon("resources/save-beacon.php?name=same-origin", payload); is true
 PASS Beacon sent successfully
 PASS Content-Type: text/plain;from-beacon=true
-PASS Origin: null
+PASS Origin: http://127.0.0.1:8000
 PASS Referer: http://127.0.0.1:8000/blink/sendbeacon/beacon-same-origin.html
 PASS Request-Method: POST
 PASS Length: 11
@@ -37,7 +37,7 @@ Sending beacon with type: [object FormData]
 PASS navigator.sendBeacon("resources/save-beacon.php?name=same-origin", payload); is true
 PASS Beacon sent successfully
 PASS Content-Type: multipart/form-data;
-PASS Origin: null
+PASS Origin: http://127.0.0.1:8000
 PASS Referer: http://127.0.0.1:8000/blink/sendbeacon/beacon-same-origin.html
 PASS Request-Method: POST
 PASS Length: 9
@@ -47,7 +47,7 @@ Sending beacon with type: [object URLSearchParams]
 PASS navigator.sendBeacon("resources/save-beacon.php?name=same-origin", payload); is true
 PASS Beacon sent successfully
 PASS Content-Type: application/x-www-form-urlencoded;charset=UTF-8
-PASS Origin: null
+PASS Origin: http://127.0.0.1:8000
 PASS Referer: http://127.0.0.1:8000/blink/sendbeacon/beacon-same-origin.html
 PASS Request-Method: POST
 PASS Length: 7
index e24c2e7f17c59f6dc7b4a90bb3aa122181f58d3f..4946e2a4c5994fddad331921ba33efc8cf595daa 100644 (file)
@@ -77,8 +77,8 @@ function stringToURLSearchParams(input)
 }
 
 testContentTypeHeader("hi!", "text/plain;charset=UTF-8", "string");
-testContentTypeHeader(stringToArrayBufferView("123"), "application/octet-stream", "ArrayBufferView"); // Specification says no content-type but browsers seem to use "application/octet-stream".
-testContentTypeHeader(stringToArrayBuffer("123"), "application/octet-stream", "ArrayBuffer"); // Specification says no content-type but browsers seem to use "application/octet-stream".
+testContentTypeHeader(stringToArrayBufferView("123"), "application/x-www-form-urlencoded", "ArrayBufferView"); // FIXME: It is unclear what the Content-Type should be here or if it should be present at all.
+testContentTypeHeader(stringToArrayBuffer("123"), "application/x-www-form-urlencoded", "ArrayBuffer"); // FIXME: It is unclear what the Content-Type should be here or if it should be present at all.
 testContentTypeHeader(stringToBlob("123"), "text/plain", "Blob");
 testContentTypeHeader(stringToFormData("qwerty"), "multipart/form-data", "FormData");
 testContentTypeHeader(stringToURLSearchParams("key1=value1&key2=value2"), "application/x-www-form-urlencoded;charset=UTF-8", "URLSearchParams");
index 93789af87f0396cf2587e161576cd37276565640..63b1df7a28cfaf352357c02123864a73a8c3ce91 100644 (file)
@@ -1,3 +1,16 @@
+2017-08-04  Chris Dumez  <cdumez@apple.com>
+
+        [Beacon] Update sendBeacon to use the CachedResourceLoader
+        https://bugs.webkit.org/show_bug.cgi?id=175192
+        <rdar://problem/33725923>
+
+        Reviewed by Youenn Fablet.
+
+        Rebaseline test as our Content-Type header has changed for ArrayBuffer / ArrayBufferView
+        payloads.
+
+        * web-platform-tests/beacon/headers/header-content-type-expected.txt:
+
 2017-08-04  Chris Dumez  <cdumez@apple.com>
 
         Match newly-clarified spec on textarea defaultValue/value/child text content
index ed48fe4d790128779c32146dbd776f62dcf87c08..27352c5fb7aeb2c53608917d2a47f108c0529fed 100644 (file)
@@ -1,6 +1,5 @@
-CONSOLE MESSAGE: line 35: This requests requires a CORS preflight but this is not supported yet.
 
 PASS Test content-type header for a body string 
-FAIL Test content-type header for a body ArrayBuffer assert_true: SendBeacon Succeeded expected true got false
+PASS Test content-type header for a body ArrayBuffer 
 PASS Test content-type header for a body FormData 
 
index a198e4f00a3216f0747066785127433c839a84a0..84ecf3cd1c1fb41302abebf5b9f56650ee86d35a 100644 (file)
@@ -1,3 +1,43 @@
+2017-08-04  Chris Dumez  <cdumez@apple.com>
+
+        [Beacon] Update sendBeacon to use the CachedResourceLoader
+        https://bugs.webkit.org/show_bug.cgi?id=175192
+        <rdar://problem/33725923>
+
+        Reviewed by Youenn Fablet.
+
+        Update sendBeacon to use the FetchRequest / CachedResourceLoader instead of
+        the PingLoader. This gets us closer to the specification which is based on
+        Fetch and reduces code duplication. This also fixes an issue where our
+        Origin header was not properly set on Beacon resquests.
+
+        In a follow-up, we will implement in CachedResourceLoader Fetch's quota for
+        inflight keepalive requests which is needed to fully support sendBeacon().
+
+        * Modules/beacon/NavigatorBeacon.cpp:
+        (WebCore::NavigatorBeacon::sendBeacon):
+        * Modules/beacon/NavigatorBeacon.h:
+        * loader/LinkLoader.cpp:
+        (WebCore::createLinkPreloadResourceClient):
+        * loader/PingLoader.cpp:
+        * loader/PingLoader.h:
+        * loader/ResourceLoadInfo.cpp:
+        (WebCore::toResourceType):
+        * loader/SubresourceLoader.cpp:
+        (WebCore::logResourceLoaded):
+        * loader/cache/CachedResource.cpp:
+        (WebCore::CachedResource::defaultPriorityForResourceType):
+        (WebCore::CachedResource::load):
+        * loader/cache/CachedResource.h:
+        * loader/cache/CachedResourceLoader.cpp:
+        (WebCore::createResource):
+        (WebCore::CachedResourceLoader::requestBeaconResource):
+        (WebCore::contentTypeFromResourceType):
+        (WebCore::CachedResourceLoader::checkInsecureContent const):
+        (WebCore::CachedResourceLoader::allowedByContentSecurityPolicy const):
+        (WebCore::isResourceSuitableForDirectReuse):
+        * loader/cache/CachedResourceLoader.h:
+
 2017-08-04  John Wilander  <wilander@apple.com>
 
         Resource Load Statistics: Report user interaction immediately, but only when needed
index 29f7b7ce555ad1a281a73d4704b3589593d0d167..7208bcc81d48a56155fc1238b72c72a1378be1f2 100644 (file)
 #include "config.h"
 #include "NavigatorBeacon.h"
 
+#include "CachedResourceLoader.h"
 #include "Document.h"
+#include "FetchRequest.h"
 #include "Navigator.h"
 #include "URL.h"
+#include <runtime/JSCJSValue.h>
 
 namespace WebCore {
 
-ExceptionOr<bool> NavigatorBeacon::sendBeacon(Navigator&, Document& document, const String& url, std::optional<BodyInit>&& data)
+ExceptionOr<bool> NavigatorBeacon::sendBeacon(Navigator&, Document& document, const String& url, std::optional<FetchBody::Init>&& body)
 {
     URL parsedUrl = document.completeURL(url);
 
@@ -43,11 +46,31 @@ ExceptionOr<bool> NavigatorBeacon::sendBeacon(Navigator&, Document& document, co
     if (!parsedUrl.protocolIsInHTTPFamily())
         return Exception { TypeError, ASCIILiteral("Beacons can only be sent over HTTP(S)") };
 
-    auto* frame = document.frame();
-    if (!frame)
+    if (!document.frame())
         return false;
 
-    return PingLoader::sendBeacon(*frame, document, parsedUrl, WTFMove(data));
+    auto& contentSecurityPolicy = *document.contentSecurityPolicy();
+    if (!document.shouldBypassMainWorldContentSecurityPolicy() && !contentSecurityPolicy.allowConnectToSource(parsedUrl)) {
+        // We simulate a network error so we return true here. This is consistent with Blink.
+        return true;
+    }
+
+    FetchRequestInit init;
+    init.method = ASCIILiteral("POST");
+    init.body = WTFMove(body);
+    init.credentials = FetchOptions::Credentials::Include;
+    init.cache = FetchOptions::Cache::NoCache;
+    init.redirect = FetchOptions::Redirect::Follow;
+    init.keepalive = true;
+    init.window = JSC::jsNull();
+
+    auto fetchRequestResult = FetchRequest::create(document, parsedUrl.string(), WTFMove(init));
+    if (fetchRequestResult.hasException())
+        return fetchRequestResult.releaseException();
+
+    auto fetchRequest = fetchRequestResult.releaseReturnValue();
+    document.cachedResourceLoader().requestBeaconResource({ fetchRequest->internalRequest(), fetchRequest->fetchOptions() });
+    return true;
 }
 
 }
index d9cb3e5097893997df984634edada6ae83744fa1..6fdf89dd5aefe36cfa2ab0751e9838a4a84a668d 100644 (file)
@@ -26,7 +26,7 @@
 #pragma once
 
 #include "ExceptionOr.h"
-#include "PingLoader.h"
+#include "FetchBody.h"
 #include <wtf/Forward.h>
 
 namespace WebCore {
@@ -36,7 +36,7 @@ class Navigator;
 
 class NavigatorBeacon {
 public:
-    static ExceptionOr<bool> sendBeacon(Navigator&, Document&, const String& url, std::optional<BodyInit>&&);
+    static ExceptionOr<bool> sendBeacon(Navigator&, Document&, const String& url, std::optional<FetchBody::Init>&&);
 };
 
 }
index 7542bb72b308d91360bc009f762701b846956ef3..6e2d4aedc8c8d4f7eeb82482d44ee6614dfc2335 100644 (file)
@@ -162,6 +162,7 @@ static std::unique_ptr<LinkPreloadResourceClient> createLinkPreloadResourceClien
 #if ENABLE(XSLT)
     case CachedResource::XSLStyleSheet:
 #endif
+    case CachedResource::Beacon:
 #if ENABLE(LINK_PREFETCH)
     case CachedResource::LinkSubresource:
     case CachedResource::LinkPrefetch:
index 104c3aa8a92bc08abb62d5cefb81d56a35682b74..865eb1cdb8052d1638634575315b8cc3bba92772 100644 (file)
 #include "config.h"
 #include "PingLoader.h"
 
-#include "Blob.h"
 #include "ContentSecurityPolicy.h"
-#include "DOMFormData.h"
 #include "Document.h"
-#include "FormData.h"
 #include "Frame.h"
 #include "FrameLoader.h"
 #include "FrameLoaderClient.h"
-#include "HTTPHeaderNames.h"
-#include "HTTPHeaderValues.h"
-#include "HTTPParsers.h"
 #include "InspectorInstrumentation.h"
 #include "LoaderStrategy.h"
 #include "Page.h"
-#include "ParsedContentType.h"
 #include "PlatformStrategies.h"
 #include "ProgressTracker.h"
 #include "ResourceHandle.h"
 #include "ResourceResponse.h"
 #include "SecurityOrigin.h"
 #include "SecurityPolicy.h"
-#include "URLSearchParams.h"
 #include "UserContentController.h"
-#include <runtime/ArrayBuffer.h>
-#include <runtime/ArrayBufferView.h>
-#include <runtime/JSCInlines.h>
 #include <wtf/text/CString.h>
 
 namespace WebCore {
@@ -190,87 +179,6 @@ void PingLoader::sendViolationReport(Frame& frame, const URL& reportURL, Ref<For
     startPingLoad(frame, request, ShouldFollowRedirects::No);
 }
 
-bool PingLoader::sendBeacon(Frame& frame, Document& document, const URL& url, std::optional<BodyInit>&& data)
-{
-    ResourceRequest request(url);
-    if (processContentExtensionRulesForLoad(frame, request, ResourceType::Raw))
-        return false;
-
-    auto& contentSecurityPolicy = *document.contentSecurityPolicy();
-    if (!document.shouldBypassMainWorldContentSecurityPolicy() && !contentSecurityPolicy.allowConnectToSource(url)) {
-        // We simulate a network error so we return true here. This is consistent with Blink.
-        return true;
-    }
-
-    bool noCors = true;
-    request.setHTTPMethod(ASCIILiteral("POST"));
-    // FIXME: We should restrict the size of payloads.
-    if (data) {
-        String mimeType;
-        WTF::switchOn(data.value(),
-            [&] (RefPtr<Blob>& blob) {
-                auto& blobType = blob->type();
-                if (!blobType.isEmpty() && isValidContentType(blobType))
-                    mimeType = blobType;
-                else
-                    mimeType = ASCIILiteral("application/octet-stream");
-                auto formData = FormData::create();
-                formData->appendBlob(blob->url());
-                request.setHTTPBody(WTFMove(formData));
-            },
-            [&] (RefPtr<JSC::ArrayBuffer>& buffer) {
-                mimeType = ASCIILiteral("application/octet-stream");
-                request.setHTTPBody(FormData::create(buffer->data(), buffer->byteLength()));
-            },
-            [&] (RefPtr<JSC::ArrayBufferView>& buffer) {
-                mimeType = ASCIILiteral("application/octet-stream");
-                request.setHTTPBody(FormData::create(buffer->baseAddress(), buffer->byteLength()));
-            },
-            [&] (RefPtr<DOMFormData>& domFormData) {
-                auto formData = FormData::createMultiPart(*domFormData, domFormData->encoding(), &document);
-                formData->generateFiles(&document);
-                mimeType = makeString("multipart/form-data; boundary=", formData->boundary().data());
-                request.setHTTPBody(WTFMove(formData));
-            },
-            [&] (RefPtr<URLSearchParams>& searchParams) {
-                mimeType = HTTPHeaderValues::formURLEncodedContentType();
-                request.setHTTPBody(FormData::create(searchParams->toString().utf8()));
-            },
-            [&] (String& string) {
-                mimeType = HTTPHeaderValues::textPlainContentType();
-                request.setHTTPBody(FormData::create(string.utf8()));
-            }
-        );
-        noCors = false;
-        if (!mimeType.isEmpty()) {
-            // If mimeType value is a CORS-safelisted request-header value for the Content-Type header, set corsMode to "no-cors".
-            if (isCrossOriginSafeRequestHeader(HTTPHeaderName::ContentType, mimeType))
-                noCors = true;
-            request.setHTTPContentType(mimeType);
-        }
-    }
-    request.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0");
-    frame.loader().addExtraFieldsToSubresourceRequest(request);
-
-    auto& sourceOrigin = document.securityOrigin();
-    bool isCrossOriginRequest = !sourceOrigin.canRequest(url);
-
-    // FIXME: We are supposed to do a preflight in this case but this is not supported yet.
-    if (isCrossOriginRequest && !noCors) {
-        document.addConsoleMessage(MessageSource::Security, MessageLevel::Error, ASCIILiteral("This requests requires a CORS preflight but this is not supported yet."));
-        return false;
-    }
-
-    FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin.toString());
-    String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), url, frame.loader().outgoingReferrer());
-    if (!referrer.isEmpty())
-        request.setHTTPReferrer(referrer);
-
-    request.setAllowCookies(true); // Credentials mode: include.
-    startPingLoad(frame, request, ShouldFollowRedirects::Yes);
-    return true;
-}
-
 void PingLoader::startPingLoad(Frame& frame, ResourceRequest& request, ShouldFollowRedirects shouldFollowRedirects)
 {
     unsigned long identifier = frame.page()->progress().createUniqueIdentifier();
index 6ab5ca37f277e12baa689e0269d43b01e6ac22d8..b7937de209bb24d82e227cb33eb48f711c29b2af 100644 (file)
 
 #include <wtf/Forward.h>
 #include <wtf/Ref.h>
-#include <wtf/Variant.h>
-
-namespace JSC {
-
-class ArrayBuffer;
-class ArrayBufferView;
-
-}
 
 namespace WebCore {
 
-class Blob;
-class DOMFormData;
-class Document;
 class FormData;
 class Frame;
 class URL;
-class URLSearchParams;
 class ResourceRequest;
 
 enum class ViolationReportType {
@@ -59,12 +47,9 @@ enum class ViolationReportType {
     XSSAuditor,
 };
 
-using BodyInit = Variant<RefPtr<Blob>, RefPtr<JSC::ArrayBufferView>, RefPtr<JSC::ArrayBuffer>, RefPtr<DOMFormData>, RefPtr<URLSearchParams>, String>;
-
 class PingLoader {
 public:
     static void loadImage(Frame&, const URL&);
-    static bool sendBeacon(Frame&, Document&, const URL&, std::optional<BodyInit>&&);
     static void sendPing(Frame&, const URL& pingURL, const URL& destinationURL);
     static void sendViolationReport(Frame&, const URL& reportURL, Ref<FormData>&& report, ViolationReportType);
 
index 027b3f7dc665db3f9a612efecab7626313b73428..13b2ae100f48ebf274b88901d184822e7d1893e2 100644 (file)
@@ -55,6 +55,7 @@ ResourceType toResourceType(CachedResource::Type type)
 #endif
         return ResourceType::Font;
 
+    case CachedResource::Beacon:
     case CachedResource::MediaResource:
     case CachedResource::Icon:
     case CachedResource::RawResource:
@@ -70,6 +71,7 @@ ResourceType toResourceType(CachedResource::Type type)
         ASSERT_NOT_REACHED();
 #endif
     };
+    return ResourceType::Raw;
 }
 
 uint16_t readResourceType(const String& name)
index 07fb52ea3f8ba72708952e157cffb05651f3c70f..8910fab16dedffbdf6c48c74e120025470d44089 100644 (file)
@@ -447,6 +447,9 @@ static void logResourceLoaded(Frame* frame, CachedResource::Type type)
 #endif
         resourceType = DiagnosticLoggingKeys::fontKey();
         break;
+    case CachedResource::Beacon:
+        ASSERT_NOT_REACHED();
+        break;
     case CachedResource::MediaResource:
     case CachedResource::Icon:
     case CachedResource::RawResource:
index d8d68fa6b5ec36353887f6006df867801be12ac7..4b442f658bda966976e8ea7ec8878ff0aedfd7dc 100644 (file)
@@ -88,6 +88,8 @@ ResourceLoadPriority CachedResource::defaultPriorityForResourceType(Type type)
 #endif
     case CachedResource::SVGDocumentResource:
         return ResourceLoadPriority::Low;
+    case CachedResource::Beacon:
+        return ResourceLoadPriority::VeryLow;
 #if ENABLE(LINK_PREFETCH)
     case CachedResource::LinkPrefetch:
         return ResourceLoadPriority::VeryLow;
@@ -257,6 +259,12 @@ void CachedResource::load(CachedResourceLoader& cachedResourceLoader)
         m_fragmentIdentifierForRequest = String();
     }
 
+    // FIXME: We should not special-case Beacon here.
+    if (m_options.keepAlive && type() == CachedResource::Beacon) {
+        platformStrategies()->loaderStrategy()->createPingHandle(frame.loader().networkingContext(), request, m_options.credentials == FetchOptions::Credentials::Include, m_options.redirect == FetchOptions::Redirect::Follow);
+        return;
+    }
+
     m_loader = platformStrategies()->loaderStrategy()->loadResource(frame, *this, request, m_options);
     if (!m_loader) {
         RELEASE_LOG_IF_ALLOWED("load: Unable to create SubresourceLoader (frame = %p)", &frame);
index 242ffaca27dbb52192ffa701ce23c39a9e2cd74b..8ba98e596825b1798c8c32f352ac76e7f4d4889d 100644 (file)
@@ -72,6 +72,7 @@ public:
         MediaResource,
         RawResource,
         Icon,
+        Beacon,
         SVGDocumentResource
 #if ENABLE(XSLT)
         , XSLStyleSheet
index fb3489b0fc034abd0b1b7d1a822fffcea0a79678..830ce51cb4731f877b60474e6e329076248744e3 100644 (file)
@@ -108,6 +108,8 @@ static CachedResource* createResource(CachedResource::Type type, CachedResourceR
     case CachedResource::Icon:
     case CachedResource::MainResource:
         return new CachedRawResource(WTFMove(request), type, sessionID);
+    case CachedResource::Beacon:
+        return new CachedResource(WTFMove(request), CachedResource::Beacon, sessionID);
 #if ENABLE(XSLT)
     case CachedResource::XSLStyleSheet:
         return new CachedXSLStyleSheet(WTFMove(request), sessionID);
@@ -287,6 +289,11 @@ CachedResourceHandle<CachedRawResource> CachedResourceLoader::requestRawResource
     return downcast<CachedRawResource>(requestResource(CachedResource::RawResource, WTFMove(request)).get());
 }
 
+CachedResourceHandle<CachedResource> CachedResourceLoader::requestBeaconResource(CachedResourceRequest&& request)
+{
+    return requestResource(CachedResource::Beacon, WTFMove(request)).get();
+}
+
 CachedResourceHandle<CachedRawResource> CachedResourceLoader::requestMainResource(CachedResourceRequest&& request)
 {
     return downcast<CachedRawResource>(requestResource(CachedResource::MainResource, WTFMove(request)).get());
@@ -312,6 +319,7 @@ static MixedContentChecker::ContentType contentTypeFromResourceType(CachedResour
         return MixedContentChecker::ContentType::Active;
 #endif
 
+    case CachedResource::Beacon:
     case CachedResource::RawResource:
     case CachedResource::Icon:
     case CachedResource::SVGDocumentResource:
@@ -381,6 +389,7 @@ bool CachedResourceLoader::checkInsecureContent(CachedResource::Type type, const
         break;
     }
     case CachedResource::MainResource:
+    case CachedResource::Beacon:
 #if ENABLE(LINK_PREFETCH)
     case CachedResource::LinkPrefetch:
     case CachedResource::LinkSubresource:
@@ -431,6 +440,7 @@ bool CachedResourceLoader::allowedByContentSecurityPolicy(CachedResource::Type t
         if (!m_document->contentSecurityPolicy()->allowMediaFromSource(url, redirectResponseReceived))
             return false;
         break;
+    case CachedResource::Beacon:
     case CachedResource::RawResource:
         return true;
     default:
@@ -626,6 +636,9 @@ static inline bool isResourceSuitableForDirectReuse(const CachedResource& resour
     if (resource.type() == CachedResource::Type::RawResource || resource.type() == CachedResource::Type::MediaResource)
         return false;
 
+    if (resource.type() == CachedResource::Beacon)
+        return false;
+
     return true;
 }
 
index 9e2ccf53dda794cc2d548abb73c6405567d047f9..71a5b88b7e0037a545e50ccce94e8cde7af7342f 100644 (file)
@@ -78,6 +78,7 @@ public:
     CachedResourceHandle<CachedFont> requestFont(CachedResourceRequest&&, bool isSVG);
     CachedResourceHandle<CachedRawResource> requestMedia(CachedResourceRequest&&);
     CachedResourceHandle<CachedRawResource> requestIcon(CachedResourceRequest&&);
+    CachedResourceHandle<CachedResource> requestBeaconResource(CachedResourceRequest&&);
     CachedResourceHandle<CachedRawResource> requestRawResource(CachedResourceRequest&&);
     CachedResourceHandle<CachedRawResource> requestMainResource(CachedResourceRequest&&);
     CachedResourceHandle<CachedSVGDocument> requestSVGDocument(CachedResourceRequest&&);
index 18f0715217f9d5fec4dc50a394b8330f14cbc4b3..242b4137c1d502a2201a02b8e072382149eb333b 100644 (file)
@@ -1,3 +1,16 @@
+2017-08-04  Chris Dumez  <cdumez@apple.com>
+
+        [Beacon] Update sendBeacon to use the CachedResourceLoader
+        https://bugs.webkit.org/show_bug.cgi?id=175192
+        <rdar://problem/33725923>
+
+        Reviewed by Youenn Fablet.
+
+        Deal with new Beacon CachedResource type.
+
+        * WebProcess/Network/WebLoaderStrategy.cpp:
+        (WebKit::maximumBufferingTime):
+
 2017-08-04  John Wilander  <wilander@apple.com>
 
         Resource Load Statistics: Report user interaction immediately, but only when needed
index 63fe8b5046e4644aa64839777179d4ee1e729e0e..20253cc3e5a4f9e4ba4927b0887fb92692f8dc30 100644 (file)
@@ -105,6 +105,7 @@ static Seconds maximumBufferingTime(CachedResource* resource)
         return 0_s;
 
     switch (resource->type()) {
+    case CachedResource::Beacon:
     case CachedResource::CSSStyleSheet:
     case CachedResource::Script:
 #if ENABLE(SVG_FONTS)