Skip to content

Commit

Permalink
Use more Functions in BackgroundFetchStoreImpl
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=255729
rdar://108090601

Reviewed by Sihui Liu.

Using more Functions to remove usage of lambda copy constructors.

* Source/WebKit/NetworkProcess/storage/BackgroundFetchStoreImpl.cpp:
(WebKit::BackgroundFetchStoreImpl::initializeFetches):
(WebKit::BackgroundFetchStoreImpl::clearFetch):
(WebKit::BackgroundFetchStoreImpl::storeFetch):
(WebKit::BackgroundFetchStoreImpl::storeFetchResponseBodyChunk):

Canonical link: https://commits.webkit.org/263213@main
  • Loading branch information
youennf committed Apr 21, 2023
1 parent eaa067b commit 6db11cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
40 changes: 28 additions & 12 deletions Source/WebKit/NetworkProcess/storage/BackgroundFetchStoreImpl.cpp
Expand Up @@ -84,7 +84,7 @@ void BackgroundFetchStoreImpl::initializeFetches(const ServiceWorkerRegistration
initializeFetches({ key.topOrigin(), SecurityOriginData::fromURL(key.scope()) }, WTFMove(callback));
}

void BackgroundFetchStoreImpl::initializeFetches(const ClientOrigin& origin, CompletionHandler<void()>&& callback)
void BackgroundFetchStoreImpl::initializeFetches(const WebCore::ClientOrigin& origin, CompletionHandler<void()>&& callback)
{
if (!m_manager) {
callback();
Expand All @@ -98,7 +98,8 @@ void BackgroundFetchStoreImpl::initializeFetches(const ClientOrigin& origin, Com
}

addResult.iterator->value.initializationCallbacks.append(WTFMove(callback));
auto internalCallback = [origin, weakEngine = WeakPtr { m_server->backgroundFetchEngine() }, protectedThis = Ref { *this }, manager = m_manager](Vector<std::pair<RefPtr<WebCore::SharedBuffer>, String>>&& fetches) {

initializeFetchesInternal(origin, [origin, weakEngine = WeakPtr { m_server->backgroundFetchEngine() }, protectedThis = Ref { *this }, manager = m_manager](Vector<std::pair<RefPtr<WebCore::SharedBuffer>, String>>&& fetches) {
if (weakEngine && manager) {
for (auto& fetch : fetches) {
weakEngine->addFetchFromStore({ fetch.first->data(), fetch.first->size() }, [&](auto& key, auto& identifier) {
Expand All @@ -122,8 +123,11 @@ void BackgroundFetchStoreImpl::initializeFetches(const ClientOrigin& origin, Com
auto callbacks = std::exchange(iterator->value.initializationCallbacks, { });
for (auto& callback : callbacks)
callback();
};
});
}

void BackgroundFetchStoreImpl::initializeFetchesInternal(const WebCore::ClientOrigin& origin, CompletionHandler<void(Vector<std::pair<RefPtr<WebCore::SharedBuffer>, String>>&&)>&& internalCallback)
{
m_manager->dispatchTaskToBackgroundFetchManager(origin, [internalCallback = WTFMove(internalCallback)](auto* backgroundFetchManager) mutable {
if (!backgroundFetchManager) {
callOnMainRunLoop([internalCallback = WTFMove(internalCallback)]() mutable {
Expand Down Expand Up @@ -160,12 +164,15 @@ void BackgroundFetchStoreImpl::clearFetch(const ServiceWorkerRegistrationKey& ke
}
m_filenameToFetch.remove(fetchStorageIdentifier);

auto internalCallback = [protectedThis = Ref { *this }, fetchStorageIdentifier, callback = WTFMove(callback)]() mutable {
clearFetchInternal(origin, fetchStorageIdentifier, [protectedThis = Ref { *this }, fetchStorageIdentifier, callback = WTFMove(callback)]() mutable {
if (protectedThis->m_manager)
protectedThis->m_manager->notifyBackgroundFetchChange(fetchStorageIdentifier, BackgroundFetchChange::Removal);
callback();
};
});
}

void BackgroundFetchStoreImpl::clearFetchInternal(const WebCore::ClientOrigin& origin, const String& fetchStorageIdentifier, CompletionHandler<void()>&& internalCallback)
{
m_manager->dispatchTaskToBackgroundFetchManager(origin, [fetchStorageIdentifier = crossThreadCopy(fetchStorageIdentifier), internalCallback = WTFMove(internalCallback)](auto* backgroundFetchManager) mutable {
if (!backgroundFetchManager) {
callOnMainRunLoop(WTFMove(internalCallback));
Expand Down Expand Up @@ -200,15 +207,18 @@ void BackgroundFetchStoreImpl::clearAllFetches(const ServiceWorkerRegistrationKe
return true;
});

auto internalCallback = [protectedThis = Ref { *this }, fetchStorageIdentifiers, callback = WTFMove(callback)]() mutable {
clearAllFetchesInternal(origin, fetchStorageIdentifiers, [protectedThis = Ref { *this }, fetchStorageIdentifiers, callback = WTFMove(callback)]() mutable {
if (protectedThis->m_manager) {
for (auto& fetchStorageIdentifier : fetchStorageIdentifiers)
protectedThis->m_manager->notifyBackgroundFetchChange(fetchStorageIdentifier, BackgroundFetchChange::Removal);
}
callback();
};
});
}

m_manager->dispatchTaskToBackgroundFetchManager(origin, [fetchStorageIdentifiers = crossThreadCopy(WTFMove(fetchStorageIdentifiers)), internalCallback = WTFMove(internalCallback)](auto* backgroundFetchManager) mutable {
void BackgroundFetchStoreImpl::clearAllFetchesInternal(const WebCore::ClientOrigin& origin, const Vector<String>& fetchStorageIdentifiers, CompletionHandler<void()>&& internalCallback)
{
m_manager->dispatchTaskToBackgroundFetchManager(origin, [fetchStorageIdentifiers = crossThreadCopy(fetchStorageIdentifiers), internalCallback = WTFMove(internalCallback)](auto* backgroundFetchManager) mutable {
if (!backgroundFetchManager) {
callOnMainRunLoop(WTFMove(internalCallback));
return;
Expand Down Expand Up @@ -238,15 +248,18 @@ void BackgroundFetchStoreImpl::storeFetch(const ServiceWorkerRegistrationKey& ke
if (isNewFetchStorageIdentifier)
fetchStorageIdentifier = BackgroundFetchStoreManager::createNewStorageIdentifier();

auto internalCallback = [protectedThis = Ref { *this }, origin, key, identifier, fetchStorageIdentifier, isNewFetchStorageIdentifier, callback = WTFMove(callback)](StoreResult result) mutable {
storeFetchInternal(origin, fetchStorageIdentifier, downloadTotal, uploadTotal, responseBodyIndexToClear, WTFMove(fetch), [protectedThis = Ref { *this }, origin, key, identifier, fetchStorageIdentifier, isNewFetchStorageIdentifier, callback = WTFMove(callback)](StoreResult result) mutable {
if (result == StoreResult::OK) {
if (protectedThis->m_manager)
protectedThis->m_manager->notifyBackgroundFetchChange(fetchStorageIdentifier, isNewFetchStorageIdentifier ? BackgroundFetchChange::Addition : BackgroundFetchChange::Update);
protectedThis->registerFetch(origin, key, identifier, WTFMove(fetchStorageIdentifier));
}
callback(result);
};
});
}

void BackgroundFetchStoreImpl::storeFetchInternal(const WebCore::ClientOrigin& origin, const String& fetchStorageIdentifier, uint64_t downloadTotal, uint64_t uploadTotal, std::optional<size_t> responseBodyIndexToClear, Vector<uint8_t>&& fetch, CompletionHandler<void(StoreResult)>&& internalCallback)
{
m_manager->dispatchTaskToBackgroundFetchManager(origin, [fetchStorageIdentifier = crossThreadCopy(fetchStorageIdentifier), downloadTotal, uploadTotal, responseBodyIndexToClear, fetch = WTFMove(fetch), internalCallback = WTFMove(internalCallback)](auto* backgroundFetchManager) mutable {
if (!backgroundFetchManager) {
callOnMainRunLoop([internalCallback = WTFMove(internalCallback)]() mutable {
Expand Down Expand Up @@ -282,12 +295,15 @@ void BackgroundFetchStoreImpl::storeFetchResponseBodyChunk(const ServiceWorkerRe
return;
}

auto internalCallback = [protectedThis = Ref { *this }, fetchStorageIdentifier, callback = WTFMove(callback)](StoreResult result) mutable {
storeFetchResponseBodyChunkInternal(origin, fetchStorageIdentifier, index, data, [protectedThis = Ref { *this }, fetchStorageIdentifier, callback = WTFMove(callback)](StoreResult result) mutable {
if (result == StoreResult::OK && protectedThis->m_manager)
protectedThis->m_manager->notifyBackgroundFetchChange(fetchStorageIdentifier, BackgroundFetchChange::Update);
callback(result);
};
});
}

void BackgroundFetchStoreImpl::storeFetchResponseBodyChunkInternal(const WebCore::ClientOrigin& origin, const String& fetchStorageIdentifier, size_t index, const SharedBuffer& data, CompletionHandler<void(StoreResult)>&& internalCallback)
{
m_manager->dispatchTaskToBackgroundFetchManager(origin, [fetchStorageIdentifier = crossThreadCopy(fetchStorageIdentifier), index, data = Ref { data }, internalCallback = WTFMove(internalCallback)](auto* backgroundFetchManager) mutable {
if (!backgroundFetchManager) {
callOnMainRunLoop([internalCallback = WTFMove(internalCallback)]() mutable {
Expand Down
Expand Up @@ -66,6 +66,12 @@ class BackgroundFetchStoreImpl : public WebCore::BackgroundFetchStore {
void storeFetchResponseBodyChunk(const WebCore::ServiceWorkerRegistrationKey&, const String&, size_t, const WebCore::SharedBuffer&, CompletionHandler<void(StoreResult)>&&) final;
void retrieveResponseBody(const WebCore::ServiceWorkerRegistrationKey&, const String&, size_t, RetrieveRecordResponseBodyCallback&&) final;

void initializeFetchesInternal(const WebCore::ClientOrigin&, CompletionHandler<void(Vector<std::pair<RefPtr<WebCore::SharedBuffer>, String>>&&)>&&);
void clearFetchInternal(const WebCore::ClientOrigin&, const String&, CompletionHandler<void()>&&);
void clearAllFetchesInternal(const WebCore::ClientOrigin&, const Vector<String>&, CompletionHandler<void()>&&);
void storeFetchInternal(const WebCore::ClientOrigin&, const String&, uint64_t, uint64_t, std::optional<size_t>, Vector<uint8_t>&&, CompletionHandler<void(StoreResult)>&&);
void storeFetchResponseBodyChunkInternal(const WebCore::ClientOrigin&, const String&, size_t index, const WebCore::SharedBuffer&, CompletionHandler<void(StoreResult)>&&);

String getFilename(const WebCore::ServiceWorkerRegistrationKey&, const String&);
void registerFetch(const WebCore::ClientOrigin&, const WebCore::ServiceWorkerRegistrationKey&, const String& backgroundFetchIdentifier, String&& fetchStorageIdentifier);
void loadAllFetches(CompletionHandler<void()>&&);
Expand Down

0 comments on commit 6db11cf

Please sign in to comment.