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();
39 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
=
40 mWorkerPrivate
->CookieJarSettings();
43 cookieJarSettings
->GetPartitionKey(partKey
);
46 nsresult rv
= BlobURLProtocolHandler::AddDataEntry(
47 mBlobImpl
, principal
, NS_ConvertUTF16toUTF8(partKey
), url
);
50 NS_WARNING("Failed to add data entry for the blob!");
51 SetDOMStringToNull(mURL
);
55 CopyUTF8toUTF16(url
, mURL
);
60 // This class revokes an URL on the main thread.
61 class RevokeURLRunnable
: public WorkerMainThreadRunnable
{
66 RevokeURLRunnable(WorkerPrivate
* aWorkerPrivate
, const nsAString
& aURL
)
67 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: RevokeURL"_ns
),
70 bool MainThreadRun() override
{
71 AssertIsOnMainThread();
73 NS_ConvertUTF16toUTF8
url(mURL
);
75 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
=
76 mWorkerPrivate
->CookieJarSettings();
79 cookieJarSettings
->GetPartitionKey(partKey
);
81 BlobURLProtocolHandler::RemoveDataEntry(url
, mWorkerPrivate
->GetPrincipal(),
82 NS_ConvertUTF16toUTF8(partKey
));
87 // This class checks if an URL is valid on the main thread.
88 class IsValidURLRunnable
: public WorkerMainThreadRunnable
{
94 IsValidURLRunnable(WorkerPrivate
* aWorkerPrivate
, const nsAString
& aURL
)
95 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: IsValidURL"_ns
),
99 bool MainThreadRun() override
{
100 AssertIsOnMainThread();
102 NS_ConvertUTF16toUTF8
url(mURL
);
103 mValid
= BlobURLProtocolHandler::HasDataEntry(url
);
108 bool IsValidURL() const { return mValid
; }
112 void URLWorker::CreateObjectURL(const GlobalObject
& aGlobal
, Blob
& aBlob
,
113 nsAString
& aResult
, mozilla::ErrorResult
& aRv
) {
114 JSContext
* cx
= aGlobal
.Context();
115 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
117 RefPtr
<BlobImpl
> blobImpl
= aBlob
.Impl();
118 MOZ_ASSERT(blobImpl
);
120 RefPtr
<CreateURLRunnable
> runnable
=
121 new CreateURLRunnable(workerPrivate
, blobImpl
, aResult
);
123 runnable
->Dispatch(Canceling
, aRv
);
124 if (NS_WARN_IF(aRv
.Failed())) {
128 WorkerGlobalScope
* scope
= workerPrivate
->GlobalScope();
131 scope
->RegisterHostObjectURI(NS_ConvertUTF16toUTF8(aResult
));
135 void URLWorker::RevokeObjectURL(const GlobalObject
& aGlobal
,
136 const nsAString
& aUrl
, ErrorResult
& aRv
) {
137 JSContext
* cx
= aGlobal
.Context();
138 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
140 RefPtr
<RevokeURLRunnable
> runnable
=
141 new RevokeURLRunnable(workerPrivate
, aUrl
);
143 runnable
->Dispatch(Canceling
, aRv
);
144 if (NS_WARN_IF(aRv
.Failed())) {
148 WorkerGlobalScope
* scope
= workerPrivate
->GlobalScope();
151 scope
->UnregisterHostObjectURI(NS_ConvertUTF16toUTF8(aUrl
));
155 bool URLWorker::IsValidObjectURL(const GlobalObject
& aGlobal
,
156 const nsAString
& aUrl
, ErrorResult
& aRv
) {
157 JSContext
* cx
= aGlobal
.Context();
158 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
160 RefPtr
<IsValidURLRunnable
> runnable
=
161 new IsValidURLRunnable(workerPrivate
, aUrl
);
163 runnable
->Dispatch(Canceling
, aRv
);
164 if (NS_WARN_IF(aRv
.Failed())) {
168 return runnable
->IsValidURL();
171 } // namespace mozilla::dom