1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#if ENABLE(SERVICE_WORKER)
#include "BackgroundFetchFailureReason.h"
#include "BackgroundFetchOptions.h"
#include "BackgroundFetchRecordIdentifier.h"
#include "BackgroundFetchRecordLoader.h"
#include "BackgroundFetchRequest.h"
#include "BackgroundFetchResult.h"
#include "BackgroundFetchStore.h"
#include "ClientOrigin.h"
#include "ResourceResponse.h"
#include "ServiceWorkerRegistrationKey.h"
#include "ServiceWorkerTypes.h"
#include <wtf/WeakPtr.h>
namespace WebCore {
class BackgroundFetchRecordLoader;
class SWServer;
class SharedBuffer;
struct BackgroundFetchRequest;
struct CacheQueryOptions;
class BackgroundFetch : public CanMakeWeakPtr<BackgroundFetch> {
WTF_MAKE_FAST_ALLOCATED;
public:
using NotificationCallback = Function<void(BackgroundFetch&)>;
BackgroundFetch(SWServerRegistration&, const String&, Vector<BackgroundFetchRequest>&&, BackgroundFetchOptions&&, Ref<BackgroundFetchStore>&&, NotificationCallback&&);
BackgroundFetch(SWServerRegistration&, String&&, BackgroundFetchOptions&&, Ref<BackgroundFetchStore>&&, NotificationCallback&&, bool pausedFlag);
~BackgroundFetch();
static std::unique_ptr<BackgroundFetch> createFromStore(std::span<const uint8_t>, SWServer&, Ref<BackgroundFetchStore>&&, NotificationCallback&&);
String identifier() const { return m_identifier; }
WEBCORE_EXPORT BackgroundFetchInformation information() const;
const ServiceWorkerRegistrationKey& registrationKey() const { return m_registrationKey; }
const BackgroundFetchOptions& options() const { return m_options; }
using RetrieveRecordResponseCallback = CompletionHandler<void(Expected<ResourceResponse, ExceptionData>&&)>;
using RetrieveRecordResponseBodyCallback = Function<void(Expected<RefPtr<SharedBuffer>, ResourceError>&&)>;
using CreateLoaderCallback = Function<std::unique_ptr<BackgroundFetchRecordLoader>(BackgroundFetchRecordLoader::Client&, const BackgroundFetchRequest&, size_t responseDataSize, const ClientOrigin&)>;
bool pausedFlagIsSet() const { return m_pausedFlag; }
void pause();
void resume(const CreateLoaderCallback&);
class Record final : public BackgroundFetchRecordLoader::Client, public RefCounted<Record> {
WTF_MAKE_FAST_ALLOCATED;
public:
static Ref<Record> create(BackgroundFetch& fetch, BackgroundFetchRequest&& request, size_t size) { return adoptRef(*new Record(fetch, WTFMove(request), size)); }
~Record();
void complete(const CreateLoaderCallback&);
void pause();
void abort();
void setAsCompleted() { m_isCompleted = true; }
bool isCompleted() const { return m_isCompleted; }
const BackgroundFetchRequest& request() const { return m_request; }
const ResourceResponse& response() const { return m_response; }
uint64_t responseDataSize() const { return m_responseDataSize; }
void clearResponseDataSize() { m_responseDataSize = 0; }
bool isMatching(const ResourceRequest&, const CacheQueryOptions&) const;
BackgroundFetchRecordInformation information() const;
void retrieveResponse(BackgroundFetchStore&, RetrieveRecordResponseCallback&&);
void retrieveRecordResponseBody(BackgroundFetchStore&, RetrieveRecordResponseBodyCallback&&);
private:
Record(BackgroundFetch&, BackgroundFetchRequest&&, size_t);
void didSendData(uint64_t) final;
void didReceiveResponse(ResourceResponse&&) final;
void didReceiveResponseBodyChunk(const SharedBuffer&) final;
void didFinish(const ResourceError&) final;
WeakPtr<BackgroundFetch> m_fetch;
BackgroundFetchRecordIdentifier m_identifier;
String m_fetchIdentifier;
ServiceWorkerRegistrationKey m_registrationKey;
BackgroundFetchRequest m_request;
size_t m_index { 0 };
ResourceResponse m_response;
std::unique_ptr<BackgroundFetchRecordLoader> m_loader;
uint64_t m_responseDataSize { 0 };
bool m_isCompleted { false };
bool m_isAborted { false };
Vector<RetrieveRecordResponseCallback> m_responseCallbacks;
Vector<RetrieveRecordResponseBodyCallback> m_responseBodyCallbacks;
};
using MatchBackgroundFetchCallback = CompletionHandler<void(Vector<Ref<Record>>&&)>;
void match(const RetrieveRecordsOptions&, MatchBackgroundFetchCallback&&);
bool abort();
void perform(const CreateLoaderCallback&);
bool isActive() const { return m_isActive; }
const ClientOrigin& origin() const { return m_origin; }
uint64_t downloadTotal() const { return m_options.downloadTotal; }
uint64_t uploadTotal() const { return m_uploadTotal; }
void doStore(CompletionHandler<void(BackgroundFetchStore::StoreResult)>&&, std::optional<size_t> responseBodyIndexToClear = { });
void unsetRecordsAvailableFlag();
private:
void didSendData(uint64_t);
void storeResponse(size_t, bool shouldClearResponseBody, ResourceResponse&&);
void storeResponseBodyChunk(size_t, const SharedBuffer&);
void didFinishRecord(const ResourceError&);
void recordIsCompleted();
void handleStoreResult(BackgroundFetchStore::StoreResult);
void updateBackgroundFetchStatus(BackgroundFetchResult, BackgroundFetchFailureReason);
void setRecords(Vector<Ref<Record>>&&);
String m_identifier;
Vector<Ref<Record>> m_records;
BackgroundFetchOptions m_options;
ServiceWorkerRegistrationKey m_registrationKey;
ServiceWorkerRegistrationIdentifier m_registrationIdentifier;
BackgroundFetchResult m_result { BackgroundFetchResult::EmptyString };
BackgroundFetchFailureReason m_failureReason { BackgroundFetchFailureReason::EmptyString };
bool m_recordsAvailableFlag { true };
bool m_abortFlag { false };
bool m_pausedFlag { false };
bool m_isActive { true };
uint64_t m_uploadTotal { 0 };
uint64_t m_currentDownloadSize { 0 };
uint64_t m_currentUploadSize { 0 };
Ref<BackgroundFetchStore> m_store;
NotificationCallback m_notificationCallback;
ClientOrigin m_origin;
};
} // namespace WebCore
#endif // ENABLE(SERVICE_WORKER)
|