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"
18 // This class creates an URL from a DOM Blob on the main thread.
19 class CreateURLRunnable
: public WorkerMainThreadRunnable
{
25 CreateURLRunnable(WorkerPrivate
* aWorkerPrivate
, BlobImpl
* aBlobImpl
,
27 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: CreateURL"_ns
),
30 MOZ_ASSERT(aBlobImpl
);
33 bool MainThreadRun() override
{
34 using namespace mozilla::ipc
;
36 AssertIsOnMainThread();
38 nsCOMPtr
<nsIPrincipal
> principal
= mWorkerPrivate
->GetPrincipal();
41 nsresult rv
= BlobURLProtocolHandler::AddDataEntry(
42 mBlobImpl
, principal
, Some(mWorkerPrivate
->AgentClusterId()), url
);
45 NS_WARNING("Failed to add data entry for the blob!");
46 SetDOMStringToNull(mURL
);
50 CopyUTF8toUTF16(url
, mURL
);
55 // This class revokes an URL on the main thread.
56 class RevokeURLRunnable
: public WorkerMainThreadRunnable
{
61 RevokeURLRunnable(WorkerPrivate
* aWorkerPrivate
, const nsAString
& aURL
)
62 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: RevokeURL"_ns
),
65 bool MainThreadRun() override
{
66 AssertIsOnMainThread();
68 NS_ConvertUTF16toUTF8
url(mURL
);
70 BlobURLProtocolHandler::RemoveDataEntry(
71 url
, mWorkerPrivate
->GetPrincipal(),
72 Some(mWorkerPrivate
->AgentClusterId()));
77 // This class checks if an URL is valid on the main thread.
78 class IsValidURLRunnable
: public WorkerMainThreadRunnable
{
84 IsValidURLRunnable(WorkerPrivate
* aWorkerPrivate
, const nsAString
& aURL
)
85 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: IsValidURL"_ns
),
89 bool MainThreadRun() override
{
90 AssertIsOnMainThread();
92 NS_ConvertUTF16toUTF8
url(mURL
);
93 mValid
= BlobURLProtocolHandler::HasDataEntry(url
);
98 bool IsValidURL() const { return mValid
; }
102 void URLWorker::CreateObjectURL(const GlobalObject
& aGlobal
, Blob
& aBlob
,
103 nsAString
& aResult
, mozilla::ErrorResult
& aRv
) {
104 JSContext
* cx
= aGlobal
.Context();
105 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
107 RefPtr
<BlobImpl
> blobImpl
= aBlob
.Impl();
108 MOZ_ASSERT(blobImpl
);
110 RefPtr
<CreateURLRunnable
> runnable
=
111 new CreateURLRunnable(workerPrivate
, blobImpl
, aResult
);
113 runnable
->Dispatch(Canceling
, aRv
);
114 if (NS_WARN_IF(aRv
.Failed())) {
118 WorkerGlobalScope
* scope
= workerPrivate
->GlobalScope();
121 scope
->RegisterHostObjectURI(NS_ConvertUTF16toUTF8(aResult
));
125 void URLWorker::RevokeObjectURL(const GlobalObject
& aGlobal
,
126 const nsAString
& aUrl
, ErrorResult
& aRv
) {
127 JSContext
* cx
= aGlobal
.Context();
128 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
130 RefPtr
<RevokeURLRunnable
> runnable
=
131 new RevokeURLRunnable(workerPrivate
, aUrl
);
133 runnable
->Dispatch(Canceling
, aRv
);
134 if (NS_WARN_IF(aRv
.Failed())) {
138 WorkerGlobalScope
* scope
= workerPrivate
->GlobalScope();
141 scope
->UnregisterHostObjectURI(NS_ConvertUTF16toUTF8(aUrl
));
145 bool URLWorker::IsValidURL(const GlobalObject
& aGlobal
, const nsAString
& aUrl
,
147 JSContext
* cx
= aGlobal
.Context();
148 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
150 RefPtr
<IsValidURLRunnable
> runnable
=
151 new IsValidURLRunnable(workerPrivate
, aUrl
);
153 runnable
->Dispatch(Canceling
, aRv
);
154 if (NS_WARN_IF(aRv
.Failed())) {
158 return runnable
->IsValidURL();
162 } // namespace mozilla