Skip to content

Commit

Permalink
Plumb an optional top SecurityOriginData into ThreadableBlobRegistry
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=254235
rdar://problem/107024226

Reviewed by Chris Dumez.

Plumb into ThreadableBlobRegistry the top SecurityOriginData associated with
the Blob URL.

For public Blob URLs, this origin is obtained from the top document. For
internal Blob URLs, the value is std::nullopt because a blob is not inherently
bound to a top origin.

This is a pre-patch for partitioning the blob registry by top origin. This
change should have any behavioral change.

* Source/WebCore/Modules/mediasource/MediaSourceRegistry.cpp:
(WebCore::MediaSourceRegistry::unregisterURL):
* Source/WebCore/Modules/mediasource/MediaSourceRegistry.h:
* Source/WebCore/fileapi/Blob.cpp:
(WebCore::BlobURLRegistry::registerURL):
(WebCore::BlobURLRegistry::unregisterURL):
(WebCore::BlobURLRegistry::unregisterURLsForContext):
(WebCore::Blob::Blob):
(WebCore::Blob::~Blob):
* Source/WebCore/fileapi/ThreadableBlobRegistry.cpp:
(WebCore::ThreadableBlobRegistry::registerBlobURL):
(WebCore::ThreadableBlobRegistry::unregisterBlobURL):
(WebCore::ThreadableBlobRegistry::registerBlobURLHandle):
(WebCore::ThreadableBlobRegistry::unregisterBlobURLHandle):
* Source/WebCore/fileapi/ThreadableBlobRegistry.h:
* Source/WebCore/fileapi/URLKeepingBlobAlive.cpp:
(WebCore::URLKeepingBlobAlive::registerBlobURLHandleIfNecessary):
(WebCore::URLKeepingBlobAlive::unregisterBlobURLHandleIfNecessary):
* Source/WebCore/html/PublicURLManager.cpp:
(WebCore::PublicURLManager::revoke):
* Source/WebCore/html/URLRegistry.h:

Canonical link: https://commits.webkit.org/266021@main
  • Loading branch information
sysrqb authored and Matthew Finkel committed Jul 13, 2023
1 parent ae256c9 commit 6f972bb
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/Modules/mediasource/MediaSourceRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void MediaSourceRegistry::registerURL(const ScriptExecutionContext& context, con
m_mediaSources.add(urlString, std::pair { RefPtr { &source }, context.identifier() });
}

