1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/dom/Blob.h"
10 #include "mozilla/dom/BlobImpl.h"
11 #include "mozilla/dom/BlobURLProtocolHandler.h"
12 #include "mozilla/dom/WorkerRunnable.h"
13 #include "mozilla/dom/WorkerScope.h"
15 namespace mozilla::dom
{
17 // This class creates an URL from a DOM Blob on the main thread.
18 class CreateURLRunnable
: public WorkerMainThreadRunnable
{
24 CreateURLRunnable(WorkerPrivate
* aWorkerPrivate
, BlobImpl
* aBlobImpl
,
26 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: CreateURL"_ns
),
29 MOZ_ASSERT(aBlobImpl
);
32 bool MainThreadRun() override
{
33 using namespace mozilla::ipc
;
35 AssertIsOnMainThread();
37 nsCOMPtr
<nsIPrincipal
> principal
= mWorkerPrivate
->GetPrincipal();
40 nsresult rv
= BlobURLProtocolHandler::AddDataEntry(
41 mBlobImpl
, principal
, Some(mWorkerPrivate
->AgentClusterId()), url
);
44 NS_WARNING("Failed to add data entry for the blob!");
45 SetDOMStringToNull(mURL
);
49 CopyUTF8toUTF16(url
, mURL
);
54 // This class revokes an URL on the main thread.
55 class RevokeURLRunnable
: public WorkerMainThreadRunnable
{
60 RevokeURLRunnable(WorkerPrivate
* aWorkerPrivate
, const nsAString
& aURL
)
61 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: RevokeURL"_ns
),
64 bool MainThreadRun() override
{
65 AssertIsOnMainThread();
67 NS_ConvertUTF16toUTF8
url(mURL
);
69 BlobURLProtocolHandler::RemoveDataEntry(
70 url
, mWorkerPrivate
->GetPrincipal(),
71 Some(mWorkerPrivate
->AgentClusterId()));
76 // This class checks if an URL is valid on the main thread.
77 class IsValidURLRunnable
: public WorkerMainThreadRunnable
{
83 IsValidURLRunnable(WorkerPrivate
* aWorkerPrivate
, const nsAString
& aURL
)
84 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: IsValidURL"_ns
),
88 bool MainThreadRun() override
{
89 AssertIsOnMainThread();
91 NS_ConvertUTF16toUTF8
url(mURL
);
92 mValid
= BlobURLProtocolHandler::HasDataEntry(url
);
97 bool IsValidURL() const { return mValid
; }
101 void URLWorker::CreateObjectURL(const GlobalObject
& aGlobal
, Blob
& aBlob
,
102 nsAString
& aResult
, mozilla::ErrorResult
& aRv
) {
103 JSContext
* cx
= aGlobal
.Context();
104 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
106 RefPtr
<BlobImpl
> blobImpl
= aBlob
.Impl();
107 MOZ_ASSERT(blobImpl
);
109 RefPtr
<CreateURLRunnable
> runnable
=
110 new CreateURLRunnable(workerPrivate
, blobImpl
, aResult
);
112 runnable
->Dispatch(Canceling
, aRv
);
113 if (NS_WARN_IF(aRv
.Failed())) {
117 WorkerGlobalScope
* scope
= workerPrivate
->GlobalScope();
120 scope
->RegisterHostObjectURI(NS_ConvertUTF16toUTF8(aResult
));
124 void URLWorker::RevokeObjectURL(const GlobalObject
& aGlobal
,
125 const nsAString
& aUrl
, ErrorResult
& aRv
) {
126 JSContext
* cx
= aGlobal
.Context();
127 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
129 RefPtr
<RevokeURLRunnable
> runnable
=
130 new RevokeURLRunnable(workerPrivate
, aUrl
);
132 runnable
->Dispatch(Canceling
, aRv
);
133 if (NS_WARN_IF(aRv
.Failed())) {
137 WorkerGlobalScope
* scope
= workerPrivate
->GlobalScope();
140 scope
->UnregisterHostObjectURI(NS_ConvertUTF16toUTF8(aUrl
));
144 bool URLWorker::IsValidObjectURL(const GlobalObject
& aGlobal
,
145 const nsAString
& aUrl
, ErrorResult
& aRv
) {
146 JSContext
* cx
= aGlobal
.Context();
147 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
149 RefPtr
<IsValidURLRunnable
> runnable
=
150 new IsValidURLRunnable(workerPrivate
, aUrl
);
152 runnable
->Dispatch(Canceling
, aRv
);
153 if (NS_WARN_IF(aRv
.Failed())) {
157 return runnable
->IsValidURL();
160 } // namespace mozilla::dom