Skip to content

Commit

Permalink
Apply patch. rdar://122170377
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Robson committed Feb 29, 2024
1 parent def30df commit 5b9cab4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
52 changes: 34 additions & 18 deletions Source/WebCore/Modules/cache/DOMCacheStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ void DOMCacheStorage::has(const String& name, DOMPromiseDeferred<IDLBoolean>&& p
});
}

Ref<DOMCache> DOMCacheStorage::findCacheOrCreate(DOMCacheEngine::CacheInfo&& info)
Ref<DOMCache> DOMCacheStorage::findCacheOrCreate(DOMCacheEngine::CacheInfo&& info, ScriptExecutionContext& context)
{
auto position = m_caches.findIf([&] (const auto& cache) { return info.identifier == cache->identifier(); });
if (position != notFound)
return m_caches[position].copyRef();
return DOMCache::create(*scriptExecutionContext(), WTFMove(info.name), info.identifier, m_connection.copyRef());
auto position = m_caches.findIf([&] (const auto& cache) { return info.identifier == cache->identifier(); });
if (position != notFound)
return m_caches[position].copyRef();
return DOMCache::create(context, WTFMove(info.name), info.identifier, m_connection.copyRef());
}

class ConnectionStorageLock {
Expand Down Expand Up @@ -183,8 +183,13 @@ void DOMCacheStorage::retrieveCaches(CompletionHandler<void(std::optional<Except
callback(DOMCacheEngine::convertToException(DOMCacheEngine::Error::Stopped));
return;
}
RefPtr context = scriptExecutionContext();
if (!result.has_value()) {
callback(DOMCacheEngine::convertToExceptionAndLog(scriptExecutionContext(), result.error()));
callback(DOMCacheEngine::convertToExceptionAndLog(context.get(), result.error()));
return;
}
if (!context) {
callback(convertToException(DOMCacheEngine::Error::Stopped));
return;
}

Expand All @@ -193,8 +198,8 @@ void DOMCacheStorage::retrieveCaches(CompletionHandler<void(std::optional<Except
if (m_updateCounter != cachesInfo.updateCounter) {
m_updateCounter = cachesInfo.updateCounter;

m_caches = WTF::map(WTFMove(cachesInfo.infos), [this] (DOMCacheEngine::CacheInfo&& info) {
return findCacheOrCreate(WTFMove(info));
m_caches = WTF::map(WTFMove(cachesInfo.infos), [&] (DOMCacheEngine::CacheInfo&& info) {
return findCacheOrCreate(WTFMove(info), *context);
});
}
callback(std::nullopt);
Expand Down Expand Up @@ -222,23 +227,34 @@ void DOMCacheStorage::open(const String& name, DOMPromiseDeferred<IDLInterface<D

void DOMCacheStorage::doOpen(const String& name, DOMPromiseDeferred<IDLInterface<DOMCache>>&& promise)
{
RefPtr context = scriptExecutionContext();
if (!context) {
promise.reject(convertToException(DOMCacheEngine::Error::Stopped));
return;
}

auto position = m_caches.findIf([&](auto& item) { return item->name() == name; });
if (position != notFound) {
promise.resolve(DOMCache::create(*scriptExecutionContext(), String { m_caches[position]->name() }, m_caches[position]->identifier(), m_connection.copyRef()));
promise.resolve(DOMCache::create(*context, String { m_caches[position]->name() }, m_caches[position]->identifier(), m_connection.copyRef()));
return;
}

m_connection->open(*origin(), name, [this, name, promise = WTFMove(promise), pendingActivity = makePendingActivity(*this), connectionStorageLock = makeUnique<ConnectionStorageLock>(m_connection.copyRef(), *origin())](const DOMCacheEngine::CacheIdentifierOrError& result) mutable {
if (!result.has_value())
promise.reject(DOMCacheEngine::convertToExceptionAndLog(scriptExecutionContext(), result.error()));
else {
if (result.value().hadStorageError)
logConsolePersistencyError(scriptExecutionContext(), name);

auto cache = DOMCache::create(*scriptExecutionContext(), String { name }, result.value().identifier, m_connection.copyRef());
promise.resolve(cache);
m_caches.append(WTFMove(cache));
RefPtr context = scriptExecutionContext();
if (!result.has_value()) {
promise.reject(DOMCacheEngine::convertToExceptionAndLog(context.get(), result.error()));
return;
}
if (!context) {
promise.reject(convertToException(DOMCacheEngine::Error::Stopped));
return;
}
if (result.value().hadStorageError)
logConsolePersistencyError(context.get(), name);

auto cache = DOMCache::create(*context, String { name }, result.value().identifier, m_connection.copyRef());
promise.resolve(cache);
m_caches.append(WTFMove(cache));
});
}

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/Modules/cache/DOMCacheStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DOMCacheStorage : public RefCounted<DOMCacheStorage>, public ActiveDOMObje
void doRemove(const String&, DOMPromiseDeferred<IDLBoolean>&&);
void doSequentialMatch(DOMCache::RequestInfo&&, CacheQueryOptions&&, Ref<DeferredPromise>&&);
void retrieveCaches(CompletionHandler<void(std::optional<Exception>&&)>&&);
Ref<DOMCache> findCacheOrCreate(DOMCacheEngine::CacheInfo&&);
Ref<DOMCache> findCacheOrCreate(DOMCacheEngine::CacheInfo&&, ScriptExecutionContext&);
std::optional<ClientOrigin> origin() const;

Vector<Ref<DOMCache>> m_caches;
Expand Down

0 comments on commit 5b9cab4

Please sign in to comment.