void MediaSourceRegistry::unregisterURL(const URL& url)
void MediaSourceRegistry::unregisterURL(const URL& url, const SecurityOriginData&)
{
// MediaSource objects are not exposed to workers.
if (!isMainThread())
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/Modules/mediasource/MediaSourceRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MediaSourceRegistry final : public URLRegistry {

// Registers a blob URL referring to the specified media source.
void registerURL(const ScriptExecutionContext&, const URL&, URLRegistrable&) final;
void unregisterURL(const URL&) final;
void unregisterURL(const URL&, const SecurityOriginData& topOrigin) final;
void unregisterURLsForContext(const ScriptExecutionContext&) final;
URLRegistrable* lookup(const String&) const final;

Expand Down
14 changes: 7 additions & 7 deletions Source/WebCore/fileapi/Blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ WTF_MAKE_ISO_ALLOCATED_IMPL(Blob);
class BlobURLRegistry final : public URLRegistry {
public:
void registerURL(const ScriptExecutionContext&, const URL&, URLRegistrable&) final;
void unregisterURL(const URL&) final;
void unregisterURL(const URL&, const SecurityOriginData&) final;
void unregisterURLsForContext(const ScriptExecutionContext&) final;

static URLRegistry& registry();
Expand All @@ -75,10 +75,10 @@ void BlobURLRegistry::registerURL(const ScriptExecutionContext& context, const U
Locker locker { m_urlsPerContextLock };
m_urlsPerContext.add(context.identifier(), HashSet<URL>()).iterator->value.add(publicURL.isolatedCopy());
}
ThreadableBlobRegistry::registerBlobURL(context.securityOrigin(), context.policyContainer(), publicURL, static_cast<Blob&>(blob).url());
ThreadableBlobRegistry::registerBlobURL(context.securityOrigin(), context.policyContainer(), publicURL, static_cast<Blob&>(blob).url(), context.topOrigin().data());
}

void BlobURLRegistry::unregisterURL(const URL& url)
void BlobURLRegistry::unregisterURL(const URL& url, const SecurityOriginData& topOrigin)
{
bool isURLRegistered = false;
{
Expand All @@ -95,7 +95,7 @@ void BlobURLRegistry::unregisterURL(const URL& url)
if (!isURLRegistered)
return;

ThreadableBlobRegistry::unregisterBlobURL(url);
ThreadableBlobRegistry::unregisterBlobURL(url, topOrigin);
}

void BlobURLRegistry::unregisterURLsForContext(const ScriptExecutionContext& context)
Expand All @@ -106,7 +106,7 @@ void BlobURLRegistry::unregisterURLsForContext(const ScriptExecutionContext& con
urlsForContext = m_urlsPerContext.take(context.identifier());
}
for (auto& url : urlsForContext)
ThreadableBlobRegistry::unregisterBlobURL(url);
ThreadableBlobRegistry::unregisterBlobURL(url, context.topOrigin().data());
}

URLRegistry& BlobURLRegistry::registry()
Expand Down Expand Up @@ -197,7 +197,7 @@ Blob::Blob(DeserializationContructor, ScriptExecutionContext* context, const URL
, m_internalURL(BlobURL::createInternalURL())
{
if (fileBackedPath.isEmpty())
ThreadableBlobRegistry::registerBlobURL(nullptr, { }, m_internalURL, srcURL);
ThreadableBlobRegistry::registerBlobURL(nullptr, { }, m_internalURL, srcURL, std::nullopt);
else
ThreadableBlobRegistry::registerInternalBlobURLOptionallyFileBacked(m_internalURL, srcURL, fileBackedPath, m_type);
}
Expand All @@ -214,7 +214,7 @@ Blob::Blob(ScriptExecutionContext* context, const URL& srcURL, long long start,

Blob::~Blob()
{
ThreadableBlobRegistry::unregisterBlobURL(m_internalURL);
ThreadableBlobRegistry::unregisterBlobURL(m_internalURL, std::nullopt);
while (!m_blobLoaders.isEmpty())
(*m_blobLoaders.begin())->cancel();
}
Expand Down
18 changes: 14 additions & 4 deletions Source/WebCore/fileapi/ThreadableBlobRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ static void unregisterBlobURLOriginIfNecessaryOnMainThread(const URL& url)
originMap().remove(urlWithoutFragment);
}

void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin* origin, PolicyContainer&& policyContainer, const URL& url, const URL& srcURL)
void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin* origin, PolicyContainer&& policyContainer, const URL& url, const URL& srcURL, const std::optional<SecurityOriginData>&)
{
if (isMainThread()) {
addToOriginMapIfNecessary(url, origin);
Expand All @@ -150,6 +150,11 @@ void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin* origin, PolicyConta
});
}

void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin* origin, PolicyContainer&& policyContainer, const URL& url, const URLKeepingBlobAlive& srcURL)
{
registerBlobURL(origin, std::forward<PolicyContainer>(policyContainer), url, srcURL, srcURL.topOrigin());
}

void ThreadableBlobRegistry::registerInternalBlobURLOptionallyFileBacked(const URL& url, const URL& srcURL, const String& fileBackedPath, const String& contentType)
{
ASSERT(isInternalBlobURL(url));
Expand Down Expand Up @@ -187,15 +192,20 @@ unsigned long long ThreadableBlobRegistry::blobSize(const URL& url)
return resultSize;
}

void ThreadableBlobRegistry::unregisterBlobURL(const URL& url)
void ThreadableBlobRegistry::unregisterBlobURL(const URL& url, const std::optional<SecurityOriginData>&)
{
ensureOnMainThread([url = url.isolatedCopy()] {
unregisterBlobURLOriginIfNecessaryOnMainThread(url);
blobRegistry().unregisterBlobURL(url);
});
}

void ThreadableBlobRegistry::registerBlobURLHandle(const URL& url)
void ThreadableBlobRegistry::unregisterBlobURL(const URLKeepingBlobAlive& url)
{
unregisterBlobURL(url, url.topOrigin());
}

