1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(SERVICE_WORKER)
29
30#include "Connection.h"
31#include <WebCore/FetchIdentifier.h>
32#include <WebCore/FetchLoader.h>
33#include <WebCore/FetchLoaderClient.h>
34#include <WebCore/ServiceWorkerFetch.h>
35#include <WebCore/ServiceWorkerTypes.h>
36#include <wtf/UniqueRef.h>
37
38namespace WebKit {
39
40class WebServiceWorkerFetchTaskClient final : public WebCore::ServiceWorkerFetch::Client {
41public:
42 static Ref<WebServiceWorkerFetchTaskClient> create(Ref<IPC::Connection>&& connection, WebCore::ServiceWorkerIdentifier serviceWorkerIdentifier, WebCore::SWServerConnectionIdentifier serverConnectionIdentifier, WebCore::FetchIdentifier fetchTaskIdentifier, bool needsContinueDidReceiveResponseMessage)
43 {
44 return adoptRef(*new WebServiceWorkerFetchTaskClient(WTFMove(connection), serviceWorkerIdentifier, serverConnectionIdentifier, fetchTaskIdentifier, needsContinueDidReceiveResponseMessage));
45 }
46
47private:
48 WebServiceWorkerFetchTaskClient(Ref<IPC::Connection>&&, WebCore::ServiceWorkerIdentifier, WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, bool needsContinueDidReceiveResponseMessage);
49
50 void didReceiveResponse(const WebCore::ResourceResponse&) final;
51 void didReceiveRedirection(const WebCore::ResourceResponse&) final;
52 void didReceiveData(Ref<WebCore::SharedBuffer>&&) final;
53 void didReceiveFormDataAndFinish(Ref<WebCore::FormData>&&) final;
54 void didFail(const WebCore::ResourceError&) final;
55 void didFinish() final;
56 void didNotHandle() final;
57 void cancel() final;
58 void continueDidReceiveResponse() final;
59
60 void cleanup();
61
62 void didReceiveBlobChunk(const char* data, size_t size);
63 void didFinishBlobLoading();
64
65 struct BlobLoader final : WebCore::FetchLoaderClient {
66 explicit BlobLoader(WebServiceWorkerFetchTaskClient& client) : client(client) { }
67
68 // FetchLoaderClient API
69 void didReceiveResponse(const WebCore::ResourceResponse&) final { }
70 void didReceiveData(const char* data, size_t size) final { client->didReceiveBlobChunk(data, size); }
71 void didFail(const WebCore::ResourceError& error) final { client->didFail(error); }
72 void didSucceed() final { client->didFinishBlobLoading(); }
73
74 Ref<WebServiceWorkerFetchTaskClient> client;
75 std::unique_ptr<WebCore::FetchLoader> loader;
76 };
77
78 RefPtr<IPC::Connection> m_connection;
79 WebCore::SWServerConnectionIdentifier m_serverConnectionIdentifier;
80 WebCore::ServiceWorkerIdentifier m_serviceWorkerIdentifier;
81 WebCore::FetchIdentifier m_fetchIdentifier;
82 Optional<BlobLoader> m_blobLoader;
83 bool m_needsContinueDidReceiveResponseMessage { false };
84 bool m_waitingForContinueDidReceiveResponseMessage { false };
85 Variant<std::nullptr_t, Ref<WebCore::SharedBuffer>, Ref<WebCore::FormData>, UniqueRef<WebCore::ResourceError>> m_responseData;
86 bool m_didFinish { false };
87};
88
89} // namespace WebKit
90
91#endif // ENABLE(SERVICE_WORKER)
92