Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / dom / url / URLWorker.cpp
blob8b0f48e9ff23454ab3ea0f97db113020a5c33391
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 {
16 namespace dom {
18 // This class creates an URL from a DOM Blob on the main thread.
19 class CreateURLRunnable : public WorkerMainThreadRunnable {
20 private:
21 BlobImpl* mBlobImpl;
22 nsAString& mURL;
24 public:
25 CreateURLRunnable(WorkerPrivate* aWorkerPrivate, BlobImpl* aBlobImpl,
26 nsAString& aURL)
27 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: CreateURL"_ns),
28 mBlobImpl(aBlobImpl),
29 mURL(aURL) {
30 MOZ_ASSERT(aBlobImpl);
33 bool MainThreadRun() override {
34 using namespace mozilla::ipc;
36 AssertIsOnMainThread();
38 nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal();
40 nsAutoCString url;
41 nsresult rv = BlobURLProtocolHandler::AddDataEntry(
42 mBlobImpl, principal, Some(mWorkerPrivate->AgentClusterId()), url);
44 if (NS_FAILED(rv)) {
45 NS_WARNING("Failed to add data entry for the blob!");
46 SetDOMStringToNull(mURL);
47 return false;
50 CopyUTF8toUTF16(url, mURL);
51 return true;
55 // This class revokes an URL on the main thread.
56 class RevokeURLRunnable : public WorkerMainThreadRunnable {
57 private:
58 const nsString mURL;
60 public:
61 RevokeURLRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aURL)
62 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: RevokeURL"_ns),
63 mURL(aURL) {}
65 bool MainThreadRun() override {
66 AssertIsOnMainThread();
68 NS_ConvertUTF16toUTF8 url(mURL);
70 BlobURLProtocolHandler::RemoveDataEntry(
71 url, mWorkerPrivate->GetPrincipal(),
72 Some(mWorkerPrivate->AgentClusterId()));
73 return true;
77 // This class checks if an URL is valid on the main thread.
78 class IsValidURLRunnable : public WorkerMainThreadRunnable {
79 private:
80 const nsString mURL;
81 bool mValid;
83 public:
84 IsValidURLRunnable(WorkerPrivate* aWorkerPrivate, const nsAString& aURL)
85 : WorkerMainThreadRunnable(aWorkerPrivate, "URL :: IsValidURL"_ns),
86 mURL(aURL),
87 mValid(false) {}
89 bool MainThreadRun() override {
90 AssertIsOnMainThread();
92 NS_ConvertUTF16toUTF8 url(mURL);
93 mValid = BlobURLProtocolHandler::HasDataEntry(url);
95 return true;
98 bool IsValidURL() const { return mValid; }
101 /* static */
102 void URLWorker::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob,
103 nsAString& aResult, mozilla::ErrorResult& aRv) {
104 JSContext* cx = aGlobal.Context();
105 WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
107 RefPtr<BlobImpl> blobImpl = aBlob.Impl();
108 MOZ_ASSERT(blobImpl);
110 RefPtr<CreateURLRunnable> runnable =
111 new CreateURLRunnable(workerPrivate, blobImpl, aResult);
113 runnable->Dispatch(Canceling, aRv);
114 if (NS_WARN_IF(aRv.Failed())) {
115 return;
118 WorkerGlobalScope* scope = workerPrivate->GlobalScope();
119 MOZ_ASSERT(scope);
121 scope->RegisterHostObjectURI(NS_ConvertUTF16toUTF8(aResult));
124 /* static */
125 void URLWorker::RevokeObjectURL(const GlobalObject& aGlobal,
126 const nsAString& aUrl, ErrorResult& aRv) {
127 JSContext* cx = aGlobal.Context();
128 WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
130 RefPtr<RevokeURLRunnable> runnable =
131 new RevokeURLRunnable(workerPrivate, aUrl);
133 runnable->Dispatch(Canceling, aRv);
134 if (NS_WARN_IF(aRv.Failed())) {
135 return;
138 WorkerGlobalScope* scope = workerPrivate->GlobalScope();
139 MOZ_ASSERT(scope);
141 scope->UnregisterHostObjectURI(NS_ConvertUTF16toUTF8(aUrl));
144 /* static */
145 bool URLWorker::IsValidURL(const GlobalObject& aGlobal, const nsAString& aUrl,
146 ErrorResult& aRv) {
147 JSContext* cx = aGlobal.Context();
148 WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
150 RefPtr<IsValidURLRunnable> runnable =
151 new IsValidURLRunnable(workerPrivate, aUrl);
153 runnable->Dispatch(Canceling, aRv);
154 if (NS_WARN_IF(aRv.Failed())) {
155 return false;
158 return runnable->IsValidURL();
161 } // namespace dom
162 } // namespace mozilla