2017-05-31 22:18:56 +03:00
|
|
|
/* -*- 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 "URLWorker.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/Blob.h"
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2018-02-26 22:43:46 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2018-04-14 02:06:35 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2017-06-09 16:20:16 +03:00
|
|
|
#include "nsStandardURL.h"
|
|
|
|
#include "nsURLHelper.h"
|
2017-05-31 22:18:56 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
2017-06-09 16:20:16 +03:00
|
|
|
|
|
|
|
using net::nsStandardURL;
|
|
|
|
|
2017-05-31 22:18:56 +03:00
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
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
|
2017-11-06 06:37:28 +03:00
|
|
|
MainThreadRun() override
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
DebugOnly<bool> isMutable;
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mBlobImpl->GetMutable(&isMutable)));
|
|
|
|
MOZ_ASSERT(!isMutable);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
|
|
|
|
nsAutoCString url;
|
|
|
|
nsresult rv =
|
2018-06-02 16:51:42 +03:00
|
|
|
BlobURLProtocolHandler::AddDataEntry(mBlobImpl, principal, url);
|
2017-05-31 22:18:56 +03:00
|
|
|
|
|
|
|
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
|
2017-11-06 06:37:28 +03:00
|
|
|
MainThreadRun() override
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 url(mURL);
|
|
|
|
|
|
|
|
nsIPrincipal* urlPrincipal =
|
2018-06-02 16:51:42 +03:00
|
|
|
BlobURLProtocolHandler::GetDataEntryPrincipal(url);
|
2017-05-31 22:18:56 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
|
|
|
|
bool subsumes;
|
|
|
|
if (urlPrincipal &&
|
|
|
|
NS_SUCCEEDED(principal->Subsumes(urlPrincipal, &subsumes)) &&
|
|
|
|
subsumes) {
|
2018-06-02 16:51:42 +03:00
|
|
|
BlobURLProtocolHandler::RemoveDataEntry(url);
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-11-06 06:37:28 +03:00
|
|
|
MainThreadRun() override
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 url(mURL);
|
2018-06-02 16:51:42 +03:00
|
|
|
mValid = BlobURLProtocolHandler::HasDataEntry(url);
|
2017-05-31 22:18:56 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsCOMPtr<nsIURI> mRetval;
|
2017-05-31 22:18:56 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-11-06 06:37:28 +03:00
|
|
|
MainThreadRun() override
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsCOMPtr<nsIURI> baseUri;
|
2017-05-31 22:18:56 +03:00
|
|
|
if (!mBase.IsVoid()) {
|
2018-04-24 13:01:46 +03:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(baseUri), mBase, nullptr, nullptr,
|
|
|
|
nsContentUtils::GetIOService());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), mURL, nullptr, baseUri,
|
|
|
|
nsContentUtils::GetIOService());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-05-31 22:18:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mRetval = std::move(uri);
|
2017-05-31 22:18:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsIURI*
|
|
|
|
GetURI(ErrorResult& aRv) const
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mRetval) {
|
|
|
|
aRv.ThrowTypeError<MSG_INVALID_URL>(mURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mRetval;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-14 02:06:35 +03:00
|
|
|
class OriginGetterRunnable : public WorkerMainThreadRunnable
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
public:
|
2018-04-14 02:06:35 +03:00
|
|
|
OriginGetterRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
nsAString& aValue,
|
2018-04-24 13:01:46 +03:00
|
|
|
nsIURI* aURI)
|
2017-05-31 22:18:56 +03:00
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate,
|
|
|
|
// We can have telemetry keys for each getter when
|
|
|
|
// needed.
|
|
|
|
NS_LITERAL_CSTRING("URL :: getter"))
|
|
|
|
, mValue(aValue)
|
2018-04-24 13:01:46 +03:00
|
|
|
, mURI(aURI)
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-11-06 06:37:28 +03:00
|
|
|
MainThreadRun() override
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
ErrorResult rv;
|
2018-04-24 13:01:46 +03:00
|
|
|
nsContentUtils::GetUTFOrigin(mURI, mValue);
|
2017-05-31 22:18:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Dispatch(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
WorkerMainThreadRunnable::Dispatch(Terminating, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAString& mValue;
|
2018-04-24 13:01:46 +03:00
|
|
|
nsCOMPtr<nsIURI> mURI;
|
2017-05-31 22:18:56 +03:00
|
|
|
};
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
class ProtocolSetterRunnable : public WorkerMainThreadRunnable
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
public:
|
2018-04-24 13:01:46 +03:00
|
|
|
ProtocolSetterRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsACString& aValue,
|
|
|
|
nsIURI* aURI)
|
2017-05-31 22:18:56 +03:00
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate,
|
2018-04-24 13:01:46 +03:00
|
|
|
NS_LITERAL_CSTRING("ProtocolSetterRunnable"))
|
2017-05-31 22:18:56 +03:00
|
|
|
, mValue(aValue)
|
2018-04-24 13:01:46 +03:00
|
|
|
, mURI(aURI)
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-11-06 06:37:28 +03:00
|
|
|
MainThreadRun() override
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsCOMPtr<nsIURI> clone;
|
|
|
|
nsresult rv = NS_MutateURI(mURI)
|
|
|
|
.SetScheme(mValue)
|
|
|
|
.Finalize(clone);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-31 22:18:56 +03:00
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsAutoCString href;
|
|
|
|
rv = clone->GetSpec(href);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return true;
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), href);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return true;
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mRetval = std::move(uri);
|
2017-05-31 22:18:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Dispatch(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
WorkerMainThreadRunnable::Dispatch(Terminating, aRv);
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsIURI*
|
|
|
|
GetRetval() const
|
|
|
|
{
|
|
|
|
return mRetval;
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:18:56 +03:00
|
|
|
private:
|
2018-04-24 13:01:46 +03:00
|
|
|
const nsCString mValue;
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
nsCOMPtr<nsIURI> mRetval;
|
2017-05-31 22:18:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2017-06-09 16:20:16 +03:00
|
|
|
RefPtr<URLWorker> url = new URLWorker(workerPrivate);
|
|
|
|
url->Init(aURL, aBase, aRv);
|
2017-05-31 22:18:56 +03:00
|
|
|
|
2017-06-09 16:20:16 +03:00
|
|
|
return aRv.Failed() ? nullptr : url.forget();
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<URLWorker>
|
|
|
|
URLWorker::Constructor(const GlobalObject& aGlobal, const nsAString& aURL,
|
|
|
|
const nsAString& aBase, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
Optional<nsAString> base;
|
|
|
|
base = &aBase;
|
|
|
|
|
2017-06-09 16:20:16 +03:00
|
|
|
return Constructor(aGlobal, aURL, base, aRv);
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
URLWorker::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob,
|
|
|
|
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, aResult);
|
|
|
|
|
|
|
|
runnable->Dispatch(Terminating, 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(Terminating, 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(Terminating, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return runnable->IsValidURL();
|
|
|
|
}
|
|
|
|
|
2017-06-09 16:20:16 +03:00
|
|
|
URLWorker::URLWorker(WorkerPrivate* aWorkerPrivate)
|
2017-05-31 22:18:56 +03:00
|
|
|
: URL(nullptr)
|
|
|
|
, mWorkerPrivate(aWorkerPrivate)
|
|
|
|
{}
|
|
|
|
|
2017-06-09 16:20:16 +03:00
|
|
|
void
|
|
|
|
URLWorker::Init(const nsAString& aURL, const Optional<nsAString>& aBase,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsAutoCString scheme;
|
|
|
|
nsresult rv = net_ExtractURLScheme(NS_ConvertUTF16toUTF8(aURL), scheme);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// this may be a relative URL, check baseURL
|
|
|
|
if (!aBase.WasPassed()) {
|
|
|
|
aRv.ThrowTypeError<MSG_INVALID_URL>(aURL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rv = net_ExtractURLScheme(NS_ConvertUTF16toUTF8(aBase.Value()), scheme);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.ThrowTypeError<MSG_INVALID_URL>(aURL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create url proxy
|
|
|
|
RefPtr<ConstructorRunnable> runnable =
|
|
|
|
new ConstructorRunnable(mWorkerPrivate, aURL, aBase);
|
|
|
|
runnable->Dispatch(Terminating, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsCOMPtr<nsIURI> uri = runnable->GetURI(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2017-06-09 16:20:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
SetURI(uri.forget());
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
URLWorker::~URLWorker() = default;
|
2018-01-29 22:51:32 +03:00
|
|
|
|
|
|
|
void
|
2018-04-24 13:01:46 +03:00
|
|
|
URLWorker::SetHref(const nsAString& aHref, ErrorResult& aRv)
|
2017-05-31 22:18:56 +03:00
|
|
|
{
|
2017-06-09 16:20:16 +03:00
|
|
|
nsAutoCString scheme;
|
|
|
|
nsresult rv = net_ExtractURLScheme(NS_ConvertUTF16toUTF8(aHref), scheme);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.ThrowTypeError<MSG_INVALID_URL>(aHref);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-09 16:20:16 +03:00
|
|
|
|
2017-06-09 16:20:16 +03:00
|
|
|
RefPtr<ConstructorRunnable> runnable =
|
|
|
|
new ConstructorRunnable(mWorkerPrivate, aHref, Optional<nsAString>());
|
|
|
|
runnable->Dispatch(Terminating, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2017-05-31 22:18:56 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsCOMPtr<nsIURI> uri = runnable->GetURI(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetURI(uri.forget());
|
2017-05-31 22:18:56 +03:00
|
|
|
UpdateURLSearchParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URLWorker::GetOrigin(nsAString& aOrigin, ErrorResult& aRv) const
|
|
|
|
{
|
2018-04-14 02:06:35 +03:00
|
|
|
RefPtr<OriginGetterRunnable> runnable =
|
2018-04-24 13:01:46 +03:00
|
|
|
new OriginGetterRunnable(mWorkerPrivate, aOrigin, GetURI());
|
2017-05-31 22:18:56 +03:00
|
|
|
|
|
|
|
runnable->Dispatch(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URLWorker::SetProtocol(const nsAString& aProtocol, ErrorResult& aRv)
|
|
|
|
{
|
2018-01-29 22:51:32 +03:00
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
aProtocol.BeginReading(start);
|
|
|
|
aProtocol.EndReading(end);
|
|
|
|
nsAString::const_iterator iter(start);
|
|
|
|
|
|
|
|
FindCharInReadable(':', iter, end);
|
|
|
|
NS_ConvertUTF16toUTF8 scheme(Substring(start, iter));
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
RefPtr<ProtocolSetterRunnable> runnable =
|
|
|
|
new ProtocolSetterRunnable(mWorkerPrivate, scheme, GetURI());
|
2017-05-31 22:18:56 +03:00
|
|
|
runnable->Dispatch(aRv);
|
2018-04-24 13:01:46 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2017-06-09 16:20:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
nsCOMPtr<nsIURI> uri = runnable->GetRetval();
|
|
|
|
if (NS_WARN_IF(!uri)) {
|
2017-06-09 16:20:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-24 13:01:46 +03:00
|
|
|
SetURI(uri.forget());
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|