| 1 | /* |
| 2 | * Copyright (C) 2018 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. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "ApplicationCacheResourceLoader.h" |
| 28 | |
| 29 | #include "CachedResourceLoader.h" |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | RefPtr<ApplicationCacheResourceLoader> ApplicationCacheResourceLoader::create(unsigned type, CachedResourceLoader& loader, ResourceRequest&& request, CompletionHandler<void(ResourceOrError&&)>&& callback) |
| 34 | { |
| 35 | ResourceLoaderOptions options; |
| 36 | options.storedCredentialsPolicy = StoredCredentialsPolicy::Use; |
| 37 | options.credentials = FetchOptions::Credentials::Include; |
| 38 | options.applicationCacheMode = ApplicationCacheMode::Bypass; |
| 39 | CachedResourceRequest cachedResourceRequest { WTFMove(request), options }; |
| 40 | auto resource = loader.requestRawResource(WTFMove(cachedResourceRequest)); |
| 41 | if (!resource.has_value()) { |
| 42 | callback(makeUnexpected(Error::CannotCreateResource)); |
| 43 | return nullptr; |
| 44 | } |
| 45 | return adoptRef(*new ApplicationCacheResourceLoader { type, WTFMove(resource.value()), WTFMove(callback) }); |
| 46 | } |
| 47 | |
| 48 | ApplicationCacheResourceLoader::ApplicationCacheResourceLoader(unsigned type, CachedResourceHandle<CachedRawResource>&& resource, CompletionHandler<void(ResourceOrError&&)>&& callback) |
| 49 | : m_type(type) |
| 50 | , m_resource(WTFMove(resource)) |
| 51 | , m_callback(WTFMove(callback)) |
| 52 | { |
| 53 | m_resource->addClient(*this); |
| 54 | } |
| 55 | |
| 56 | ApplicationCacheResourceLoader::~ApplicationCacheResourceLoader() |
| 57 | { |
| 58 | if (auto callback = WTFMove(m_callback)) |
| 59 | callback(makeUnexpected(Error::Abort)); |
| 60 | |
| 61 | if (m_resource) |
| 62 | m_resource->removeClient(*this); |
| 63 | } |
| 64 | |
| 65 | void ApplicationCacheResourceLoader::cancel(Error error) |
| 66 | { |
| 67 | auto protectedThis = makeRef(*this); |
| 68 | |
| 69 | if (auto callback = WTFMove(m_callback)) |
| 70 | callback(makeUnexpected(error)); |
| 71 | |
| 72 | if (m_resource) { |
| 73 | m_resource->removeClient(*this); |
| 74 | m_resource = nullptr; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void ApplicationCacheResourceLoader::responseReceived(CachedResource& resource, const ResourceResponse& response, CompletionHandler<void()>&& completionHandler) |
| 79 | { |
| 80 | ASSERT_UNUSED(resource, &resource == m_resource); |
| 81 | CompletionHandlerCallingScope completionHandlerCaller(WTFMove(completionHandler)); |
| 82 | |
| 83 | if (response.httpStatusCode() == 404 || response.httpStatusCode() == 410) { |
| 84 | cancel(Error::NotFound); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if (response.httpStatusCode() == 304) { |
| 89 | notifyFinished(*m_resource); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (response.httpStatusCode() / 100 != 2) { |
| 94 | cancel(Error::NotOK); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | m_applicationCacheResource = ApplicationCacheResource::create(m_resource->url(), response, m_type); |
| 99 | } |
| 100 | |
| 101 | void ApplicationCacheResourceLoader::dataReceived(CachedResource&, const char* data, int length) |
| 102 | { |
| 103 | m_applicationCacheResource->data().append(data, length); |
| 104 | } |
| 105 | |
| 106 | void ApplicationCacheResourceLoader::redirectReceived(CachedResource&, ResourceRequest&& newRequest, const ResourceResponse&, CompletionHandler<void(ResourceRequest&&)>&& callback) |
| 107 | { |
| 108 | m_hasRedirection = true; |
| 109 | bool isRedirectionDisallowed = (m_type & ApplicationCacheResource::Type::Manifest) || (m_type & ApplicationCacheResource::Explicit) || (m_type & ApplicationCacheResource::Fallback); |
| 110 | |
| 111 | if (isRedirectionDisallowed) { |
| 112 | cancel(Error::RedirectForbidden); |
| 113 | callback({ }); |
| 114 | return; |
| 115 | } |
| 116 | callback(WTFMove(newRequest)); |
| 117 | } |
| 118 | |
| 119 | void ApplicationCacheResourceLoader::notifyFinished(CachedResource& resource) |
| 120 | { |
| 121 | auto protectedThis = makeRef(*this); |
| 122 | |
| 123 | ASSERT_UNUSED(resource, &resource == m_resource); |
| 124 | |
| 125 | if (m_resource->errorOccurred()) { |
| 126 | cancel(Error::NetworkError); |
| 127 | return; |
| 128 | } |
| 129 | if (auto callback = WTFMove(m_callback)) |
| 130 | callback(WTFMove(m_applicationCacheResource)); |
| 131 | |
| 132 | CachedResourceHandle<CachedRawResource> resourceHandle; |
| 133 | std::swap(resourceHandle, m_resource); |
| 134 | if (resourceHandle) |
| 135 | resourceHandle->removeClient(*this); |
| 136 | } |
| 137 | |
| 138 | } // namespace WebCore |
| 139 | |