#include "config.h"
#include "DOMURL.h"
#include "ActiveDOMObject.h"
#include "Blob.h"
#include "BlobURL.h"
#include "MemoryCache.h"
#include "PublicURLManager.h"
#include "ResourceRequest.h"
#include "ScriptExecutionContext.h"
#include "SecurityOrigin.h"
#include "URLSearchParams.h"
#include <wtf/MainThread.h>
namespace WebCore {
inline DOMURL::DOMURL(URL&& completeURL, const URL& baseURL)
: m_baseURL(baseURL)
, m_url(WTFMove(completeURL))
{
}
ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const URL& base)
{
if (!base.isValid())
return Exception { TypeError };
URL completeURL { base, url };
if (!completeURL.isValid())
return Exception { TypeError };
return adoptRef(*new DOMURL(WTFMove(completeURL), base));
}
ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const String& base)
{
return create(url, base.isNull() ? aboutBlankURL() : URL { URL { }, base });
}
ExceptionOr<Ref<DOMURL>> DOMURL::create(const String& url, const DOMURL& base)
{
return create(url, base.href());
}
DOMURL::~DOMURL()
{
if (m_searchParams)
m_searchParams->associatedURLDestroyed();
}
ExceptionOr<void> DOMURL::setHref(const String& url)
{
URL completeURL { m_baseURL, url };
if (!completeURL.isValid())
return Exception { TypeError };
m_url = WTFMove(completeURL);
if (m_searchParams)
m_searchParams->updateFromAssociatedURL();
return { };
}
void DOMURL::setQuery(const String& query)
{
m_url.setQuery(query);
}
String DOMURL::createObjectURL(ScriptExecutionContext& scriptExecutionContext, Blob& blob)
{
return createPublicURL(scriptExecutionContext, blob);
}
String DOMURL::createPublicURL(ScriptExecutionContext& scriptExecutionContext, URLRegistrable& registrable)
{
URL publicURL = BlobURL::createPublicURL(scriptExecutionContext.securityOrigin());
if (publicURL.isEmpty())
return String();
scriptExecutionContext.publicURLManager().registerURL(publicURL, registrable);
return publicURL.string();
}
URLSearchParams& DOMURL::searchParams()
{
if (!m_searchParams)
m_searchParams = URLSearchParams::create(search(), this);
return *m_searchParams;
}
void DOMURL::revokeObjectURL(ScriptExecutionContext& scriptExecutionContext, const String& urlString)
{
URL url(URL(), urlString);
ResourceRequest request(url);
request.setDomainForCachePartition(scriptExecutionContext.domainForCachePartition());
MemoryCache::removeRequestFromSessionCaches(scriptExecutionContext, request);
scriptExecutionContext.publicURLManager().revoke(url);
}
}