6139e5f25083477bef6088b43257179d7dcf0b06
[WebKit-https.git] / Source / WebKit / WebProcess / Storage / ServiceWorkerClientFetch.cpp
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 #include "config.h"
27 #include "ServiceWorkerClientFetch.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "DataReference.h"
32 #include "WebServiceWorkerProvider.h"
33 #include <WebCore/NotImplemented.h>
34
35 using namespace WebCore;
36
37 namespace WebKit {
38
39 ServiceWorkerClientFetch::ServiceWorkerClientFetch(WebServiceWorkerProvider& serviceWorkerProvider, Ref<WebCore::ResourceLoader>&& loader, uint64_t identifier, Ref<IPC::Connection>&& connection, Callback&& callback)
40     : m_serviceWorkerProvider(serviceWorkerProvider)
41     , m_loader(WTFMove(loader))
42     , m_identifier(identifier)
43     , m_connection(WTFMove(connection))
44     , m_callback(WTFMove(callback))
45 {
46 }
47
48 void ServiceWorkerClientFetch::didReceiveResponse(WebCore::ResourceResponse&& response)
49 {
50     auto protectedThis = makeRef(*this);
51
52     if (!(response.httpStatusCode() <= 300 || response.httpStatusCode() >= 400 || response.httpStatusCode() == 304 || response.httpStatusCode() == 305 || response.httpStatusCode() == 306)) {
53         // FIXME: Support redirections.
54         notImplemented();
55         m_loader->didFail({ });
56         if (auto callback = WTFMove(m_callback))
57             callback(Result::Succeeded);
58         return;
59     }
60
61     if (response.type() == ResourceResponse::Type::Error) {
62         // Add support for a better error.
63         m_loader->didFail({ });
64         if (auto callback = WTFMove(m_callback))
65             callback(Result::Succeeded);
66         return;
67     }
68
69     response.setSource(ResourceResponse::Source::ServiceWorker);
70     m_loader->didReceiveResponse(response);
71     if (auto callback = WTFMove(m_callback))
72         callback(Result::Succeeded);
73 }
74
75 void ServiceWorkerClientFetch::didReceiveData(const IPC::DataReference& data, int64_t encodedDataLength)
76 {
77     m_loader->didReceiveData(reinterpret_cast<const char*>(data.data()), data.size(), encodedDataLength, DataPayloadBytes);
78 }
79
80 void ServiceWorkerClientFetch::didFinish()
81 {
82     ASSERT(!m_callback);
83
84     auto protectedThis = makeRef(*this);
85     m_loader->didFinishLoading(NetworkLoadMetrics { });
86     m_serviceWorkerProvider.fetchFinished(m_identifier);
87 }
88
89 void ServiceWorkerClientFetch::didFail()
90 {
91     if (auto callback = WTFMove(m_callback))
92         callback(Result::Unhandled);
93
94     m_serviceWorkerProvider.fetchFinished(m_identifier);
95 }
96
97 void ServiceWorkerClientFetch::cancel()
98 {
99     if (auto callback = WTFMove(m_callback))
100         callback(Result::Cancelled);
101 }
102
103 } // namespace WebKit
104
105 #endif // ENABLE(SERVICE_WORKER)