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"
|
2017-05-31 22:18:56 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
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)
|
2020-07-01 11:29:29 +03:00
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate, "URL :: CreateURL"_ns),
|
2017-05-31 22:18:56 +03:00
|
|
|
mBlobImpl(aBlobImpl),
|
|
|
|
mURL(aURL) {
|
|
|
|
MOZ_ASSERT(aBlobImpl);
|
|
|
|
}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
bool MainThreadRun() override {
|
2017-05-31 22:18:56 +03:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
|
|
|
|
nsAutoCString url;
|
2020-08-14 22:45:52 +03:00
|
|
|
nsresult rv = BlobURLProtocolHandler::AddDataEntry(
|
|
|
|
mBlobImpl, principal, Some(mWorkerPrivate->AgentClusterId()), 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-07-01 11:29:29 +03:00
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate, "URL :: RevokeURL"_ns),
|
2017-05-31 22:18:56 +03:00
|
|
|
mURL(aURL) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
bool MainThreadRun() override {
|
2017-05-31 22:18:56 +03:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 url(mURL);
|
|
|
|
|
2020-08-17 12:57:04 +03:00
|
|
|
BlobURLProtocolHandler::RemoveDataEntry(
|
|
|
|
url, mWorkerPrivate->GetPrincipal(),
|
|
|
|
Some(mWorkerPrivate->AgentClusterId()));
|
2017-05-31 22:18:56 +03:00
|
|
|
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)
|
2020-07-01 11:29:29 +03:00
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate, "URL :: IsValidURL"_ns),
|
2017-05-31 22:18:56 +03:00
|
|
|
mURL(aURL),
|
|
|
|
mValid(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
bool 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; }
|
|
|
|
};
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void URLWorker::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob,
|
|
|
|
nsAString& aResult, mozilla::ErrorResult& aRv) {
|
2017-05-31 22:18:56 +03:00
|
|
|
JSContext* cx = aGlobal.Context();
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
|
|
|
|
|
|
|
|
RefPtr<BlobImpl> blobImpl = aBlob.Impl();
|
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
|
|
|
RefPtr<CreateURLRunnable> runnable =
|
|
|
|
new CreateURLRunnable(workerPrivate, blobImpl, aResult);
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
runnable->Dispatch(Canceling, aRv);
|
2017-05-31 22:18:56 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:24:37 +03:00
|
|
|
WorkerGlobalScope* scope = workerPrivate->GlobalScope();
|
|
|
|
MOZ_ASSERT(scope);
|
2017-05-31 22:18:56 +03:00
|
|
|
|
2020-02-02 12:24:37 +03:00
|
|
|
scope->RegisterHostObjectURI(NS_ConvertUTF16toUTF8(aResult));
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void URLWorker::RevokeObjectURL(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aUrl, ErrorResult& aRv) {
|
2017-05-31 22:18:56 +03:00
|
|
|
JSContext* cx = aGlobal.Context();
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
|
|
|
|
|
|
|
|
RefPtr<RevokeURLRunnable> runnable =
|
|
|
|
new RevokeURLRunnable(workerPrivate, aUrl);
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
runnable->Dispatch(Canceling, aRv);
|
2017-05-31 22:18:56 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:24:37 +03:00
|
|
|
WorkerGlobalScope* scope = workerPrivate->GlobalScope();
|
|
|
|
MOZ_ASSERT(scope);
|
2017-05-31 22:18:56 +03:00
|
|
|
|
2020-02-02 12:24:37 +03:00
|
|
|
scope->UnregisterHostObjectURI(NS_ConvertUTF16toUTF8(aUrl));
|
2017-05-31 22:18:56 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
bool URLWorker::IsValidURL(const GlobalObject& aGlobal, const nsAString& aUrl,
|
|
|
|
ErrorResult& aRv) {
|
2017-05-31 22:18:56 +03:00
|
|
|
JSContext* cx = aGlobal.Context();
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
|
|
|
|
|
|
|
|
RefPtr<IsValidURLRunnable> runnable =
|
|
|
|
new IsValidURLRunnable(workerPrivate, aUrl);
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
runnable->Dispatch(Canceling, aRv);
|
2017-05-31 22:18:56 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return runnable->IsValidURL();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|