Changeset 220927 in webkit


Ignore:
Timestamp:
Aug 18, 2017 12:32:17 PM (7 years ago)
Author:
commit-queue@webkit.org
Message:

[Fetch API] Add support for a callback-based fetch
https://bugs.webkit.org/show_bug.cgi?id=175710

Patch by Youenn Fablet <youenn@apple.com> on 2017-08-18
Reviewed by Alex Christensen.

No change of behavior.

Moving from a DOMPromise to a Callback taking an ExceptionOr<FetchResponse&>.
Updating window and worker call sites.

  • Modules/fetch/DOMWindowFetch.cpp:

(WebCore::DOMWindowFetch::fetch):

  • Modules/fetch/FetchResponse.cpp:

(WebCore::FetchResponse::fetch):
(WebCore::FetchResponse::BodyLoader::didFail):
(WebCore::FetchResponse::BodyLoader::BodyLoader):
(WebCore::FetchResponse::BodyLoader::didReceiveResponse):
(WebCore::FetchResponse::BodyLoader::stop):

  • Modules/fetch/FetchResponse.h:
  • Modules/fetch/WorkerGlobalScopeFetch.cpp:

(WebCore::WorkerGlobalScopeFetch::fetch):

Location:
trunk/Source/WebCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r220924 r220927  
     12017-08-18  Youenn Fablet  <youenn@apple.com>
     2
     3        [Fetch API] Add support for a callback-based fetch
     4        https://bugs.webkit.org/show_bug.cgi?id=175710
     5
     6        Reviewed by Alex Christensen.
     7
     8        No change of behavior.
     9
     10        Moving from a DOMPromise to a Callback taking an ExceptionOr<FetchResponse&>.
     11        Updating window and worker call sites.
     12
     13        * Modules/fetch/DOMWindowFetch.cpp:
     14        (WebCore::DOMWindowFetch::fetch):
     15        * Modules/fetch/FetchResponse.cpp:
     16        (WebCore::FetchResponse::fetch):
     17        (WebCore::FetchResponse::BodyLoader::didFail):
     18        (WebCore::FetchResponse::BodyLoader::BodyLoader):
     19        (WebCore::FetchResponse::BodyLoader::didReceiveResponse):
     20        (WebCore::FetchResponse::BodyLoader::stop):
     21        * Modules/fetch/FetchResponse.h:
     22        * Modules/fetch/WorkerGlobalScopeFetch.cpp:
     23        (WebCore::WorkerGlobalScopeFetch::fetch):
     24
    1252017-08-18  Brady Eidson  <beidson@apple.com>
    226
  • trunk/Source/WebCore/Modules/fetch/DOMWindowFetch.cpp

    r220241 r220927  
    3434#include "Document.h"
    3535#include "FetchResponse.h"
     36#include "JSFetchResponse.h"
    3637
    3738namespace WebCore {
    3839
    39 void DOMWindowFetch::fetch(DOMWindow& window, FetchRequest::Info&& input, FetchRequest::Init&& init, Ref<DeferredPromise>&& promise)
     40using FetchResponsePromise = DOMPromiseDeferred<IDLInterface<FetchResponse>>;
     41
     42void DOMWindowFetch::fetch(DOMWindow& window, FetchRequest::Info&& input, FetchRequest::Init&& init, Ref<DeferredPromise>&& deferred)
    4043{
     44    FetchResponsePromise promise = WTFMove(deferred);
     45
    4146    auto* document = window.document();
    4247    if (!document) {
    43         promise->reject(InvalidStateError);
     48        promise.reject(InvalidStateError);
    4449        return;
    4550    }
     
    4752    auto request = FetchRequest::create(*document, WTFMove(input), WTFMove(init));
    4853    if (request.hasException()) {
    49         promise->reject(request.releaseException());
     54        promise.reject(request.releaseException());
    5055        return;
    5156    }
    5257
    53     FetchResponse::fetch(*document, request.releaseReturnValue().get(), WTFMove(promise));
     58    FetchResponse::fetch(*document, request.releaseReturnValue().get(), [promise = WTFMove(promise)](ExceptionOr<FetchResponse&>&& result) mutable {
     59        promise.settle(WTFMove(result));
     60    });
    5461}
    5562
  • trunk/Source/WebCore/Modules/fetch/FetchResponse.cpp

    r220751 r220927  
    3333#include "HTTPParsers.h"
    3434#include "JSBlob.h"
    35 #include "JSFetchResponse.h"
    3635#include "ResourceError.h"
    3736#include "ScriptExecutionContext.h"
     
    104103}
    105104
    106 void FetchResponse::fetch(ScriptExecutionContext& context, FetchRequest& request, FetchPromise&& promise)
     105void FetchResponse::fetch(ScriptExecutionContext& context, FetchRequest& request, NotificationCallback&& responseCallback)
    107106{
    108107    if (request.isBodyReadableStream()) {
    109         promise.reject(TypeError, "ReadableStream uploading is not supported");
     108        if (responseCallback)
     109            responseCallback(Exception { NotSupportedError, "ReadableStream uploading is not supported" });
    110110        return;
    111111    }
    112112    auto response = adoptRef(*new FetchResponse(context, FetchBody::loadingBody(), FetchHeaders::create(FetchHeaders::Guard::Immutable), { }));
    113113
    114     response->m_bodyLoader.emplace(response.get(), WTFMove(promise));
     114    response->m_bodyLoader.emplace(response.get(), WTFMove(responseCallback));
    115115    if (!response->m_bodyLoader->start(context, request))
    116116        response->m_bodyLoader = std::nullopt;
     
    143143{
    144144    ASSERT(m_response.hasPendingActivity());
    145     if (m_promise)
    146         std::exchange(m_promise, std::nullopt)->reject(Exception { TypeError, String(error.localizedDescription()) });
     145    if (auto responseCallback = WTFMove(m_responseCallback))
     146        responseCallback(Exception { TypeError, String(error.localizedDescription()) });
    147147
    148148#if ENABLE(STREAMS_API)
     
    161161}
    162162
    163 FetchResponse::BodyLoader::BodyLoader(FetchResponse& response, FetchPromise&& promise)
     163FetchResponse::BodyLoader::BodyLoader(FetchResponse& response, NotificationCallback&& responseCallback)
    164164    : m_response(response)
    165     , m_promise(WTFMove(promise))
     165    , m_responseCallback(WTFMove(responseCallback))
    166166{
    167167    m_response.setPendingActivity(&m_response);
     
    175175void FetchResponse::BodyLoader::didReceiveResponse(const ResourceResponse& resourceResponse)
    176176{
    177     ASSERT(m_promise);
    178 
    179177    m_response.m_response = ResourceResponseBase::filter(resourceResponse);
    180178    m_response.m_shouldExposeBody = resourceResponse.tainting() != ResourceResponse::Tainting::Opaque;
     
    183181    m_response.updateContentType();
    184182
    185     std::exchange(m_promise, std::nullopt)->resolve(m_response);
     183    if (auto responseCallback = WTFMove(m_responseCallback))
     184        responseCallback(m_response);
    186185}
    187186
     
    221220void FetchResponse::BodyLoader::stop()
    222221{
    223     m_promise = std::nullopt;
     222    m_responseCallback = { };
    224223    if (m_loader)
    225224        m_loader->stop();
  • trunk/Source/WebCore/Modules/fetch/FetchResponse.h

    r220758 r220927  
    6060    static Ref<FetchResponse> create(ScriptExecutionContext& context, std::optional<FetchBody>&& body, Ref<FetchHeaders>&& headers, ResourceResponse&& response) { return adoptRef(*new FetchResponse(context, WTFMove(body), WTFMove(headers), WTFMove(response))); }
    6161
    62     using FetchPromise = DOMPromiseDeferred<IDLInterface<FetchResponse>>;
    63     static void fetch(ScriptExecutionContext&, FetchRequest&, FetchPromise&&);
     62    using NotificationCallback = WTF::Function<void(ExceptionOr<FetchResponse&>&&)>;
     63    static void fetch(ScriptExecutionContext&, FetchRequest&, NotificationCallback&&);
    6464
    6565    void consume(unsigned, Ref<DeferredPromise>&&);
     
    9999    FetchResponse(ScriptExecutionContext&, std::optional<FetchBody>&&, Ref<FetchHeaders>&&, ResourceResponse&&);
    100100
    101     static void startFetching(ScriptExecutionContext&, const FetchRequest&, FetchPromise&&);
    102 
    103101    void stop() final;
    104102    const char* activeDOMObjectName() const final;
     
    111109    class BodyLoader final : public FetchLoaderClient {
    112110    public:
    113         BodyLoader(FetchResponse&, FetchPromise&&);
     111        BodyLoader(FetchResponse&, NotificationCallback&&);
    114112        ~BodyLoader();
    115113
     
    129127
    130128        FetchResponse& m_response;
    131         std::optional<FetchPromise> m_promise;
     129        NotificationCallback m_responseCallback;
    132130        std::unique_ptr<FetchLoader> m_loader;
    133131    };
  • trunk/Source/WebCore/Modules/fetch/WorkerGlobalScopeFetch.cpp

    r220241 r220927  
    3131
    3232#include "FetchResponse.h"
     33#include "JSFetchResponse.h"
    3334#include "WorkerGlobalScope.h"
    3435
    3536namespace WebCore {
    3637
    37 void WorkerGlobalScopeFetch::fetch(WorkerGlobalScope& scope, FetchRequest::Info&& input, FetchRequest::Init&& init, Ref<DeferredPromise>&& promise)
     38using FetchResponsePromise = DOMPromiseDeferred<IDLInterface<FetchResponse>>;
     39
     40void WorkerGlobalScopeFetch::fetch(WorkerGlobalScope& scope, FetchRequest::Info&& input, FetchRequest::Init&& init, Ref<DeferredPromise>&& deferred)
    3841{
     42    FetchResponsePromise promise = WTFMove(deferred);
     43
    3944    auto request = FetchRequest::create(scope, WTFMove(input), WTFMove(init));
    4045    if (request.hasException()) {
    41         promise->reject(request.releaseException());
     46        promise.reject(request.releaseException());
    4247        return;
    4348    }
    4449
    45     FetchResponse::fetch(scope, request.releaseReturnValue().get(), WTFMove(promise));
     50    FetchResponse::fetch(scope, request.releaseReturnValue().get(), [promise = WTFMove(promise)](ExceptionOr<FetchResponse&>&& result) mutable {
     51        promise.settle(WTFMove(result));
     52    });
    4653}
    4754
Note: See TracChangeset for help on using the changeset viewer.