Bug 1866894 - Update failing subtest for content-visibility-auto-resize.html. r=fredw
[gecko.git] / dom / url / URLWorker.cpp
blobe4ae3d62483d8c2999cdba8c81a1988de0a887c8
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 nsAString& mURL;
23 public:
24 CreateURLRunnable(WorkerPrivate* aWorkerPrivate, BlobImpl* aBlobImpl,
25 nsAString& 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 nsAutoCString url;
46 nsresult rv = BlobURLProtocolHandler::AddDataEntry(
47 mBlobImpl, principal, NS_ConvertUTF16toUTF8(partKey), url);
49 if (NS_FAILED(rv)) {
50 NS_WARNING("Failed to add data entry for the blob!");
51 SetDOMStringToNull(mURL);
52 return false;
55 CopyUTF8toUTF16(url, mURL);
56 return true;
60 // This class revokes an URL on the main thread.
61 class RevokeURLRunnable : public WorkerMainThreadRunnable {
62 private:
63 const nsString mURL;
65 public:
66 RevokeURLRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aURL)
67 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: RevokeURL"_ns),
68 mURL(aURL) {}
70 bool MainThreadRun() override {
71 AssertIsOnMainThread();
73 NS_ConvertUTF16toUTF8 url(mURL);
75 nsCOMPtr<nsICookieJarSettings> cookieJarSettings =
76 mWorkerPrivate->CookieJarSettings();
78 nsAutoString partKey;
79 cookieJarSettings->GetPartitionKey(partKey);
81 BlobURLProtocolHandler::RemoveDataEntry(url, mWorkerPrivate->GetPrincipal(),
82 NS_ConvertUTF16toUTF8(partKey));
83 return true;
87 // This class checks if an URL is valid on the main thread.
88 class IsValidURLRunnable : public WorkerMainThreadRunnable {
89 private:
90 const nsString mURL;
91 bool mValid;
93 public:
94 IsValidURLRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aURL)
95 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: IsValidURL"_ns),
96 mURL(aURL),
97 mValid(false) {}
99 bool MainThreadRun() override {
100 AssertIsOnMainThread();
102 NS_ConvertUTF16toUTF8 url(mURL);
103 mValid = BlobURLProtocolHandler::HasDataEntry(url);
105 return true;
108 bool IsValidURL() const { return mValid; }
111 /* static */
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())) {
125 return;
128 WorkerGlobalScope* scope = workerPrivate->GlobalScope();
129 MOZ_ASSERT(scope);
131 scope->RegisterHostObjectURI(NS_ConvertUTF16toUTF8(aResult));
134 /* static */
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())) {
145 return;
148 WorkerGlobalScope* scope = workerPrivate->GlobalScope();
149 MOZ_ASSERT(scope);
151 scope->UnregisterHostObjectURI(NS_ConvertUTF16toUTF8(aUrl));
154 /* static */
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())) {
165 return false;
168 return runnable->IsValidURL();
171 } // namespace mozilla::dom