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"
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
);
31 nsCOMPtr
<nsIPrincipal
> principal
=
32 nsContentUtils::ObjectPrincipal(aGlobal
.Get());
35 aRv
= BlobURLProtocolHandler::AddDataEntry(aBlob
.Impl(), principal
,
36 global
->GetAgentClusterId(), url
);
37 if (NS_WARN_IF(aRv
.Failed())) {
41 global
->RegisterHostObjectURI(url
);
42 CopyASCIItoUTF16(url
, aResult
);
46 void URLMainThread::CreateObjectURL(const GlobalObject
& aGlobal
,
47 MediaSource
& aSource
, nsAString
& aResult
,
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
);
57 nsCOMPtr
<nsIPrincipal
> principal
=
58 nsContentUtils::ObjectPrincipal(aGlobal
.Get());
61 aRv
= BlobURLProtocolHandler::AddDataEntry(&aSource
, principal
,
62 global
->GetAgentClusterId(), url
);
63 if (NS_WARN_IF(aRv
.Failed())) {
67 nsCOMPtr
<nsIRunnable
> revocation
= NS_NewRunnableFunction(
68 "dom::URLMainThread::CreateObjectURL",
69 [url
] { BlobURLProtocolHandler::RemoveDataEntry(url
); });
71 nsContentUtils::RunInStableState(revocation
.forget());
73 CopyASCIItoUTF16(url
, aResult
);
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());
82 aRv
.Throw(NS_ERROR_FAILURE
);
86 NS_LossyConvertUTF16toASCII
asciiurl(aURL
);
88 if (BlobURLProtocolHandler::RemoveDataEntry(
89 asciiurl
, nsContentUtils::ObjectPrincipal(aGlobal
.Get()),
90 global
->GetAgentClusterId())) {
91 global
->UnregisterHostObjectURI(asciiurl
);
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
);
104 } // namespace mozilla