Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / dom / url / URLMainThread.cpp
blobad5e8eb59376581a850a68dfcca220ca516e2b33
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 "URLMainThread.h"
9 #include "mozilla/dom/BindingUtils.h"
10 #include "mozilla/dom/Blob.h"
11 #include "mozilla/dom/BlobURLProtocolHandler.h"
12 #include "mozilla/Unused.h"
13 #include "nsContentUtils.h"
14 #include "nsNetUtil.h"
15 #include "nsThreadUtils.h"
17 namespace mozilla {
18 namespace dom {
20 /* static */
21 void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob,
22 nsAString& aResult, ErrorResult& aRv) {
23 MOZ_ASSERT(NS_IsMainThread());
25 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
26 if (NS_WARN_IF(!global)) {
27 aRv.Throw(NS_ERROR_FAILURE);
28 return;
31 nsCOMPtr<nsIPrincipal> principal =
32 nsContentUtils::ObjectPrincipal(aGlobal.Get());
34 nsAutoCString url;
35 aRv = BlobURLProtocolHandler::AddDataEntry(aBlob.Impl(), principal,
36 global->GetAgentClusterId(), url);
37 if (NS_WARN_IF(aRv.Failed())) {
38 return;
41 global->RegisterHostObjectURI(url);
42 CopyASCIItoUTF16(url, aResult);
45 /* static */
46 void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal,
47 MediaSource& aSource, nsAString& aResult,
48 ErrorResult& aRv) {
49 MOZ_ASSERT(NS_IsMainThread());
51 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
52 if (NS_WARN_IF(!global)) {
53 aRv.Throw(NS_ERROR_FAILURE);
54 return;
57 nsCOMPtr<nsIPrincipal> principal =
58 nsContentUtils::ObjectPrincipal(aGlobal.Get());
60 nsAutoCString url;
61 aRv = BlobURLProtocolHandler::AddDataEntry(&aSource, principal,
62 global->GetAgentClusterId(), url);
63 if (NS_WARN_IF(aRv.Failed())) {
64 return;
67 nsCOMPtr<nsIRunnable> revocation = NS_NewRunnableFunction(
68 "dom::URLMainThread::CreateObjectURL",
69 [url] { BlobURLProtocolHandler::RemoveDataEntry(url); });
71 nsContentUtils::RunInStableState(revocation.forget());
73 CopyASCIItoUTF16(url, aResult);
76 /* static */
77 void URLMainThread::RevokeObjectURL(const GlobalObject& aGlobal,
78 const nsAString& aURL, ErrorResult& aRv) {
79 MOZ_ASSERT(NS_IsMainThread());
80 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
81 if (!global) {
82 aRv.Throw(NS_ERROR_FAILURE);
83 return;
86 NS_LossyConvertUTF16toASCII asciiurl(aURL);
88 if (BlobURLProtocolHandler::RemoveDataEntry(
89 asciiurl, nsContentUtils::ObjectPrincipal(aGlobal.Get()),
90 global->GetAgentClusterId())) {
91 global->UnregisterHostObjectURI(asciiurl);
95 /* static */
96 bool URLMainThread::IsValidURL(const GlobalObject& aGlobal,
97 const nsAString& aURL, ErrorResult& aRv) {
98 MOZ_ASSERT(NS_IsMainThread());
99 NS_LossyConvertUTF16toASCII asciiurl(aURL);
100 return BlobURLProtocolHandler::HasDataEntry(asciiurl);
103 } // namespace dom
104 } // namespace mozilla