diff options
Diffstat (limited to 'dom/url')
35 files changed, 5036 insertions, 0 deletions
diff --git a/dom/url/URL.cpp b/dom/url/URL.cpp new file mode 100644 index 0000000000..1f15e11514 --- /dev/null +++ b/dom/url/URL.cpp @@ -0,0 +1,1832 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "URL.h" + +#include "DOMMediaStream.h" +#include "mozilla/dom/File.h" +#include "mozilla/dom/MediaSource.h" +#include "mozilla/dom/URLBinding.h" +#include "mozilla/dom/ipc/BlobChild.h" +#include "mozilla/dom/ipc/nsIRemoteBlob.h" +#include "mozilla/ipc/BackgroundChild.h" +#include "nsContentUtils.h" +#include "nsEscape.h" +#include "nsHostObjectProtocolHandler.h" +#include "nsIIOService.h" +#include "nsIURIWithQuery.h" +#include "nsIURL.h" +#include "nsNetCID.h" +#include "nsNetUtil.h" +#include "nsServiceManagerUtils.h" +#include "WorkerPrivate.h" +#include "WorkerRunnable.h" +#include "WorkerScope.h" + +namespace mozilla { +namespace dom { + +/////////////////////////////////////////////////////////////////////////////// +// URL for main-thread +/////////////////////////////////////////////////////////////////////////////// + +namespace { + +template<typename T> +void +CreateObjectURLInternal(const GlobalObject& aGlobal, T aObject, + nsAString& aResult, ErrorResult& aRv) +{ + nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); + if (NS_WARN_IF(!global)) { + aRv.Throw(NS_ERROR_FAILURE); + return; + } + + nsCOMPtr<nsIPrincipal> principal = + nsContentUtils::ObjectPrincipal(aGlobal.Get()); + + nsAutoCString url; + aRv = nsHostObjectProtocolHandler::AddDataEntry(aObject, principal, url); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + global->RegisterHostObjectURI(url); + CopyASCIItoUTF16(url, aResult); +} + +// The URL implementation for the main-thread +class URLMainThread final : public URL +{ +public: + static already_AddRefed<URLMainThread> + Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + URL& aBase, ErrorResult& aRv); + + static already_AddRefed<URLMainThread> + Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + const Optional<nsAString>& aBase, ErrorResult& aRv); + + static already_AddRefed<URLMainThread> + Constructor(nsISupports* aParent, const nsAString& aURL, + const nsAString& aBase, ErrorResult& aRv); + + static already_AddRefed<URLMainThread> + Constructor(nsISupports* aParent, const nsAString& aURL, nsIURI* aBase, + ErrorResult& aRv); + + static void + CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, + const objectURLOptions& aOptions, nsAString& aResult, + ErrorResult& aRv) + { + MOZ_ASSERT(NS_IsMainThread()); + CreateObjectURLInternal(aGlobal, aBlob.Impl(), aResult, aRv); + } + + static void + CreateObjectURL(const GlobalObject& aGlobal, DOMMediaStream& aStream, + const objectURLOptions& aOptions, nsAString& aResult, + ErrorResult& aRv) + { + MOZ_ASSERT(NS_IsMainThread()); + CreateObjectURLInternal(aGlobal, &aStream, aResult, aRv); + } + + static void + CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource, + const objectURLOptions& aOptions, nsAString& aResult, + ErrorResult& aRv); + + static void + RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL, + ErrorResult& aRv); + + static bool + IsValidURL(const GlobalObject& aGlobal, const nsAString& aURL, + ErrorResult& aRv); + + URLMainThread(nsISupports* aParent, already_AddRefed<nsIURI> aURI) + : URL(aParent) + , mURI(aURI) + { + MOZ_ASSERT(NS_IsMainThread()); + } + + virtual void + GetHref(nsAString& aHref, ErrorResult& aRv) const override; + + virtual void + SetHref(const nsAString& aHref, ErrorResult& aRv) override; + + virtual void + GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const override; + + virtual void + GetProtocol(nsAString& aProtocol, ErrorResult& aRv) const override; + + virtual void + SetProtocol(const nsAString& aProtocol, ErrorResult& aRv) override; + + virtual void + GetUsername(nsAString& aUsername, ErrorResult& aRv) const override; + + virtual void + SetUsername(const nsAString& aUsername, ErrorResult& aRv) override; + + virtual void + GetPassword(nsAString& aPassword, ErrorResult& aRv) const override; + + virtual void + SetPassword(const nsAString& aPassword, ErrorResult& aRv) override; + + virtual void + GetHost(nsAString& aHost, ErrorResult& aRv) const override; + + virtual void + SetHost(const nsAString& aHost, ErrorResult& aRv) override; + + virtual void + GetHostname(nsAString& aHostname, ErrorResult& aRv) const override; + + virtual void + SetHostname(const nsAString& aHostname, ErrorResult& aRv) override; + + virtual void + GetPort(nsAString& aPort, ErrorResult& aRv) const override; + + virtual void + SetPort(const nsAString& aPort, ErrorResult& aRv) override; + + virtual void + GetPathname(nsAString& aPathname, ErrorResult& aRv) const override; + + virtual void + SetPathname(const nsAString& aPathname, ErrorResult& aRv) override; + + virtual void + GetSearch(nsAString& aSearch, ErrorResult& aRv) const override; + + virtual void + GetHash(nsAString& aHost, ErrorResult& aRv) const override; + + virtual void + SetHash(const nsAString& aHash, ErrorResult& aRv) override; + + virtual void UpdateURLSearchParams() override; + + virtual void + SetSearchInternal(const nsAString& aSearch, ErrorResult& aRv) override; + + nsIURI* + GetURI() const + { + MOZ_ASSERT(NS_IsMainThread()); + return mURI; + } + +private: + ~URLMainThread() + { + MOZ_ASSERT(NS_IsMainThread()); + } + + nsCOMPtr<nsIURI> mURI; +}; + +/* static */ already_AddRefed<URLMainThread> +URLMainThread::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + URL& aBase, ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread()); + URLMainThread& base = static_cast<URLMainThread&>(aBase); + return Constructor(aGlobal.GetAsSupports(), aURL, base.GetURI(), aRv); +} + +/* static */ already_AddRefed<URLMainThread> +URLMainThread::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + const Optional<nsAString>& aBase, ErrorResult& aRv) +{ + if (aBase.WasPassed()) { + return Constructor(aGlobal.GetAsSupports(), aURL, aBase.Value(), aRv); + } + + return Constructor(aGlobal.GetAsSupports(), aURL, nullptr, aRv); +} + +/* static */ already_AddRefed<URLMainThread> +URLMainThread::Constructor(nsISupports* aParent, const nsAString& aURL, + const nsAString& aBase, ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsCOMPtr<nsIURI> baseUri; + nsresult rv = NS_NewURI(getter_AddRefs(baseUri), aBase, nullptr, nullptr, + nsContentUtils::GetIOService()); + if (NS_WARN_IF(NS_FAILED(rv))) { + aRv.ThrowTypeError<MSG_INVALID_URL>(aBase); + return nullptr; + } + + return Constructor(aParent, aURL, baseUri, aRv); +} + +/* static */ already_AddRefed<URLMainThread> +URLMainThread::Constructor(nsISupports* aParent, const nsAString& aURL, + nsIURI* aBase, ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsCOMPtr<nsIURI> uri; + nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, nullptr, aBase, + nsContentUtils::GetIOService()); + 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>(aURL); + return nullptr; + } + + RefPtr<URLMainThread> url = new URLMainThread(aParent, uri.forget()); + return url.forget(); +} + +/* static */ void +URLMainThread::CreateObjectURL(const GlobalObject& aGlobal, + MediaSource& aSource, + const objectURLOptions& aOptions, + nsAString& aResult, ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread()); + + nsCOMPtr<nsIPrincipal> principal = + nsContentUtils::ObjectPrincipal(aGlobal.Get()); + + nsAutoCString url; + aRv = nsHostObjectProtocolHandler::AddDataEntry(&aSource, principal, url); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + nsCOMPtr<nsIRunnable> revocation = NS_NewRunnableFunction( + [url] { + nsHostObjectProtocolHandler::RemoveDataEntry(url); + }); + + nsContentUtils::RunInStableState(revocation.forget()); + + CopyASCIItoUTF16(url, aResult); +} + +/* static */ void +URLMainThread::RevokeObjectURL(const GlobalObject& aGlobal, + const nsAString& aURL, ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread()); + nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); + if (!global) { + aRv.Throw(NS_ERROR_FAILURE); + return; + } + + nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); + + NS_LossyConvertUTF16toASCII asciiurl(aURL); + + nsIPrincipal* urlPrincipal = + nsHostObjectProtocolHandler::GetDataEntryPrincipal(asciiurl); + + if (urlPrincipal && principal->Subsumes(urlPrincipal)) { + global->UnregisterHostObjectURI(asciiurl); + nsHostObjectProtocolHandler::RemoveDataEntry(asciiurl); + } +} + +/* static */ bool +URLMainThread::IsValidURL(const GlobalObject& aGlobal, const nsAString& aURL, + ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread()); + NS_LossyConvertUTF16toASCII asciiurl(aURL); + return nsHostObjectProtocolHandler::HasDataEntry(asciiurl); +} + +void +URLMainThread::GetHref(nsAString& aHref, ErrorResult& aRv) const +{ + aHref.Truncate(); + + nsAutoCString href; + nsresult rv = mURI->GetSpec(href); + if (NS_SUCCEEDED(rv)) { + CopyUTF8toUTF16(href, aHref); + } +} + +void +URLMainThread::SetHref(const nsAString& aHref, ErrorResult& aRv) +{ + NS_ConvertUTF16toUTF8 href(aHref); + + nsresult rv; + nsCOMPtr<nsIIOService> ioService(do_GetService(NS_IOSERVICE_CONTRACTID, &rv)); + if (NS_FAILED(rv)) { + aRv.Throw(rv); + return; + } + + nsCOMPtr<nsIURI> uri; + rv = ioService->NewURI(href, nullptr, nullptr, getter_AddRefs(uri)); + if (NS_FAILED(rv)) { + aRv.ThrowTypeError<MSG_INVALID_URL>(aHref); + return; + } + + mURI = uri; + UpdateURLSearchParams(); +} + +void +URLMainThread::GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const +{ + nsContentUtils::GetUTFOrigin(mURI, aOrigin); +} + +void +URLMainThread::GetProtocol(nsAString& aProtocol, ErrorResult& aRv) const +{ + nsAutoCString protocol; + if (NS_SUCCEEDED(mURI->GetScheme(protocol))) { + aProtocol.Truncate(); + } + + CopyASCIItoUTF16(protocol, aProtocol); + aProtocol.Append(char16_t(':')); +} + +void +URLMainThread::SetProtocol(const nsAString& aProtocol, ErrorResult& aRv) +{ + nsAString::const_iterator start, end; + aProtocol.BeginReading(start); + aProtocol.EndReading(end); + nsAString::const_iterator iter(start); + + FindCharInReadable(':', iter, end); + + // Changing the protocol of a URL, changes the "nature" of the URI + // implementation. In order to do this properly, we have to serialize the + // existing URL and reparse it in a new object. + nsCOMPtr<nsIURI> clone; + nsresult rv = mURI->Clone(getter_AddRefs(clone)); + if (NS_WARN_IF(NS_FAILED(rv)) || !clone) { + return; + } + + rv = clone->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter))); + if (NS_WARN_IF(NS_FAILED(rv))) { + return; + } + + nsAutoCString href; + rv = clone->GetSpec(href); + if (NS_WARN_IF(NS_FAILED(rv))) { + return; + } + + nsCOMPtr<nsIURI> uri; + rv = NS_NewURI(getter_AddRefs(uri), href); + if (NS_WARN_IF(NS_FAILED(rv))) { + return; + } + + mURI = uri; +} + +#define URL_GETTER( value, func ) \ + value.Truncate(); \ + nsAutoCString tmp; \ + nsresult rv = mURI->func(tmp); \ + if (NS_SUCCEEDED(rv)) { \ + CopyUTF8toUTF16(tmp, value); \ + } + +void +URLMainThread::GetUsername(nsAString& aUsername, ErrorResult& aRv) const +{ + URL_GETTER(aUsername, GetUsername); +} + +void +URLMainThread::SetUsername(const nsAString& aUsername, ErrorResult& aRv) +{ + mURI->SetUsername(NS_ConvertUTF16toUTF8(aUsername)); +} + +void +URLMainThread::GetPassword(nsAString& aPassword, ErrorResult& aRv) const +{ + URL_GETTER(aPassword, GetPassword); +} + +void +URLMainThread::SetPassword(const nsAString& aPassword, ErrorResult& aRv) +{ + mURI->SetPassword(NS_ConvertUTF16toUTF8(aPassword)); +} + +void +URLMainThread::GetHost(nsAString& aHost, ErrorResult& aRv) const +{ + URL_GETTER(aHost, GetHostPort); +} + +void +URLMainThread::SetHost(const nsAString& aHost, ErrorResult& aRv) +{ + mURI->SetHostPort(NS_ConvertUTF16toUTF8(aHost)); +} + +void +URLMainThread::UpdateURLSearchParams() +{ + if (!mSearchParams) { + return; + } + + nsAutoCString search; + nsCOMPtr<nsIURL> url(do_QueryInterface(mURI)); + if (url) { + nsresult rv = url->GetQuery(search); + if (NS_WARN_IF(NS_FAILED(rv))) { + search.Truncate(); + } + } + + mSearchParams->ParseInput(search); +} + +void +URLMainThread::GetHostname(nsAString& aHostname, ErrorResult& aRv) const +{ + aHostname.Truncate(); + nsContentUtils::GetHostOrIPv6WithBrackets(mURI, aHostname); +} + +void +URLMainThread::SetHostname(const nsAString& aHostname, ErrorResult& aRv) +{ + // nsStandardURL returns NS_ERROR_UNEXPECTED for an empty hostname + // The return code is silently ignored + mURI->SetHost(NS_ConvertUTF16toUTF8(aHostname)); +} + +void +URLMainThread::GetPort(nsAString& aPort, ErrorResult& aRv) const +{ + 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); + } +} + +void +URLMainThread::SetPort(const nsAString& aPort, ErrorResult& aRv) +{ + nsresult rv; + nsAutoString portStr(aPort); + int32_t port = -1; + + // nsIURI uses -1 as default value. + if (!portStr.IsEmpty()) { + port = portStr.ToInteger(&rv); + if (NS_FAILED(rv)) { + return; + } + } + + mURI->SetPort(port); +} + +void +URLMainThread::GetPathname(nsAString& aPathname, ErrorResult& aRv) const +{ + aPathname.Truncate(); + + // Do not throw! Not having a valid URI or URL should result in an empty + // string. + + nsCOMPtr<nsIURIWithQuery> url(do_QueryInterface(mURI)); + if (url) { + nsAutoCString file; + nsresult rv = url->GetFilePath(file); + if (NS_SUCCEEDED(rv)) { + CopyUTF8toUTF16(file, aPathname); + } + + return; + } + + nsAutoCString path; + nsresult rv = mURI->GetPath(path); + if (NS_SUCCEEDED(rv)) { + CopyUTF8toUTF16(path, aPathname); + } +} + +void +URLMainThread::SetPathname(const nsAString& aPathname, ErrorResult& aRv) +{ + // Do not throw! + + nsCOMPtr<nsIURIWithQuery> url(do_QueryInterface(mURI)); + if (url) { + url->SetFilePath(NS_ConvertUTF16toUTF8(aPathname)); + return; + } +} + +void +URLMainThread::GetSearch(nsAString& aSearch, ErrorResult& aRv) const +{ + aSearch.Truncate(); + + // Do not throw! Not having a valid URI or URL should result in an empty + // string. + + nsAutoCString search; + nsresult rv; + + nsCOMPtr<nsIURIWithQuery> url(do_QueryInterface(mURI)); + if (url) { + rv = url->GetQuery(search); + if (NS_SUCCEEDED(rv) && !search.IsEmpty()) { + CopyUTF8toUTF16(NS_LITERAL_CSTRING("?") + search, aSearch); + } + return; + } +} + +void +URLMainThread::GetHash(nsAString& aHash, ErrorResult& aRv) const +{ + aHash.Truncate(); + + nsAutoCString ref; + nsresult rv = mURI->GetRef(ref); + if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) { + aHash.Assign(char16_t('#')); + if (nsContentUtils::GettersDecodeURLHash()) { + NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes! + } + AppendUTF8toUTF16(ref, aHash); + } +} + +void +URLMainThread::SetHash(const nsAString& aHash, ErrorResult& aRv) +{ + mURI->SetRef(NS_ConvertUTF16toUTF8(aHash)); +} + +void +URLMainThread::SetSearchInternal(const nsAString& aSearch, ErrorResult& aRv) +{ + // Ignore failures to be compatible with NS4. + + nsCOMPtr<nsIURIWithQuery> uriWithQuery(do_QueryInterface(mURI)); + if (uriWithQuery) { + uriWithQuery->SetQuery(NS_ConvertUTF16toUTF8(aSearch)); + return; + } +} + +} // anonymous namespace + +/////////////////////////////////////////////////////////////////////////////// +// URL for Workers +/////////////////////////////////////////////////////////////////////////////// + +namespace { + +using namespace workers; + +// Proxy class to forward all the requests to a URLMainThread object. +class URLProxy final +{ +public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(URLProxy) + + explicit URLProxy(already_AddRefed<URLMainThread> aURL) + : mURL(aURL) + { + MOZ_ASSERT(NS_IsMainThread()); + } + + URLMainThread* URL() + { + MOZ_ASSERT(NS_IsMainThread()); + return mURL; + } + + nsIURI* URI() + { + MOZ_ASSERT(NS_IsMainThread()); + return mURL->GetURI(); + } + + void ReleaseURI() + { + MOZ_ASSERT(NS_IsMainThread()); + mURL = nullptr; + } + +private: + // Private destructor, to discourage deletion outside of Release(): + ~URLProxy() + { + MOZ_ASSERT(!mURL); + } + + RefPtr<URLMainThread> mURL; +}; + +// URLWorker implements the URL object in workers. +class URLWorker final : public URL +{ +public: + static already_AddRefed<URLWorker> + Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + URL& aBase, ErrorResult& aRv); + + static already_AddRefed<URLWorker> + Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + const Optional<nsAString>& aBase, ErrorResult& aRv); + + static already_AddRefed<URLWorker> + Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + const nsAString& aBase, ErrorResult& aRv); + + static void + CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, + const mozilla::dom::objectURLOptions& aOptions, + nsAString& aResult, mozilla::ErrorResult& aRv); + + static void + RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aUrl, + ErrorResult& aRv); + + static bool + IsValidURL(const GlobalObject& aGlobal, const nsAString& aUrl, + ErrorResult& aRv); + + URLWorker(WorkerPrivate* aWorkerPrivate, URLProxy* aURLProxy); + + virtual void + GetHref(nsAString& aHref, ErrorResult& aRv) const override; + + virtual void + SetHref(const nsAString& aHref, ErrorResult& aRv) override; + + virtual void + GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const override; + + virtual void + GetProtocol(nsAString& aProtocol, ErrorResult& aRv) const override; + + virtual void + SetProtocol(const nsAString& aProtocol, ErrorResult& aRv) override; + + virtual void + GetUsername(nsAString& aUsername, ErrorResult& aRv) const override; + + virtual void + SetUsername(const nsAString& aUsername, ErrorResult& aRv) override; + + virtual void + GetPassword(nsAString& aPassword, ErrorResult& aRv) const override; + + virtual void + SetPassword(const nsAString& aPassword, ErrorResult& aRv) override; + + virtual void + GetHost(nsAString& aHost, ErrorResult& aRv) const override; + + virtual void + SetHost(const nsAString& aHost, ErrorResult& aRv) override; + + virtual void + GetHostname(nsAString& aHostname, ErrorResult& aRv) const override; + + virtual void + SetHostname(const nsAString& aHostname, ErrorResult& aRv) override; + + virtual void + GetPort(nsAString& aPort, ErrorResult& aRv) const override; + + virtual void + SetPort(const nsAString& aPort, ErrorResult& aRv) override; + + virtual void + GetPathname(nsAString& aPathname, ErrorResult& aRv) const override; + + virtual void + SetPathname(const nsAString& aPathname, ErrorResult& aRv) override; + + virtual void + GetSearch(nsAString& aSearch, ErrorResult& aRv) const override; + + virtual void + GetHash(nsAString& aHost, ErrorResult& aRv) const override; + + virtual void + SetHash(const nsAString& aHash, ErrorResult& aRv) override; + + virtual void UpdateURLSearchParams() override; + + virtual void + SetSearchInternal(const nsAString& aSearch, ErrorResult& aRv) override; + + URLProxy* + GetURLProxy() const + { + mWorkerPrivate->AssertIsOnWorkerThread(); + return mURLProxy; + } + +private: + ~URLWorker(); + + workers::WorkerPrivate* mWorkerPrivate; + RefPtr<URLProxy> mURLProxy; +}; + +// This class creates an URL from a DOM Blob on the main thread. +class CreateURLRunnable : public WorkerMainThreadRunnable +{ +private: + BlobImpl* mBlobImpl; + nsAString& mURL; + +public: + CreateURLRunnable(WorkerPrivate* aWorkerPrivate, BlobImpl* aBlobImpl, + const objectURLOptions& aOptions, + nsAString& aURL) + : WorkerMainThreadRunnable(aWorkerPrivate, + NS_LITERAL_CSTRING("URL :: CreateURL")) + , mBlobImpl(aBlobImpl) + , mURL(aURL) + { + MOZ_ASSERT(aBlobImpl); + + DebugOnly<bool> isMutable; + MOZ_ASSERT(NS_SUCCEEDED(aBlobImpl->GetMutable(&isMutable))); + MOZ_ASSERT(!isMutable); + } + + bool + MainThreadRun() + { + using namespace mozilla::ipc; + + AssertIsOnMainThread(); + + RefPtr<BlobImpl> newBlobImplHolder; + + if (nsCOMPtr<nsIRemoteBlob> remoteBlob = do_QueryInterface(mBlobImpl)) { + if (BlobChild* blobChild = remoteBlob->GetBlobChild()) { + if (PBackgroundChild* blobManager = blobChild->GetBackgroundManager()) { + PBackgroundChild* backgroundManager = + BackgroundChild::GetForCurrentThread(); + MOZ_ASSERT(backgroundManager); + + if (blobManager != backgroundManager) { + // Always make sure we have a blob from an actor we can use on this + // thread. + blobChild = BlobChild::GetOrCreate(backgroundManager, mBlobImpl); + MOZ_ASSERT(blobChild); + + newBlobImplHolder = blobChild->GetBlobImpl(); + MOZ_ASSERT(newBlobImplHolder); + + mBlobImpl = newBlobImplHolder; + } + } + } + } + + DebugOnly<bool> isMutable; + MOZ_ASSERT(NS_SUCCEEDED(mBlobImpl->GetMutable(&isMutable))); + MOZ_ASSERT(!isMutable); + + nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal(); + + nsAutoCString url; + nsresult rv = + nsHostObjectProtocolHandler::AddDataEntry(mBlobImpl, principal, url); + + if (NS_FAILED(rv)) { + NS_WARNING("Failed to add data entry for the blob!"); + SetDOMStringToNull(mURL); + return false; + } + + if (!mWorkerPrivate->IsSharedWorker() && + !mWorkerPrivate->IsServiceWorker()) { + // Walk up to top worker object. + WorkerPrivate* wp = mWorkerPrivate; + while (WorkerPrivate* parent = wp->GetParent()) { + wp = parent; + } + + nsCOMPtr<nsIScriptContext> sc = wp->GetScriptContext(); + // We could not have a ScriptContext in JSM code. In this case, we leak. + if (sc) { + nsCOMPtr<nsIGlobalObject> global = sc->GetGlobalObject(); + MOZ_ASSERT(global); + + global->RegisterHostObjectURI(url); + } + } + + mURL = NS_ConvertUTF8toUTF16(url); + return true; + } +}; + +// This class revokes an URL on the main thread. +class RevokeURLRunnable : public WorkerMainThreadRunnable +{ +private: + const nsString mURL; + +public: + RevokeURLRunnable(WorkerPrivate* aWorkerPrivate, + const nsAString& aURL) + : WorkerMainThreadRunnable(aWorkerPrivate, + NS_LITERAL_CSTRING("URL :: RevokeURL")) + , mURL(aURL) + {} + + bool + MainThreadRun() + { + AssertIsOnMainThread(); + + NS_ConvertUTF16toUTF8 url(mURL); + + nsIPrincipal* urlPrincipal = + nsHostObjectProtocolHandler::GetDataEntryPrincipal(url); + + nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal(); + + bool subsumes; + if (urlPrincipal && + NS_SUCCEEDED(principal->Subsumes(urlPrincipal, &subsumes)) && + subsumes) { + nsHostObjectProtocolHandler::RemoveDataEntry(url); + } + + if (!mWorkerPrivate->IsSharedWorker() && + !mWorkerPrivate->IsServiceWorker()) { + // Walk up to top worker object. + WorkerPrivate* wp = mWorkerPrivate; + while (WorkerPrivate* parent = wp->GetParent()) { + wp = parent; + } + + nsCOMPtr<nsIScriptContext> sc = wp->GetScriptContext(); + // We could not have a ScriptContext in JSM code. In this case, we leak. + if (sc) { + nsCOMPtr<nsIGlobalObject> global = sc->GetGlobalObject(); + MOZ_ASSERT(global); + + global->UnregisterHostObjectURI(url); + } + } + + return true; + } +}; + +// This class checks if an URL is valid on the main thread. +class IsValidURLRunnable : public WorkerMainThreadRunnable +{ +private: + const nsString mURL; + bool mValid; + +public: + IsValidURLRunnable(WorkerPrivate* aWorkerPrivate, + const nsAString& aURL) + : WorkerMainThreadRunnable(aWorkerPrivate, + NS_LITERAL_CSTRING("URL :: IsValidURL")) + , mURL(aURL) + , mValid(false) + {} + + bool + MainThreadRun() + { + AssertIsOnMainThread(); + + NS_ConvertUTF16toUTF8 url(mURL); + mValid = nsHostObjectProtocolHandler::HasDataEntry(url); + + return true; + } + + bool + IsValidURL() const + { + return mValid; + } +}; + +// This class creates a URL object on the main thread. +class ConstructorRunnable : public WorkerMainThreadRunnable +{ +private: + const nsString mURL; + + nsString mBase; // IsVoid() if we have no base URI string. + RefPtr<URLProxy> mBaseProxy; + + RefPtr<URLProxy> mRetval; + +public: + ConstructorRunnable(WorkerPrivate* aWorkerPrivate, + const nsAString& aURL, const Optional<nsAString>& aBase) + : WorkerMainThreadRunnable(aWorkerPrivate, + NS_LITERAL_CSTRING("URL :: Constructor")) + , mURL(aURL) + { + if (aBase.WasPassed()) { + mBase = aBase.Value(); + } else { + mBase.SetIsVoid(true); + } + mWorkerPrivate->AssertIsOnWorkerThread(); + } + + ConstructorRunnable(WorkerPrivate* aWorkerPrivate, + const nsAString& aURL, URLProxy* aBaseProxy) + : WorkerMainThreadRunnable(aWorkerPrivate, + NS_LITERAL_CSTRING("URL :: Constructor with BaseURL")) + , mURL(aURL) + , mBaseProxy(aBaseProxy) + { + mBase.SetIsVoid(true); + mWorkerPrivate->AssertIsOnWorkerThread(); + } + + bool + MainThreadRun() + { + AssertIsOnMainThread(); + + ErrorResult rv; + RefPtr<URLMainThread> url; + if (mBaseProxy) { + url = URLMainThread::Constructor(nullptr, mURL, mBaseProxy->URI(), rv); + } else if (!mBase.IsVoid()) { + url = URLMainThread::Constructor(nullptr, mURL, mBase, rv); + } else { + url = URLMainThread::Constructor(nullptr, mURL, nullptr, rv); + } + + if (rv.Failed()) { + rv.SuppressException(); + return true; + } + + mRetval = new URLProxy(url.forget()); + return true; + } + + URLProxy* + GetURLProxy(ErrorResult& aRv) const + { + MOZ_ASSERT(mWorkerPrivate); + mWorkerPrivate->AssertIsOnWorkerThread(); + + if (!mRetval) { + aRv.ThrowTypeError<MSG_INVALID_URL>(mURL); + } + + return mRetval; + } +}; + +class TeardownURLRunnable : public Runnable +{ +public: + explicit TeardownURLRunnable(URLProxy* aURLProxy) + : mURLProxy(aURLProxy) + { + } + + NS_IMETHOD Run() + { + AssertIsOnMainThread(); + + mURLProxy->ReleaseURI(); + mURLProxy = nullptr; + + return NS_OK; + } + +private: + RefPtr<URLProxy> mURLProxy; +}; + +// This class is the generic getter for any URL property. +class GetterRunnable : public WorkerMainThreadRunnable +{ +public: + enum GetterType { + GetterHref, + GetterOrigin, + GetterProtocol, + GetterUsername, + GetterPassword, + GetterHost, + GetterHostname, + GetterPort, + GetterPathname, + GetterSearch, + GetterHash, + }; + + GetterRunnable(WorkerPrivate* aWorkerPrivate, + GetterType aType, nsAString& aValue, + URLProxy* aURLProxy) + : WorkerMainThreadRunnable(aWorkerPrivate, + // We can have telemetry keys for each getter when + // needed. + NS_LITERAL_CSTRING("URL :: getter")) + , mValue(aValue) + , mType(aType) + , mURLProxy(aURLProxy) + { + mWorkerPrivate->AssertIsOnWorkerThread(); + } + + bool + MainThreadRun() + { + AssertIsOnMainThread(); + ErrorResult rv; + + switch (mType) { + case GetterHref: + mURLProxy->URL()->GetHref(mValue, rv); + break; + + case GetterOrigin: + mURLProxy->URL()->GetOrigin(mValue, rv); + break; + + case GetterProtocol: + mURLProxy->URL()->GetProtocol(mValue, rv); + break; + + case GetterUsername: + mURLProxy->URL()->GetUsername(mValue, rv); + break; + + case GetterPassword: + mURLProxy->URL()->GetPassword(mValue, rv); + break; + + case GetterHost: + mURLProxy->URL()->GetHost(mValue, rv); + break; + + case GetterHostname: + mURLProxy->URL()->GetHostname(mValue, rv); + break; + + case GetterPort: + mURLProxy->URL()->GetPort(mValue, rv); + break; + + case GetterPathname: + mURLProxy->URL()->GetPathname(mValue, rv); + break; + + case GetterSearch: + mURLProxy->URL()->GetSearch(mValue, rv); + break; + + case GetterHash: + mURLProxy->URL()->GetHash(mValue, rv); + break; + } + + MOZ_ASSERT(!rv.Failed(), "Main-thread getters do not fail."); + return true; + } + +private: + nsAString& mValue; + GetterType mType; + RefPtr<URLProxy> mURLProxy; +}; + +// This class is the generic setter for any URL property. +class SetterRunnable : public WorkerMainThreadRunnable +{ +public: + enum SetterType { + SetterHref, + SetterProtocol, + SetterUsername, + SetterPassword, + SetterHost, + SetterHostname, + SetterPort, + SetterPathname, + SetterSearch, + SetterHash, + }; + + SetterRunnable(WorkerPrivate* aWorkerPrivate, + SetterType aType, const nsAString& aValue, + URLProxy* aURLProxy) + : WorkerMainThreadRunnable(aWorkerPrivate, + // We can have telemetry keys for each setter when + // needed. + NS_LITERAL_CSTRING("URL :: setter")) + , mValue(aValue) + , mType(aType) + , mURLProxy(aURLProxy) + , mFailed(false) + { + mWorkerPrivate->AssertIsOnWorkerThread(); + } + + bool + MainThreadRun() + { + AssertIsOnMainThread(); + ErrorResult rv; + + switch (mType) { + case SetterHref: { + mURLProxy->URL()->SetHref(mValue, rv); + break; + } + + case SetterProtocol: + mURLProxy->URL()->SetProtocol(mValue, rv); + break; + + case SetterUsername: + mURLProxy->URL()->SetUsername(mValue, rv); + break; + + case SetterPassword: + mURLProxy->URL()->SetPassword(mValue, rv); + break; + + case SetterHost: + mURLProxy->URL()->SetHost(mValue, rv); + break; + + case SetterHostname: + mURLProxy->URL()->SetHostname(mValue, rv); + break; + + case SetterPort: + mURLProxy->URL()->SetPort(mValue, rv); + break; + + case SetterPathname: + mURLProxy->URL()->SetPathname(mValue, rv); + break; + + case SetterSearch: + mURLProxy->URL()->SetSearch(mValue, rv); + break; + + case SetterHash: + mURLProxy->URL()->SetHash(mValue, rv); + break; + } + + if (NS_WARN_IF(rv.Failed())) { + rv.SuppressException(); + mFailed = true; + } + + return true; + } + + bool Failed() const + { + return mFailed; + } + +private: + const nsString mValue; + SetterType mType; + RefPtr<URLProxy> mURLProxy; + bool mFailed; +}; + +already_AddRefed<URLWorker> +FinishConstructor(JSContext* aCx, WorkerPrivate* aPrivate, + ConstructorRunnable* aRunnable, ErrorResult& aRv) +{ + aRunnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + RefPtr<URLProxy> proxy = aRunnable->GetURLProxy(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + RefPtr<URLWorker> url = new URLWorker(aPrivate, proxy); + return url.forget(); +} + +/* static */ already_AddRefed<URLWorker> +URLWorker::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + URL& aBase, ErrorResult& aRv) +{ + MOZ_ASSERT(!NS_IsMainThread()); + + JSContext* cx = aGlobal.Context(); + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); + + URLWorker& base = static_cast<URLWorker&>(aBase); + RefPtr<ConstructorRunnable> runnable = + new ConstructorRunnable(workerPrivate, aURL, base.GetURLProxy()); + + return FinishConstructor(cx, workerPrivate, runnable, aRv); +} + +/* static */ already_AddRefed<URLWorker> +URLWorker::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + const Optional<nsAString>& aBase, ErrorResult& aRv) +{ + JSContext* cx = aGlobal.Context(); + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); + + RefPtr<ConstructorRunnable> runnable = + new ConstructorRunnable(workerPrivate, aURL, aBase); + + return FinishConstructor(cx, workerPrivate, runnable, aRv); +} + +/* static */ already_AddRefed<URLWorker> +URLWorker::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + const nsAString& aBase, ErrorResult& aRv) +{ + JSContext* cx = aGlobal.Context(); + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); + + Optional<nsAString> base; + base = &aBase; + + RefPtr<ConstructorRunnable> runnable = + new ConstructorRunnable(workerPrivate, aURL, base); + + return FinishConstructor(cx, workerPrivate, runnable, aRv); +} + +/* static */ void +URLWorker::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, + const mozilla::dom::objectURLOptions& aOptions, + nsAString& aResult, mozilla::ErrorResult& aRv) +{ + JSContext* cx = aGlobal.Context(); + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); + + RefPtr<BlobImpl> blobImpl = aBlob.Impl(); + MOZ_ASSERT(blobImpl); + + aRv = blobImpl->SetMutable(false); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + RefPtr<CreateURLRunnable> runnable = + new CreateURLRunnable(workerPrivate, blobImpl, aOptions, aResult); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + if (workerPrivate->IsSharedWorker() || workerPrivate->IsServiceWorker()) { + WorkerGlobalScope* scope = workerPrivate->GlobalScope(); + MOZ_ASSERT(scope); + + scope->RegisterHostObjectURI(NS_ConvertUTF16toUTF8(aResult)); + } +} + +/* static */ void +URLWorker::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aUrl, + ErrorResult& aRv) +{ + JSContext* cx = aGlobal.Context(); + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); + + RefPtr<RevokeURLRunnable> runnable = + new RevokeURLRunnable(workerPrivate, aUrl); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + if (workerPrivate->IsSharedWorker() || workerPrivate->IsServiceWorker()) { + WorkerGlobalScope* scope = workerPrivate->GlobalScope(); + MOZ_ASSERT(scope); + + scope->UnregisterHostObjectURI(NS_ConvertUTF16toUTF8(aUrl)); + } +} + +/* static */ bool +URLWorker::IsValidURL(const GlobalObject& aGlobal, const nsAString& aUrl, + ErrorResult& aRv) +{ + JSContext* cx = aGlobal.Context(); + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx); + + RefPtr<IsValidURLRunnable> runnable = + new IsValidURLRunnable(workerPrivate, aUrl); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return false; + } + + return runnable->IsValidURL(); +} + +URLWorker::URLWorker(WorkerPrivate* aWorkerPrivate, URLProxy* aURLProxy) + : URL(nullptr) + , mWorkerPrivate(aWorkerPrivate) + , mURLProxy(aURLProxy) +{} + +URLWorker::~URLWorker() +{ + if (mURLProxy) { + mWorkerPrivate->AssertIsOnWorkerThread(); + + RefPtr<TeardownURLRunnable> runnable = + new TeardownURLRunnable(mURLProxy); + mURLProxy = nullptr; + + if (NS_FAILED(NS_DispatchToMainThread(runnable))) { + NS_ERROR("Failed to dispatch teardown runnable!"); + } + } +} + +void +URLWorker::GetHref(nsAString& aHref, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterHref, aHref, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::SetHref(const nsAString& aHref, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterHref, aHref, + mURLProxy); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + if (runnable->Failed()) { + aRv.ThrowTypeError<MSG_INVALID_URL>(aHref); + return; + } + + UpdateURLSearchParams(); +} + +void +URLWorker::GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterOrigin, aOrigin, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::GetProtocol(nsAString& aProtocol, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterProtocol, aProtocol, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::SetProtocol(const nsAString& aProtocol, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterProtocol, + aProtocol, mURLProxy); + + runnable->Dispatch(aRv); + + MOZ_ASSERT(!runnable->Failed()); +} + +void +URLWorker::GetUsername(nsAString& aUsername, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterUsername, aUsername, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::SetUsername(const nsAString& aUsername, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterUsername, + aUsername, mURLProxy); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + MOZ_ASSERT(!runnable->Failed()); +} + +void +URLWorker::GetPassword(nsAString& aPassword, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterPassword, aPassword, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::SetPassword(const nsAString& aPassword, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterPassword, + aPassword, mURLProxy); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + MOZ_ASSERT(!runnable->Failed()); +} + +void +URLWorker::GetHost(nsAString& aHost, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterHost, aHost, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::SetHost(const nsAString& aHost, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterHost, + aHost, mURLProxy); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + MOZ_ASSERT(!runnable->Failed()); +} + +void +URLWorker::GetHostname(nsAString& aHostname, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterHostname, aHostname, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::SetHostname(const nsAString& aHostname, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterHostname, + aHostname, mURLProxy); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + MOZ_ASSERT(!runnable->Failed()); +} + +void +URLWorker::GetPort(nsAString& aPort, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterPort, aPort, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::SetPort(const nsAString& aPort, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterPort, + aPort, mURLProxy); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + MOZ_ASSERT(!runnable->Failed()); +} + +void +URLWorker::GetPathname(nsAString& aPathname, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterPathname, + aPathname, mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::SetPathname(const nsAString& aPathname, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterPathname, + aPathname, mURLProxy); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + MOZ_ASSERT(!runnable->Failed()); +} + +void +URLWorker::GetSearch(nsAString& aSearch, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterSearch, aSearch, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::GetHash(nsAString& aHash, ErrorResult& aRv) const +{ + RefPtr<GetterRunnable> runnable = + new GetterRunnable(mWorkerPrivate, GetterRunnable::GetterHash, aHash, + mURLProxy); + + runnable->Dispatch(aRv); +} + +void +URLWorker::SetHash(const nsAString& aHash, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterHash, + aHash, mURLProxy); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + MOZ_ASSERT(!runnable->Failed()); +} + +void +URLWorker::SetSearchInternal(const nsAString& aSearch, ErrorResult& aRv) +{ + RefPtr<SetterRunnable> runnable = + new SetterRunnable(mWorkerPrivate, SetterRunnable::SetterSearch, + aSearch, mURLProxy); + + runnable->Dispatch(aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + MOZ_ASSERT(!runnable->Failed()); +} + +void +URLWorker::UpdateURLSearchParams() +{ + if (mSearchParams) { + nsAutoString search; + + ErrorResult rv; + GetSearch(search, rv); + if (NS_WARN_IF(rv.Failed())) { + rv.SuppressException(); + } + + mSearchParams->ParseInput(NS_ConvertUTF16toUTF8(Substring(search, 1))); + } +} + +} // anonymous namespace + +/////////////////////////////////////////////////////////////////////////////// +// Base class for URL +/////////////////////////////////////////////////////////////////////////////// + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(URL, mParent, mSearchParams) + +NS_IMPL_CYCLE_COLLECTING_ADDREF(URL) +NS_IMPL_CYCLE_COLLECTING_RELEASE(URL) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(URL) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +JSObject* +URL::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) +{ + return URLBinding::Wrap(aCx, this, aGivenProto); +} + +/* static */ already_AddRefed<URL> +URL::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + URL& aBase, ErrorResult& aRv) +{ + if (NS_IsMainThread()) { + return URLMainThread::Constructor(aGlobal, aURL, aBase, aRv); + } + + return URLWorker::Constructor(aGlobal, aURL, aBase, aRv); +} + +/* static */ already_AddRefed<URL> +URL::Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + const Optional<nsAString>& aBase, ErrorResult& aRv) +{ + if (NS_IsMainThread()) { + return URLMainThread::Constructor(aGlobal, aURL, aBase, aRv); + } + + return URLWorker::Constructor(aGlobal, aURL, aBase, aRv); +} + +/* static */ already_AddRefed<URL> +URL::WorkerConstructor(const GlobalObject& aGlobal, const nsAString& aURL, + const nsAString& aBase, ErrorResult& aRv) +{ + return URLWorker::Constructor(aGlobal, aURL, aBase, aRv); +} + +void +URL::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, + const objectURLOptions& aOptions, nsAString& aResult, + ErrorResult& aRv) +{ + if (NS_IsMainThread()) { + URLMainThread::CreateObjectURL(aGlobal, aBlob, aOptions, aResult, aRv); + } else { + URLWorker::CreateObjectURL(aGlobal, aBlob, aOptions, aResult, aRv); + } +} + +void +URL::CreateObjectURL(const GlobalObject& aGlobal, DOMMediaStream& aStream, + const objectURLOptions& aOptions, nsAString& aResult, + ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread()); + URLMainThread::CreateObjectURL(aGlobal, aStream, aOptions, aResult, aRv); +} + +void +URL::CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource, + const objectURLOptions& aOptions, + nsAString& aResult, + ErrorResult& aRv) +{ + MOZ_ASSERT(NS_IsMainThread()); + URLMainThread::CreateObjectURL(aGlobal, aSource, aOptions, aResult, aRv); +} + +void +URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL, + ErrorResult& aRv) +{ + if (NS_IsMainThread()) { + URLMainThread::RevokeObjectURL(aGlobal, aURL, aRv); + } else { + URLWorker::RevokeObjectURL(aGlobal, aURL, aRv); + } +} + +bool +URL::IsValidURL(const GlobalObject& aGlobal, const nsAString& aURL, + ErrorResult& aRv) +{ + if (NS_IsMainThread()) { + return URLMainThread::IsValidURL(aGlobal, aURL, aRv); + } + return URLWorker::IsValidURL(aGlobal, aURL, aRv); +} + +URLSearchParams* +URL::SearchParams() +{ + CreateSearchParamsIfNeeded(); + return mSearchParams; +} + +bool IsChromeURI(nsIURI* aURI) +{ + bool isChrome = false; + if (NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome))) + return isChrome; + return false; +} + +void +URL::CreateSearchParamsIfNeeded() +{ + if (!mSearchParams) { + mSearchParams = new URLSearchParams(mParent, this); + UpdateURLSearchParams(); + } +} + +void +URL::SetSearch(const nsAString& aSearch, ErrorResult& aRv) +{ + SetSearchInternal(aSearch, aRv); + UpdateURLSearchParams(); +} + +void +URL::URLSearchParamsUpdated(URLSearchParams* aSearchParams) +{ + MOZ_ASSERT(mSearchParams); + MOZ_ASSERT(mSearchParams == aSearchParams); + + nsAutoString search; + mSearchParams->Serialize(search); + + ErrorResult rv; + SetSearchInternal(search, rv); + NS_WARNING_ASSERTION(!rv.Failed(), "SetSearchInternal failed"); + rv.SuppressException(); +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/url/URL.h b/dom/url/URL.h new file mode 100644 index 0000000000..16b4678baf --- /dev/null +++ b/dom/url/URL.h @@ -0,0 +1,183 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_URL_h +#define mozilla_dom_URL_h + +#include "mozilla/dom/BindingDeclarations.h" +#include "mozilla/dom/URLSearchParams.h" +#include "nsCycleCollectionParticipant.h" +#include "nsString.h" +#include "nsWrapperCache.h" + +class nsISupports; +class nsIURI; + +namespace mozilla { + +class ErrorResult; +class DOMMediaStream; + +namespace dom { + +class Blob; +class MediaSource; +class GlobalObject; +struct objectURLOptions; + +class URL : public URLSearchParamsObserver + , public nsWrapperCache +{ +public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(URL) + + URL(nsISupports* aParent) + : mParent(aParent) + {} + + // WebIDL methods + nsISupports* GetParentObject() const + { + return mParent; + } + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; + + static already_AddRefed<URL> + Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + URL& aBase, ErrorResult& aRv); + + static already_AddRefed<URL> + Constructor(const GlobalObject& aGlobal, const nsAString& aURL, + const Optional<nsAString>& aBase, ErrorResult& aRv); + + // Helper for Fetch API + static already_AddRefed<URL> + WorkerConstructor(const GlobalObject& aGlobal, const nsAString& aURL, + const nsAString& aBase, ErrorResult& aRv); + + + static void + CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, + const objectURLOptions& aOptions, + nsAString& aResult, ErrorResult& aRv); + + static void + CreateObjectURL(const GlobalObject& aGlobal, DOMMediaStream& aStream, + const objectURLOptions& aOptions, nsAString& aResult, + ErrorResult& aRv); + + static void + CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource, + const objectURLOptions& aOptions, nsAString& aResult, + ErrorResult& aRv); + + static void + RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL, + ErrorResult& aRv); + + static bool + IsValidURL(const GlobalObject& aGlobal, const nsAString& aURL, + ErrorResult& aRv); + + virtual void + GetHref(nsAString& aHref, ErrorResult& aRv) const = 0; + + virtual void + SetHref(const nsAString& aHref, ErrorResult& aRv) = 0; + + virtual void + GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const = 0; + + virtual void + GetProtocol(nsAString& aProtocol, ErrorResult& aRv) const = 0; + + virtual void + SetProtocol(const nsAString& aProtocol, ErrorResult& aRv) = 0; + + virtual void + GetUsername(nsAString& aUsername, ErrorResult& aRv) const = 0; + + virtual void + SetUsername(const nsAString& aUsername, ErrorResult& aRv) = 0; + + virtual void + GetPassword(nsAString& aPassword, ErrorResult& aRv) const = 0; + + virtual void + SetPassword(const nsAString& aPassword, ErrorResult& aRv) = 0; + + virtual void + GetHost(nsAString& aHost, ErrorResult& aRv) const = 0; + + virtual void + SetHost(const nsAString& aHost, ErrorResult& aRv) = 0; + + virtual void + GetHostname(nsAString& aHostname, ErrorResult& aRv) const = 0; + + virtual void + SetHostname(const nsAString& aHostname, ErrorResult& aRv) = 0; + + virtual void + GetPort(nsAString& aPort, ErrorResult& aRv) const = 0; + + virtual void + SetPort(const nsAString& aPort, ErrorResult& aRv) = 0; + + virtual void + GetPathname(nsAString& aPathname, ErrorResult& aRv) const = 0; + + virtual void + SetPathname(const nsAString& aPathname, ErrorResult& aRv) = 0; + + virtual void + GetSearch(nsAString& aSearch, ErrorResult& aRv) const = 0; + + virtual void + SetSearch(const nsAString& aSearch, ErrorResult& aRv); + + URLSearchParams* SearchParams(); + + virtual void + GetHash(nsAString& aHost, ErrorResult& aRv) const = 0; + + virtual void + SetHash(const nsAString& aHash, ErrorResult& aRv) = 0; + + void Stringify(nsAString& aRetval, ErrorResult& aRv) const + { + GetHref(aRetval, aRv); + } + + // URLSearchParamsObserver + void + URLSearchParamsUpdated(URLSearchParams* aSearchParams) override; + +protected: + virtual ~URL() + {} + + virtual void + UpdateURLSearchParams() = 0; + + virtual void + SetSearchInternal(const nsAString& aSearch, ErrorResult& aRv) = 0; + + void CreateSearchParamsIfNeeded(); + + nsCOMPtr<nsISupports> mParent; + RefPtr<URLSearchParams> mSearchParams; +}; + +bool IsChromeURI(nsIURI* aURI); + +} // namespace dom +} // namespace mozilla + +#endif /* mozilla_dom_URL_h */ diff --git a/dom/url/URLSearchParams.cpp b/dom/url/URLSearchParams.cpp new file mode 100644 index 0000000000..d9492f81c6 --- /dev/null +++ b/dom/url/URLSearchParams.cpp @@ -0,0 +1,549 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "URLSearchParams.h" +#include "mozilla/dom/URLSearchParamsBinding.h" +#include "mozilla/dom/EncodingUtils.h" +#include "nsDOMString.h" +#include "nsIInputStream.h" +#include "nsStringStream.h" + +namespace mozilla { +namespace dom { + +bool +URLParams::Has(const nsAString& aName) +{ + for (uint32_t i = 0, len = mParams.Length(); i < len; ++i) { + if (mParams[i].mKey.Equals(aName)) { + return true; + } + } + + return false; +} + +void +URLParams::Get(const nsAString& aName, nsString& aRetval) +{ + SetDOMStringToNull(aRetval); + + for (uint32_t i = 0, len = mParams.Length(); i < len; ++i) { + if (mParams[i].mKey.Equals(aName)) { + aRetval.Assign(mParams[i].mValue); + break; + } + } +} + +void +URLParams::GetAll(const nsAString& aName, nsTArray<nsString>& aRetval) +{ + aRetval.Clear(); + + for (uint32_t i = 0, len = mParams.Length(); i < len; ++i) { + if (mParams[i].mKey.Equals(aName)) { + aRetval.AppendElement(mParams[i].mValue); + } + } +} + +void +URLParams::Append(const nsAString& aName, const nsAString& aValue) +{ + Param* param = mParams.AppendElement(); + param->mKey = aName; + param->mValue = aValue; +} + +void +URLParams::Set(const nsAString& aName, const nsAString& aValue) +{ + Param* param = nullptr; + for (uint32_t i = 0, len = mParams.Length(); i < len;) { + if (!mParams[i].mKey.Equals(aName)) { + ++i; + continue; + } + if (!param) { + param = &mParams[i]; + ++i; + continue; + } + // Remove duplicates. + mParams.RemoveElementAt(i); + --len; + } + + if (!param) { + param = mParams.AppendElement(); + param->mKey = aName; + } + + param->mValue = aValue; +} + +bool +URLParams::Delete(const nsAString& aName) +{ + bool found = false; + for (uint32_t i = 0; i < mParams.Length();) { + if (mParams[i].mKey.Equals(aName)) { + mParams.RemoveElementAt(i); + found = true; + } else { + ++i; + } + } + + return found; +} + +void +URLParams::ConvertString(const nsACString& aInput, nsAString& aOutput) +{ + aOutput.Truncate(); + + if (!mDecoder) { + mDecoder = EncodingUtils::DecoderForEncoding("UTF-8"); + if (!mDecoder) { + MOZ_ASSERT(mDecoder, "Failed to create a decoder."); + return; + } + } + + int32_t inputLength = aInput.Length(); + int32_t outputLength = 0; + + nsresult rv = mDecoder->GetMaxLength(aInput.BeginReading(), inputLength, + &outputLength); + if (NS_WARN_IF(NS_FAILED(rv))) { + return; + } + + if (!aOutput.SetLength(outputLength, fallible)) { + return; + } + + int32_t newOutputLength = outputLength; + rv = mDecoder->Convert(aInput.BeginReading(), &inputLength, + aOutput.BeginWriting(), &newOutputLength); + if (NS_FAILED(rv)) { + aOutput.Truncate(); + return; + } + if (newOutputLength < outputLength) { + aOutput.Truncate(newOutputLength); + } +} + +void +URLParams::DecodeString(const nsACString& aInput, nsAString& aOutput) +{ + nsACString::const_iterator start, end; + aInput.BeginReading(start); + aInput.EndReading(end); + + nsCString unescaped; + + while (start != end) { + // replace '+' with U+0020 + if (*start == '+') { + unescaped.Append(' '); + ++start; + continue; + } + + // Percent decode algorithm + if (*start == '%') { + nsACString::const_iterator first(start); + ++first; + + nsACString::const_iterator second(first); + ++second; + +#define ASCII_HEX_DIGIT( x ) \ + ((x >= 0x41 && x <= 0x46) || \ + (x >= 0x61 && x <= 0x66) || \ + (x >= 0x30 && x <= 0x39)) + +#define HEX_DIGIT( x ) \ + (*x >= 0x30 && *x <= 0x39 \ + ? *x - 0x30 \ + : (*x >= 0x41 && *x <= 0x46 \ + ? *x - 0x37 \ + : *x - 0x57)) + + if (first != end && second != end && + ASCII_HEX_DIGIT(*first) && ASCII_HEX_DIGIT(*second)) { + unescaped.Append(HEX_DIGIT(first) * 16 + HEX_DIGIT(second)); + start = ++second; + continue; + + } else { + unescaped.Append('%'); + ++start; + continue; + } + } + + unescaped.Append(*start); + ++start; + } + + ConvertString(unescaped, aOutput); +} + +void +URLParams::ParseInput(const nsACString& aInput) +{ + // Remove all the existing data before parsing a new input. + DeleteAll(); + + nsACString::const_iterator start, end; + aInput.BeginReading(start); + aInput.EndReading(end); + nsACString::const_iterator iter(start); + + while (start != end) { + nsAutoCString string; + + if (FindCharInReadable('&', iter, end)) { + string.Assign(Substring(start, iter)); + start = ++iter; + } else { + string.Assign(Substring(start, end)); + start = end; + } + + if (string.IsEmpty()) { + continue; + } + + nsACString::const_iterator eqStart, eqEnd; + string.BeginReading(eqStart); + string.EndReading(eqEnd); + nsACString::const_iterator eqIter(eqStart); + + nsAutoCString name; + nsAutoCString value; + + if (FindCharInReadable('=', eqIter, eqEnd)) { + name.Assign(Substring(eqStart, eqIter)); + + ++eqIter; + value.Assign(Substring(eqIter, eqEnd)); + } else { + name.Assign(string); + } + + nsAutoString decodedName; + DecodeString(name, decodedName); + + nsAutoString decodedValue; + DecodeString(value, decodedValue); + + Append(decodedName, decodedValue); + } +} + +namespace { + +void SerializeString(const nsCString& aInput, nsAString& aValue) +{ + const unsigned char* p = (const unsigned char*) aInput.get(); + const unsigned char* end = p + aInput.Length(); + + while (p != end) { + // ' ' to '+' + if (*p == 0x20) { + aValue.Append(0x2B); + // Percent Encode algorithm + } else if (*p == 0x2A || *p == 0x2D || *p == 0x2E || + (*p >= 0x30 && *p <= 0x39) || + (*p >= 0x41 && *p <= 0x5A) || *p == 0x5F || + (*p >= 0x61 && *p <= 0x7A)) { + aValue.Append(*p); + } else { + aValue.AppendPrintf("%%%.2X", *p); + } + + ++p; + } +} + +} // namespace + +void +URLParams::Serialize(nsAString& aValue) const +{ + aValue.Truncate(); + bool first = true; + + for (uint32_t i = 0, len = mParams.Length(); i < len; ++i) { + if (first) { + first = false; + } else { + aValue.Append('&'); + } + + SerializeString(NS_ConvertUTF16toUTF8(mParams[i].mKey), aValue); + aValue.Append('='); + SerializeString(NS_ConvertUTF16toUTF8(mParams[i].mValue), aValue); + } +} + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(URLSearchParams, mParent, mObserver) +NS_IMPL_CYCLE_COLLECTING_ADDREF(URLSearchParams) +NS_IMPL_CYCLE_COLLECTING_RELEASE(URLSearchParams) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(URLSearchParams) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsIXHRSendable) + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +URLSearchParams::URLSearchParams(nsISupports* aParent, + URLSearchParamsObserver* aObserver) + : mParams(new URLParams()) + , mParent(aParent) + , mObserver(aObserver) +{ +} + +URLSearchParams::URLSearchParams(nsISupports* aParent, + const URLSearchParams& aOther) + : mParams(new URLParams(*aOther.mParams.get())) + , mParent(aParent) + , mObserver(nullptr) +{ +} + +URLSearchParams::~URLSearchParams() +{ + DeleteAll(); +} + +JSObject* +URLSearchParams::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) +{ + return URLSearchParamsBinding::Wrap(aCx, this, aGivenProto); +} + +/* static */ already_AddRefed<URLSearchParams> +URLSearchParams::Constructor(const GlobalObject& aGlobal, + const nsAString& aInit, + ErrorResult& aRv) +{ + RefPtr<URLSearchParams> sp = + new URLSearchParams(aGlobal.GetAsSupports(), nullptr); + + NS_ConvertUTF16toUTF8 input(aInit); + + if (StringBeginsWith(input, NS_LITERAL_CSTRING("?"))) { + sp->ParseInput(Substring(input, 1, input.Length() - 1)); + } else { + sp->ParseInput(input); + } + + return sp.forget(); +} + +/* static */ already_AddRefed<URLSearchParams> +URLSearchParams::Constructor(const GlobalObject& aGlobal, + URLSearchParams& aInit, + ErrorResult& aRv) +{ + RefPtr<URLSearchParams> sp = + new URLSearchParams(aGlobal.GetAsSupports(), aInit); + + return sp.forget(); +} + +void +URLSearchParams::ParseInput(const nsACString& aInput) +{ + mParams->ParseInput(aInput); +} + +void +URLSearchParams::Get(const nsAString& aName, nsString& aRetval) +{ + return mParams->Get(aName, aRetval); +} + +void +URLSearchParams::GetAll(const nsAString& aName, nsTArray<nsString>& aRetval) +{ + return mParams->GetAll(aName, aRetval); +} + +void +URLSearchParams::Set(const nsAString& aName, const nsAString& aValue) +{ + mParams->Set(aName, aValue); + NotifyObserver(); +} + +void +URLSearchParams::Append(const nsAString& aName, const nsAString& aValue) +{ + mParams->Append(aName, aValue); + NotifyObserver(); +} + +bool +URLSearchParams::Has(const nsAString& aName) +{ + return mParams->Has(aName); +} + +void +URLSearchParams::Delete(const nsAString& aName) +{ + if (mParams->Delete(aName)) { + NotifyObserver(); + } +} + +void +URLSearchParams::DeleteAll() +{ + mParams->DeleteAll(); +} + +void +URLSearchParams::Serialize(nsAString& aValue) const +{ + mParams->Serialize(aValue); +} + +void +URLSearchParams::NotifyObserver() +{ + if (mObserver) { + mObserver->URLSearchParamsUpdated(this); + } +} + +uint32_t +URLSearchParams::GetIterableLength() const +{ + return mParams->Length(); +} + +const nsAString& +URLSearchParams::GetKeyAtIndex(uint32_t aIndex) const +{ + return mParams->GetKeyAtIndex(aIndex); +} + +const nsAString& +URLSearchParams::GetValueAtIndex(uint32_t aIndex) const +{ + return mParams->GetValueAtIndex(aIndex); +} + +// Helper functions for structured cloning +inline bool +ReadString(JSStructuredCloneReader* aReader, nsString& aString) +{ + MOZ_ASSERT(aReader); + + bool read; + uint32_t nameLength, zero; + read = JS_ReadUint32Pair(aReader, &nameLength, &zero); + if (!read) { + return false; + } + MOZ_ASSERT(zero == 0); + aString.SetLength(nameLength); + size_t charSize = sizeof(nsString::char_type); + read = JS_ReadBytes(aReader, (void*) aString.BeginWriting(), + nameLength * charSize); + if (!read) { + return false; + } + + return true; +} + +inline bool +WriteString(JSStructuredCloneWriter* aWriter, const nsString& aString) +{ + MOZ_ASSERT(aWriter); + + size_t charSize = sizeof(nsString::char_type); + return JS_WriteUint32Pair(aWriter, aString.Length(), 0) && + JS_WriteBytes(aWriter, aString.get(), aString.Length() * charSize); +} + +bool +URLParams::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const +{ + const uint32_t& nParams = mParams.Length(); + if (!JS_WriteUint32Pair(aWriter, nParams, 0)) { + return false; + } + for (uint32_t i = 0; i < nParams; ++i) { + if (!WriteString(aWriter, mParams[i].mKey) || + !WriteString(aWriter, mParams[i].mValue)) { + return false; + } + } + return true; +} + +bool +URLParams::ReadStructuredClone(JSStructuredCloneReader* aReader) +{ + MOZ_ASSERT(aReader); + + DeleteAll(); + + uint32_t nParams, zero; + nsAutoString key, value; + if (!JS_ReadUint32Pair(aReader, &nParams, &zero)) { + return false; + } + MOZ_ASSERT(zero == 0); + for (uint32_t i = 0; i < nParams; ++i) { + if (!ReadString(aReader, key) || !ReadString(aReader, value)) { + return false; + } + Append(key, value); + } + return true; +} + +bool +URLSearchParams::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const +{ + return mParams->WriteStructuredClone(aWriter); +} + +bool +URLSearchParams::ReadStructuredClone(JSStructuredCloneReader* aReader) +{ + return mParams->ReadStructuredClone(aReader); +} + +NS_IMETHODIMP +URLSearchParams::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength, + nsACString& aContentType, nsACString& aCharset) +{ + aContentType.AssignLiteral("application/x-www-form-urlencoded"); + aCharset.AssignLiteral("UTF-8"); + + nsAutoString serialized; + Serialize(serialized); + NS_ConvertUTF16toUTF8 converted(serialized); + *aContentLength = converted.Length(); + return NS_NewCStringInputStream(aBody, converted); +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/url/URLSearchParams.h b/dom/url/URLSearchParams.h new file mode 100644 index 0000000000..4b0aaa9917 --- /dev/null +++ b/dom/url/URLSearchParams.h @@ -0,0 +1,223 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_URLSearchParams_h +#define mozilla_dom_URLSearchParams_h + +#include "js/StructuredClone.h" +#include "mozilla/dom/BindingDeclarations.h" +#include "mozilla/ErrorResult.h" +#include "nsCycleCollectionParticipant.h" +#include "nsWrapperCache.h" +#include "nsISupports.h" +#include "nsIUnicodeDecoder.h" +#include "nsIXMLHttpRequest.h" + +namespace mozilla { +namespace dom { + +class URLSearchParams; + +class URLSearchParamsObserver : public nsISupports +{ +public: + virtual ~URLSearchParamsObserver() {} + + virtual void URLSearchParamsUpdated(URLSearchParams* aFromThis) = 0; +}; + +// This class is used in BasePrincipal and it's _extremely_ important that the +// attributes are kept in the correct order. If this changes, please, update +// BasePrincipal code. + +class URLParams final +{ +public: + URLParams() {} + + ~URLParams() + { + DeleteAll(); + } + + explicit URLParams(const URLParams& aOther) + : mParams(aOther.mParams) + {} + + URLParams(const URLParams&& aOther) + : mParams(Move(aOther.mParams)) + {} + + class ForEachIterator + { + public: + virtual bool + URLParamsIterator(const nsString& aName, const nsString& aValue) = 0; + }; + + void + ParseInput(const nsACString& aInput); + + bool + ForEach(ForEachIterator& aIterator) const + { + for (uint32_t i = 0; i < mParams.Length(); ++i) { + if (!aIterator.URLParamsIterator(mParams[i].mKey, mParams[i].mValue)) { + return false; + } + } + + return true; + } + + void Serialize(nsAString& aValue) const; + + void Get(const nsAString& aName, nsString& aRetval); + + void GetAll(const nsAString& aName, nsTArray<nsString >& aRetval); + + void Set(const nsAString& aName, const nsAString& aValue); + + void Append(const nsAString& aName, const nsAString& aValue); + + bool Has(const nsAString& aName); + + // Returns true if aName was found and deleted, false otherwise. + bool Delete(const nsAString& aName); + + void DeleteAll() + { + mParams.Clear(); + } + + uint32_t Length() const + { + return mParams.Length(); + } + + const nsAString& GetKeyAtIndex(uint32_t aIndex) const + { + MOZ_ASSERT(aIndex < mParams.Length()); + return mParams[aIndex].mKey; + } + + const nsAString& GetValueAtIndex(uint32_t aIndex) const + { + MOZ_ASSERT(aIndex < mParams.Length()); + return mParams[aIndex].mValue; + } + + bool + ReadStructuredClone(JSStructuredCloneReader* aReader); + + bool + WriteStructuredClone(JSStructuredCloneWriter* aWriter) const; + +private: + void DecodeString(const nsACString& aInput, nsAString& aOutput); + void ConvertString(const nsACString& aInput, nsAString& aOutput); + + struct Param + { + nsString mKey; + nsString mValue; + }; + + nsTArray<Param> mParams; + nsCOMPtr<nsIUnicodeDecoder> mDecoder; +}; + +class URLSearchParams final : public nsIXHRSendable, + public nsWrapperCache +{ + ~URLSearchParams(); + +public: + NS_DECL_NSIXHRSENDABLE + + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(URLSearchParams) + + explicit URLSearchParams(nsISupports* aParent, + URLSearchParamsObserver* aObserver=nullptr); + + URLSearchParams(nsISupports* aParent, + const URLSearchParams& aOther); + + // WebIDL methods + nsISupports* GetParentObject() const + { + return mParent; + } + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; + + static already_AddRefed<URLSearchParams> + Constructor(const GlobalObject& aGlobal, const nsAString& aInit, + ErrorResult& aRv); + + static already_AddRefed<URLSearchParams> + Constructor(const GlobalObject& aGlobal, URLSearchParams& aInit, + ErrorResult& aRv); + + void ParseInput(const nsACString& aInput); + + void Serialize(nsAString& aValue) const; + + void Get(const nsAString& aName, nsString& aRetval); + + void GetAll(const nsAString& aName, nsTArray<nsString>& aRetval); + + void Set(const nsAString& aName, const nsAString& aValue); + + void Append(const nsAString& aName, const nsAString& aValue); + + bool Has(const nsAString& aName); + + void Delete(const nsAString& aName); + + uint32_t GetIterableLength() const; + const nsAString& GetKeyAtIndex(uint32_t aIndex) const; + const nsAString& GetValueAtIndex(uint32_t aIndex) const; + + void Stringify(nsString& aRetval) const + { + Serialize(aRetval); + } + + typedef URLParams::ForEachIterator ForEachIterator; + + bool + ForEach(ForEachIterator& aIterator) const + { + return mParams->ForEach(aIterator); + + return true; + } + + bool + ReadStructuredClone(JSStructuredCloneReader* aReader); + + bool + WriteStructuredClone(JSStructuredCloneWriter* aWriter) const; + +private: + void AppendInternal(const nsAString& aName, const nsAString& aValue); + + void DeleteAll(); + + void NotifyObserver(); + + UniquePtr<URLParams> mParams; + nsCOMPtr<nsISupports> mParent; + RefPtr<URLSearchParamsObserver> mObserver; +}; + +} // namespace dom +} // namespace mozilla + +#endif /* mozilla_dom_URLSearchParams_h */ diff --git a/dom/url/moz.build b/dom/url/moz.build new file mode 100644 index 0000000000..628b691014 --- /dev/null +++ b/dom/url/moz.build @@ -0,0 +1,27 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +EXPORTS.mozilla.dom += [ + 'URL.h', + 'URLSearchParams.h', +] + +UNIFIED_SOURCES += [ + 'URL.cpp', + 'URLSearchParams.cpp', +] + +LOCAL_INCLUDES += [ + '../workers', +] + +MOCHITEST_MANIFESTS += ['tests/mochitest.ini'] +MOCHITEST_CHROME_MANIFESTS += [ 'tests/chrome.ini' ] +BROWSER_CHROME_MANIFESTS += [ 'tests/browser.ini' ] + +include('/ipc/chromium/chromium-config.mozbuild') + +FINAL_LIBRARY = 'xul' diff --git a/dom/url/tests/browser.ini b/dom/url/tests/browser.ini new file mode 100644 index 0000000000..ad58be9034 --- /dev/null +++ b/dom/url/tests/browser.ini @@ -0,0 +1,5 @@ +[DEFAULT] +support-files = + empty.html + +[browser_download_after_revoke.js] diff --git a/dom/url/tests/browser_download_after_revoke.js b/dom/url/tests/browser_download_after_revoke.js new file mode 100644 index 0000000000..3e521eafef --- /dev/null +++ b/dom/url/tests/browser_download_after_revoke.js @@ -0,0 +1,53 @@ +function test () { + waitForExplicitFinish(); + gBrowser.selectedTab = gBrowser.addTab(); + + function onLoad() { + info("Page loaded."); + gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); + + var listener = { + onOpenWindow: function(aXULWindow) { + info("Download window shown..."); + Services.wm.removeListener(listener); + + function downloadOnLoad() { + domwindow.removeEventListener("load", downloadOnLoad, true); + + is(domwindow.document.location.href, "chrome://mozapps/content/downloads/unknownContentType.xul", "Download page appeared"); + + domwindow.close(); + gBrowser.removeTab(gBrowser.selectedTab); + finish(); + } + + var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindow); + domwindow.addEventListener("load", downloadOnLoad, true); + }, + onCloseWindow: function(aXULWindow) {}, + onWindowTitleChange: function(aXULWindow, aNewTitle) {} + } + + Services.wm.addListener(listener); + + info("Creating BlobURL and clicking on a HTMLAnchorElement..."); + ContentTask.spawn(gBrowser.selectedBrowser, null, function() { + let blob = new content.Blob(['test'], { type: 'text/plain' }); + let url = content.URL.createObjectURL(blob); + + let link = content.document.createElement('a'); + link.href = url; + link.download = 'example.txt'; + content.document.body.appendChild(link); + link.click(); + + content.URL.revokeObjectURL(url); + }); + } + + gBrowser.selectedBrowser.addEventListener("load", onLoad, true); + + info("Loading download page..."); + content.location = "http://example.com/browser/dom/url/tests/empty.html"; +} diff --git a/dom/url/tests/chrome.ini b/dom/url/tests/chrome.ini new file mode 100644 index 0000000000..256fda02b9 --- /dev/null +++ b/dom/url/tests/chrome.ini @@ -0,0 +1,12 @@ +[DEFAULT] +skip-if = os == 'android' +support-files = + file_url.jsm + file_worker_url.jsm + test_bug883784.jsm + jsm_url_worker.js + !/dom/workers/test/dom_worker_helper.js + +[test_bug883784.xul] +[test_url.xul] +[test_worker_url.xul] diff --git a/dom/url/tests/empty.html b/dom/url/tests/empty.html new file mode 100644 index 0000000000..358db717dd --- /dev/null +++ b/dom/url/tests/empty.html @@ -0,0 +1,2 @@ +<!DOCTYPE HTML> +<html><body></body></html> diff --git a/dom/url/tests/file_url.jsm b/dom/url/tests/file_url.jsm new file mode 100644 index 0000000000..a90f8c95dc --- /dev/null +++ b/dom/url/tests/file_url.jsm @@ -0,0 +1,22 @@ +this.EXPORTED_SYMBOLS = ['checkFromJSM']; + +this.checkFromJSM = function checkFromJSM(ok, is) { + Components.utils.importGlobalProperties(['URL', 'Blob']); + + var url = new URL('http://www.example.com'); + is(url.href, "http://www.example.com/", "JSM should have URL"); + + var url2 = new URL('/foobar', url); + is(url2.href, "http://www.example.com/foobar", "JSM should have URL - based on another URL"); + + var blob = new Blob(['a']); + var url = URL.createObjectURL(blob); + ok(url, "URL is created!"); + + var u = new URL(url); + ok(u, "URL created"); + is(u.origin, "null", "Url doesn't have an origin if created in a JSM"); + + URL.revokeObjectURL(url); + ok(true, "URL is revoked"); +} diff --git a/dom/url/tests/file_worker_url.jsm b/dom/url/tests/file_worker_url.jsm new file mode 100644 index 0000000000..d0bbf62fed --- /dev/null +++ b/dom/url/tests/file_worker_url.jsm @@ -0,0 +1,26 @@ +this.EXPORTED_SYMBOLS = ['checkFromJSM']; + +Components.utils.importGlobalProperties(['URL']); + +this.checkFromJSM = function checkFromJSM(ok, is, finish) { + let worker = new ChromeWorker("jsm_url_worker.js"); + worker.onmessage = function(event) { + + if (event.data.type == 'finish') { + finish(); + } else if (event.data.type == 'url') { + URL.revokeObjectURL(event.data.url); + } else if (event.data.type == 'status') { + ok(event.data.status, event.data.msg); + } + } + + worker.onerror = function(event) { + is(event.target, worker); + ok(false, "Worker had an error: " + event.data); + worker.terminate(); + finish(); + }; + + worker.postMessage(0); +} diff --git a/dom/url/tests/jsm_url_worker.js b/dom/url/tests/jsm_url_worker.js new file mode 100644 index 0000000000..539ace40d2 --- /dev/null +++ b/dom/url/tests/jsm_url_worker.js @@ -0,0 +1,83 @@ +onmessage = function(event) { + if (event.data != 0) { + var worker = new Worker('jsm_url_worker.js'); + worker.onmessage = function(event) { + postMessage(event.data); + } + + worker.postMessage(event.data - 1); + return; + } + + status = false; + try { + if ((URL instanceof Object)) { + status = true; + } + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'URL object:' + URL}); + + status = false; + var blob = null; + try { + blob = new Blob([]); + status = true; + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'Blob:' + blob}); + + status = false; + var url = null; + try { + url = URL.createObjectURL(blob); + status = true; + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'Blob URL:' + url}); + + status = false; + try { + URL.revokeObjectURL(url); + status = true; + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'Blob Revoke URL'}); + + status = false; + var url = null; + try { + url = URL.createObjectURL(true); + } catch(e) { + status = true; + } + + postMessage({type: 'status', status: status, msg: 'CreateObjectURL should fail if the arg is not a blob'}); + + status = false; + var url = null; + try { + url = URL.createObjectURL(blob); + status = true; + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'Blob URL2:' + url}); + + status = false; + try { + URL.createObjectURL(new Object()); + } catch(e) { + status = true; + } + + postMessage({type: 'status', status: status, msg: 'Exception wanted' }); + + postMessage({type: 'url', url: url}); + + postMessage({type: 'finish' }); +} diff --git a/dom/url/tests/mochitest.ini b/dom/url/tests/mochitest.ini new file mode 100644 index 0000000000..3c641ba96a --- /dev/null +++ b/dom/url/tests/mochitest.ini @@ -0,0 +1,21 @@ +[DEFAULT] +support-files = + url_worker.js + urlApi_worker.js + urlSearchParams_worker.js + url_exceptions_worker.js + +[test_url.html] +[test_url_data.html] +[test_url_empty_port.html] +[test_url_malformedHost.html] +[test_urlExceptions.html] +[test_urlSearchParams.html] +[test_urlSearchParams_utf8.html] +[test_urlutils_stringify.html] +[test_worker_url.html] +[test_worker_urlApi.html] +[test_worker_url_exceptions.html] +[test_worker_urlSearchParams.html] +[test_unknown_url_origin.html] +[test_bloburl_location.html] diff --git a/dom/url/tests/test_bloburl_location.html b/dom/url/tests/test_bloburl_location.html new file mode 100644 index 0000000000..3d190e125d --- /dev/null +++ b/dom/url/tests/test_bloburl_location.html @@ -0,0 +1,31 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test for blobURL in location</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> + <script type="application/javascript"> + +var expectedData = null; +onmessage = function(e) { + if (expectedData === null) { + expectedData = e.data; + } else { + is(e.data, expectedData, "Pathname should be not be changed"); + SimpleTest.finish(); + } +} + +var ifr = document.createElement('iframe'); +document.body.appendChild(ifr); + +ifr.src = "data:html,<script>location=URL.createObjectURL(new%20Blob(['<script>parent.postMessage(location.pathname,\"*\");location.pathname=\"foo\";parent.postMessage(location.pathname,\"*\");<\/s' +'cript>'], {type:\"text/html\"}));<\/script>"; + +SimpleTest.waitForExplicitFinish(); + + </script> +</body> +</html> diff --git a/dom/url/tests/test_bug883784.jsm b/dom/url/tests/test_bug883784.jsm new file mode 100644 index 0000000000..a5747817d9 --- /dev/null +++ b/dom/url/tests/test_bug883784.jsm @@ -0,0 +1,42 @@ +this.EXPORTED_SYMBOLS = ["Test"]; + +const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; +Cu.importGlobalProperties(["URL"]); + +this.Test = { + start: function(ok, is, finish) { + let worker = new ChromeWorker("jsm_url_worker.js"); + worker.onmessage = function(event) { + if (event.data.type == 'status') { + ok(event.data.status, event.data.msg); + } else if (event.data.type == 'url') { + var xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] + .createInstance(Components.interfaces.nsIXMLHttpRequest); + xhr.open('GET', event.data.url, false); + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + ok(true, "Blob readable!"); + URL.revokeObjectURL(event.data.url); + finish(); + } + } + xhr.onerror = function() { + ok(false, "Blob unreadable, should not happen!"); + URL.revokeObjectURL(event.data.url); + finish(); + } + xhr.send(); + } + }; + + var self = this; + worker.onerror = function(event) { + is(event.target, worker); + ok(false, "Worker had an error: " + event.data); + self.worker.terminate(); + finish(); + }; + + worker.postMessage(0); + } +}; diff --git a/dom/url/tests/test_bug883784.xul b/dom/url/tests/test_bug883784.xul new file mode 100644 index 0000000000..4fdcf5eb10 --- /dev/null +++ b/dom/url/tests/test_bug883784.xul @@ -0,0 +1,36 @@ +<?xml version="1.0"?> +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> +<window title="DOM Worker Threads Test" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + onload="test();"> + + <script type="application/javascript" + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> + <script type="application/javascript" + src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> + <script type="application/javascript" src="chrome://mochitests/content/chrome/dom/workers/test/dom_worker_helper.js"/> + + <script type="application/javascript"> + <![CDATA[ + + function test() + { + waitForWorkerFinish(); + + Components.utils.import("chrome://mochitests/content/chrome/dom/url/tests/test_bug883784.jsm"); + Test.start(ok, is, finish); + } + + ]]> + </script> + + <body xmlns="http://www.w3.org/1999/xhtml"> + <p id="display"></p> + <div id="content" style="display:none;"></div> + <pre id="test"></pre> + </body> + <label id="test-result"/> +</window> diff --git a/dom/url/tests/test_unknown_url_origin.html b/dom/url/tests/test_unknown_url_origin.html new file mode 100644 index 0000000000..052bb6ca49 --- /dev/null +++ b/dom/url/tests/test_unknown_url_origin.html @@ -0,0 +1,17 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test for unknwon URL.origin</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> + <script type="application/javascript"> + + is ((new URL("blob:http://foo.com/bar")).origin, "http://foo.com"); + is ((new URL("blob:blob:http://foo.com/bar")).origin, "http://foo.com"); + + </script> +</body> +</html> diff --git a/dom/url/tests/test_url.html b/dom/url/tests/test_url.html new file mode 100644 index 0000000000..3f3f727d63 --- /dev/null +++ b/dom/url/tests/test_url.html @@ -0,0 +1,442 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test URL API</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887364">Mozilla Bug 887364</a> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=991471">Mozilla Bug 991471</a> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=996055">Mozilla Bug 996055</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + /** Test for Bug 887364 **/ + ok("URL" in window, "window.URL exists"); + + var tests = [ + { url: 'http://www.abc.com', + base: undefined, + error: false, + href: 'http://www.abc.com/', + origin: 'http://www.abc.com', + protocol: 'http:', + username: '', + password: '', + host: 'www.abc.com', + hostname: 'www.abc.com', + port: '', + pathname: '/', + search: '', + hash: '' + }, + { url: 'ftp://auser:apw@www.abc.com', + base: undefined, + error: false, + href: 'ftp://auser:apw@www.abc.com/', + origin: 'ftp://www.abc.com', + protocol: 'ftp:', + username: 'auser', + password: 'apw', + host: 'www.abc.com', + hostname: 'www.abc.com', + port: '', + pathname: '/', + search: '', + hash: '' + }, + { url: 'http://www.abc.com:90/apath/', + base: undefined, + error: false, + href: 'http://www.abc.com:90/apath/', + origin: 'http://www.abc.com:90', + protocol: 'http:', + username: '', + password: '', + host: 'www.abc.com:90', + hostname: 'www.abc.com', + port: '90', + pathname: '/apath/', + search: '', + hash: '' + }, + { url: 'http://www.abc.com/apath/afile.txt#ahash', + base: undefined, + error: false, + href: 'http://www.abc.com/apath/afile.txt#ahash', + origin: 'http://www.abc.com', + protocol: 'http:', + username: '', + password: '', + host: 'www.abc.com', + hostname: 'www.abc.com', + port: '', + pathname: '/apath/afile.txt', + search: '', + hash: '#ahash' + }, + { url: 'http://example.com/?test#hash', + base: undefined, + error: false, + href: 'http://example.com/?test#hash', + origin: 'http://example.com', + protocol: 'http:', + username: '', + password: '', + host: 'example.com', + hostname: 'example.com', + port: '', + pathname: '/', + search: '?test', + hash: '#hash' + }, + { url: 'http://example.com/?test', + base: undefined, + error: false, + href: 'http://example.com/?test', + origin: 'http://example.com', + protocol: 'http:', + username: '', + password: '', + host: 'example.com', + hostname: 'example.com', + port: '', + pathname: '/', + search: '?test', + hash: '' + }, + { url: 'http://example.com/carrot#question%3f', + base: undefined, + error: false, + hash: '#question%3f' + }, + { url: 'https://example.com:4443?', + base: undefined, + error: false, + protocol: 'https:', + port: '4443', + pathname: '/', + hash: '', + search: '' + }, + { url: 'http://www.abc.com/apath/afile.txt#ahash?asearch', + base: undefined, + error: false, + href: 'http://www.abc.com/apath/afile.txt#ahash?asearch', + protocol: 'http:', + pathname: '/apath/afile.txt', + hash: '#ahash?asearch', + search: '' + }, + { url: 'http://www.abc.com/apath/afile.txt?asearch#ahash', + base: undefined, + error: false, + href: 'http://www.abc.com/apath/afile.txt?asearch#ahash', + protocol: 'http:', + pathname: '/apath/afile.txt', + hash: '#ahash', + search: '?asearch' + }, + { url: 'http://abc.com/apath/afile.txt?#ahash', + base: undefined, + error: false, + pathname: '/apath/afile.txt', + hash: '#ahash', + search: '' + }, + { url: 'http://auser:apassword@www.abc.com:90/apath/afile.txt?asearch#ahash', + base: undefined, + error: false, + protocol: 'http:', + username: 'auser', + password: 'apassword', + host: 'www.abc.com:90', + hostname: 'www.abc.com', + port: '90', + pathname: '/apath/afile.txt', + hash: '#ahash', + search: '?asearch', + origin: 'http://www.abc.com:90' + }, + + { url: '/foo#bar', + base: 'www.test.org', + error: true, + }, + { url: '/foo#bar', + base: null, + error: true, + }, + { url: '/foo#bar', + base: 42, + error: true, + }, + { url: 'ftp://ftp.something.net', + base: undefined, + error: false, + protocol: 'ftp:', + }, + { url: 'file:///tmp/file', + base: undefined, + error: false, + protocol: 'file:', + }, + { url: 'gopher://gopher.something.net', + base: undefined, + error: false, + protocol: 'gopher:', + }, + { url: 'ws://ws.something.net', + base: undefined, + error: false, + protocol: 'ws:', + }, + { url: 'wss://ws.something.net', + base: undefined, + error: false, + protocol: 'wss:', + }, + { url: 'foo://foo.something.net', + base: undefined, + error: false, + protocol: 'foo:', + }, + + { url: 'about:blank', + base: undefined, + error: false, + protocol: 'about:', + pathname: 'blank', + skip_setters: false, + }, + + { url: 'foo:bar?what#yeah', + base: undefined, + error: false, + protocol: 'foo:', + pathname: 'bar', + search: '?what', + hash: '#yeah', + skip_setters: false, + }, + ]; + + while(tests.length) { + var test = tests.shift(); + + var error = false; + var url; + try { + if (test.base) { + url = new URL(test.url, test.base); + } else { + url = new URL(test.url); + } + } catch(e) { + error = true; + } + + is(test.error, error, "Error creating URL"); + if (test.error) { + continue; + } + + if ('href' in test) is(url.href, test.href, "href"); + if ('origin' in test) is(url.origin, test.origin, "origin"); + if ('protocol' in test) is(url.protocol, test.protocol, "protocol"); + if ('username' in test) is(url.username, test.username, "username"); + if ('password' in test) is(url.password, test.password, "password"); + if ('host' in test) is(url.host, test.host, "host"); + if ('hostname' in test) is(url.hostname, test.hostname, "hostname"); + if ('port' in test) is(url.port, test.port, "port"); + if ('pathname' in test) is(url.pathname, test.pathname, "pathname"); + if ('search' in test) is(url.search, test.search, "search"); + if ('hash' in test) is(url.hash, test.hash, "hash"); + + if ('skip_setters' in test && test.skip_setters == false) { + info("Skip setter methods for URL: " + test); + continue; + } + + url = new URL('https://www.example.net/what#foo?bar'); + ok(url, "Url exists!"); + + if ('href' in test) url.href = test.href; + if ('protocol' in test) url.protocol = test.protocol; + if ('username' in test && test.username) url.username = test.username; + if ('password' in test && test.password) url.password = test.password; + if ('host' in test) url.host = test.host; + if ('hostname' in test) url.hostname = test.hostname; + if ('port' in test) url.port = test.port; + if ('pathname' in test) url.pathname = test.pathname; + if ('search' in test) url.search = test.search; + if ('hash' in test) url.hash = test.hash; + + if ('href' in test) is(url.href, test.href, "href"); + if ('origin' in test) is(url.origin, test.origin, "origin"); + if ('protocol' in test) is(url.protocol, test.protocol, "protocol"); + if ('username' in test) is(url.username, test.username, "username"); + if ('password' in test) is(url.password, test.password, "password"); + if ('host' in test) is(url.host, test.host, "host"); + if ('hostname' in test) is(test.hostname, url.hostname, "hostname"); + if ('port' in test) is(test.port, url.port, "port"); + if ('pathname' in test) is(test.pathname, url.pathname, "pathname"); + if ('search' in test) is(test.search, url.search, "search"); + if ('hash' in test) is(test.hash, url.hash, "hash"); + + if ('href' in test) is (test.href, url + '', 'stringify works'); + } + + </script> + + <script> + /** Test for Bug 991471 **/ + var url = new URL("http://localhost/"); + url.hostname = ""; + url.username = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"; + url.hostname = "www.mozilla.org"; + url.username = ""; + url.hostname = "www.mozilla.org"; + is(url.href, "http://www.mozilla.org/", "No parsing error with empty host"); + </script> + + <script> + /** Test for Bug 996055 **/ + var url = new URL("http://localhost/"); + url.hostname = ""; + is(url.href, "http://localhost/", "Empty hostname is ignored"); + </script> + + <script> + /** Test for Bug 960014 **/ + var url = new URL("http://localhost/"); + url.hostname = "[2001::1]"; + is(url.hostname, "[2001::1]", "IPv6 hostname"); + is(url.href, "http://[2001::1]/"); + + url.hostname = "[::192.9.5.5]"; + is(url.hostname, "[::192.9.5.5]", "IPv6 hostname"); + is(url.href, "http://[::192.9.5.5]/"); + + url = new URL("http://localhost/"); + url.hostname = "[::]"; + is(url.hostname, "[::]", "IPv6 hostname"); + + url = new URL("http://localhost/"); + url.host = "[2001::1]:30"; + is(url.hostname, "[2001::1]", "IPv6 hostname"); + is(url.port, "30", "Port"); + is(url.host, "[2001::1]:30", "IPv6 host"); + + url = new URL("http://localhost/"); + // This should silently fail since it's missing the brackets + url.hostname = "2001::1"; + is(url.hostname, "localhost", "Setting bad hostname fails"); + </script> + + <script> + var blob = new Blob(['a']); + var url = URL.createObjectURL(blob); + + var u = new URL(url); + ok(u.origin, 'http://mochi.test:8888', "The URL generated from a blob URI has an origin"); + </script> + + <script> + var blob = new Blob(['a']); + var url = URL.createObjectURL(blob); + + var a = document.createElement('A'); + a.href = url; + ok(a.origin, 'http://mochi.test:8888', "The 'a' element has the correct origin"); + </script> + + <script> + var blob = new Blob(['a']); + var url = URL.createObjectURL(blob); + URL.revokeObjectURL(url); + URL.revokeObjectURL(url); + ok(true, "Calling revokeObjectURL twice should be ok"); + </script> + + <script> + URL.revokeObjectURL('blob:something'); + ok(true, "This should not throw."); + </script> + + <script> + var base = new URL("http:\\\\test.com\\path/to\\file?query\\backslash#hash\\"); + is(base.href, "http://test.com/path/to/file?query\\backslash#hash\\"); + + var url = new URL("..\\", base); + is(url.href, "http://test.com/path/"); + + url = new URL("\\test", base); + is(url.href, "http://test.com/test"); + + url = new URL("\\test\\", base); + is(url.href, "http://test.com/test/"); + + url = new URL("http://example.org/test", base); + is(url.href, "http://example.org/test"); + + url = new URL("ftp://tmp/test", base); + is(url.href, "ftp://tmp/test"); + + url = new URL("ftp:\\\\tmp\\test", base); + is(url.href, "ftp://tmp/test"); + + url = new URL("scheme://tmp\\test", base); + is(url.href, "scheme://tmp\\test"); + </script> + + <script> + var url = new URL("scheme:path/to/file?query#hash"); + is(url.href, "scheme:path/to/file?query#hash"); + is(url.pathname, "path/to/file"); + is(url.search, "?query"); + is(url.hash, "#hash"); + + // pathname cannot be overwritten. + url.pathname = "new/path?newquery#newhash"; + is(url.href, "scheme:path/to/file?query#hash"); + + // don't escape '#' until we implement a spec-compliant parser. + url.search = "?newquery#newhash"; + is(url.href, "scheme:path/to/file?newquery#newhash#hash"); + + // nulls get encoded, whitespace gets stripped + url = new URL("scheme:pa\0\nth/to/fi\0\nle?qu\0\nery#ha\0\nsh"); + is(url.href, "scheme:pa%00th/to/fi%00le?qu%00ery#ha%00sh"); + + url.search = "new\0\nquery"; + is(url.href, "scheme:pa%00th/to/fi%00le?new%00%0Aquery#ha%00sh"); + url.hash = "new\0\nhash"; + is(url.href, "scheme:pa%00th/to/fi%00le?new%00%0Aquery#new%00%0Ahash"); + + url = new URL("scheme:path#hash"); + is(url.href, "scheme:path#hash"); + url.search = "query"; + is(url.href, "scheme:path?query#hash"); + url.hash = ""; + is(url.href, "scheme:path?query"); + url.hash = "newhash"; + is(url.href, "scheme:path?query#newhash"); + url.search = ""; + is(url.href, "scheme:path#newhash"); + + // we don't implement a spec-compliant parser yet. + // make sure we are bug compatible with existing implementations. + url = new URL("data:text/html,<a href=\"http://example.org/?q\">Link</a>"); + is(url.href, "data:text/html,<a%20href=\"http://example.org/?q\">Link</a>"); + </script> +</body> +</html> diff --git a/dom/url/tests/test_url.xul b/dom/url/tests/test_url.xul new file mode 100644 index 0000000000..026e9df93b --- /dev/null +++ b/dom/url/tests/test_url.xul @@ -0,0 +1,26 @@ +<?xml version="1.0"?> +<?xml-stylesheet type="text/css" href="chrome://global/skin"?> +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> +<window title="Test for URL API" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> + + <!-- test results are displayed in the html:body --> + <body xmlns="http://www.w3.org/1999/xhtml"> + </body> + + <!-- test code goes here --> + <script type="application/javascript"><![CDATA[ + + /** Test for URL API. **/ + const Cu = Components.utils; + + // Import our test JSM. We first strip the filename off + // the chrome url, then append the jsm filename. + var base = /.*\//.exec(window.location.href)[0]; + Cu.import(base + "file_url.jsm"); + + checkFromJSM(ok, is); + + ]]></script> +</window> diff --git a/dom/url/tests/test_urlExceptions.html b/dom/url/tests/test_urlExceptions.html new file mode 100644 index 0000000000..b75c9d1142 --- /dev/null +++ b/dom/url/tests/test_urlExceptions.html @@ -0,0 +1,57 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=926890 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 926890</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=926890">Mozilla Bug 926890</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> + <script type="application/javascript"> + + // URL.href throws + var url = new URL('http://www.example.com'); + ok(url, "URL created"); + + try { + url.href = '42'; + ok(false, "url.href = 42 should throw"); + } catch(e) { + ok(true, "url.href = 42 should throw"); + ok(e instanceof TypeError, "error type typeError"); + } + + url.href = 'http://www.example.org'; + ok(true, "url.href should not throw"); + + try { + new URL('42'); + ok(false, "new URL(42) should throw"); + } catch(e) { + ok(true, "new URL(42) should throw"); + ok(e instanceof TypeError, "error type typeError"); + } + + try { + new URL('http://www.example.com', '42'); + ok(false, "new URL(something, 42) should throw"); + } catch(e) { + ok(true, "new URL(something, 42) should throw"); + ok(e instanceof TypeError, "error type typeError"); + } + + </script> +</body> +</html> + diff --git a/dom/url/tests/test_urlSearchParams.html b/dom/url/tests/test_urlSearchParams.html new file mode 100644 index 0000000000..d5a65e1c59 --- /dev/null +++ b/dom/url/tests/test_urlSearchParams.html @@ -0,0 +1,334 @@ + +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=887836 +--> +<head> + <meta charset="utf-8"> + <title>Test for URLSearchParams</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887836">Mozilla Bug 887836</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> +<script type="application/javascript"> + + /** Test for Bug 887836 **/ + ok("URLSearchParams" in window, "window.URLSearchParams exists"); + + function testSimpleURLSearchParams() { + var u = new URLSearchParams(); + ok(u, "URLSearchParams created"); + is(u.has('foo'), false, 'URLSearchParams.has(foo)'); + is(u.get('foo'), null, 'URLSearchParams.get(foo)'); + is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)'); + + u.append('foo', 'bar'); + is(u.has('foo'), true, 'URLSearchParams.has(foo)'); + is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)'); + is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); + + u.set('foo', 'bar2'); + is(u.get('foo'), 'bar2', 'URLSearchParams.get(foo)'); + is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); + + is(u + "", "foo=bar2", "stringifier"); + + u.delete('foo'); + + runTest(); + } + + function testCopyURLSearchParams() { + var u = new URLSearchParams(); + ok(u, "URLSearchParams created"); + u.append('foo', 'bar'); + + var uu = new URLSearchParams(u); + is(uu.get('foo'), 'bar', 'uu.get()'); + + u.append('foo', 'bar2'); + is(u.getAll('foo').length, 2, "u.getAll()"); + is(uu.getAll('foo').length, 1, "uu.getAll()"); + + runTest(); + } + + function testParserURLSearchParams() { + var checks = [ + { input: '', data: {} }, + { input: 'a', data: { 'a' : [''] } }, + { input: 'a=b', data: { 'a' : ['b'] } }, + { input: 'a=', data: { 'a' : [''] } }, + { input: '=b', data: { '' : ['b'] } }, + { input: '&', data: {} }, + { input: '&a', data: { 'a' : [''] } }, + { input: 'a&', data: { 'a' : [''] } }, + { input: 'a&a', data: { 'a' : ['', ''] } }, + { input: 'a&b&c', data: { 'a' : [''], 'b' : [''], 'c' : [''] } }, + { input: 'a=b&c=d', data: { 'a' : ['b'], 'c' : ['d'] } }, + { input: 'a=b&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, + { input: '&&&a=b&&&&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, + { input: 'a=a&a=b&a=c', data: { 'a' : ['a', 'b', 'c'] } }, + { input: 'a==a', data: { 'a' : ['=a'] } }, + { input: 'a=a+b+c+d', data: { 'a' : ['a b c d'] } }, + { input: '%=a', data: { '%' : ['a'] } }, + { input: '%a=a', data: { '%a' : ['a'] } }, + { input: '%a_=a', data: { '%a_' : ['a'] } }, + { input: '%61=a', data: { 'a' : ['a'] } }, + { input: '%=a', data: { '%' : ['a'] } }, + { input: '%a=a', data: { '%a' : ['a'] } }, + { input: '%a_=a', data: { '%a_' : ['a'] } }, + { input: '%61=a', data: { 'a' : ['a'] } }, + { input: '%61+%4d%4D=', data: { 'a MM' : [''] } }, + { input: '?a=1', data: { 'a' : ['1'] } }, + { input: '?', data: {} }, + { input: '?=b', data: { '' : ['b'] } }, + ]; + + for (var i = 0; i < checks.length; ++i) { + var u = new URLSearchParams(checks[i].input); + + var count = 0; + for (var key in checks[i].data) { + ++count; + ok(u.has(key), "key " + key + " found"); + + var all = u.getAll(key); + is(all.length, checks[i].data[key].length, "same number of elements"); + + for (var k = 0; k < all.length; ++k) { + is(all[k], checks[i].data[key][k], "value matches"); + } + } + } + + runTest(); + } + + function testURL() { + var url = new URL('http://www.example.net?a=b&c=d'); + ok(url.searchParams, "URL searchParams exists!"); + ok(url.searchParams.has('a'), "URL.searchParams.has('a')"); + is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')"); + ok(url.searchParams.has('c'), "URL.searchParams.has('c')"); + is(url.searchParams.get('c'), 'd', "URL.searchParams.get('c')"); + + url.searchParams.set('e', 'f'); + ok(url.href.indexOf('e=f') != 1, 'URL right'); + + runTest(); + } + + function testEncoding() { + var encoding = [ [ '1', '1' ], + [ 'a b', 'a+b' ], + [ '<>', '%3C%3E' ], + [ '\u0541', '%D5%81'] ]; + + for (var i = 0; i < encoding.length; ++i) { + var url = new URL('http://www.example.net'); + url.searchParams.set('a', encoding[i][0]); + is(url.href, 'http://www.example.net/?a=' + encoding[i][1]); + + var url2 = new URL(url.href); + is(url2.searchParams.get('a'), encoding[i][0], 'a is still there'); + } + + runTest(); + } + + function testOrdering() { + var a = new URLSearchParams("a=1&a=2&b=3&c=4&c=5&a=6"); + is(a.toString(), "a=1&a=2&b=3&c=4&c=5&a=6", "Order is correct"); + is(a.getAll('a').length, 3, "Correct length of getAll()"); + + var b = new URLSearchParams(); + b.append('a', '1'); + b.append('b', '2'); + b.append('a', '3'); + is(b.toString(), "a=1&b=2&a=3", "Order is correct"); + is(b.getAll('a').length, 2, "Correct length of getAll()"); + + runTest(); + } + + function testDelete() { + var a = new URLSearchParams("a=1&a=2&b=3&c=4&c=5&a=6"); + is(a.toString(), "a=1&a=2&b=3&c=4&c=5&a=6", "Order is correct"); + is(a.getAll('a').length, 3, "Correct length of getAll()"); + + a.delete('a'); + is(a.getAll('a').length, 0, "Correct length of getAll()"); + is(a.toString(), "b=3&c=4&c=5", "Order is correct"); + + runTest(); + } + + function testGetNULL() { + + var u = new URLSearchParams(); + is(typeof u.get(''), "object", "typeof URL.searchParams.get('')"); + is(u.get(''), null, "URL.searchParams.get('') should be null"); + + var url = new URL('http://www.example.net?a=b'); + is(url.searchParams.get('b'), null, "URL.searchParams.get('b') should be null"); + is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')"); + + runTest(); + } + + function testSet() { + var u = new URLSearchParams(); + u.set('a','b'); + u.set('e','c'); + u.set('i','d'); + u.set('o','f'); + u.set('u','g'); + + is(u.get('a'), 'b', "URL.searchParams.get('a') should return b"); + is(u.getAll('a').length, 1, "URLSearchParams.getAll('a').length should be 1"); + + u.set('a','h1'); + u.set('a','h2'); + u.set('a','h3'); + u.set('a','h4'); + is(u.get('a'), 'h4', "URL.searchParams.get('a') should return h4"); + is(u.getAll('a').length, 1, "URLSearchParams.getAll('a').length should be 1"); + + is(u.get('e'), 'c', "URL.searchParams.get('e') should return c"); + is(u.get('i'), 'd', "URL.searchParams.get('i') should return d"); + is(u.get('o'), 'f', "URL.searchParams.get('o') should return f"); + is(u.get('u'), 'g', "URL.searchParams.get('u') should return g"); + + is(u.getAll('e').length, 1, "URLSearchParams.getAll('e').length should be 1"); + is(u.getAll('i').length, 1, "URLSearchParams.getAll('i').length should be 1"); + is(u.getAll('o').length, 1, "URLSearchParams.getAll('o').length should be 1"); + is(u.getAll('u').length, 1, "URLSearchParams.getAll('u').length should be 1"); + + u = new URLSearchParams("name1=value1&name1=value2&name1=value3"); + is(u.get('name1'), 'value1', "URL.searchParams.get('name1') should return value1"); + is(u.getAll('name1').length, 3, "URLSearchParams.getAll('name1').length should be 3"); + u.set('name1','firstPair'); + is(u.get('name1'), 'firstPair', "URL.searchParams.get('name1') should return firstPair"); + is(u.getAll('name1').length, 1, "URLSearchParams.getAll('name1').length should be 1"); + + runTest(); + } + + function testIterable() { + var u = new URLSearchParams(); + u.set('1','2'); + u.set('2','4'); + u.set('3','6'); + u.set('4','8'); + u.set('5','10'); + + var key_iter = u.keys(); + var value_iter = u.values(); + var entries_iter = u.entries(); + for (var i = 0; i < 5; ++i) { + var v = i + 1; + var key = key_iter.next(); + var value = value_iter.next(); + var entry = entries_iter.next(); + is(key.value, v.toString(), "Correct Key iterator: " + v.toString()); + ok(!key.done, "Key.done is false"); + is(value.value, (v * 2).toString(), "Correct Value iterator: " + (v * 2).toString()); + ok(!value.done, "Value.done is false"); + is(entry.value[0], v.toString(), "Correct Entry 0 iterator: " + v.toString()); + is(entry.value[1], (v * 2).toString(), "Correct Entry 1 iterator: " + (v * 2).toString()); + ok(!entry.done, "Entry.done is false"); + } + + var last = key_iter.next(); + ok(last.done, "Nothing more to read."); + is(last.value, undefined, "Undefined is the last key"); + + last = value_iter.next(); + ok(last.done, "Nothing more to read."); + is(last.value, undefined, "Undefined is the last value"); + + last = entries_iter.next(); + ok(last.done, "Nothing more to read."); + + key_iter = u.keys(); + key_iter.next(); + key_iter.next(); + u.delete('1'); + u.delete('2'); + u.delete('3'); + u.delete('4'); + u.delete('5'); + + last = key_iter.next(); + ok(last.done, "Nothing more to read."); + is(last.value, undefined, "Undefined is the last key"); + + runTest(); + } + + function testZeroHandling() { + var u = new URLSearchParams; + u.set("a", "b\0c"); + u.set("d\0e", "f"); + u.set("g\0h", "i\0j"); + is(u.toString(), "a=b%00c&d%00e=f&g%00h=i%00j", + "Should encode U+0000 as %00"); + + runTest(); + } + + function testCopyConstructor() { + var url = new URL("http://example.com/"); + var p = url.searchParams; + var q = new URLSearchParams(p); + q.set("a", "b"); + is(url.href, "http://example.com/", + "Messing with copy of URLSearchParams should not affect URL"); + p.set("c", "d"); + is(url.href, "http://example.com/?c=d", + "Messing with URLSearchParams should affect URL"); + + runTest(); + } + + var tests = [ + testSimpleURLSearchParams, + testCopyURLSearchParams, + testParserURLSearchParams, + testURL, + testEncoding, + testOrdering, + testDelete, + testGetNULL, + testSet, + testIterable, + testZeroHandling, + testCopyConstructor, + ]; + + function runTest() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var test = tests.shift(); + test(); + } + + SimpleTest.waitForExplicitFinish(); + runTest(); + +</script> +</body> +</html> diff --git a/dom/url/tests/test_urlSearchParams_utf8.html b/dom/url/tests/test_urlSearchParams_utf8.html new file mode 100644 index 0000000000..22c85de45b --- /dev/null +++ b/dom/url/tests/test_urlSearchParams_utf8.html @@ -0,0 +1,40 @@ + +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=1032511 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 1032511</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1032511">Mozilla Bug 1032511</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> +<a href="http://www.example.net?a=b&c=d" id="anchor">foobar</a> +<area href="http://www.example.net?a=b&c=d" id="area">foobar</area> +<script type="application/javascript"> + + /** Test for Bug 1032511 **/ + var a = new URLSearchParams("%e2"); + ok(a, "a exists"); + is(a.toString(), '=', "The value should be here."); + + a = new URLSearchParams("a%e2"); + // This is a known decoder bug that fails to emit a REPLACEMENT CHARACTER. + is(a.toString(), 'a=', "The value should be here."); + + a = new URLSearchParams("a%e2b"); + is(a.toString(), 'a%EF%BF%BDb=', "The value should be here."); + +</script> +</body> +</html> diff --git a/dom/url/tests/test_url_data.html b/dom/url/tests/test_url_data.html new file mode 100644 index 0000000000..082128f6d9 --- /dev/null +++ b/dom/url/tests/test_url_data.html @@ -0,0 +1,37 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test URL API - data:plain</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1018682">Mozilla Bug 1018682</a> + +<script type="application/javascript"> + +var base = new URL("data:text/plain,"); + +base.protocol = "chrome:"; +is(base.protocol, 'data:', "The protocol should not change from data to chrome."); + +try { + var relative = new URL("a", base); + ok(false, "Relative URL from a data:text/plain should not work."); +} catch(e) { + ok(true, "Relative URL from a data:text/plain should not work."); +} + +base.protocol = "http:"; +ok(true, "Protocol: http changed"); +is(base.href, "http://text/plain,", "Base URL is correct"); + +var relative = new URL("a", base); +ok(relative, "This works."); +is(relative.href, "http://text/a", "Relative URL is correct"); + +</script> + +</body> +</html> diff --git a/dom/url/tests/test_url_empty_port.html b/dom/url/tests/test_url_empty_port.html new file mode 100644 index 0000000000..f3169730c8 --- /dev/null +++ b/dom/url/tests/test_url_empty_port.html @@ -0,0 +1,53 @@ + +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=930450 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 930450</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=930450">Mozilla Bug 930450</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> + <a id="link" href="http://www.example.com:8080">foobar</a> + <area id="area" href="http://www.example.com:8080" /> + <script type="application/javascript"> + + var url = new URL('http://www.example.com:8080'); + is(url.port, '8080', 'URL.port is 8080'); + url.port = ''; + is(url.port, '', 'URL.port is \'\''); + url.port = 0; + is(url.port, '0', 'URL.port is 0'); + + var link = document.getElementById("link"); + is(link.port, '8080', 'URL.port is 8080'); + link.port = ''; + is(link.href, 'http://www.example.com/', "link.href matches"); + is(link.port, '', 'URL.port is \'\''); + link.port = 0; + is(link.href, 'http://www.example.com:0/', "link.href matches"); + is(link.port, '0', 'URL.port is 0'); + + var area = document.getElementById("area"); + is(area.port, '8080', 'URL.port is 8080'); + area.port = ''; + is(area.href, 'http://www.example.com/', "area.href matches"); + is(area.port, '', 'URL.port is \'\''); + area.port = 0; + is(area.href, 'http://www.example.com:0/', "area.href matches"); + is(area.port, '0', 'URL.port is 0'); + + </script> +</body> +</html> diff --git a/dom/url/tests/test_url_malformedHost.html b/dom/url/tests/test_url_malformedHost.html new file mode 100644 index 0000000000..5b813b817e --- /dev/null +++ b/dom/url/tests/test_url_malformedHost.html @@ -0,0 +1,48 @@ + +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=1020041 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 1020041</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1020041">Mozilla Bug 1020041</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> + <a id="link" href="http://www.example.com:8080">foobar</a> + <area id="area" href="http://www.example.com:8080" /> + <script type="application/javascript"> + + var tests = [ + { host: '?', expected: 'www.example.com' }, + { host: 'what?' , expected: 'what' }, + { host: 'so what' , expected: 'www.example.com' }, + { host: 'aa#bb' , expected: 'aa' }, + { host: 'a/b' , expected: 'a' }, + { host: 'a\\b', expected: 'a' }, + { host: '[2001::1]#bla:10', expected: '[2001::1]'} + ]; + + for (var i = 0; i < tests.length; ++i) { + var url = new URL('http://www.example.com'); + url.host = tests[i].host; + is (url.host, tests[i].expected, "URL.host is: " + url.host); + + url = new URL('http://www.example.com'); + url.hostname = tests[i].host; + is (url.hostname, tests[i].expected, "URL.hostname is: " + url.host); + } + + </script> +</body> +</html> diff --git a/dom/url/tests/test_urlutils_stringify.html b/dom/url/tests/test_urlutils_stringify.html new file mode 100644 index 0000000000..a2b32d45f1 --- /dev/null +++ b/dom/url/tests/test_urlutils_stringify.html @@ -0,0 +1,38 @@ + +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=959190 +--> +<head> + <meta charset="utf-8"> + <title>Test for Bug 959190</title> + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=959190">Mozilla Bug 959190</a> +<p id="display"></p> +<div id="content" style="display: none"> + <iframe name="x" id="x"></iframe> + <iframe name="y" id="y"></iframe> +</div> +<pre id="test"> +</pre> + <a id="link" href="http://www.example.com:8080">foobar</a> + <area id="area" href="http://www.example.com:8080" /> + <script type="application/javascript"> + + var url = new URL('http://www.example.com:8080'); + is(url + '', 'http://www.example.com:8080/', 'URL stringify'); + + var link = document.getElementById("link"); + is(link + '', 'http://www.example.com:8080/', 'Anchor stringify'); + + var area = document.getElementById("area"); + is(area + '', 'http://www.example.com:8080/', 'Area stringify'); + + is((location + '').indexOf('http://mochi.test:8888/tests/dom/url/tests/test_urlutils_stringify.html'), 0, 'Location stringify'); + </script> +</body> +</html> diff --git a/dom/url/tests/test_worker_url.html b/dom/url/tests/test_worker_url.html new file mode 100644 index 0000000000..6b010608cb --- /dev/null +++ b/dom/url/tests/test_worker_url.html @@ -0,0 +1,67 @@ +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> +<!DOCTYPE HTML> +<html> +<head> + <title>Test for URL object in workers</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> +<p id="display"></p> +<div id="content" style="display: none"></div> +<pre id="test"></pre> +<script class="testbody" type="text/javascript"> + + var worker = new Worker("url_worker.js"); + + worker.onmessage = function(event) { + is(event.target, worker, "Correct worker"); + + if (event.data.type == 'finish') { + runTest(); + } else if (event.data.type == 'status') { + ok(event.data.status, event.data.msg); + } else if (event.data.type == 'url') { + var xhr = new XMLHttpRequest(); + xhr.open('GET', event.data.url, false); + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + ok(true, "Blob readable!"); + } + } + xhr.send(); + } + }; + + worker.onerror = function(event) { + is(event.target, worker, "Correct worker"); + ok(false, "Worker had an error: " + event.message); + SimpleTest.finish(); + }; + + var tests = [ + function() { worker.postMessage(0); }, + function() { worker.postMessage(1); } + ]; + + function runTest() { + if (!tests.length) { + SimpleTest.finish(); + return; + } + + var test = tests.shift(); + test(); + } + + runTest(); + SimpleTest.waitForExplicitFinish(); + +</script> +</pre> +</body> +</html> + diff --git a/dom/url/tests/test_worker_url.xul b/dom/url/tests/test_worker_url.xul new file mode 100644 index 0000000000..2f4d79b2c9 --- /dev/null +++ b/dom/url/tests/test_worker_url.xul @@ -0,0 +1,35 @@ +<?xml version="1.0"?> +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> +<window title="DOM Worker Threads Test" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + onload="test();"> + + <script type="application/javascript" + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> + <script type="application/javascript" + src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> + + <script type="application/javascript"> + <![CDATA[ + + function test() + { + SimpleTest.waitForExplicitFinish(); + + Components.utils.import("chrome://mochitests/content/chrome/dom/url/tests/file_worker_url.jsm"); + checkFromJSM(ok, is, SimpleTest.finish); + } + + ]]> + </script> + + <body xmlns="http://www.w3.org/1999/xhtml"> + <p id="display"></p> + <div id="content" style="display:none;"></div> + <pre id="test"></pre> + </body> + <label id="test-result"/> +</window> diff --git a/dom/url/tests/test_worker_urlApi.html b/dom/url/tests/test_worker_urlApi.html new file mode 100644 index 0000000000..654d2a1974 --- /dev/null +++ b/dom/url/tests/test_worker_urlApi.html @@ -0,0 +1,45 @@ +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> +<!DOCTYPE HTML> +<html> +<head> + <title>Test for URL API object in workers</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> +<p id="display"></p> +<div id="content" style="display: none"></div> +<pre id="test"></pre> +<script class="testbody" type="text/javascript"> + + var worker = new Worker("urlApi_worker.js"); + + worker.onmessage = function(event) { + is(event.target, worker); + + if (event.data.type == 'finish') { + SimpleTest.finish(); + } else if (event.data.type == 'status') { + ok(event.data.status, event.data.msg); + } + }; + + worker.onerror = function(event) { + is(event.target, worker); + ok(false, "Worker had an error: " + event.data); + SimpleTest.finish(); + }; + + worker.postMessage(true); + + SimpleTest.waitForExplicitFinish(); + +</script> +</pre> +</body> +</html> + + diff --git a/dom/url/tests/test_worker_urlSearchParams.html b/dom/url/tests/test_worker_urlSearchParams.html new file mode 100644 index 0000000000..7578d5c352 --- /dev/null +++ b/dom/url/tests/test_worker_urlSearchParams.html @@ -0,0 +1,43 @@ +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> +<!DOCTYPE HTML> +<html> +<head> + <title>Test for URLSearchParams object in workers</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> +<p id="display"></p> +<div id="content" style="display: none"></div> +<pre id="test"></pre> +<script class="testbody" type="text/javascript"> + + var worker = new Worker("urlSearchParams_worker.js"); + + worker.onmessage = function(event) { + is(event.target, worker); + + if (event.data.type == 'finish') { + SimpleTest.finish(); + } else if (event.data.type == 'status') { + ok(event.data.status, event.data.msg); + } + }; + + worker.onerror = function(event) { + is(event.target, worker); + ok(false, "Worker had an error: " + event.message); + SimpleTest.finish(); + }; + + worker.postMessage(true); + + SimpleTest.waitForExplicitFinish(); + +</script> +</pre> +</body> +</html> diff --git a/dom/url/tests/test_worker_url_exceptions.html b/dom/url/tests/test_worker_url_exceptions.html new file mode 100644 index 0000000000..17993fe4d6 --- /dev/null +++ b/dom/url/tests/test_worker_url_exceptions.html @@ -0,0 +1,44 @@ +<!-- + Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ +--> +<!DOCTYPE HTML> +<html> +<head> + <title>Test for URL exceptions in workers</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> +<p id="display"></p> +<div id="content" style="display: none"></div> +<pre id="test"></pre> +<script class="testbody" type="text/javascript"> + + var worker = new Worker("url_exceptions_worker.js"); + + worker.onmessage = function(event) { + is(event.target, worker); + + if (event.data.type == 'finish') { + SimpleTest.finish(); + } else if (event.data.type == 'status') { + ok(event.data.status, event.data.msg); + } + }; + + worker.onerror = function(event) { + is(event.target, worker); + ok(false, "Worker had an error: " + event.message); + SimpleTest.finish(); + }; + + worker.postMessage(0); + SimpleTest.waitForExplicitFinish(); + +</script> +</pre> +</body> +</html> + + diff --git a/dom/url/tests/urlApi_worker.js b/dom/url/tests/urlApi_worker.js new file mode 100644 index 0000000000..a8b88e046f --- /dev/null +++ b/dom/url/tests/urlApi_worker.js @@ -0,0 +1,272 @@ +function ok(a, msg) { + dump("OK: " + !!a + " => " + a + " " + msg + "\n"); + postMessage({type: 'status', status: !!a, msg: a + ": " + msg }); +} + +function is(a, b, msg) { + dump("IS: " + (a===b) + " => " + a + " | " + b + " " + msg + "\n"); + postMessage({type: 'status', status: a === b, msg: a + " === " + b + ": " + msg }); +} + +onmessage = function() { + status = false; + try { + if ((URL instanceof Object)) { + status = true; + } + } catch(e) { + } + + var tests = [ + { url: 'http://www.abc.com', + base: undefined, + error: false, + href: 'http://www.abc.com/', + origin: 'http://www.abc.com', + protocol: 'http:', + username: '', + password: '', + host: 'www.abc.com', + hostname: 'www.abc.com', + port: '', + pathname: '/', + search: '', + hash: '' + }, + { url: 'ftp://auser:apw@www.abc.com', + base: undefined, + error: false, + href: 'ftp://auser:apw@www.abc.com/', + origin: 'ftp://www.abc.com', + protocol: 'ftp:', + username: 'auser', + password: 'apw', + host: 'www.abc.com', + hostname: 'www.abc.com', + port: '', + pathname: '/', + search: '', + hash: '' + }, + { url: 'http://www.abc.com:90/apath/', + base: undefined, + error: false, + href: 'http://www.abc.com:90/apath/', + origin: 'http://www.abc.com:90', + protocol: 'http:', + username: '', + password: '', + host: 'www.abc.com:90', + hostname: 'www.abc.com', + port: '90', + pathname: '/apath/', + search: '', + hash: '' + }, + { url: 'http://www.abc.com/apath/afile.txt#ahash', + base: undefined, + error: false, + href: 'http://www.abc.com/apath/afile.txt#ahash', + origin: 'http://www.abc.com', + protocol: 'http:', + username: '', + password: '', + host: 'www.abc.com', + hostname: 'www.abc.com', + port: '', + pathname: '/apath/afile.txt', + search: '', + hash: '#ahash' + }, + { url: 'http://example.com/?test#hash', + base: undefined, + error: false, + href: 'http://example.com/?test#hash', + origin: 'http://example.com', + protocol: 'http:', + username: '', + password: '', + host: 'example.com', + hostname: 'example.com', + port: '', + pathname: '/', + search: '?test', + hash: '#hash' + }, + { url: 'http://example.com/?test', + base: undefined, + error: false, + href: 'http://example.com/?test', + origin: 'http://example.com', + protocol: 'http:', + username: '', + password: '', + host: 'example.com', + hostname: 'example.com', + port: '', + pathname: '/', + search: '?test', + hash: '' + }, + { url: 'http://example.com/carrot#question%3f', + base: undefined, + error: false, + hash: '#question%3f' + }, + { url: 'https://example.com:4443?', + base: undefined, + error: false, + protocol: 'https:', + port: '4443', + pathname: '/', + hash: '', + search: '' + }, + { url: 'http://www.abc.com/apath/afile.txt#ahash?asearch', + base: undefined, + error: false, + href: 'http://www.abc.com/apath/afile.txt#ahash?asearch', + protocol: 'http:', + pathname: '/apath/afile.txt', + hash: '#ahash?asearch', + search: '' + }, + { url: 'http://www.abc.com/apath/afile.txt?asearch#ahash', + base: undefined, + error: false, + href: 'http://www.abc.com/apath/afile.txt?asearch#ahash', + protocol: 'http:', + pathname: '/apath/afile.txt', + hash: '#ahash', + search: '?asearch' + }, + { url: 'http://abc.com/apath/afile.txt?#ahash', + base: undefined, + error: false, + pathname: '/apath/afile.txt', + hash: '#ahash', + search: '' + }, + { url: 'http://auser:apassword@www.abc.com:90/apath/afile.txt?asearch#ahash', + base: undefined, + error: false, + protocol: 'http:', + username: 'auser', + password: 'apassword', + host: 'www.abc.com:90', + hostname: 'www.abc.com', + port: '90', + pathname: '/apath/afile.txt', + hash: '#ahash', + search: '?asearch', + origin: 'http://www.abc.com:90' + }, + + { url: '/foo#bar', + base: 'www.test.org', + error: true, + }, + { url: '/foo#bar', + base: null, + error: true, + }, + { url: '/foo#bar', + base: 42, + error: true, + }, + { url: 'ftp://ftp.something.net', + base: undefined, + error: false, + protocol: 'ftp:', + }, + { url: 'file:///tmp/file', + base: undefined, + error: false, + protocol: 'file:', + }, + { url: 'gopher://gopher.something.net', + base: undefined, + error: false, + protocol: 'gopher:', + }, + { url: 'ws://ws.something.net', + base: undefined, + error: false, + protocol: 'ws:', + }, + { url: 'wss://ws.something.net', + base: undefined, + error: false, + protocol: 'wss:', + }, + { url: 'foo://foo.something.net', + base: undefined, + error: false, + protocol: 'foo:', + }, + ]; + + while(tests.length) { + var test = tests.shift(); + + var error = false; + var url; + try { + if (test.base) { + url = new URL(test.url, test.base); + } else { + url = new URL(test.url); + } + } catch(e) { + error = true; + } + + is(test.error, error, "Error creating URL"); + if (test.error) { + continue; + } + + if ('href' in test) is(url.href, test.href, "href"); + if ('origin' in test) is(url.origin, test.origin, "origin"); + if ('protocol' in test) is(url.protocol, test.protocol, "protocol"); + if ('username' in test) is(url.username, test.username, "username"); + if ('password' in test) is(url.password, test.password, "password"); + if ('host' in test) is(url.host, test.host, "host"); + if ('hostname' in test) is(url.hostname, test.hostname, "hostname"); + if ('port' in test) is(url.port, test.port, "port"); + if ('pathname' in test) is(url.pathname, test.pathname, "pathname"); + if ('search' in test) is(url.search, test.search, "search"); + if ('hash' in test) is(url.hash, test.hash, "hash"); + + url = new URL('https://www.example.net/what#foo?bar'); + ok(url, "Url exists!"); + + if ('href' in test) url.href = test.href; + if ('protocol' in test) url.protocol = test.protocol; + if ('username' in test && test.username) url.username = test.username; + if ('password' in test && test.password) url.password = test.password; + if ('host' in test) url.host = test.host; + if ('hostname' in test) url.hostname = test.hostname; + if ('port' in test) url.port = test.port; + if ('pathname' in test) url.pathname = test.pathname; + if ('search' in test) url.search = test.search; + if ('hash' in test) url.hash = test.hash; + + if ('href' in test) is(url.href, test.href, "href"); + if ('origin' in test) is(url.origin, test.origin, "origin"); + if ('protocol' in test) is(url.protocol, test.protocol, "protocol"); + if ('username' in test) is(url.username, test.username, "username"); + if ('password' in test) is(url.password, test.password, "password"); + if ('host' in test) is(url.host, test.host, "host"); + if ('hostname' in test) is(test.hostname, url.hostname, "hostname"); + if ('port' in test) is(test.port, url.port, "port"); + if ('pathname' in test) is(test.pathname, url.pathname, "pathname"); + if ('search' in test) is(test.search, url.search, "search"); + if ('hash' in test) is(test.hash, url.hash, "hash"); + + if ('href' in test) is (test.href, url + '', 'stringify works'); + } + + postMessage({type: 'finish' }); +} + diff --git a/dom/url/tests/urlSearchParams_worker.js b/dom/url/tests/urlSearchParams_worker.js new file mode 100644 index 0000000000..03b104f3f7 --- /dev/null +++ b/dom/url/tests/urlSearchParams_worker.js @@ -0,0 +1,162 @@ +function ok(a, msg) { + dump("OK: " + !!a + " => " + a + " " + msg + "\n"); + postMessage({type: 'status', status: !!a, msg: a + ": " + msg }); +} + +function is(a, b, msg) { + dump("IS: " + (a===b) + " => " + a + " | " + b + " " + msg + "\n"); + postMessage({type: 'status', status: a === b, msg: a + " === " + b + ": " + msg }); +} + +onmessage = function() { + status = false; + try { + if ((URLSearchParams instanceof Object)) { + status = true; + } + } catch(e) { + } + ok(status, "URLSearchParams in workers \\o/"); + + function testSimpleURLSearchParams() { + var u = new URLSearchParams(); + ok(u, "URLSearchParams created"); + is(u.has('foo'), false, 'URLSearchParams.has(foo)'); + is(u.get('foo'), null, 'URLSearchParams.get(foo)'); + is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)'); + + u.append('foo', 'bar'); + is(u.has('foo'), true, 'URLSearchParams.has(foo)'); + is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)'); + is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); + + u.set('foo', 'bar2'); + is(u.get('foo'), 'bar2', 'URLSearchParams.get(foo)'); + is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)'); + + is(u + "", "foo=bar2", "stringify"); + + u.delete('foo'); + + runTest(); + } + + function testCopyURLSearchParams() { + var u = new URLSearchParams(); + ok(u, "URLSearchParams created"); + u.append('foo', 'bar'); + + var uu = new URLSearchParams(u); + is(uu.get('foo'), 'bar', 'uu.get()'); + + u.append('foo', 'bar2'); + is(u.getAll('foo').length, 2, "u.getAll()"); + is(uu.getAll('foo').length, 1, "uu.getAll()"); + + runTest(); + } + + function testParserURLSearchParams() { + var checks = [ + { input: '', data: {} }, + { input: 'a', data: { 'a' : [''] } }, + { input: 'a=b', data: { 'a' : ['b'] } }, + { input: 'a=', data: { 'a' : [''] } }, + { input: '=b', data: { '' : ['b'] } }, + { input: '&', data: {} }, + { input: '&a', data: { 'a' : [''] } }, + { input: 'a&', data: { 'a' : [''] } }, + { input: 'a&a', data: { 'a' : ['', ''] } }, + { input: 'a&b&c', data: { 'a' : [''], 'b' : [''], 'c' : [''] } }, + { input: 'a=b&c=d', data: { 'a' : ['b'], 'c' : ['d'] } }, + { input: 'a=b&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, + { input: '&&&a=b&&&&c=d&', data: { 'a' : ['b'], 'c' : ['d'] } }, + { input: 'a=a&a=b&a=c', data: { 'a' : ['a', 'b', 'c'] } }, + { input: 'a==a', data: { 'a' : ['=a'] } }, + { input: 'a=a+b+c+d', data: { 'a' : ['a b c d'] } }, + { input: '%=a', data: { '%' : ['a'] } }, + { input: '%a=a', data: { '%a' : ['a'] } }, + { input: '%a_=a', data: { '%a_' : ['a'] } }, + { input: '%61=a', data: { 'a' : ['a'] } }, + { input: '%=a', data: { '%' : ['a'] } }, + { input: '%a=a', data: { '%a' : ['a'] } }, + { input: '%a_=a', data: { '%a_' : ['a'] } }, + { input: '%61=a', data: { 'a' : ['a'] } }, + { input: '%61+%4d%4D=', data: { 'a MM' : [''] } }, + { input: '?a=1', data: { 'a' : ['1'] } }, + { input: '?', data: {} }, + { input: '?=b', data: { '' : ['b'] } }, + ]; + + for (var i = 0; i < checks.length; ++i) { + var u = new URLSearchParams(checks[i].input); + + var count = 0; + for (var key in checks[i].data) { + ++count; + ok(u.has(key), "key " + key + " found"); + + var all = u.getAll(key); + is(all.length, checks[i].data[key].length, "same number of elements"); + + for (var k = 0; k < all.length; ++k) { + is(all[k], checks[i].data[key][k], "value matches"); + } + } + } + + runTest(); + } + + function testURL() { + var url = new URL('http://www.example.net?a=b&c=d'); + ok(url.searchParams, "URL searchParams exists!"); + ok(url.searchParams.has('a'), "URL.searchParams.has('a')"); + is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')"); + ok(url.searchParams.has('c'), "URL.searchParams.has('c')"); + is(url.searchParams.get('c'), 'd', "URL.searchParams.get('c')"); + + url.searchParams.set('e', 'f'); + ok(url.href.indexOf('e=f') != 1, 'URL right'); + + runTest(); + } + + function testEncoding() { + var encoding = [ [ '1', '1' ], + [ 'a b', 'a+b' ], + [ '<>', '%3C%3E' ], + [ '\u0541', '%D5%81'] ]; + + for (var i = 0; i < encoding.length; ++i) { + var url = new URL('http://www.example.net'); + url.searchParams.set('a', encoding[i][0]); + is(url.href, 'http://www.example.net/?a=' + encoding[i][1]); + + var url2 = new URL(url.href); + is(url2.searchParams.get('a'), encoding[i][0], 'a is still there'); + } + + runTest(); + } + + var tests = [ + testSimpleURLSearchParams, + testCopyURLSearchParams, + testParserURLSearchParams, + testURL, + testEncoding, + ]; + + function runTest() { + if (!tests.length) { + postMessage({type: 'finish' }); + return; + } + + var test = tests.shift(); + test(); + } + + runTest(); +} diff --git a/dom/url/tests/url_exceptions_worker.js b/dom/url/tests/url_exceptions_worker.js new file mode 100644 index 0000000000..caefc22021 --- /dev/null +++ b/dom/url/tests/url_exceptions_worker.js @@ -0,0 +1,38 @@ +function ok(a, msg) { + postMessage({type: 'status', status: !!a, msg: msg }); +} + +onmessage = function(event) { + // URL.href throws + var url = new URL('http://www.example.com'); + ok(url, "URL created"); + + var status = false; + try { + url.href = '42'; + } catch(e) { + status = true; + } + ok(status, "url.href = 42 should throw"); + + url.href = 'http://www.example.org'; + ok(true, "url.href should not throw"); + + status = false + try { + new URL('42'); + } catch(e) { + status = true; + } + ok(status, "new URL(42) should throw"); + + status = false + try { + new URL('http://www.example.com', '42'); + } catch(e) { + status = true; + } + ok(status, "new URL(something, 42) should throw"); + + postMessage({type: 'finish' }); +} diff --git a/dom/url/tests/url_worker.js b/dom/url/tests/url_worker.js new file mode 100644 index 0000000000..2127acee7c --- /dev/null +++ b/dom/url/tests/url_worker.js @@ -0,0 +1,91 @@ +onmessage = function(event) { + if (event.data != 0) { + var worker = new Worker('url_worker.js'); + worker.onmessage = function(event) { + postMessage(event.data); + } + + worker.postMessage(event.data - 1); + return; + } + + status = false; + try { + if ((URL instanceof Object)) { + status = true; + } + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'URL object:' + URL}); + + status = false; + var blob = null; + try { + blob = new Blob([]); + status = true; + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'Blob:' + blob}); + + status = false; + var url = null; + try { + url = URL.createObjectURL(blob); + status = true; + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'Blob URL:' + url}); + + status = false; + try { + URL.revokeObjectURL(url); + status = true; + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'Blob Revoke URL'}); + + status = false; + var url = null; + try { + url = URL.createObjectURL(true); + } catch(e) { + status = true; + } + + postMessage({type: 'status', status: status, msg: 'CreateObjectURL should fail if the arg is not a blob'}); + + status = false; + var url = null; + try { + url = URL.createObjectURL(blob); + status = true; + } catch(e) { + } + + postMessage({type: 'status', status: status, msg: 'Blob URL2:' + url}); + postMessage({type: 'url', url: url}); + + status = false; + try { + URL.createObjectURL(new Object()); + } catch(e) { + status = true; + } + + postMessage({type: 'status', status: status, msg: 'Exception wanted' }); + + var blob = new Blob([123]); + var uri = URL.createObjectURL(blob); + postMessage({type: 'status', status: !!uri, + msg: "The URI has been generated from the blob"}); + + var u = new URL(uri); + postMessage({type: 'status', status: u.origin == 'http://mochi.test:8888', + msg: "The URL generated from a blob URI has an origin."}); + + postMessage({type: 'finish' }); +} |