void ThreadableBlobRegistry::registerBlobURLHandle(const URL& url, const std::optional<SecurityOriginData>&)
{
ensureOnMainThread([url = url.isolatedCopy()] {
if (isBlobURLContainsNullOrigin(url))
Expand All @@ -205,7 +215,7 @@ void ThreadableBlobRegistry::registerBlobURLHandle(const URL& url)
});
}

void ThreadableBlobRegistry::unregisterBlobURLHandle(const URL& url)
void ThreadableBlobRegistry::unregisterBlobURLHandle(const URL& url, const std::optional<SecurityOriginData>&)
{
ensureOnMainThread([url = url.isolatedCopy()] {
unregisterBlobURLOriginIfNecessaryOnMainThread(url);
Expand Down
10 changes: 6 additions & 4 deletions Source/WebCore/fileapi/ThreadableBlobRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ struct PolicyContainer;

class ThreadableBlobRegistry {
public:
static void registerBlobURL(SecurityOrigin*, PolicyContainer&&, const URL&, const URL& srcURL);
static void registerBlobURL(SecurityOrigin*, PolicyContainer&&, const URL&, const URL& srcURL, const std::optional<SecurityOriginData>& topOrigin);
static void registerBlobURL(SecurityOrigin*, PolicyContainer&&, const URL&, const URLKeepingBlobAlive& srcURL);
static void registerInternalFileBlobURL(const URL&, const String& path, const String& replacementPath, const String& contentType);
static void registerInternalBlobURL(const URL&, Vector<BlobPart>&& blobParts, const String& contentType);
static void registerInternalBlobURLOptionallyFileBacked(const URL&, const URL& srcURL, const String& fileBackedPath, const String& contentType);
static void registerInternalBlobURLForSlice(const URL& newURL, const URL& srcURL, long long start, long long end, const String& contentType);
static void unregisterBlobURL(const URL&);
static void unregisterBlobURL(const URL&, const std::optional<SecurityOriginData>& topOrigin);
static void unregisterBlobURL(const URLKeepingBlobAlive&);

static void registerBlobURLHandle(const URL&);
static void unregisterBlobURLHandle(const URL&);
static void registerBlobURLHandle(const URL&, const std::optional<SecurityOriginData>& topOrigin);
static void unregisterBlobURLHandle(const URL&, const std::optional<SecurityOriginData>& topOrigin);

WEBCORE_EXPORT static unsigned long long blobSize(const URL&);

Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/fileapi/URLKeepingBlobAlive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ URLKeepingBlobAlive& URLKeepingBlobAlive::operator=(URLKeepingBlobAlive&& other)
void URLKeepingBlobAlive::registerBlobURLHandleIfNecessary()
{
if (m_url.protocolIsBlob())
ThreadableBlobRegistry::registerBlobURLHandle(m_url);
ThreadableBlobRegistry::registerBlobURLHandle(m_url, m_topOrigin);
}

void URLKeepingBlobAlive::unregisterBlobURLHandleIfNecessary()
{
if (m_url.protocolIsBlob())
ThreadableBlobRegistry::unregisterBlobURLHandle(m_url);
ThreadableBlobRegistry::unregisterBlobURLHandle(m_url, m_topOrigin);
}

URLKeepingBlobAlive URLKeepingBlobAlive::isolatedCopy() const
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/PublicURLManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void PublicURLManager::revoke(const URL& url)
return;

URLRegistry::forEach([&](auto& registry) {
registry.unregisterURL(url);
registry.unregisterURL(url, scriptExecutionContext()->topOrigin().data());
});
}

Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/html/URLRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
namespace WebCore {

class ScriptExecutionContext;
class SecurityOriginData;
class URLRegistry;

class URLRegistrable {
Expand All @@ -53,7 +54,7 @@ class URLRegistry {

virtual ~URLRegistry();
virtual void registerURL(const ScriptExecutionContext&, const URL&, URLRegistrable&) = 0;
virtual void unregisterURL(const URL&) = 0;
virtual void unregisterURL(const URL&, const SecurityOriginData& topOrigin) = 0;
virtual void unregisterURLsForContext(const ScriptExecutionContext&) = 0;

// This is an optional API
Expand Down

0 comments on commit 6f972bb

Please sign in to comment.