diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
commit | da4c7e7ed675c3bf405668739c3012d140856109 (patch) | |
tree | cdd868dba063fecba609a1d819de271f0d51b23e /dom/url | |
parent | Adding upstream version 125.0.3. (diff) | |
download | firefox-da4c7e7ed675c3bf405668739c3012d140856109.tar.xz firefox-da4c7e7ed675c3bf405668739c3012d140856109.zip |
Adding upstream version 126.0.upstream/126.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/url')
-rw-r--r-- | dom/url/URL.cpp | 223 | ||||
-rw-r--r-- | dom/url/URL.h | 85 | ||||
-rw-r--r-- | dom/url/URLMainThread.cpp | 33 | ||||
-rw-r--r-- | dom/url/URLMainThread.h | 11 | ||||
-rw-r--r-- | dom/url/URLSearchParams.cpp | 60 | ||||
-rw-r--r-- | dom/url/URLSearchParams.h | 26 | ||||
-rw-r--r-- | dom/url/URLWorker.cpp | 40 | ||||
-rw-r--r-- | dom/url/URLWorker.h | 16 |
8 files changed, 213 insertions, 281 deletions
diff --git a/dom/url/URL.cpp b/dom/url/URL.cpp index 799bb72cff..1011eda210 100644 --- a/dom/url/URL.cpp +++ b/dom/url/URL.cpp @@ -36,8 +36,8 @@ JSObject* URL::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { /* static */ already_AddRefed<URL> URL::Constructor(const GlobalObject& aGlobal, - const nsAString& aURL, - const Optional<nsAString>& aBase, + const nsACString& aURL, + const Optional<nsACString>& aBase, ErrorResult& aRv) { if (aBase.WasPassed()) { return Constructor(aGlobal.GetAsSupports(), aURL, aBase.Value(), aRv); @@ -48,20 +48,13 @@ already_AddRefed<URL> URL::Constructor(const GlobalObject& aGlobal, /* static */ already_AddRefed<URL> URL::Constructor(nsISupports* aParent, - const nsAString& aURL, - const nsAString& aBase, + const nsACString& aURL, + const nsACString& aBase, ErrorResult& aRv) { - // Don't use NS_ConvertUTF16toUTF8 because that doesn't let us handle OOM. - nsAutoCString base; - if (!AppendUTF16toUTF8(aBase, base, fallible)) { - aRv.Throw(NS_ERROR_OUT_OF_MEMORY); - return nullptr; - } - nsCOMPtr<nsIURI> baseUri; - nsresult rv = NS_NewURI(getter_AddRefs(baseUri), base); + nsresult rv = NS_NewURI(getter_AddRefs(baseUri), aBase); if (NS_WARN_IF(NS_FAILED(rv))) { - aRv.ThrowTypeError<MSG_INVALID_URL>(base); + aRv.ThrowTypeError<MSG_INVALID_URL>(aBase); return nullptr; } @@ -70,21 +63,14 @@ already_AddRefed<URL> URL::Constructor(nsISupports* aParent, /* static */ already_AddRefed<URL> URL::Constructor(nsISupports* aParent, - const nsAString& aURL, nsIURI* aBase, + const nsACString& aURL, nsIURI* aBase, ErrorResult& aRv) { - // Don't use NS_ConvertUTF16toUTF8 because that doesn't let us handle OOM. - nsAutoCString urlStr; - if (!AppendUTF16toUTF8(aURL, urlStr, fallible)) { - aRv.Throw(NS_ERROR_OUT_OF_MEMORY); - return nullptr; - } - nsCOMPtr<nsIURI> uri; - nsresult rv = NS_NewURI(getter_AddRefs(uri), urlStr, nullptr, aBase); + nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, nullptr, aBase); if (NS_FAILED(rv)) { // No need to warn in this case. It's common to use the URL constructor // to determine if a URL is valid and an exception will be propagated. - aRv.ThrowTypeError<MSG_INVALID_URL>(urlStr); + aRv.ThrowTypeError<MSG_INVALID_URL>(aURL); return nullptr; } @@ -96,7 +82,7 @@ already_AddRefed<URL> URL::FromURI(GlobalObject& aGlobal, nsIURI* aURI) { } void URL::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, - nsAString& aResult, ErrorResult& aRv) { + nsACString& aResult, ErrorResult& aRv) { if (NS_IsMainThread()) { URLMainThread::CreateObjectURL(aGlobal, aBlob, aResult, aRv); } else { @@ -105,12 +91,12 @@ void URL::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, } void URL::CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource, - nsAString& aResult, ErrorResult& aRv) { + nsACString& aResult, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); URLMainThread::CreateObjectURL(aGlobal, aSource, aResult, aRv); } -void URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL, +void URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsACString& aURL, ErrorResult& aRv) { if (aURL.Contains('#')) { // Don't revoke URLs that contain fragments. @@ -124,7 +110,7 @@ void URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL, } } -bool URL::IsValidObjectURL(const GlobalObject& aGlobal, const nsAString& aURL, +bool URL::IsValidObjectURL(const GlobalObject& aGlobal, const nsACString& aURL, ErrorResult& aRv) { if (NS_IsMainThread()) { return URLMainThread::IsValidObjectURL(aGlobal, aURL, aRv); @@ -132,32 +118,40 @@ bool URL::IsValidObjectURL(const GlobalObject& aGlobal, const nsAString& aURL, return URLWorker::IsValidObjectURL(aGlobal, aURL, aRv); } -bool URL::CanParse(const GlobalObject& aGlobal, const nsAString& aURL, - const Optional<nsAString>& aBase) { +already_AddRefed<nsIURI> URL::ParseURI(const nsACString& aURL, + const Optional<nsACString>& aBase) { nsCOMPtr<nsIURI> baseUri; - if (aBase.WasPassed()) { - // Don't use NS_ConvertUTF16toUTF8 because that doesn't let us handle OOM. - nsAutoCString base; - if (!AppendUTF16toUTF8(aBase.Value(), base, fallible)) { - // Just return false with OOM errors as no ErrorResult. - return false; - } + nsCOMPtr<nsIURI> uri; - nsresult rv = NS_NewURI(getter_AddRefs(baseUri), base); + if (aBase.WasPassed()) { + nsresult rv = NS_NewURI(getter_AddRefs(baseUri), aBase.Value()); if (NS_FAILED(rv)) { - // Invalid base URL, return false. - return false; + return nullptr; } } - nsAutoCString urlStr; - if (!AppendUTF16toUTF8(aURL, urlStr, fallible)) { - // Just return false with OOM errors as no ErrorResult. - return false; + nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, nullptr, baseUri); + if (NS_FAILED(rv)) { + return nullptr; } - nsCOMPtr<nsIURI> uri; - return NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), urlStr, nullptr, baseUri)); + return uri.forget(); +}; + +already_AddRefed<URL> URL::Parse(const GlobalObject& aGlobal, + const nsACString& aURL, + const Optional<nsACString>& aBase) { + nsCOMPtr<nsIURI> uri = ParseURI(aURL, aBase); + if (!uri) { + return nullptr; + } + return MakeAndAddRef<URL>(aGlobal.GetAsSupports(), std::move(uri)); +} + +bool URL::CanParse(const GlobalObject& aGlobal, const nsACString& aURL, + const Optional<nsACString>& aBase) { + nsCOMPtr<nsIURI> uri = ParseURI(aURL, aBase); + return !!uri; } URLSearchParams* URL::SearchParams() { @@ -174,7 +168,7 @@ void URL::CreateSearchParamsIfNeeded() { } } -void URL::SetSearch(const nsAString& aSearch) { +void URL::SetSearch(const nsACString& aSearch) { SetSearchInternal(aSearch); UpdateURLSearchParams(); } @@ -183,35 +177,22 @@ void URL::URLSearchParamsUpdated(URLSearchParams* aSearchParams) { MOZ_ASSERT(mSearchParams); MOZ_ASSERT(mSearchParams == aSearchParams); - nsAutoString search; + nsAutoCString search; mSearchParams->Serialize(search); - SetSearchInternal(search); } -#define URL_GETTER(value, func) \ - MOZ_ASSERT(mURI); \ - value.Truncate(); \ - nsAutoCString tmp; \ - nsresult rv = mURI->func(tmp); \ - if (NS_SUCCEEDED(rv)) { \ - CopyUTF8toUTF16(tmp, value); \ - } - -void URL::GetHref(nsAString& aHref) const { URL_GETTER(aHref, GetSpec); } +#define URL_GETTER(value, func) \ + MOZ_ASSERT(mURI); \ + mURI->func(value); -void URL::SetHref(const nsAString& aHref, ErrorResult& aRv) { - // Don't use NS_ConvertUTF16toUTF8 because that doesn't let us handle OOM. - nsAutoCString href; - if (!AppendUTF16toUTF8(aHref, href, fallible)) { - aRv.Throw(NS_ERROR_OUT_OF_MEMORY); - return; - } +void URL::GetHref(nsACString& aHref) const { URL_GETTER(aHref, GetSpec); } +void URL::SetHref(const nsACString& aHref, ErrorResult& aRv) { nsCOMPtr<nsIURI> uri; - nsresult rv = NS_NewURI(getter_AddRefs(uri), href); + nsresult rv = NS_NewURI(getter_AddRefs(uri), aHref); if (NS_FAILED(rv)) { - aRv.ThrowTypeError<MSG_INVALID_URL>(href); + aRv.ThrowTypeError<MSG_INVALID_URL>(aHref); return; } @@ -219,7 +200,7 @@ void URL::SetHref(const nsAString& aHref, ErrorResult& aRv) { UpdateURLSearchParams(); } -void URL::GetOrigin(nsAString& aOrigin) const { +void URL::GetOrigin(nsACString& aOrigin) const { nsresult rv = nsContentUtils::GetWebExposedOriginSerialization(URI(), aOrigin); if (NS_WARN_IF(NS_FAILED(rv))) { @@ -227,12 +208,12 @@ void URL::GetOrigin(nsAString& aOrigin) const { } } -void URL::GetProtocol(nsAString& aProtocol) const { +void URL::GetProtocol(nsACString& aProtocol) const { URL_GETTER(aProtocol, GetScheme); aProtocol.Append(char16_t(':')); } -void URL::SetProtocol(const nsAString& aProtocol) { +void URL::SetProtocol(const nsACString& aProtocol) { nsCOMPtr<nsIURI> uri(URI()); if (!uri) { return; @@ -244,81 +225,67 @@ void URL::SetProtocol(const nsAString& aProtocol) { mURI = std::move(uri); } -void URL::GetUsername(nsAString& aUsername) const { +void URL::GetUsername(nsACString& aUsername) const { URL_GETTER(aUsername, GetUsername); } -void URL::SetUsername(const nsAString& aUsername) { +void URL::SetUsername(const nsACString& aUsername) { MOZ_ASSERT(mURI); - - Unused << NS_MutateURI(mURI) - .SetUsername(NS_ConvertUTF16toUTF8(aUsername)) - .Finalize(mURI); + Unused << NS_MutateURI(mURI).SetUsername(aUsername).Finalize(mURI); } -void URL::GetPassword(nsAString& aPassword) const { +void URL::GetPassword(nsACString& aPassword) const { URL_GETTER(aPassword, GetPassword); } -void URL::SetPassword(const nsAString& aPassword) { +void URL::SetPassword(const nsACString& aPassword) { MOZ_ASSERT(mURI); - Unused << NS_MutateURI(mURI) - .SetPassword(NS_ConvertUTF16toUTF8(aPassword)) - .Finalize(mURI); + Unused << NS_MutateURI(mURI).SetPassword(aPassword).Finalize(mURI); } -void URL::GetHost(nsAString& aHost) const { URL_GETTER(aHost, GetHostPort); } +void URL::GetHost(nsACString& aHost) const { URL_GETTER(aHost, GetHostPort); } -void URL::SetHost(const nsAString& aHost) { +void URL::SetHost(const nsACString& aHost) { MOZ_ASSERT(mURI); - - Unused << NS_MutateURI(mURI) - .SetHostPort(NS_ConvertUTF16toUTF8(aHost)) - .Finalize(mURI); + Unused << NS_MutateURI(mURI).SetHostPort(aHost).Finalize(mURI); } -void URL::GetHostname(nsAString& aHostname) const { +void URL::GetHostname(nsACString& aHostname) const { MOZ_ASSERT(mURI); - aHostname.Truncate(); nsContentUtils::GetHostOrIPv6WithBrackets(mURI, aHostname); } -void URL::SetHostname(const nsAString& aHostname) { +void URL::SetHostname(const nsACString& aHostname) { MOZ_ASSERT(mURI); // nsStandardURL returns NS_ERROR_UNEXPECTED for an empty hostname // The return code is silently ignored - mozilla::Unused << NS_MutateURI(mURI) - .SetHost(NS_ConvertUTF16toUTF8(aHostname)) - .Finalize(mURI); + Unused << NS_MutateURI(mURI).SetHost(aHostname).Finalize(mURI); } -void URL::GetPort(nsAString& aPort) const { +void URL::GetPort(nsACString& aPort) const { MOZ_ASSERT(mURI); - aPort.Truncate(); int32_t port; nsresult rv = mURI->GetPort(&port); if (NS_SUCCEEDED(rv) && port != -1) { - nsAutoString portStr; - portStr.AppendInt(port, 10); - aPort.Assign(portStr); + aPort.AppendInt(port, 10); } } -void URL::SetPort(const nsAString& aPort) { +void URL::SetPort(const nsACString& aPort) { nsresult rv; - nsAutoString portStr(aPort); + nsAutoCString portStr(aPort); int32_t port = -1; // nsIURI uses -1 as default value. portStr.StripTaggedASCII(ASCIIMask::MaskCRLFTab()); if (!portStr.IsEmpty()) { // To be valid, the port must start with an ASCII digit. - // (nsAString::ToInteger ignores leading junk, so check before calling.) + // (nsACString::ToInteger ignores leading junk, so check before calling.) if (!IsAsciiDigit(portStr[0])) { return; } @@ -331,32 +298,21 @@ void URL::SetPort(const nsAString& aPort) { Unused << NS_MutateURI(mURI).SetPort(port).Finalize(mURI); } -void URL::GetPathname(nsAString& aPathname) const { +void URL::GetPathname(nsACString& aPathname) const { MOZ_ASSERT(mURI); - - aPathname.Truncate(); - // Do not throw! Not having a valid URI or URL should result in an empty // string. - - nsAutoCString file; - nsresult rv = mURI->GetFilePath(file); - if (NS_SUCCEEDED(rv)) { - CopyUTF8toUTF16(file, aPathname); - } + mURI->GetFilePath(aPathname); } -void URL::SetPathname(const nsAString& aPathname) { +void URL::SetPathname(const nsACString& aPathname) { MOZ_ASSERT(mURI); // Do not throw! - - Unused << NS_MutateURI(mURI) - .SetFilePath(NS_ConvertUTF16toUTF8(aPathname)) - .Finalize(mURI); + Unused << NS_MutateURI(mURI).SetFilePath(aPathname).Finalize(mURI); } -void URL::GetSearch(nsAString& aSearch) const { +void URL::GetSearch(nsACString& aSearch) const { MOZ_ASSERT(mURI); aSearch.Truncate(); @@ -364,44 +320,33 @@ void URL::GetSearch(nsAString& aSearch) const { // Do not throw! Not having a valid URI or URL should result in an empty // string. - nsAutoCString search; nsresult rv; - - rv = mURI->GetQuery(search); - if (NS_SUCCEEDED(rv) && !search.IsEmpty()) { - aSearch.Assign(u'?'); - AppendUTF8toUTF16(search, aSearch); + rv = mURI->GetQuery(aSearch); + if (NS_SUCCEEDED(rv) && !aSearch.IsEmpty()) { + aSearch.Insert('?', 0); } } -void URL::GetHash(nsAString& aHash) const { +void URL::GetHash(nsACString& aHash) const { MOZ_ASSERT(mURI); - aHash.Truncate(); - - nsAutoCString ref; - nsresult rv = mURI->GetRef(ref); - if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) { - aHash.Assign(char16_t('#')); - AppendUTF8toUTF16(ref, aHash); + nsresult rv = mURI->GetRef(aHash); + if (NS_SUCCEEDED(rv) && !aHash.IsEmpty()) { + aHash.Insert('#', 0); } } -void URL::SetHash(const nsAString& aHash) { +void URL::SetHash(const nsACString& aHash) { MOZ_ASSERT(mURI); - Unused - << NS_MutateURI(mURI).SetRef(NS_ConvertUTF16toUTF8(aHash)).Finalize(mURI); + Unused << NS_MutateURI(mURI).SetRef(aHash).Finalize(mURI); } -void URL::SetSearchInternal(const nsAString& aSearch) { +void URL::SetSearchInternal(const nsACString& aSearch) { MOZ_ASSERT(mURI); // Ignore failures to be compatible with NS4. - - Unused << NS_MutateURI(mURI) - .SetQuery(NS_ConvertUTF16toUTF8(aSearch)) - .Finalize(mURI); + Unused << NS_MutateURI(mURI).SetQuery(aSearch).Finalize(mURI); } void URL::UpdateURLSearchParams() { diff --git a/dom/url/URL.h b/dom/url/URL.h index 7c2563d095..cf72a7be63 100644 --- a/dom/url/URL.h +++ b/dom/url/URL.h @@ -44,79 +44,73 @@ class URL final : public URLSearchParamsObserver, public nsWrapperCache { JS::Handle<JSObject*> aGivenProto) override; static already_AddRefed<URL> Constructor(const GlobalObject& aGlobal, - const nsAString& aURL, - const Optional<nsAString>& aBase, + const nsACString& aURL, + const Optional<nsACString>& aBase, ErrorResult& aRv); static already_AddRefed<URL> Constructor(nsISupports* aParent, - const nsAString& aURL, - const nsAString& aBase, + const nsACString& aURL, + const nsACString& aBase, ErrorResult& aRv); static already_AddRefed<URL> Constructor(nsISupports* aParent, - const nsAString& aURL, nsIURI* aBase, - ErrorResult& aRv); + const nsACString& aURL, + nsIURI* aBase, ErrorResult& aRv); static void CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, - nsAString& aResult, ErrorResult& aRv); + nsACString& aResult, ErrorResult& aRv); static void CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource, - nsAString& aResult, ErrorResult& aRv); + nsACString& aResult, ErrorResult& aRv); static void RevokeObjectURL(const GlobalObject& aGlobal, - const nsAString& aURL, ErrorResult& aRv); + const nsACString& aURL, ErrorResult& aRv); static bool IsValidObjectURL(const GlobalObject& aGlobal, - const nsAString& aURL, ErrorResult& aRv); - - static bool CanParse(const GlobalObject& aGlobal, const nsAString& aURL, - const Optional<nsAString>& aBase); - - void GetHref(nsAString& aHref) const; - - void SetHref(const nsAString& aHref, ErrorResult& aRv); - - void GetOrigin(nsAString& aOrigin) const; - - void GetProtocol(nsAString& aProtocol) const; + const nsACString& aURL, ErrorResult& aRv); - void SetProtocol(const nsAString& aProtocol); + static already_AddRefed<URL> Parse(const GlobalObject& aGlobal, + const nsACString& aURL, + const Optional<nsACString>& aBase); - void GetUsername(nsAString& aUsername) const; + static bool CanParse(const GlobalObject& aGlobal, const nsACString& aURL, + const Optional<nsACString>& aBase); - void SetUsername(const nsAString& aUsername); + void GetHref(nsACString& aHref) const; + void SetHref(const nsACString& aHref, ErrorResult& aRv); - void GetPassword(nsAString& aPassword) const; + void GetOrigin(nsACString& aOrigin) const; - void SetPassword(const nsAString& aPassword); + void GetProtocol(nsACString& aProtocol) const; + void SetProtocol(const nsACString& aProtocol); - void GetHost(nsAString& aHost) const; + void GetUsername(nsACString& aUsername) const; + void SetUsername(const nsACString& aUsername); - void SetHost(const nsAString& aHost); + void GetPassword(nsACString& aPassword) const; + void SetPassword(const nsACString& aPassword); - void GetHostname(nsAString& aHostname) const; + void GetHost(nsACString& aHost) const; + void SetHost(const nsACString& aHost); - void SetHostname(const nsAString& aHostname); + void GetHostname(nsACString& aHostname) const; + void SetHostname(const nsACString& aHostname); - void GetPort(nsAString& aPort) const; + void GetPort(nsACString& aPort) const; + void SetPort(const nsACString& aPort); - void SetPort(const nsAString& aPort); + void GetPathname(nsACString& aPathname) const; + void SetPathname(const nsACString& aPathname); - void GetPathname(nsAString& aPathname) const; - - void SetPathname(const nsAString& aPathname); - - void GetSearch(nsAString& aSearch) const; - - void SetSearch(const nsAString& aSearch); + void GetSearch(nsACString& aSearch) const; + void SetSearch(const nsACString& aSearch); URLSearchParams* SearchParams(); - void GetHash(nsAString& aHost) const; + void GetHash(nsACString& aHash) const; + void SetHash(const nsACString& aHash); - void SetHash(const nsAString& aHash); - - void ToJSON(nsAString& aResult) const { GetHref(aResult); } + void ToJSON(nsACString& aResult) const { GetHref(aResult); } // URLSearchParamsObserver void URLSearchParamsUpdated(URLSearchParams* aSearchParams) override; @@ -127,10 +121,13 @@ class URL final : public URLSearchParamsObserver, public nsWrapperCache { private: ~URL() = default; + static already_AddRefed<nsIURI> ParseURI(const nsACString& aURL, + const Optional<nsACString>& aBase); + void UpdateURLSearchParams(); private: - void SetSearchInternal(const nsAString& aSearch); + void SetSearchInternal(const nsACString& aSearch); void CreateSearchParamsIfNeeded(); diff --git a/dom/url/URLMainThread.cpp b/dom/url/URLMainThread.cpp index 95afd0db45..fb57be4401 100644 --- a/dom/url/URLMainThread.cpp +++ b/dom/url/URLMainThread.cpp @@ -19,7 +19,7 @@ namespace mozilla::dom { /* static */ void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, - nsAString& aResult, ErrorResult& aRv) { + nsACString& aResult, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); @@ -41,20 +41,18 @@ void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); - nsAutoCString url; aRv = BlobURLProtocolHandler::AddDataEntry( - aBlob.Impl(), principal, NS_ConvertUTF16toUTF8(partKey), url); + aBlob.Impl(), principal, NS_ConvertUTF16toUTF8(partKey), aResult); if (NS_WARN_IF(aRv.Failed())) { return; } - global->RegisterHostObjectURI(url); - CopyASCIItoUTF16(url, aResult); + global->RegisterHostObjectURI(aResult); } /* static */ void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal, - MediaSource& aSource, nsAString& aResult, + MediaSource& aSource, nsACString& aResult, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); @@ -77,25 +75,23 @@ void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal, nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); - nsAutoCString url; aRv = BlobURLProtocolHandler::AddDataEntry( - &aSource, principal, NS_ConvertUTF16toUTF8(partKey), url); + &aSource, principal, NS_ConvertUTF16toUTF8(partKey), aResult); if (NS_WARN_IF(aRv.Failed())) { return; } nsCOMPtr<nsIRunnable> revocation = NS_NewRunnableFunction( - "dom::URLMainThread::CreateObjectURL", - [url] { BlobURLProtocolHandler::RemoveDataEntry(url); }); + "dom::URLMainThread::CreateObjectURL", [result = nsCString(aResult)] { + BlobURLProtocolHandler::RemoveDataEntry(result); + }); nsContentUtils::RunInStableState(revocation.forget()); - - CopyASCIItoUTF16(url, aResult); } /* static */ void URLMainThread::RevokeObjectURL(const GlobalObject& aGlobal, - const nsAString& aURL, ErrorResult& aRv) { + const nsACString& aURL, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); if (!global) { @@ -113,21 +109,18 @@ void URLMainThread::RevokeObjectURL(const GlobalObject& aGlobal, } } - NS_LossyConvertUTF16toASCII asciiurl(aURL); - if (BlobURLProtocolHandler::RemoveDataEntry( - asciiurl, nsContentUtils::ObjectPrincipal(aGlobal.Get()), + aURL, nsContentUtils::ObjectPrincipal(aGlobal.Get()), NS_ConvertUTF16toUTF8(partKey))) { - global->UnregisterHostObjectURI(asciiurl); + global->UnregisterHostObjectURI(aURL); } } /* static */ bool URLMainThread::IsValidObjectURL(const GlobalObject& aGlobal, - const nsAString& aURL, ErrorResult& aRv) { + const nsACString& aURL, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); - NS_LossyConvertUTF16toASCII asciiurl(aURL); - return BlobURLProtocolHandler::HasDataEntry(asciiurl); + return BlobURLProtocolHandler::HasDataEntry(aURL); } } // namespace mozilla::dom diff --git a/dom/url/URLMainThread.h b/dom/url/URLMainThread.h index 5314f2b3e0..1d419aab78 100644 --- a/dom/url/URLMainThread.h +++ b/dom/url/URLMainThread.h @@ -14,16 +14,13 @@ namespace mozilla::dom { class URLMainThread final { public: static void CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, - nsAString& aResult, ErrorResult& aRv); - + nsACString& aResult, ErrorResult& aRv); static void CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource, - nsAString& aResult, ErrorResult& aRv); - + nsACString& aResult, ErrorResult& aRv); static void RevokeObjectURL(const GlobalObject& aGlobal, - const nsAString& aURL, ErrorResult& aRv); - + const nsACString& aURL, ErrorResult& aRv); static bool IsValidObjectURL(const GlobalObject& aGlobal, - const nsAString& aURL, ErrorResult& aRv); + const nsACString& aURL, ErrorResult& aRv); }; } // namespace mozilla::dom diff --git a/dom/url/URLSearchParams.cpp b/dom/url/URLSearchParams.cpp index d43217c8b6..c141766e83 100644 --- a/dom/url/URLSearchParams.cpp +++ b/dom/url/URLSearchParams.cpp @@ -60,23 +60,24 @@ JSObject* URLSearchParams::WrapObject(JSContext* aCx, /* static */ already_AddRefed<URLSearchParams> URLSearchParams::Constructor( const GlobalObject& aGlobal, - const USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString& aInit, + const UTF8StringSequenceSequenceOrUTF8StringUTF8StringRecordOrUTF8String& + aInit, ErrorResult& aRv) { RefPtr<URLSearchParams> sp = new URLSearchParams(aGlobal.GetAsSupports(), nullptr); - if (aInit.IsUSVString()) { - NS_ConvertUTF16toUTF8 input(aInit.GetAsUSVString()); + if (aInit.IsUTF8String()) { + const auto& input = aInit.GetAsUTF8String(); if (StringBeginsWith(input, "?"_ns)) { sp->ParseInput(Substring(input, 1, input.Length() - 1)); } else { sp->ParseInput(input); } - } else if (aInit.IsUSVStringSequenceSequence()) { - const Sequence<Sequence<nsString>>& list = - aInit.GetAsUSVStringSequenceSequence(); + } else if (aInit.IsUTF8StringSequenceSequence()) { + const Sequence<Sequence<nsCString>>& list = + aInit.GetAsUTF8StringSequenceSequence(); for (uint32_t i = 0; i < list.Length(); ++i) { - const Sequence<nsString>& item = list[i]; + const Sequence<nsCString>& item = list[i]; if (item.Length() != 2) { nsPrintfCString err("Expected 2 items in pair but got %zu", item.Length()); @@ -85,9 +86,9 @@ already_AddRefed<URLSearchParams> URLSearchParams::Constructor( } sp->Append(item[0], item[1]); } - } else if (aInit.IsUSVStringUSVStringRecord()) { - const Record<nsString, nsString>& record = - aInit.GetAsUSVStringUSVStringRecord(); + } else if (aInit.IsUTF8StringUTF8StringRecord()) { + const Record<nsCString, nsCString>& record = + aInit.GetAsUTF8StringUTF8StringRecord(); for (auto& entry : record.Entries()) { sp->Append(entry.mKey, entry.mValue); } @@ -104,35 +105,36 @@ void URLSearchParams::ParseInput(const nsACString& aInput) { uint32_t URLSearchParams::Size() const { return mParams->Length(); } -void URLSearchParams::Get(const nsAString& aName, nsString& aRetval) { +void URLSearchParams::Get(const nsACString& aName, nsACString& aRetval) { return mParams->Get(aName, aRetval); } -void URLSearchParams::GetAll(const nsAString& aName, - nsTArray<nsString>& aRetval) { +void URLSearchParams::GetAll(const nsACString& aName, + nsTArray<nsCString>& aRetval) { return mParams->GetAll(aName, aRetval); } -void URLSearchParams::Set(const nsAString& aName, const nsAString& aValue) { +void URLSearchParams::Set(const nsACString& aName, const nsACString& aValue) { mParams->Set(aName, aValue); NotifyObserver(); } -void URLSearchParams::Append(const nsAString& aName, const nsAString& aValue) { +void URLSearchParams::Append(const nsACString& aName, + const nsACString& aValue) { mParams->Append(aName, aValue); NotifyObserver(); } -bool URLSearchParams::Has(const nsAString& aName, - const Optional<nsAString>& aValue) { +bool URLSearchParams::Has(const nsACString& aName, + const Optional<nsACString>& aValue) { if (!aValue.WasPassed()) { return mParams->Has(aName); } return mParams->Has(aName, aValue.Value()); } -void URLSearchParams::Delete(const nsAString& aName, - const Optional<nsAString>& aValue) { +void URLSearchParams::Delete(const nsACString& aName, + const Optional<nsACString>& aValue) { if (!aValue.WasPassed()) { mParams->Delete(aName); NotifyObserver(); @@ -144,10 +146,17 @@ void URLSearchParams::Delete(const nsAString& aName, void URLSearchParams::DeleteAll() { mParams->DeleteAll(); } -void URLSearchParams::Serialize(nsAString& aValue) const { +void URLSearchParams::Serialize(nsACString& aValue) const { mParams->Serialize(aValue, true); } +// TODO(emilio): Allow stringifier attributes with CString return values. +void URLSearchParams::Stringify(nsAString& aValue) const { + nsAutoCString serialized; + mParams->Serialize(serialized, true); + CopyUTF8toUTF16(serialized, aValue); +} + void URLSearchParams::NotifyObserver() { if (mObserver) { mObserver->URLSearchParamsUpdated(this); @@ -158,11 +167,11 @@ uint32_t URLSearchParams::GetIterableLength() const { return mParams->Length(); } -const nsAString& URLSearchParams::GetKeyAtIndex(uint32_t aIndex) const { +const nsACString& URLSearchParams::GetKeyAtIndex(uint32_t aIndex) const { return mParams->GetKeyAtIndex(aIndex); } -const nsAString& URLSearchParams::GetValueAtIndex(uint32_t aIndex) const { +const nsACString& URLSearchParams::GetValueAtIndex(uint32_t aIndex) const { return mParams->GetValueAtIndex(aIndex); } @@ -182,11 +191,10 @@ nsresult URLSearchParams::GetSendInfo(nsIInputStream** aBody, "application/x-www-form-urlencoded;charset=UTF-8"); aCharset.AssignLiteral("UTF-8"); - nsAutoString serialized; + nsAutoCString serialized; Serialize(serialized); - NS_ConvertUTF16toUTF8 converted(serialized); - *aContentLength = converted.Length(); - return NS_NewCStringInputStream(aBody, std::move(converted)); + *aContentLength = serialized.Length(); + return NS_NewCStringInputStream(aBody, std::move(serialized)); } } // namespace mozilla::dom diff --git a/dom/url/URLSearchParams.h b/dom/url/URLSearchParams.h index 9ddff1e512..25bd293f4d 100644 --- a/dom/url/URLSearchParams.h +++ b/dom/url/URLSearchParams.h @@ -37,7 +37,7 @@ namespace dom { class GlobalObject; class URLSearchParams; -class USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString; +class UTF8StringSequenceSequenceOrUTF8StringUTF8StringRecordOrUTF8String; template <typename T> class Optional; @@ -66,42 +66,42 @@ class URLSearchParams final : public nsISupports, public nsWrapperCache { static already_AddRefed<URLSearchParams> Constructor( const GlobalObject& aGlobal, - const USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString& + const UTF8StringSequenceSequenceOrUTF8StringUTF8StringRecordOrUTF8String& aInit, ErrorResult& aRv); void ParseInput(const nsACString& aInput); - void Serialize(nsAString& aValue) const; + void Serialize(nsACString& aValue) const; uint32_t Size() const; - void Get(const nsAString& aName, nsString& aRetval); + void Get(const nsACString& aName, nsACString& aRetval); - void GetAll(const nsAString& aName, nsTArray<nsString>& aRetval); + void GetAll(const nsACString& aName, nsTArray<nsCString>& aRetval); - void Set(const nsAString& aName, const nsAString& aValue); + void Set(const nsACString& aName, const nsACString& aValue); - void Append(const nsAString& aName, const nsAString& aValue); + void Append(const nsACString& aName, const nsACString& aValue); - bool Has(const nsAString& aName, const Optional<nsAString>& aValue); + bool Has(const nsACString& aName, const Optional<nsACString>& aValue); - void Delete(const nsAString& aName, const Optional<nsAString>& aValue); + void Delete(const nsACString& aName, const Optional<nsACString>& aValue); uint32_t GetIterableLength() const; - const nsAString& GetKeyAtIndex(uint32_t aIndex) const; - const nsAString& GetValueAtIndex(uint32_t aIndex) const; + const nsACString& GetKeyAtIndex(uint32_t aIndex) const; + const nsACString& GetValueAtIndex(uint32_t aIndex) const; void Sort(ErrorResult& aRv); - void Stringify(nsString& aRetval) const { Serialize(aRetval); } + void Stringify(nsAString&) const; nsresult GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength, nsACString& aContentTypeWithCharset, nsACString& aCharset) const; private: - void AppendInternal(const nsAString& aName, const nsAString& aValue); + void AppendInternal(const nsACString& aName, const nsACString& aValue); void DeleteAll(); diff --git a/dom/url/URLWorker.cpp b/dom/url/URLWorker.cpp index e4ae3d6248..cb902b8cd3 100644 --- a/dom/url/URLWorker.cpp +++ b/dom/url/URLWorker.cpp @@ -18,11 +18,11 @@ namespace mozilla::dom { class CreateURLRunnable : public WorkerMainThreadRunnable { private: BlobImpl* mBlobImpl; - nsAString& mURL; + nsACString& mURL; public: CreateURLRunnable(WorkerPrivate* aWorkerPrivate, BlobImpl* aBlobImpl, - nsAString& aURL) + nsACString& aURL) : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: CreateURL"_ns), mBlobImpl(aBlobImpl), mURL(aURL) { @@ -42,17 +42,13 @@ class CreateURLRunnable : public WorkerMainThreadRunnable { nsAutoString partKey; cookieJarSettings->GetPartitionKey(partKey); - nsAutoCString url; nsresult rv = BlobURLProtocolHandler::AddDataEntry( - mBlobImpl, principal, NS_ConvertUTF16toUTF8(partKey), url); - + mBlobImpl, principal, NS_ConvertUTF16toUTF8(partKey), mURL); if (NS_FAILED(rv)) { NS_WARNING("Failed to add data entry for the blob!"); - SetDOMStringToNull(mURL); + mURL.SetIsVoid(true); return false; } - - CopyUTF8toUTF16(url, mURL); return true; } }; @@ -60,26 +56,24 @@ class CreateURLRunnable : public WorkerMainThreadRunnable { // This class revokes an URL on the main thread. class RevokeURLRunnable : public WorkerMainThreadRunnable { private: - const nsString mURL; + const nsCString mURL; public: - RevokeURLRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aURL) + RevokeURLRunnable(WorkerPrivate* aWorkerPrivate, const nsACString& aURL) : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: RevokeURL"_ns), mURL(aURL) {} bool MainThreadRun() override { AssertIsOnMainThread(); - NS_ConvertUTF16toUTF8 url(mURL); - nsCOMPtr<nsICookieJarSettings> cookieJarSettings = mWorkerPrivate->CookieJarSettings(); nsAutoString partKey; cookieJarSettings->GetPartitionKey(partKey); - BlobURLProtocolHandler::RemoveDataEntry(url, mWorkerPrivate->GetPrincipal(), - NS_ConvertUTF16toUTF8(partKey)); + BlobURLProtocolHandler::RemoveDataEntry( + mURL, mWorkerPrivate->GetPrincipal(), NS_ConvertUTF16toUTF8(partKey)); return true; } }; @@ -87,11 +81,11 @@ class RevokeURLRunnable : public WorkerMainThreadRunnable { // This class checks if an URL is valid on the main thread. class IsValidURLRunnable : public WorkerMainThreadRunnable { private: - const nsString mURL; + const nsCString mURL; bool mValid; public: - IsValidURLRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aURL) + IsValidURLRunnable(WorkerPrivate* aWorkerPrivate, const nsACString& aURL) : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: IsValidURL"_ns), mURL(aURL), mValid(false) {} @@ -99,8 +93,7 @@ class IsValidURLRunnable : public WorkerMainThreadRunnable { bool MainThreadRun() override { AssertIsOnMainThread(); - NS_ConvertUTF16toUTF8 url(mURL); - mValid = BlobURLProtocolHandler::HasDataEntry(url); + mValid = BlobURLProtocolHandler::HasDataEntry(mURL); return true; } @@ -110,7 +103,8 @@ class IsValidURLRunnable : public WorkerMainThreadRunnable { /* static */ void URLWorker::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, - nsAString& aResult, mozilla::ErrorResult& aRv) { + nsACString& aResult, + mozilla::ErrorResult& aRv) { JSContext* cx = aGlobal.Context(); WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); @@ -128,12 +122,12 @@ void URLWorker::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, WorkerGlobalScope* scope = workerPrivate->GlobalScope(); MOZ_ASSERT(scope); - scope->RegisterHostObjectURI(NS_ConvertUTF16toUTF8(aResult)); + scope->RegisterHostObjectURI(aResult); } /* static */ void URLWorker::RevokeObjectURL(const GlobalObject& aGlobal, - const nsAString& aUrl, ErrorResult& aRv) { + const nsACString& aUrl, ErrorResult& aRv) { JSContext* cx = aGlobal.Context(); WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); @@ -148,12 +142,12 @@ void URLWorker::RevokeObjectURL(const GlobalObject& aGlobal, WorkerGlobalScope* scope = workerPrivate->GlobalScope(); MOZ_ASSERT(scope); - scope->UnregisterHostObjectURI(NS_ConvertUTF16toUTF8(aUrl)); + scope->UnregisterHostObjectURI(aUrl); } /* static */ bool URLWorker::IsValidObjectURL(const GlobalObject& aGlobal, - const nsAString& aUrl, ErrorResult& aRv) { + const nsACString& aUrl, ErrorResult& aRv) { JSContext* cx = aGlobal.Context(); WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); diff --git a/dom/url/URLWorker.h b/dom/url/URLWorker.h index be8ab512a1..8424e35cca 100644 --- a/dom/url/URLWorker.h +++ b/dom/url/URLWorker.h @@ -21,22 +21,20 @@ namespace dom { class URLWorker final { public: static already_AddRefed<URLWorker> Constructor( - const GlobalObject& aGlobal, const nsAString& aURL, - const Optional<nsAString>& aBase, ErrorResult& aRv); + const GlobalObject& aGlobal, const nsACString& aURL, + const Optional<nsACString>& aBase, ErrorResult& aRv); static already_AddRefed<URLWorker> Constructor(const GlobalObject& aGlobal, - const nsAString& aURL, - const nsAString& aBase, + const nsACString& aURL, + const nsACString& aBase, ErrorResult& aRv); static void CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, - nsAString& aResult, mozilla::ErrorResult& aRv); - + nsACString& aResult, mozilla::ErrorResult& aRv); static void RevokeObjectURL(const GlobalObject& aGlobal, - const nsAString& aUrl, ErrorResult& aRv); - + const nsACString& aUrl, ErrorResult& aRv); static bool IsValidObjectURL(const GlobalObject& aGlobal, - const nsAString& aUrl, ErrorResult& aRv); + const nsACString& aUrl, ErrorResult& aRv); }; } // namespace dom |