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
);
45 nsresult rv
= BlobURLProtocolHandler::AddDataEntry(
46 mBlobImpl
, principal
, NS_ConvertUTF16toUTF8(partKey
), mURL
);
48 NS_WARNING("Failed to add data entry for the blob!");
56 // This class revokes an URL on the main thread.
57 class RevokeURLRunnable
: public WorkerMainThreadRunnable
{
62 RevokeURLRunnable(WorkerPrivate
* aWorkerPrivate
, const nsACString
& aURL
)
63 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: RevokeURL"_ns
),
66 bool MainThreadRun() override
{
67 AssertIsOnMainThread();
69 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
=
70 mWorkerPrivate
->CookieJarSettings();
73 cookieJarSettings
->GetPartitionKey(partKey
);
75 BlobURLProtocolHandler::RemoveDataEntry(
76 mURL
, mWorkerPrivate
->GetPrincipal(), NS_ConvertUTF16toUTF8(partKey
));
81 // This class checks if an URL is valid on the main thread.
82 class IsValidURLRunnable
: public WorkerMainThreadRunnable
{
88 IsValidURLRunnable(WorkerPrivate
* aWorkerPrivate
, const nsACString
& aURL
)
89 : WorkerMainThreadRunnable(aWorkerPrivate
, "URL :: IsValidURL"_ns
),
93 bool MainThreadRun() override
{
94 AssertIsOnMainThread();
96 mValid
= BlobURLProtocolHandler::HasDataEntry(mURL
);
101 bool IsValidURL() const { return mValid
; }
105 void URLWorker::CreateObjectURL(const GlobalObject
& aGlobal
, Blob
& aBlob
,
107 mozilla::ErrorResult
& aRv
) {
108 JSContext
* cx
= aGlobal
.Context();
109 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
111 RefPtr
<BlobImpl
> blobImpl
= aBlob
.Impl();
112 MOZ_ASSERT(blobImpl
);
114 RefPtr
<CreateURLRunnable
> runnable
=
115 new CreateURLRunnable(workerPrivate
, blobImpl
, aResult
);
117 runnable
->Dispatch(Canceling
, aRv
);
118 if (NS_WARN_IF(aRv
.Failed())) {
122 WorkerGlobalScope
* scope
= workerPrivate
->GlobalScope();
125 scope
->RegisterHostObjectURI(aResult
);
129 void URLWorker::RevokeObjectURL(const GlobalObject
& aGlobal
,
130 const nsACString
& aUrl
, ErrorResult
& aRv
) {
131 JSContext
* cx
= aGlobal
.Context();
132 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
134 RefPtr
<RevokeURLRunnable
> runnable
=
135 new RevokeURLRunnable(workerPrivate
, aUrl
);
137 runnable
->Dispatch(Canceling
, aRv
);
138 if (NS_WARN_IF(aRv
.Failed())) {
142 WorkerGlobalScope
* scope
= workerPrivate
->GlobalScope();
145 scope
->UnregisterHostObjectURI(aUrl
);
149 bool URLWorker::IsValidObjectURL(const GlobalObject
& aGlobal
,
150 const nsACString
& aUrl
, ErrorResult
& aRv
) {
151 JSContext
* cx
= aGlobal
.Context();
152 WorkerPrivate
* workerPrivate
= GetWorkerPrivateFromContext(cx
);
154 RefPtr
<IsValidURLRunnable
> runnable
=
155 new IsValidURLRunnable(workerPrivate
, aUrl
);
157 runnable
->Dispatch(Canceling
, aRv
);
158 if (NS_WARN_IF(aRv
.Failed())) {
162 return runnable
->IsValidURL();
165 } // namespace mozilla::dom