Bug 1848468 - Mark k-rate-dynamics-compressor-connections.html subtest as failing...
[gecko.git] / dom / url / URLWorker.cpp
blob2ecddd35a21dbd4f559ab013ef1c4965b4063439
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 nsAutoCString url;
40 nsresult rv = BlobURLProtocolHandler::AddDataEntry(
41 mBlobImpl, principal, Some(mWorkerPrivate->AgentClusterId()), url);
43 if (NS_FAILED(rv)) {
44 NS_WARNING("Failed to add data entry for the blob!");
45 SetDOMStringToNull(mURL);
46 return false;
49 CopyUTF8toUTF16(url, mURL);
50 return true;
54 // This class revokes an URL on the main thread.
55 class RevokeURLRunnable : public WorkerMainThreadRunnable {
56 private:
57 const nsString mURL;
59 public:
60 RevokeURLRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aURL)
61 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: RevokeURL"_ns),
62 mURL(aURL) {}
64 bool MainThreadRun() override {
65 AssertIsOnMainThread();
67 NS_ConvertUTF16toUTF8 url(mURL);
69 BlobURLProtocolHandler::RemoveDataEntry(
70 url, mWorkerPrivate->GetPrincipal(),
71 Some(mWorkerPrivate->AgentClusterId()));
72 return true;
76 // This class checks if an URL is valid on the main thread.
77 class IsValidURLRunnable : public WorkerMainThreadRunnable {
78 private:
79 const nsString mURL;
80 bool mValid;
82 public:
83 IsValidURLRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aURL)
84 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: IsValidURL"_ns),
85 mURL(aURL),
86 mValid(false) {}
88 bool MainThreadRun() override {
89 AssertIsOnMainThread();
91 NS_ConvertUTF16toUTF8 url(mURL);
92 mValid = BlobURLProtocolHandler::HasDataEntry(url);
94 return true;
97 bool IsValidURL() const { return mValid; }
100 /* static */
101 void URLWorker::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob,
102 nsAString& aResult, mozilla::ErrorResult& aRv) {
103 JSContext* cx = aGlobal.Context();
104 WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
106 RefPtr<BlobImpl> blobImpl = aBlob.Impl();
107 MOZ_ASSERT(blobImpl);
109 RefPtr<CreateURLRunnable> runnable =
110 new CreateURLRunnable(workerPrivate, blobImpl, aResult);
112 runnable->Dispatch(Canceling, aRv);
113 if (NS_WARN_IF(aRv.Failed())) {
114 return;
117 WorkerGlobalScope* scope = workerPrivate->GlobalScope();
118 MOZ_ASSERT(scope);
120 scope->RegisterHostObjectURI(NS_ConvertUTF16toUTF8(aResult));
123 /* static */
124 void URLWorker::RevokeObjectURL(const GlobalObject& aGlobal,
125 const nsAString& aUrl, ErrorResult& aRv) {
126 JSContext* cx = aGlobal.Context();
127 WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
129 RefPtr<RevokeURLRunnable> runnable =
130 new RevokeURLRunnable(workerPrivate, aUrl);
132 runnable->Dispatch(Canceling, aRv);
133 if (NS_WARN_IF(aRv.Failed())) {
134 return;
137 WorkerGlobalScope* scope = workerPrivate->GlobalScope();
138 MOZ_ASSERT(scope);
140 scope->UnregisterHostObjectURI(NS_ConvertUTF16toUTF8(aUrl));
143 /* static */
144 bool URLWorker::IsValidObjectURL(const GlobalObject& aGlobal,
145 const nsAString& aUrl, ErrorResult& aRv) {
146 JSContext* cx = aGlobal.Context();
147 WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
149 RefPtr<IsValidURLRunnable> runnable =
150 new IsValidURLRunnable(workerPrivate, aUrl);
152 runnable->Dispatch(Canceling, aRv);
153 if (NS_WARN_IF(aRv.Failed())) {
154 return false;
157 return runnable->IsValidURL();
160 } // namespace mozilla::dom