Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / url / URLWorker.cpp
blobcb902b8cd3546f300b763f8951d091d3d805b56c
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/. */
7 #include "URLWorker.h"
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 {
19 private:
20 BlobImpl* mBlobImpl;
21 nsACString& mURL;
23 public:
24 CreateURLRunnable(WorkerPrivate* aWorkerPrivate, BlobImpl* aBlobImpl,
25 nsACString& aURL)
26 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: CreateURL"_ns),
27 mBlobImpl(aBlobImpl),
28 mURL(aURL) {
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();
42 nsAutoString partKey;
43 cookieJarSettings->GetPartitionKey(partKey);
45 nsresult rv = BlobURLProtocolHandler::AddDataEntry(
46 mBlobImpl, principal, NS_ConvertUTF16toUTF8(partKey), mURL);
47 if (NS_FAILED(rv)) {
48 NS_WARNING("Failed to add data entry for the blob!");
49 mURL.SetIsVoid(true);
50 return false;
52 return true;
56 // This class revokes an URL on the main thread.
57 class RevokeURLRunnable : public WorkerMainThreadRunnable {
58 private:
59 const nsCString mURL;
61 public:
62 RevokeURLRunnable(WorkerPrivate* aWorkerPrivate, const nsACString& aURL)
63 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: RevokeURL"_ns),
64 mURL(aURL) {}
66 bool MainThreadRun() override {
67 AssertIsOnMainThread();
69 nsCOMPtr<nsICookieJarSettings> cookieJarSettings =
70 mWorkerPrivate->CookieJarSettings();
72 nsAutoString partKey;
73 cookieJarSettings->GetPartitionKey(partKey);
75 BlobURLProtocolHandler::RemoveDataEntry(
76 mURL, mWorkerPrivate->GetPrincipal(), NS_ConvertUTF16toUTF8(partKey));
77 return true;
81 // This class checks if an URL is valid on the main thread.
82 class IsValidURLRunnable : public WorkerMainThreadRunnable {
83 private:
84 const nsCString mURL;
85 bool mValid;
87 public:
88 IsValidURLRunnable(WorkerPrivate* aWorkerPrivate, const nsACString& aURL)
89 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: IsValidURL"_ns),
90 mURL(aURL),
91 mValid(false) {}
93 bool MainThreadRun() override {
94 AssertIsOnMainThread();
96 mValid = BlobURLProtocolHandler::HasDataEntry(mURL);
98 return true;
101 bool IsValidURL() const { return mValid; }
104 /* static */
105 void URLWorker::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob,
106 nsACString& aResult,
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())) {
119 return;
122 WorkerGlobalScope* scope = workerPrivate->GlobalScope();
123 MOZ_ASSERT(scope);
125 scope->RegisterHostObjectURI(aResult);
128 /* static */
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())) {
139 return;
142 WorkerGlobalScope* scope = workerPrivate->GlobalScope();
143 MOZ_ASSERT(scope);
145 scope->UnregisterHostObjectURI(aUrl);
148 /* static */
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())) {
159 return false;
162 return runnable->IsValidURL();
165 } // namespace mozilla::dom