Skip to content

Commit

Permalink
Cherry-pick 274013@main (656c046). https://bugs.webkit.org/show_bug.c…
Browse files Browse the repository at this point in the history
…gi?id=268625

    Null check scriptExecutionContext before using it in DOMCacheStorage.cpp
    https://bugs.webkit.org/show_bug.cgi?id=268625
    rdar://122170377

    Reviewed by Sihui Liu, Brent Fulgham and Youenn Fablet.

    * Source/WebCore/Modules/cache/DOMCache.cpp:
    (WebCore::DOMCache::create):
    (WebCore::DOMCache::DOMCache):
    * Source/WebCore/Modules/cache/DOMCache.h:
    * Source/WebCore/Modules/cache/DOMCacheStorage.cpp:
    (WebCore::DOMCacheStorage::findCacheOrCreate):
    (WebCore::DOMCacheStorage::doOpen):

    Canonical link: https://commits.webkit.org/274013@main

Canonical link: https://commits.webkit.org/266719.363@webkitglib/2.42
  • Loading branch information
cdumez authored and aperezdc committed Mar 14, 2024
1 parent 301a10b commit bc24a92
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
40 changes: 27 additions & 13 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());
}

void DOMCacheStorage::retrieveCaches(CompletionHandler<void(std::optional<Exception>&&)>&& callback)
Expand All @@ -162,8 +162,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 @@ -172,8 +177,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 @@ -201,20 +206,29 @@ 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)](const DOMCacheEngine::CacheIdentifierOrError& result) mutable {
RefPtr context = scriptExecutionContext();
if (!result.has_value())
promise.reject(DOMCacheEngine::convertToExceptionAndLog(scriptExecutionContext(), result.error()));
else {
promise.reject(DOMCacheEngine::convertToExceptionAndLog(context.get(), result.error()));
else if (!context) {
promise.reject(convertToException(DOMCacheEngine::Error::Stopped));
} else {
if (result.value().hadStorageError)
logConsolePersistencyError(scriptExecutionContext(), name);
logConsolePersistencyError(context.get(), name);

auto cache = DOMCache::create(*scriptExecutionContext(), String { name }, result.value().identifier, m_connection.copyRef());
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 bc24a92

Please sign in to comment.