1/*
2 * Copyright (C) 2019 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 <WebCore/FetchIdentifier.h>
31#include <WebCore/ServiceWorkerTypes.h>
32#include <pal/SessionID.h>
33#include <wtf/RefCounted.h>
34
35namespace WebCore {
36class ResourceError;
37class ResourceResponse;
38}
39
40namespace IPC {
41class Connection;
42class DataReference;
43class Decoder;
44class FormDataReference;
45}
46
47namespace WebKit {
48
49class ServiceWorkerFetchTask : public RefCounted<ServiceWorkerFetchTask> {
50public:
51 static Ref<ServiceWorkerFetchTask> create(PAL::SessionID sessionID, Ref<IPC::Connection>&& connection, WebCore::SWServerConnectionIdentifier connectionIdentifier, WebCore::FetchIdentifier fetchIdentifier) { return adoptRef(*new ServiceWorkerFetchTask(sessionID, WTFMove(connection), connectionIdentifier, fetchIdentifier)); }
52
53 void fail(const WebCore::ResourceError& error) { didFail(error); }
54 void didReceiveMessage(IPC::Connection&, IPC::Decoder&);
55
56 struct Identifier {
57 WebCore::SWServerConnectionIdentifier connectionIdentifier;
58 WebCore::FetchIdentifier fetchIdentifier;
59
60 unsigned hash() const
61 {
62 unsigned hashes[2];
63 hashes[0] = WTF::intHash(connectionIdentifier.toUInt64());
64 hashes[1] = WTF::intHash(fetchIdentifier.toUInt64());
65 return StringHasher::hashMemory(hashes, sizeof(hashes));
66 }
67 };
68
69 const Identifier& identifier() const { return m_identifier; }
70
71private:
72 ServiceWorkerFetchTask(PAL::SessionID sessionID, Ref<IPC::Connection>&& connection, WebCore::SWServerConnectionIdentifier connectionIdentifier, WebCore::FetchIdentifier fetchIdentifier)
73 : m_sessionID(sessionID)
74 , m_connection(WTFMove(connection))
75 , m_identifier { connectionIdentifier, fetchIdentifier }
76 { }
77
78 void didReceiveRedirectResponse(const WebCore::ResourceResponse&);
79 void didReceiveResponse(const WebCore::ResourceResponse&, bool needsContinueDidReceiveResponseMessage);
80 void didReceiveData(const IPC::DataReference&, int64_t encodedDataLength);
81 void didReceiveFormData(const IPC::FormDataReference&);
82 void didFinish();
83 void didFail(const WebCore::ResourceError&);
84 void didNotHandle();
85
86 PAL::SessionID m_sessionID;
87 Ref<IPC::Connection> m_connection;
88 Identifier m_identifier;
89};
90
91inline bool operator==(const ServiceWorkerFetchTask::Identifier& a, const ServiceWorkerFetchTask::Identifier& b)
92{
93 return a.connectionIdentifier == b.connectionIdentifier && a.fetchIdentifier == b.fetchIdentifier;
94}
95
96} // namespace WebKit
97
98
99namespace WTF {
100
101struct ServiceWorkerFetchTaskIdentifierHash {
102 static unsigned hash(const WebKit::ServiceWorkerFetchTask::Identifier& key) { return key.hash(); }
103 static bool equal(const WebKit::ServiceWorkerFetchTask::Identifier& a, const WebKit::ServiceWorkerFetchTask::Identifier& b) { return a == b; }
104 static const bool safeToCompareToEmptyOrDeleted = true;
105};
106
107template<> struct HashTraits<WebKit::ServiceWorkerFetchTask::Identifier> : GenericHashTraits<WebKit::ServiceWorkerFetchTask::Identifier> {
108 static WebKit::ServiceWorkerFetchTask::Identifier emptyValue() { return { }; }
109
110 static void constructDeletedValue(WebKit::ServiceWorkerFetchTask::Identifier& slot) { slot.connectionIdentifier = makeObjectIdentifier<WebCore::SWServerConnectionIdentifierType>(std::numeric_limits<uint64_t>::max()); }
111
112 static bool isDeletedValue(const WebKit::ServiceWorkerFetchTask::Identifier& slot) { return slot.connectionIdentifier.toUInt64() == std::numeric_limits<uint64_t>::max(); }
113};
114
115template<> struct DefaultHash<WebKit::ServiceWorkerFetchTask::Identifier> {
116 using Hash = ServiceWorkerFetchTaskIdentifierHash;
117};
118
119}
120
121#endif // ENABLE(SERVICE_WORKER)
122