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"
16 #include "mozilla/dom/Document.h"
18 namespace mozilla::dom
{
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
);
32 if (nsCOMPtr
<nsPIDOMWindowInner
> owner
= do_QueryInterface(global
)) {
33 if (Document
* doc
= owner
->GetExtantDoc()) {
34 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
=
35 doc
->CookieJarSettings();
37 cookieJarSettings
->GetPartitionKey(partKey
);
41 nsCOMPtr
<nsIPrincipal
> principal
=
42 nsContentUtils::ObjectPrincipal(aGlobal
.Get());
45 aRv
= BlobURLProtocolHandler::AddDataEntry(
46 aBlob
.Impl(), principal
, NS_ConvertUTF16toUTF8(partKey
), url
);
47 if (NS_WARN_IF(aRv
.Failed())) {
51 global
->RegisterHostObjectURI(url
);
52 CopyASCIItoUTF16(url
, aResult
);
56 void URLMainThread::CreateObjectURL(const GlobalObject
& aGlobal
,
57 MediaSource
& aSource
, nsAString
& aResult
,
59 MOZ_ASSERT(NS_IsMainThread());
61 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(aGlobal
.GetAsSupports());
62 if (NS_WARN_IF(!global
)) {
63 aRv
.Throw(NS_ERROR_FAILURE
);
68 if (nsCOMPtr
<nsPIDOMWindowInner
> owner
= do_QueryInterface(global
)) {
69 if (Document
* doc
= owner
->GetExtantDoc()) {
70 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
=
71 doc
->CookieJarSettings();
73 cookieJarSettings
->GetPartitionKey(partKey
);
77 nsCOMPtr
<nsIPrincipal
> principal
=
78 nsContentUtils::ObjectPrincipal(aGlobal
.Get());
81 aRv
= BlobURLProtocolHandler::AddDataEntry(
82 &aSource
, principal
, NS_ConvertUTF16toUTF8(partKey
), url
);
83 if (NS_WARN_IF(aRv
.Failed())) {
87 nsCOMPtr
<nsIRunnable
> revocation
= NS_NewRunnableFunction(
88 "dom::URLMainThread::CreateObjectURL",
89 [url
] { BlobURLProtocolHandler::RemoveDataEntry(url
); });
91 nsContentUtils::RunInStableState(revocation
.forget());
93 CopyASCIItoUTF16(url
, aResult
);
97 void URLMainThread::RevokeObjectURL(const GlobalObject
& aGlobal
,
98 const nsAString
& aURL
, ErrorResult
& aRv
) {
99 MOZ_ASSERT(NS_IsMainThread());
100 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(aGlobal
.GetAsSupports());
102 aRv
.Throw(NS_ERROR_FAILURE
);
106 nsAutoString partKey
;
107 if (nsCOMPtr
<nsPIDOMWindowInner
> owner
= do_QueryInterface(global
)) {
108 if (Document
* doc
= owner
->GetExtantDoc()) {
109 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
=
110 doc
->CookieJarSettings();
112 cookieJarSettings
->GetPartitionKey(partKey
);
116 NS_LossyConvertUTF16toASCII
asciiurl(aURL
);
118 if (BlobURLProtocolHandler::RemoveDataEntry(
119 asciiurl
, nsContentUtils::ObjectPrincipal(aGlobal
.Get()),
120 NS_ConvertUTF16toUTF8(partKey
))) {
121 global
->UnregisterHostObjectURI(asciiurl
);
126 bool URLMainThread::IsValidObjectURL(const GlobalObject
& aGlobal
,
127 const nsAString
& aURL
, ErrorResult
& aRv
) {
128 MOZ_ASSERT(NS_IsMainThread());
129 NS_LossyConvertUTF16toASCII
asciiurl(aURL
);
130 return BlobURLProtocolHandler::HasDataEntry(asciiurl
);
133 } // namespace mozilla::dom