Changeset 220927 in webkit
- Timestamp:
- Aug 18, 2017 12:32:17 PM (7 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r220924 r220927 1 2017-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 1 25 2017-08-18 Brady Eidson <beidson@apple.com> 2 26 -
trunk/Source/WebCore/Modules/fetch/DOMWindowFetch.cpp
r220241 r220927 34 34 #include "Document.h" 35 35 #include "FetchResponse.h" 36 #include "JSFetchResponse.h" 36 37 37 38 namespace WebCore { 38 39 39 void DOMWindowFetch::fetch(DOMWindow& window, FetchRequest::Info&& input, FetchRequest::Init&& init, Ref<DeferredPromise>&& promise) 40 using FetchResponsePromise = DOMPromiseDeferred<IDLInterface<FetchResponse>>; 41 42 void DOMWindowFetch::fetch(DOMWindow& window, FetchRequest::Info&& input, FetchRequest::Init&& init, Ref<DeferredPromise>&& deferred) 40 43 { 44 FetchResponsePromise promise = WTFMove(deferred); 45 41 46 auto* document = window.document(); 42 47 if (!document) { 43 promise ->reject(InvalidStateError);48 promise.reject(InvalidStateError); 44 49 return; 45 50 } … … 47 52 auto request = FetchRequest::create(*document, WTFMove(input), WTFMove(init)); 48 53 if (request.hasException()) { 49 promise ->reject(request.releaseException());54 promise.reject(request.releaseException()); 50 55 return; 51 56 } 52 57 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 }); 54 61 } 55 62 -
trunk/Source/WebCore/Modules/fetch/FetchResponse.cpp
r220751 r220927 33 33 #include "HTTPParsers.h" 34 34 #include "JSBlob.h" 35 #include "JSFetchResponse.h"36 35 #include "ResourceError.h" 37 36 #include "ScriptExecutionContext.h" … … 104 103 } 105 104 106 void FetchResponse::fetch(ScriptExecutionContext& context, FetchRequest& request, FetchPromise&& promise)105 void FetchResponse::fetch(ScriptExecutionContext& context, FetchRequest& request, NotificationCallback&& responseCallback) 107 106 { 108 107 if (request.isBodyReadableStream()) { 109 promise.reject(TypeError, "ReadableStream uploading is not supported"); 108 if (responseCallback) 109 responseCallback(Exception { NotSupportedError, "ReadableStream uploading is not supported" }); 110 110 return; 111 111 } 112 112 auto response = adoptRef(*new FetchResponse(context, FetchBody::loadingBody(), FetchHeaders::create(FetchHeaders::Guard::Immutable), { })); 113 113 114 response->m_bodyLoader.emplace(response.get(), WTFMove( promise));114 response->m_bodyLoader.emplace(response.get(), WTFMove(responseCallback)); 115 115 if (!response->m_bodyLoader->start(context, request)) 116 116 response->m_bodyLoader = std::nullopt; … … 143 143 { 144 144 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()) }); 147 147 148 148 #if ENABLE(STREAMS_API) … … 161 161 } 162 162 163 FetchResponse::BodyLoader::BodyLoader(FetchResponse& response, FetchPromise&& promise)163 FetchResponse::BodyLoader::BodyLoader(FetchResponse& response, NotificationCallback&& responseCallback) 164 164 : m_response(response) 165 , m_ promise(WTFMove(promise))165 , m_responseCallback(WTFMove(responseCallback)) 166 166 { 167 167 m_response.setPendingActivity(&m_response); … … 175 175 void FetchResponse::BodyLoader::didReceiveResponse(const ResourceResponse& resourceResponse) 176 176 { 177 ASSERT(m_promise);178 179 177 m_response.m_response = ResourceResponseBase::filter(resourceResponse); 180 178 m_response.m_shouldExposeBody = resourceResponse.tainting() != ResourceResponse::Tainting::Opaque; … … 183 181 m_response.updateContentType(); 184 182 185 std::exchange(m_promise, std::nullopt)->resolve(m_response); 183 if (auto responseCallback = WTFMove(m_responseCallback)) 184 responseCallback(m_response); 186 185 } 187 186 … … 221 220 void FetchResponse::BodyLoader::stop() 222 221 { 223 m_ promise = std::nullopt;222 m_responseCallback = { }; 224 223 if (m_loader) 225 224 m_loader->stop(); -
trunk/Source/WebCore/Modules/fetch/FetchResponse.h
r220758 r220927 60 60 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))); } 61 61 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&&); 64 64 65 65 void consume(unsigned, Ref<DeferredPromise>&&); … … 99 99 FetchResponse(ScriptExecutionContext&, std::optional<FetchBody>&&, Ref<FetchHeaders>&&, ResourceResponse&&); 100 100 101 static void startFetching(ScriptExecutionContext&, const FetchRequest&, FetchPromise&&);102 103 101 void stop() final; 104 102 const char* activeDOMObjectName() const final; … … 111 109 class BodyLoader final : public FetchLoaderClient { 112 110 public: 113 BodyLoader(FetchResponse&, FetchPromise&&);111 BodyLoader(FetchResponse&, NotificationCallback&&); 114 112 ~BodyLoader(); 115 113 … … 129 127 130 128 FetchResponse& m_response; 131 std::optional<FetchPromise> m_promise;129 NotificationCallback m_responseCallback; 132 130 std::unique_ptr<FetchLoader> m_loader; 133 131 }; -
trunk/Source/WebCore/Modules/fetch/WorkerGlobalScopeFetch.cpp
r220241 r220927 31 31 32 32 #include "FetchResponse.h" 33 #include "JSFetchResponse.h" 33 34 #include "WorkerGlobalScope.h" 34 35 35 36 namespace WebCore { 36 37 37 void WorkerGlobalScopeFetch::fetch(WorkerGlobalScope& scope, FetchRequest::Info&& input, FetchRequest::Init&& init, Ref<DeferredPromise>&& promise) 38 using FetchResponsePromise = DOMPromiseDeferred<IDLInterface<FetchResponse>>; 39 40 void WorkerGlobalScopeFetch::fetch(WorkerGlobalScope& scope, FetchRequest::Info&& input, FetchRequest::Init&& init, Ref<DeferredPromise>&& deferred) 38 41 { 42 FetchResponsePromise promise = WTFMove(deferred); 43 39 44 auto request = FetchRequest::create(scope, WTFMove(input), WTFMove(init)); 40 45 if (request.hasException()) { 41 promise ->reject(request.releaseException());46 promise.reject(request.releaseException()); 42 47 return; 43 48 } 44 49 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 }); 46 53 } 47 54
Note: See TracChangeset
for help on using the changeset viewer.